VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuest-linux.c@ 97956

最後變更 在這個檔案從97956是 97828,由 vboxsync 提交於 2 年 前

Addition: Linux: vboxguest: Mouse integration: invert vertical and horizontal scroll values on the guest side as it is done in upstream PS/2 mouse driver, bugref:10285:16.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 46.9 KB
 
1/* $Id: VBoxGuest-linux.c 97828 2022-12-16 18:25:13Z vboxsync $ */
2/** @file
3 * VBoxGuest - Linux specifics.
4 *
5 * Note. Unfortunately, the difference between this and SUPDrv-linux.c is
6 * a little bit too big to be helpful.
7 */
8
9/*
10 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
11 *
12 * This file is part of VirtualBox base platform packages, as
13 * available from https://www.alldomusa.eu.org.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, in version 3 of the
18 * License.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, see <https://www.gnu.org/licenses>.
27 *
28 * The contents of this file may alternatively be used under the terms
29 * of the Common Development and Distribution License Version 1.0
30 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
31 * in the VirtualBox distribution, in which case the provisions of the
32 * CDDL are applicable instead of those of the GPL.
33 *
34 * You may elect to license modified versions of this file under the
35 * terms and conditions of either the GPL or the CDDL or both.
36 *
37 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
38 */
39
40
41/*********************************************************************************************************************************
42* Header Files *
43*********************************************************************************************************************************/
44#define LOG_GROUP LOG_GROUP_SUP_DRV
45
46#include "the-linux-kernel.h"
47
48#if RTLNX_VER_MIN(2,6,15)
49# define VBOXGUEST_WITH_INPUT_DRIVER
50#endif
51
52#if RTLNX_VER_MIN(4,15,0)
53# define CONST_4_15 const
54#else
55# define CONST_4_15
56#endif
57
58#include "VBoxGuestInternal.h"
59#ifdef VBOXGUEST_WITH_INPUT_DRIVER
60# include <linux/input.h>
61#endif
62#include <linux/miscdevice.h>
63#include <linux/poll.h>
64#if RTLNX_VER_MIN(2,6,28)
65# include <linux/tty.h>
66#endif
67#include <VBox/version.h>
68#include "revision-generated.h"
69
70#include <iprt/assert.h>
71#include <iprt/asm.h>
72#include <iprt/ctype.h>
73#include <iprt/initterm.h>
74#include <iprt/mem.h>
75#include <iprt/mp.h>
76#include <iprt/process.h>
77#include <iprt/spinlock.h>
78#include <iprt/semaphore.h>
79#include <iprt/string.h>
80#include <VBox/err.h>
81#include <VBox/log.h>
82
83
84/*********************************************************************************************************************************
85* Defined Constants And Macros *
86*********************************************************************************************************************************/
87/** The device name. */
88#define DEVICE_NAME "vboxguest"
89/** The device name for the device node open to everyone. */
90#define DEVICE_NAME_USER "vboxuser"
91/** The name of the PCI driver */
92#define DRIVER_NAME DEVICE_NAME
93
94
95/* 2.4.x compatibility macros that may or may not be defined. */
96#ifndef IRQ_RETVAL
97# define irqreturn_t void
98# define IRQ_RETVAL(n)
99#endif
100
101/* uidgid.h was introduced in 3.5.0. */
102#if RTLNX_VER_MAX(3,5,0)
103# define kgid_t gid_t
104# define kuid_t uid_t
105#endif
106
107#ifdef VBOXGUEST_WITH_INPUT_DRIVER
108/** The name of input pointing device for mouse integration. */
109# define VBOX_INPUT_DEVICE_NAME "VirtualBox mouse integration"
110#endif
111
112
113/*********************************************************************************************************************************
114* Internal Functions *
115*********************************************************************************************************************************/
116static void vgdrvLinuxTermPci(struct pci_dev *pPciDev);
117static int vgdrvLinuxProbePci(struct pci_dev *pPciDev, const struct pci_device_id *id);
118static int __init vgdrvLinuxModInit(void);
119static void __exit vgdrvLinuxModExit(void);
120static int vgdrvLinuxOpen(struct inode *pInode, struct file *pFilp);
121static int vgdrvLinuxRelease(struct inode *pInode, struct file *pFilp);
122#ifdef HAVE_UNLOCKED_IOCTL
123static long vgdrvLinuxIOCtl(struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
124#else
125static int vgdrvLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
126#endif
127static int vgdrvLinuxIOCtlSlow(struct file *pFilp, unsigned int uCmd, unsigned long ulArg, PVBOXGUESTSESSION pSession);
128static int vgdrvLinuxFAsync(int fd, struct file *pFile, int fOn);
129static unsigned int vgdrvLinuxPoll(struct file *pFile, poll_table *pPt);
130static ssize_t vgdrvLinuxRead(struct file *pFile, char *pbBuf, size_t cbRead, loff_t *poff);
131
132
133/*********************************************************************************************************************************
134* Global Variables *
135*********************************************************************************************************************************/
136/**
137 * Device extention & session data association structure.
138 */
139static VBOXGUESTDEVEXT g_DevExt;
140/** The PCI device. */
141static struct pci_dev *g_pPciDev = NULL;
142/** The base of the I/O port range. */
143static RTIOPORT g_IOPortBase;
144/** The base of the MMIO range. */
145static RTHCPHYS g_MMIOPhysAddr = NIL_RTHCPHYS;
146/** The size of the MMIO range as seen by PCI. */
147static uint32_t g_cbMMIO;
148/** The pointer to the mapping of the MMIO range. */
149static void *g_pvMMIOBase;
150/** Wait queue used by polling. */
151static wait_queue_head_t g_PollEventQueue;
152/** Asynchronous notification stuff. */
153static struct fasync_struct *g_pFAsyncQueue;
154#ifdef VBOXGUEST_WITH_INPUT_DRIVER
155/** Pre-allocated mouse status VMMDev requests for use in the IRQ handler. */
156static VMMDevReqMouseStatusEx *g_pMouseStatusReqEx;
157#endif
158#if RTLNX_VER_MIN(2,6,0)
159/** Whether we've create the logger or not. */
160static volatile bool g_fLoggerCreated;
161/** Release logger group settings. */
162static char g_szLogGrp[128];
163/** Release logger flags settings. */
164static char g_szLogFlags[128];
165/** Release logger destination settings. */
166static char g_szLogDst[128];
167# if 0
168/** Debug logger group settings. */
169static char g_szDbgLogGrp[128];
170/** Debug logger flags settings. */
171static char g_szDbgLogFlags[128];
172/** Debug logger destination settings. */
173static char g_szDbgLogDst[128];
174# endif
175#endif
176
177/** The input device handle */
178#ifdef VBOXGUEST_WITH_INPUT_DRIVER
179static struct input_dev *g_pInputDevice = NULL;
180#endif
181
182/** The file_operations structure. */
183static struct file_operations g_FileOps =
184{
185 owner: THIS_MODULE,
186 open: vgdrvLinuxOpen,
187 release: vgdrvLinuxRelease,
188#ifdef HAVE_UNLOCKED_IOCTL
189 unlocked_ioctl: vgdrvLinuxIOCtl,
190#else
191 ioctl: vgdrvLinuxIOCtl,
192#endif
193 fasync: vgdrvLinuxFAsync,
194 read: vgdrvLinuxRead,
195 poll: vgdrvLinuxPoll,
196 llseek: no_llseek,
197};
198
199/** The miscdevice structure. */
200static struct miscdevice g_MiscDevice =
201{
202 minor: MISC_DYNAMIC_MINOR,
203 name: DEVICE_NAME,
204 fops: &g_FileOps,
205};
206
207/** The file_operations structure for the user device.
208 * @remarks For the time being we'll be using the same implementation as
209 * /dev/vboxguest here. */
210static struct file_operations g_FileOpsUser =
211{
212 owner: THIS_MODULE,
213 open: vgdrvLinuxOpen,
214 release: vgdrvLinuxRelease,
215#ifdef HAVE_UNLOCKED_IOCTL
216 unlocked_ioctl: vgdrvLinuxIOCtl,
217#else
218 ioctl: vgdrvLinuxIOCtl,
219#endif
220};
221
222/** The miscdevice structure for the user device. */
223static struct miscdevice g_MiscDeviceUser =
224{
225 minor: MISC_DYNAMIC_MINOR,
226 name: DEVICE_NAME_USER,
227 fops: &g_FileOpsUser,
228};
229
230
231/** PCI hotplug structure. */
232static const struct pci_device_id g_VBoxGuestPciId[] =
233{
234 {
235 vendor: VMMDEV_VENDORID,
236 device: VMMDEV_DEVICEID
237 },
238 {
239 /* empty entry */
240 }
241};
242
243MODULE_DEVICE_TABLE(pci, g_VBoxGuestPciId);
244
245/** Structure for registering the PCI driver. */
246static struct pci_driver g_PciDriver =
247{
248 name: DRIVER_NAME,
249 id_table: g_VBoxGuestPciId,
250 probe: vgdrvLinuxProbePci,
251 remove: vgdrvLinuxTermPci
252};
253
254#ifdef VBOXGUEST_WITH_INPUT_DRIVER
255/** Kernel IDC session to ourselves for use with the mouse events. */
256static PVBOXGUESTSESSION g_pKernelSession = NULL;
257#endif
258
259
260
261/**
262 * Converts a VBox status code to a linux error code.
263 *
264 * @returns corresponding negative linux error code.
265 * @param rc supdrv error code (SUPDRV_ERR_* defines).
266 */
267static int vgdrvLinuxConvertToNegErrno(int rc)
268{
269 if ( rc > -1000
270 && rc < 1000)
271 return -RTErrConvertToErrno(rc);
272 switch (rc)
273 {
274 case VERR_HGCM_SERVICE_NOT_FOUND: return -ESRCH;
275 case VINF_HGCM_CLIENT_REJECTED: return 0;
276 case VERR_HGCM_INVALID_CMD_ADDRESS: return -EFAULT;
277 case VINF_HGCM_ASYNC_EXECUTE: return 0;
278 case VERR_HGCM_INTERNAL: return -EPROTO;
279 case VERR_HGCM_INVALID_CLIENT_ID: return -EINVAL;
280 case VINF_HGCM_SAVE_STATE: return 0;
281 /* No reason to return this to a guest */
282 // case VERR_HGCM_SERVICE_EXISTS: return -EEXIST;
283 default:
284 AssertMsgFailed(("Unhandled error code %Rrc\n", rc));
285 return -EPROTO;
286 }
287}
288
289
290/**
291 * Does the PCI detection and init of the device.
292 *
293 * @returns 0 on success, negated errno on failure.
294 */
295static int vgdrvLinuxProbePci(struct pci_dev *pPciDev, const struct pci_device_id *id)
296{
297 int rc;
298
299 NOREF(id);
300 AssertReturn(!g_pPciDev, -EINVAL);
301 rc = pci_enable_device(pPciDev);
302 if (rc >= 0)
303 {
304 /* I/O Ports are mandatory, the MMIO bit is not. */
305 g_IOPortBase = pci_resource_start(pPciDev, 0);
306 if (g_IOPortBase != 0)
307 {
308 /*
309 * Map the register address space.
310 */
311 g_MMIOPhysAddr = pci_resource_start(pPciDev, 1);
312 g_cbMMIO = pci_resource_len(pPciDev, 1);
313 if (request_mem_region(g_MMIOPhysAddr, g_cbMMIO, DEVICE_NAME) != NULL)
314 {
315 g_pvMMIOBase = ioremap(g_MMIOPhysAddr, g_cbMMIO);
316 if (g_pvMMIOBase)
317 {
318 /** @todo why aren't we requesting ownership of the I/O ports as well? */
319 g_pPciDev = pPciDev;
320 return 0;
321 }
322
323 /* failure cleanup path */
324 LogRel((DEVICE_NAME ": ioremap failed; MMIO Addr=%RHp cb=%#x\n", g_MMIOPhysAddr, g_cbMMIO));
325 rc = -ENOMEM;
326 release_mem_region(g_MMIOPhysAddr, g_cbMMIO);
327 }
328 else
329 {
330 LogRel((DEVICE_NAME ": failed to obtain adapter memory\n"));
331 rc = -EBUSY;
332 }
333 g_MMIOPhysAddr = NIL_RTHCPHYS;
334 g_cbMMIO = 0;
335 g_IOPortBase = 0;
336 }
337 else
338 {
339 LogRel((DEVICE_NAME ": did not find expected hardware resources\n"));
340 rc = -ENXIO;
341 }
342 pci_disable_device(pPciDev);
343 }
344 else
345 LogRel((DEVICE_NAME ": could not enable device: %d\n", rc));
346 return rc;
347}
348
349
350/**
351 * Clean up the usage of the PCI device.
352 */
353static void vgdrvLinuxTermPci(struct pci_dev *pPciDev)
354{
355 g_pPciDev = NULL;
356 if (pPciDev)
357 {
358 iounmap(g_pvMMIOBase);
359 g_pvMMIOBase = NULL;
360
361 release_mem_region(g_MMIOPhysAddr, g_cbMMIO);
362 g_MMIOPhysAddr = NIL_RTHCPHYS;
363 g_cbMMIO = 0;
364
365 pci_disable_device(pPciDev);
366 }
367}
368
369
370/**
371 * Interrupt service routine.
372 *
373 * @returns In 2.4 it returns void.
374 * In 2.6 we indicate whether we've handled the IRQ or not.
375 *
376 * @param iIrq The IRQ number.
377 * @param pvDevId The device ID, a pointer to g_DevExt.
378 * @param pRegs Register set. Removed in 2.6.19.
379 */
380#if RTLNX_VER_MIN(2,6,19) && !defined(DOXYGEN_RUNNING)
381static irqreturn_t vgdrvLinuxISR(int iIrq, void *pvDevId)
382#else
383static irqreturn_t vgdrvLinuxISR(int iIrq, void *pvDevId, struct pt_regs *pRegs)
384#endif
385{
386 bool fTaken = VGDrvCommonISR(&g_DevExt);
387 return IRQ_RETVAL(fTaken);
388}
389
390
391/**
392 * Registers the ISR and initializes the poll wait queue.
393 */
394static int __init vgdrvLinuxInitISR(void)
395{
396 int rc;
397
398 init_waitqueue_head(&g_PollEventQueue);
399 rc = request_irq(g_pPciDev->irq,
400 vgdrvLinuxISR,
401#if RTLNX_VER_MIN(2,6,20)
402 IRQF_SHARED,
403#else
404 SA_SHIRQ,
405#endif
406 DEVICE_NAME,
407 &g_DevExt);
408 if (rc)
409 {
410 LogRel((DEVICE_NAME ": could not request IRQ %d: err=%d\n", g_pPciDev->irq, rc));
411 return rc;
412 }
413 return 0;
414}
415
416
417/**
418 * Deregisters the ISR.
419 */
420static void vgdrvLinuxTermISR(void)
421{
422 free_irq(g_pPciDev->irq, &g_DevExt);
423}
424
425
426#ifdef VBOXGUEST_WITH_INPUT_DRIVER
427/**
428 * Check if extended mouse pointer state request protocol is currently used by driver.
429 *
430 * @returns True if extended protocol is used, False otherwise.
431 */
432static bool vgdrvLinuxUsesMouseStatusEx(void)
433{
434 return g_pMouseStatusReqEx->Core.header.requestType == VMMDevReq_GetMouseStatusEx;
435}
436
437/**
438 * Reports the mouse integration status to the host.
439 *
440 * Calls the kernel IOCtl to report mouse status to the host on behalf of
441 * our kernel session.
442 *
443 * @param fStatus The mouse status to report.
444 */
445static int vgdrvLinuxSetMouseStatus(uint32_t fStatus)
446{
447 int rc;
448 VBGLIOCSETMOUSESTATUS Req;
449 VBGLREQHDR_INIT(&Req.Hdr, SET_MOUSE_STATUS);
450 Req.u.In.fStatus = fStatus;
451 rc = VGDrvCommonIoCtl(VBGL_IOCTL_SET_MOUSE_STATUS, &g_DevExt, g_pKernelSession, &Req.Hdr, sizeof(Req));
452 if (RT_SUCCESS(rc))
453 rc = Req.Hdr.rc;
454 return rc;
455}
456
457
458/**
459 * Called when the input device is first opened.
460 *
461 * Sets up absolute mouse reporting.
462 */
463static int vboxguestOpenInputDevice(struct input_dev *pDev)
464{
465 int rc = vgdrvLinuxSetMouseStatus( VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE
466 | VMMDEV_MOUSE_NEW_PROTOCOL
467 | (vgdrvLinuxUsesMouseStatusEx() ? VMMDEV_MOUSE_GUEST_USES_FULL_STATE_PROTOCOL : 0));
468 if (RT_FAILURE(rc))
469 return ENODEV;
470 NOREF(pDev);
471 return 0;
472}
473
474
475/**
476 * Called if all open handles to the input device are closed.
477 *
478 * Disables absolute reporting.
479 */
480static void vboxguestCloseInputDevice(struct input_dev *pDev)
481{
482 NOREF(pDev);
483 vgdrvLinuxSetMouseStatus(0);
484}
485
486
487/**
488 * Free corresponding mouse status request structure.
489 */
490static void vgdrvLinuxFreeMouseStatusReq(void)
491{
492 VbglR0GRFree(&g_pMouseStatusReqEx->Core.header);
493 g_pMouseStatusReqEx = NULL;
494}
495
496/**
497 * Creates the kernel input device.
498 */
499static int __init vgdrvLinuxCreateInputDevice(void)
500{
501 /* Try to allocate legacy request data first, and check if host supports extended protocol. */
502 int rc = VbglR0GRAlloc((VMMDevRequestHeader **)&g_pMouseStatusReqEx, sizeof(VMMDevReqMouseStatus), VMMDevReq_GetMouseStatus);
503 if (RT_SUCCESS(rc))
504 {
505 /* Check if host supports extended mouse state reporting. */
506 g_pMouseStatusReqEx->Core.mouseFeatures = 0;
507 rc = VbglR0GRPerform(&g_pMouseStatusReqEx->Core.header);
508 if (RT_SUCCESS(rc))
509 {
510 if (g_pMouseStatusReqEx->Core.mouseFeatures & VMMDEV_MOUSE_HOST_USES_FULL_STATE_PROTOCOL)
511 {
512 VMMDevReqMouseStatusEx *pReqEx = NULL;
513 rc = VbglR0GRAlloc((VMMDevRequestHeader **)&pReqEx, sizeof(*pReqEx), VMMDevReq_GetMouseStatusEx);
514 if (RT_SUCCESS(rc))
515 {
516 /* De-allocate legacy request data, */
517 VbglR0GRFree(&g_pMouseStatusReqEx->Core.header);
518 /* ..and switch to extended requests. */
519 g_pMouseStatusReqEx = pReqEx;
520 LogRel(("Host supports full mouse state reporting, switching to extended mouse integration protocol\n"));
521 }
522 else
523 LogRel(("Host supports full mouse state reporting, but feature cannot be initialized, switching to legacy mouse integration protocol\n"));
524 }
525 else
526 LogRel(("Host does not support full mouse state reporting, switching to legacy mouse integration protocol\n"));
527 }
528 else
529 LogRel(("Unable to get host mouse capabilities, switching to legacy mouse integration protocol\n"));
530 }
531 else
532 rc = -ENOMEM;
533
534 if (RT_SUCCESS(rc))
535 {
536 g_pInputDevice = input_allocate_device();
537 if (g_pInputDevice)
538 {
539 g_pInputDevice->name = VBOX_INPUT_DEVICE_NAME;
540 g_pInputDevice->id.bustype = BUS_PCI;
541 g_pInputDevice->id.vendor = VMMDEV_VENDORID;
542 g_pInputDevice->id.product = VMMDEV_DEVICEID;
543 g_pInputDevice->id.version = VBOX_SHORT_VERSION;
544 g_pInputDevice->open = vboxguestOpenInputDevice;
545 g_pInputDevice->close = vboxguestCloseInputDevice;
546# if RTLNX_VER_MAX(2,6,22)
547 g_pInputDevice->cdev.dev = &g_pPciDev->dev;
548# else
549 g_pInputDevice->dev.parent = &g_pPciDev->dev;
550# endif
551 /* Set up input device capabilities. */
552 ASMBitSet(g_pInputDevice->evbit, EV_ABS);
553 ASMBitSet(g_pInputDevice->evbit, EV_KEY);
554# ifdef EV_SYN
555 ASMBitSet(g_pInputDevice->evbit, EV_SYN);
556# endif
557 ASMBitSet(g_pInputDevice->absbit, ABS_X);
558 ASMBitSet(g_pInputDevice->absbit, ABS_Y);
559
560 input_set_abs_params(g_pInputDevice, ABS_X, VMMDEV_MOUSE_RANGE_MIN, VMMDEV_MOUSE_RANGE_MAX, 0, 0);
561 input_set_abs_params(g_pInputDevice, ABS_Y, VMMDEV_MOUSE_RANGE_MIN, VMMDEV_MOUSE_RANGE_MAX, 0, 0);
562
563 ASMBitSet(g_pInputDevice->keybit, BTN_MOUSE);
564
565 /* Report extra capabilities to input layer if extended mouse state protocol
566 * will be used in communication with host. */
567 if (vgdrvLinuxUsesMouseStatusEx())
568 {
569 ASMBitSet(g_pInputDevice->evbit, EV_REL);
570 ASMBitSet(g_pInputDevice->evbit, EV_KEY);
571
572 ASMBitSet(g_pInputDevice->relbit, REL_WHEEL);
573 ASMBitSet(g_pInputDevice->relbit, REL_HWHEEL);
574
575 ASMBitSet(g_pInputDevice->keybit, BTN_LEFT);
576 ASMBitSet(g_pInputDevice->keybit, BTN_RIGHT);
577 ASMBitSet(g_pInputDevice->keybit, BTN_MIDDLE);
578 ASMBitSet(g_pInputDevice->keybit, BTN_SIDE);
579 ASMBitSet(g_pInputDevice->keybit, BTN_EXTRA);
580 }
581
582 rc = input_register_device(g_pInputDevice);
583 if (rc == 0)
584 return 0;
585
586 input_free_device(g_pInputDevice);
587 }
588 else
589 rc = -ENOMEM;
590
591 vgdrvLinuxFreeMouseStatusReq();
592 }
593
594 return rc;
595}
596
597
598/**
599 * Terminates the kernel input device.
600 */
601static void vgdrvLinuxTermInputDevice(void)
602{
603 /* Notify host that mouse integration is no longer available. */
604 vgdrvLinuxSetMouseStatus(0);
605
606 vgdrvLinuxFreeMouseStatusReq();
607
608 /* See documentation of input_register_device(): input_free_device()
609 * should not be called after a device has been registered. */
610 input_unregister_device(g_pInputDevice);
611}
612
613#endif /* VBOXGUEST_WITH_INPUT_DRIVER */
614
615/**
616 * Creates the device nodes.
617 *
618 * @returns 0 on success, negated errno on failure.
619 */
620static int __init vgdrvLinuxInitDeviceNodes(void)
621{
622 /*
623 * The full feature device node.
624 */
625 int rc = misc_register(&g_MiscDevice);
626 if (!rc)
627 {
628 /*
629 * The device node intended to be accessible by all users.
630 */
631 rc = misc_register(&g_MiscDeviceUser);
632 if (!rc)
633 return 0;
634 LogRel((DEVICE_NAME ": misc_register failed for %s (rc=%d)\n", DEVICE_NAME_USER, rc));
635 misc_deregister(&g_MiscDevice);
636 }
637 else
638 LogRel((DEVICE_NAME ": misc_register failed for %s (rc=%d)\n", DEVICE_NAME, rc));
639 return rc;
640}
641
642
643/**
644 * Deregisters the device nodes.
645 */
646static void vgdrvLinuxTermDeviceNodes(void)
647{
648 misc_deregister(&g_MiscDevice);
649 misc_deregister(&g_MiscDeviceUser);
650}
651
652
653/**
654 * Initialize module.
655 *
656 * @returns appropriate status code.
657 */
658static int __init vgdrvLinuxModInit(void)
659{
660 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
661 PRTLOGGER pRelLogger;
662 int rc;
663
664 /*
665 * Initialize IPRT first.
666 */
667 rc = RTR0Init(0);
668 if (RT_FAILURE(rc))
669 {
670 printk(KERN_ERR DEVICE_NAME ": RTR0Init failed, rc=%d.\n", rc);
671 return -EINVAL;
672 }
673
674 /*
675 * Create the release log.
676 * (We do that here instead of common code because we want to log
677 * early failures using the LogRel macro.)
678 */
679 rc = RTLogCreate(&pRelLogger, 0 /* fFlags */, "all",
680 "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
681 RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER | RTLOGDEST_USER, NULL);
682 if (RT_SUCCESS(rc))
683 {
684#if RTLNX_VER_MIN(2,6,0)
685 RTLogGroupSettings(pRelLogger, g_szLogGrp);
686 RTLogFlags(pRelLogger, g_szLogFlags);
687 RTLogDestinations(pRelLogger, g_szLogDst);
688#endif
689 RTLogRelSetDefaultInstance(pRelLogger);
690 }
691#if RTLNX_VER_MIN(2,6,0)
692 g_fLoggerCreated = true;
693#endif
694
695 /*
696 * Locate and initialize the PCI device.
697 */
698 rc = pci_register_driver(&g_PciDriver);
699 if (rc >= 0 && g_pPciDev)
700 {
701 /*
702 * Call the common device extension initializer.
703 */
704#if RTLNX_VER_MIN(2,6,0) && defined(RT_ARCH_X86)
705 VBOXOSTYPE enmOSType = VBOXOSTYPE_Linux26;
706#elif RTLNX_VER_MIN(2,6,0) && defined(RT_ARCH_AMD64)
707 VBOXOSTYPE enmOSType = VBOXOSTYPE_Linux26_x64;
708#elif RTLNX_VER_MIN(2,4,0) && defined(RT_ARCH_X86)
709 VBOXOSTYPE enmOSType = VBOXOSTYPE_Linux24;
710#elif RTLNX_VER_MIN(2,4,0) && defined(RT_ARCH_AMD64)
711 VBOXOSTYPE enmOSType = VBOXOSTYPE_Linux24_x64;
712#else
713# warning "huh? which arch + version is this?"
714 VBOXOSTYPE enmOsType = VBOXOSTYPE_Linux;
715#endif
716 rc = VGDrvCommonInitDevExt(&g_DevExt,
717 g_IOPortBase,
718 g_pvMMIOBase,
719 g_cbMMIO,
720 enmOSType,
721 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
722 if (RT_SUCCESS(rc))
723 {
724 /*
725 * Register the interrupt service routine for it now that g_DevExt can handle IRQs.
726 */
727 rc = vgdrvLinuxInitISR();
728 if (rc >= 0) /** @todo r=bird: status check differs from that inside vgdrvLinuxInitISR. */
729 {
730#ifdef VBOXGUEST_WITH_INPUT_DRIVER
731 /*
732 * Create the kernel session for this driver.
733 */
734 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &g_pKernelSession);
735 if (RT_SUCCESS(rc))
736 {
737 /*
738 * Create the kernel input device.
739 */
740 rc = vgdrvLinuxCreateInputDevice();
741 if (rc >= 0)
742 {
743#endif
744 /*
745 * Read host configuration.
746 */
747 VGDrvCommonProcessOptionsFromHost(&g_DevExt);
748
749 /*
750 * Finally, create the device nodes.
751 */
752 rc = vgdrvLinuxInitDeviceNodes();
753 if (rc >= 0)
754 {
755 /* some useful information for the user but don't show this on the console */
756 LogRel((DEVICE_NAME ": Successfully loaded version " VBOX_VERSION_STRING " r" __stringify(VBOX_SVN_REV) "\n"));
757 LogRel((DEVICE_NAME ": misc device minor %d, IRQ %d, I/O port %RTiop, MMIO at %RHp (size 0x%x)\n",
758 g_MiscDevice.minor, g_pPciDev->irq, g_IOPortBase, g_MMIOPhysAddr, g_cbMMIO));
759 printk(KERN_DEBUG DEVICE_NAME ": Successfully loaded version "
760 VBOX_VERSION_STRING " r" __stringify(VBOX_SVN_REV) " (interface " RT_XSTR(VMMDEV_VERSION) ")\n");
761 return rc;
762 }
763
764 /* bail out */
765#ifdef VBOXGUEST_WITH_INPUT_DRIVER
766 vgdrvLinuxTermInputDevice();
767 }
768 else
769 {
770 LogRel((DEVICE_NAME ": vboxguestCreateInputDevice failed with rc=%Rrc\n", rc));
771 rc = RTErrConvertFromErrno(rc);
772 }
773 VGDrvCommonCloseSession(&g_DevExt, g_pKernelSession);
774 }
775#endif
776 vgdrvLinuxTermISR();
777 }
778 VGDrvCommonDeleteDevExt(&g_DevExt);
779 }
780 else
781 {
782 LogRel((DEVICE_NAME ": VGDrvCommonInitDevExt failed with rc=%Rrc\n", rc));
783 rc = RTErrConvertFromErrno(rc);
784 }
785 }
786 else
787 {
788 LogRel((DEVICE_NAME ": PCI device not found, probably running on physical hardware.\n"));
789 rc = -ENODEV;
790 }
791 pci_unregister_driver(&g_PciDriver);
792 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
793 RTLogDestroy(RTLogSetDefaultInstance(NULL));
794 RTR0Term();
795 return rc;
796}
797
798
799/**
800 * Unload the module.
801 */
802static void __exit vgdrvLinuxModExit(void)
803{
804 /*
805 * Inverse order of init.
806 */
807 vgdrvLinuxTermDeviceNodes();
808#ifdef VBOXGUEST_WITH_INPUT_DRIVER
809 vgdrvLinuxTermInputDevice();
810 VGDrvCommonCloseSession(&g_DevExt, g_pKernelSession);
811#endif
812 vgdrvLinuxTermISR();
813 VGDrvCommonDeleteDevExt(&g_DevExt);
814 pci_unregister_driver(&g_PciDriver);
815 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
816 RTLogDestroy(RTLogSetDefaultInstance(NULL));
817 RTR0Term();
818}
819
820
821/**
822 * Get the process user ID.
823 *
824 * @returns UID.
825 */
826DECLINLINE(RTUID) vgdrvLinuxGetUid(void)
827{
828#if RTLNX_VER_MIN(2,6,29)
829# if RTLNX_VER_MIN(3,5,0)
830 return from_kuid(current_user_ns(), current->cred->uid);
831# else
832 return current->cred->uid;
833# endif
834#else
835 return current->uid;
836#endif
837}
838
839
840/**
841 * Checks if the given group number is zero or not.
842 *
843 * @returns true / false.
844 * @param gid The group to check for.
845 */
846DECLINLINE(bool) vgdrvLinuxIsGroupZero(kgid_t gid)
847{
848#if RTLNX_VER_MIN(3,5,0)
849 return from_kgid(current_user_ns(), gid);
850#else
851 return gid == 0;
852#endif
853}
854
855
856/**
857 * Searches the effective group and supplementary groups for @a gid.
858 *
859 * @returns true if member, false if not.
860 * @param gid The group to check for.
861 */
862DECLINLINE(RTGID) vgdrvLinuxIsInGroupEff(kgid_t gid)
863{
864 return in_egroup_p(gid) != 0;
865}
866
867
868/**
869 * Check if we can positively or negatively determine that the process is
870 * running under a login on the physical machine console.
871 *
872 * Havne't found a good way to figure this out for graphical sessions, so this
873 * is mostly pointless. But let us try do what we can do.
874 *
875 * @returns VMMDEV_REQUESTOR_CON_XXX.
876 */
877static uint32_t vgdrvLinuxRequestorOnConsole(void)
878{
879 uint32_t fRet = VMMDEV_REQUESTOR_CON_DONT_KNOW;
880
881#if RTLNX_VER_MIN(2,6,28) /* First with tty_kref_put(). */
882 /*
883 * Check for tty0..63, ASSUMING that these are only used for the physical console.
884 */
885 struct tty_struct *pTty = get_current_tty();
886 if (pTty)
887 {
888# if RTLNX_VER_MIN(4,2,0)
889 const char *pszName = tty_name(pTty);
890# else
891 char szBuf[64];
892 const char *pszName = tty_name(pTty, szBuf);
893# endif
894 if ( pszName
895 && pszName[0] == 't'
896 && pszName[1] == 't'
897 && pszName[2] == 'y'
898 && RT_C_IS_DIGIT(pszName[3])
899 && ( pszName[4] == '\0'
900 || ( RT_C_IS_DIGIT(pszName[4])
901 && pszName[5] == '\0'
902 && (pszName[3] - '0') * 10 + (pszName[4] - '0') <= 63)) )
903 fRet = VMMDEV_REQUESTOR_CON_YES;
904 tty_kref_put(pTty);
905 }
906#endif
907
908 return fRet;
909}
910
911
912/**
913 * Device open. Called on open /dev/vboxdrv
914 *
915 * @param pInode Pointer to inode info structure.
916 * @param pFilp Associated file pointer.
917 */
918static int vgdrvLinuxOpen(struct inode *pInode, struct file *pFilp)
919{
920 int rc;
921 PVBOXGUESTSESSION pSession;
922 uint32_t fRequestor;
923 Log((DEVICE_NAME ": pFilp=%p pid=%d/%d %s\n", pFilp, RTProcSelf(), current->pid, current->comm));
924
925 /*
926 * Figure out the requestor flags.
927 * ASSUMES that the gid of /dev/vboxuser is what we should consider the special vbox group.
928 */
929 fRequestor = VMMDEV_REQUESTOR_USERMODE | VMMDEV_REQUESTOR_TRUST_NOT_GIVEN;
930 if (vgdrvLinuxGetUid() == 0)
931 fRequestor |= VMMDEV_REQUESTOR_USR_ROOT;
932 else
933 fRequestor |= VMMDEV_REQUESTOR_USR_USER;
934 if (MINOR(pInode->i_rdev) == g_MiscDeviceUser.minor)
935 {
936 fRequestor |= VMMDEV_REQUESTOR_USER_DEVICE;
937 if (!vgdrvLinuxIsGroupZero(pInode->i_gid) && vgdrvLinuxIsInGroupEff(pInode->i_gid))
938 fRequestor |= VMMDEV_REQUESTOR_GRP_VBOX;
939 }
940 fRequestor |= vgdrvLinuxRequestorOnConsole();
941
942 /*
943 * Call common code to create the user session. Associate it with
944 * the file so we can access it in the other methods.
945 */
946 rc = VGDrvCommonCreateUserSession(&g_DevExt, fRequestor, &pSession);
947 if (RT_SUCCESS(rc))
948 pFilp->private_data = pSession;
949
950 Log(("vgdrvLinuxOpen: g_DevExt=%p pSession=%p rc=%d/%d (pid=%d/%d %s)\n",
951 &g_DevExt, pSession, rc, vgdrvLinuxConvertToNegErrno(rc), RTProcSelf(), current->pid, current->comm));
952 return vgdrvLinuxConvertToNegErrno(rc);
953}
954
955
956/**
957 * Close device.
958 *
959 * @param pInode Pointer to inode info structure.
960 * @param pFilp Associated file pointer.
961 */
962static int vgdrvLinuxRelease(struct inode *pInode, struct file *pFilp)
963{
964 Log(("vgdrvLinuxRelease: pFilp=%p pSession=%p pid=%d/%d %s\n",
965 pFilp, pFilp->private_data, RTProcSelf(), current->pid, current->comm));
966
967#if RTLNX_VER_MAX(2,6,28)
968 /* This housekeeping was needed in older kernel versions to ensure that
969 * the file pointer didn't get left on the polling queue. */
970 vgdrvLinuxFAsync(-1, pFilp, 0);
971#endif
972 VGDrvCommonCloseSession(&g_DevExt, (PVBOXGUESTSESSION)pFilp->private_data);
973 pFilp->private_data = NULL;
974 return 0;
975}
976
977
978/**
979 * Device I/O Control entry point.
980 *
981 * @param pFilp Associated file pointer.
982 * @param uCmd The function specified to ioctl().
983 * @param ulArg The argument specified to ioctl().
984 */
985#if defined(HAVE_UNLOCKED_IOCTL) || defined(DOXYGEN_RUNNING)
986static long vgdrvLinuxIOCtl(struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
987#else
988static int vgdrvLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
989#endif
990{
991 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pFilp->private_data;
992 int rc;
993#ifndef HAVE_UNLOCKED_IOCTL
994 unlock_kernel();
995#endif
996
997#if 0 /* no fast I/O controls defined atm. */
998 if (RT_LIKELY( ( uCmd == SUP_IOCTL_FAST_DO_RAW_RUN
999 || uCmd == SUP_IOCTL_FAST_DO_HM_RUN
1000 || uCmd == SUP_IOCTL_FAST_DO_NOP)
1001 && pSession->fUnrestricted == true))
1002 rc = VGDrvCommonIoCtlFast(uCmd, ulArg, &g_DevExt, pSession);
1003 else
1004#endif
1005 rc = vgdrvLinuxIOCtlSlow(pFilp, uCmd, ulArg, pSession);
1006
1007#ifndef HAVE_UNLOCKED_IOCTL
1008 lock_kernel();
1009#endif
1010 return rc;
1011}
1012
1013
1014/**
1015 * Device I/O Control entry point, slow variant.
1016 *
1017 * @param pFilp Associated file pointer.
1018 * @param uCmd The function specified to ioctl().
1019 * @param ulArg The argument specified to ioctl().
1020 * @param pSession The session instance.
1021 */
1022static int vgdrvLinuxIOCtlSlow(struct file *pFilp, unsigned int uCmd, unsigned long ulArg, PVBOXGUESTSESSION pSession)
1023{
1024 int rc;
1025 VBGLREQHDR Hdr;
1026 PVBGLREQHDR pHdr;
1027 uint32_t cbBuf;
1028
1029 Log6(("vgdrvLinuxIOCtlSlow: pFilp=%p uCmd=%#x ulArg=%p pid=%d/%d\n", pFilp, uCmd, (void *)ulArg, RTProcSelf(), current->pid));
1030
1031 /*
1032 * Read the header.
1033 */
1034 if (RT_FAILURE(RTR0MemUserCopyFrom(&Hdr, ulArg, sizeof(Hdr))))
1035 {
1036 Log(("vgdrvLinuxIOCtlSlow: copy_from_user(,%#lx,) failed; uCmd=%#x\n", ulArg, uCmd));
1037 return -EFAULT;
1038 }
1039 if (RT_UNLIKELY(Hdr.uVersion != VBGLREQHDR_VERSION))
1040 {
1041 Log(("vgdrvLinuxIOCtlSlow: bad header version %#x; uCmd=%#x\n", Hdr.uVersion, uCmd));
1042 return -EINVAL;
1043 }
1044
1045 /*
1046 * Buffer the request.
1047 * Note! The header is revalidated by the common code.
1048 */
1049 cbBuf = RT_MAX(Hdr.cbIn, Hdr.cbOut);
1050 if (RT_UNLIKELY(cbBuf > _1M*16))
1051 {
1052 Log(("vgdrvLinuxIOCtlSlow: too big cbBuf=%#x; uCmd=%#x\n", cbBuf, uCmd));
1053 return -E2BIG;
1054 }
1055 if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr)
1056 || (cbBuf != _IOC_SIZE(uCmd) && _IOC_SIZE(uCmd) != 0)))
1057 {
1058 Log(("vgdrvLinuxIOCtlSlow: bad ioctl cbBuf=%#x _IOC_SIZE=%#x; uCmd=%#x\n", cbBuf, _IOC_SIZE(uCmd), uCmd));
1059 return -EINVAL;
1060 }
1061 pHdr = RTMemAlloc(cbBuf);
1062 if (RT_UNLIKELY(!pHdr))
1063 {
1064 LogRel(("vgdrvLinuxIOCtlSlow: failed to allocate buffer of %d bytes for uCmd=%#x\n", cbBuf, uCmd));
1065 return -ENOMEM;
1066 }
1067 if (RT_FAILURE(RTR0MemUserCopyFrom(pHdr, ulArg, Hdr.cbIn)))
1068 {
1069 Log(("vgdrvLinuxIOCtlSlow: copy_from_user(,%#lx, %#x) failed; uCmd=%#x\n", ulArg, Hdr.cbIn, uCmd));
1070 RTMemFree(pHdr);
1071 return -EFAULT;
1072 }
1073 if (Hdr.cbIn < cbBuf)
1074 RT_BZERO((uint8_t *)pHdr + Hdr.cbIn, cbBuf - Hdr.cbIn);
1075
1076 /*
1077 * Process the IOCtl.
1078 */
1079 rc = VGDrvCommonIoCtl(uCmd, &g_DevExt, pSession, pHdr, cbBuf);
1080
1081 /*
1082 * Copy ioctl data and output buffer back to user space.
1083 */
1084 if (RT_SUCCESS(rc))
1085 {
1086 uint32_t cbOut = pHdr->cbOut;
1087 if (RT_UNLIKELY(cbOut > cbBuf))
1088 {
1089 LogRel(("vgdrvLinuxIOCtlSlow: too much output! %#x > %#x; uCmd=%#x!\n", cbOut, cbBuf, uCmd));
1090 cbOut = cbBuf;
1091 }
1092 if (RT_FAILURE(RTR0MemUserCopyTo(ulArg, pHdr, cbOut)))
1093 {
1094 /* this is really bad! */
1095 LogRel(("vgdrvLinuxIOCtlSlow: copy_to_user(%#lx,,%#x); uCmd=%#x!\n", ulArg, cbOut, uCmd));
1096 rc = -EFAULT;
1097 }
1098 }
1099 else
1100 {
1101 Log(("vgdrvLinuxIOCtlSlow: pFilp=%p uCmd=%#x ulArg=%p failed, rc=%d\n", pFilp, uCmd, (void *)ulArg, rc));
1102 rc = -EINVAL;
1103 }
1104 RTMemFree(pHdr);
1105
1106 Log6(("vgdrvLinuxIOCtlSlow: returns %d (pid=%d/%d)\n", rc, RTProcSelf(), current->pid));
1107 return rc;
1108}
1109
1110
1111/**
1112 * @note This code is duplicated on other platforms with variations, so please
1113 * keep them all up to date when making changes!
1114 */
1115int VBOXCALL VBoxGuestIDC(void *pvSession, uintptr_t uReq, PVBGLREQHDR pReqHdr, size_t cbReq)
1116{
1117 /*
1118 * Simple request validation (common code does the rest).
1119 */
1120 int rc;
1121 if ( RT_VALID_PTR(pReqHdr)
1122 && cbReq >= sizeof(*pReqHdr))
1123 {
1124 /*
1125 * All requests except the connect one requires a valid session.
1126 */
1127 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
1128 if (pSession)
1129 {
1130 if ( RT_VALID_PTR(pSession)
1131 && pSession->pDevExt == &g_DevExt)
1132 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
1133 else
1134 rc = VERR_INVALID_HANDLE;
1135 }
1136 else if (uReq == VBGL_IOCTL_IDC_CONNECT)
1137 {
1138 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &pSession);
1139 if (RT_SUCCESS(rc))
1140 {
1141 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
1142 if (RT_FAILURE(rc))
1143 VGDrvCommonCloseSession(&g_DevExt, pSession);
1144 }
1145 }
1146 else
1147 rc = VERR_INVALID_HANDLE;
1148 }
1149 else
1150 rc = VERR_INVALID_POINTER;
1151 return rc;
1152}
1153EXPORT_SYMBOL(VBoxGuestIDC);
1154
1155
1156/**
1157 * Asynchronous notification activation method.
1158 *
1159 * @returns 0 on success, negative errno on failure.
1160 *
1161 * @param fd The file descriptor.
1162 * @param pFile The file structure.
1163 * @param fOn On/off indicator.
1164 */
1165static int vgdrvLinuxFAsync(int fd, struct file *pFile, int fOn)
1166{
1167 return fasync_helper(fd, pFile, fOn, &g_pFAsyncQueue);
1168}
1169
1170
1171/**
1172 * Poll function.
1173 *
1174 * This returns ready to read if the mouse pointer mode or the pointer position
1175 * has changed since last call to read.
1176 *
1177 * @returns 0 if no changes, POLLIN | POLLRDNORM if there are unseen changes.
1178 *
1179 * @param pFile The file structure.
1180 * @param pPt The poll table.
1181 *
1182 * @remarks This is probably not really used, X11 is said to use the fasync
1183 * interface instead.
1184 */
1185static unsigned int vgdrvLinuxPoll(struct file *pFile, poll_table *pPt)
1186{
1187 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pFile->private_data;
1188 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
1189 unsigned int fMask = pSession->u32MousePosChangedSeq != u32CurSeq
1190 ? POLLIN | POLLRDNORM
1191 : 0;
1192 poll_wait(pFile, &g_PollEventQueue, pPt);
1193 return fMask;
1194}
1195
1196
1197/**
1198 * Read to go with our poll/fasync response.
1199 *
1200 * @returns 1 or -EINVAL.
1201 *
1202 * @param pFile The file structure.
1203 * @param pbBuf The buffer to read into.
1204 * @param cbRead The max number of bytes to read.
1205 * @param poff The current file position.
1206 *
1207 * @remarks This is probably not really used as X11 lets the driver do its own
1208 * event reading. The poll condition is therefore also cleared when we
1209 * see VMMDevReq_GetMouseStatus in vgdrvIoCtl_VMMRequest.
1210 */
1211static ssize_t vgdrvLinuxRead(struct file *pFile, char *pbBuf, size_t cbRead, loff_t *poff)
1212{
1213 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pFile->private_data;
1214 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
1215
1216 if (*poff != 0)
1217 return -EINVAL;
1218
1219 /*
1220 * Fake a single byte read if we're not up to date with the current mouse position.
1221 */
1222 if ( pSession->u32MousePosChangedSeq != u32CurSeq
1223 && cbRead > 0)
1224 {
1225 pSession->u32MousePosChangedSeq = u32CurSeq;
1226 pbBuf[0] = 0;
1227 return 1;
1228 }
1229 return 0;
1230}
1231
1232
1233#ifdef VBOXGUEST_WITH_INPUT_DRIVER
1234/**
1235 * Get host mouse state.
1236 *
1237 * @returns IPRT status code.
1238 * @param pfMouseFeatures Where to store host mouse capabilities.
1239 * @param pX Where to store X axis coordinate.
1240 * @param pY Where to store Y axis coordinate.
1241 * @param pDz Where to store vertical wheel movement offset (only set if in case of VMMDevReq_GetMouseStatusEx request).
1242 * @param pDw Where to store horizontal wheel movement offset (only set if in case of VMMDevReq_GetMouseStatusEx request).
1243 * @param pfButtons Where to store mouse buttons state (only set if in case of VMMDevReq_GetMouseStatusEx request).
1244 */
1245static int vgdrvLinuxGetHostMouseState(uint32_t *pfMouseFeatures, int32_t *pX, int32_t *pY, int32_t *pDz, int32_t *pDw, uint32_t *pfButtons)
1246{
1247 int rc = VERR_INVALID_PARAMETER;
1248
1249 Assert(pfMouseFeatures);
1250 Assert(pX);
1251 Assert(pY);
1252 Assert(pDz);
1253 Assert(pDw);
1254 Assert(pfButtons);
1255
1256 /* Initialize legacy request data. */
1257 g_pMouseStatusReqEx->Core.mouseFeatures = 0;
1258 g_pMouseStatusReqEx->Core.pointerXPos = 0;
1259 g_pMouseStatusReqEx->Core.pointerYPos = 0;
1260
1261 /* Initialize extended request data if VMMDevReq_GetMouseStatusEx is used. */
1262 if (vgdrvLinuxUsesMouseStatusEx())
1263 {
1264 g_pMouseStatusReqEx->dz = 0;
1265 g_pMouseStatusReqEx->dw = 0;
1266 g_pMouseStatusReqEx->fButtons = 0;
1267 }
1268
1269 /* Get host mouse state - either lagacy or extended version. */
1270 rc = VbglR0GRPerform(&g_pMouseStatusReqEx->Core.header);
1271 if (RT_SUCCESS(rc))
1272 {
1273 *pfMouseFeatures = g_pMouseStatusReqEx->Core.mouseFeatures;
1274 *pX = g_pMouseStatusReqEx->Core.pointerXPos;
1275 *pY = g_pMouseStatusReqEx->Core.pointerYPos;
1276
1277 /* Get extended mouse state data in case of VMMDevReq_GetMouseStatusEx. */
1278 if (vgdrvLinuxUsesMouseStatusEx())
1279 {
1280 *pDz = g_pMouseStatusReqEx->dz;
1281 *pDw = g_pMouseStatusReqEx->dw;
1282 *pfButtons = g_pMouseStatusReqEx->fButtons;
1283 }
1284 }
1285
1286 return rc;
1287}
1288#endif /* VBOXGUEST_WITH_INPUT_DRIVER */
1289
1290
1291void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
1292{
1293#ifdef VBOXGUEST_WITH_INPUT_DRIVER
1294 int rc;
1295 uint32_t fMouseFeatures = 0;
1296 int32_t x = 0;
1297 int32_t y = 0;
1298 int32_t dz = 0;
1299 int32_t dw = 0;
1300 uint32_t fButtons = 0;
1301#endif
1302 NOREF(pDevExt);
1303
1304 /*
1305 * Wake up everyone that's in a poll() and post anyone that has
1306 * subscribed to async notifications.
1307 */
1308 Log3(("VGDrvNativeISRMousePollEvent: wake_up_all\n"));
1309 wake_up_all(&g_PollEventQueue);
1310 Log3(("VGDrvNativeISRMousePollEvent: kill_fasync\n"));
1311 kill_fasync(&g_pFAsyncQueue, SIGIO, POLL_IN);
1312#ifdef VBOXGUEST_WITH_INPUT_DRIVER
1313 rc = vgdrvLinuxGetHostMouseState(&fMouseFeatures, &x, &y, &dz, &dw, &fButtons);
1314 if (RT_SUCCESS(rc))
1315 {
1316 input_report_abs(g_pInputDevice, ABS_X, x);
1317 input_report_abs(g_pInputDevice, ABS_Y, y);
1318
1319 if ( vgdrvLinuxUsesMouseStatusEx()
1320 && fMouseFeatures & VMMDEV_MOUSE_HOST_USES_FULL_STATE_PROTOCOL
1321 && fMouseFeatures & VMMDEV_MOUSE_GUEST_USES_FULL_STATE_PROTOCOL)
1322 {
1323 /* Vertical and horizontal scroll values come as-is from GUI.
1324 * Invert values here as it is done in PS/2 mouse driver, so
1325 * scrolling direction will be exectly the same. */
1326 input_report_rel(g_pInputDevice, REL_WHEEL, -dz);
1327 input_report_rel(g_pInputDevice, REL_HWHEEL, -dw);
1328
1329 input_report_key(g_pInputDevice, BTN_LEFT, RT_BOOL(fButtons & VMMDEV_MOUSE_BUTTON_LEFT));
1330 input_report_key(g_pInputDevice, BTN_RIGHT, RT_BOOL(fButtons & VMMDEV_MOUSE_BUTTON_RIGHT));
1331 input_report_key(g_pInputDevice, BTN_MIDDLE, RT_BOOL(fButtons & VMMDEV_MOUSE_BUTTON_MIDDLE));
1332 input_report_key(g_pInputDevice, BTN_SIDE, RT_BOOL(fButtons & VMMDEV_MOUSE_BUTTON_X1));
1333 input_report_key(g_pInputDevice, BTN_EXTRA, RT_BOOL(fButtons & VMMDEV_MOUSE_BUTTON_X2));
1334 }
1335
1336# ifdef EV_SYN
1337 input_sync(g_pInputDevice);
1338# endif
1339 }
1340#endif
1341 Log3(("VGDrvNativeISRMousePollEvent: done\n"));
1342}
1343
1344
1345bool VGDrvNativeProcessOption(PVBOXGUESTDEVEXT pDevExt, const char *pszName, const char *pszValue)
1346{
1347 RT_NOREF(pDevExt); RT_NOREF(pszName); RT_NOREF(pszValue);
1348 return false;
1349}
1350
1351
1352#if RTLNX_VER_MIN(2,6,0)
1353
1354/** log and dbg_log parameter setter. */
1355static int vgdrvLinuxParamLogGrpSet(const char *pszValue, CONST_4_15 struct kernel_param *pParam)
1356{
1357 if (g_fLoggerCreated)
1358 {
1359 PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelGetDefaultInstance();
1360 if (pLogger)
1361 RTLogGroupSettings(pLogger, pszValue);
1362 }
1363 else if (pParam->name[0] != 'd')
1364 strlcpy(&g_szLogGrp[0], pszValue, sizeof(g_szLogGrp));
1365
1366 return 0;
1367}
1368
1369/** log and dbg_log parameter getter. */
1370static int vgdrvLinuxParamLogGrpGet(char *pszBuf, CONST_4_15 struct kernel_param *pParam)
1371{
1372 PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelGetDefaultInstance();
1373 *pszBuf = '\0';
1374 if (pLogger)
1375 RTLogQueryGroupSettings(pLogger, pszBuf, _4K);
1376 return strlen(pszBuf);
1377}
1378
1379
1380/** log and dbg_log_flags parameter setter. */
1381static int vgdrvLinuxParamLogFlagsSet(const char *pszValue, CONST_4_15 struct kernel_param *pParam)
1382{
1383 if (g_fLoggerCreated)
1384 {
1385 PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelGetDefaultInstance();
1386 if (pLogger)
1387 RTLogFlags(pLogger, pszValue);
1388 }
1389 else if (pParam->name[0] != 'd')
1390 strlcpy(&g_szLogFlags[0], pszValue, sizeof(g_szLogFlags));
1391 return 0;
1392}
1393
1394/** log and dbg_log_flags parameter getter. */
1395static int vgdrvLinuxParamLogFlagsGet(char *pszBuf, CONST_4_15 struct kernel_param *pParam)
1396{
1397 PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelGetDefaultInstance();
1398 *pszBuf = '\0';
1399 if (pLogger)
1400 RTLogQueryFlags(pLogger, pszBuf, _4K);
1401 return strlen(pszBuf);
1402}
1403
1404
1405/** log and dbg_log_dest parameter setter. */
1406static int vgdrvLinuxParamLogDstSet(const char *pszValue, CONST_4_15 struct kernel_param *pParam)
1407{
1408 if (g_fLoggerCreated)
1409 {
1410 PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelGetDefaultInstance();
1411 if (pLogger)
1412 RTLogDestinations(pLogger, pszValue);
1413 }
1414 else if (pParam->name[0] != 'd')
1415 strlcpy(&g_szLogDst[0], pszValue, sizeof(g_szLogDst));
1416 return 0;
1417}
1418
1419/** log and dbg_log_dest parameter getter. */
1420static int vgdrvLinuxParamLogDstGet(char *pszBuf, CONST_4_15 struct kernel_param *pParam)
1421{
1422 PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelGetDefaultInstance();
1423 *pszBuf = '\0';
1424 if (pLogger)
1425 RTLogQueryDestinations(pLogger, pszBuf, _4K);
1426 return strlen(pszBuf);
1427}
1428
1429
1430/** r3_log_to_host parameter setter. */
1431static int vgdrvLinuxParamR3LogToHostSet(const char *pszValue, CONST_4_15 struct kernel_param *pParam)
1432{
1433 g_DevExt.fLoggingEnabled = VBDrvCommonIsOptionValueTrue(pszValue);
1434 return 0;
1435}
1436
1437/** r3_log_to_host parameter getter. */
1438static int vgdrvLinuxParamR3LogToHostGet(char *pszBuf, CONST_4_15 struct kernel_param *pParam)
1439{
1440 strcpy(pszBuf, g_DevExt.fLoggingEnabled ? "enabled" : "disabled");
1441 return strlen(pszBuf);
1442}
1443
1444
1445/*
1446 * Define module parameters.
1447 */
1448module_param_call(log, vgdrvLinuxParamLogGrpSet, vgdrvLinuxParamLogGrpGet, NULL, 0664);
1449module_param_call(log_flags, vgdrvLinuxParamLogFlagsSet, vgdrvLinuxParamLogFlagsGet, NULL, 0664);
1450module_param_call(log_dest, vgdrvLinuxParamLogDstSet, vgdrvLinuxParamLogDstGet, NULL, 0664);
1451# ifdef LOG_ENABLED
1452module_param_call(dbg_log, vgdrvLinuxParamLogGrpSet, vgdrvLinuxParamLogGrpGet, NULL, 0664);
1453module_param_call(dbg_log_flags, vgdrvLinuxParamLogFlagsSet, vgdrvLinuxParamLogFlagsGet, NULL, 0664);
1454module_param_call(dbg_log_dest, vgdrvLinuxParamLogDstSet, vgdrvLinuxParamLogDstGet, NULL, 0664);
1455# endif
1456module_param_call(r3_log_to_host, vgdrvLinuxParamR3LogToHostSet, vgdrvLinuxParamR3LogToHostGet, NULL, 0664);
1457
1458#endif /* 2.6.0 and later */
1459
1460
1461module_init(vgdrvLinuxModInit);
1462module_exit(vgdrvLinuxModExit);
1463
1464MODULE_AUTHOR(VBOX_VENDOR);
1465MODULE_DESCRIPTION(VBOX_PRODUCT " Guest Additions for Linux Module");
1466MODULE_LICENSE("GPL");
1467#ifdef MODULE_VERSION
1468MODULE_VERSION(VBOX_VERSION_STRING " r" RT_XSTR(VBOX_SVN_REV));
1469#endif
1470
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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