VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/vboxvideo/vboxvideo.c@ 50900

Last change on this file since 50900 was 50668, checked in by vboxsync, 11 years ago

Additions/x11/vboxvideo: Fix debug crash on Solaris 10 (ends up calling strlen with NULL and strlen doesn't check for it).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.0 KB
Line 
1/* $Id: vboxvideo.c 50668 2014-03-03 16:22:36Z vboxsync $ */
2/** @file
3 *
4 * Linux Additions X11 graphics driver
5 */
6
7/*
8 * Copyright (C) 2006-2013 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 * --------------------------------------------------------------------
18 *
19 * This code is based on the X.Org VESA driver with the following copyrights:
20 *
21 * Copyright (c) 2000 by Conectiva S.A. (http://www.conectiva.com)
22 * Copyright 2008 Red Hat, Inc.
23 * Copyright 2012 Red Hat, Inc.
24 *
25 * and the following permission notice (not all original sourse files include
26 * the last paragraph):
27 *
28 * Permission is hereby granted, free of charge, to any person obtaining a
29 * copy of this software and associated documentation files (the "Software"),
30 * to deal in the Software without restriction, including without limitation
31 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
32 * and/or sell copies of the Software, and to permit persons to whom the
33 * Software is furnished to do so, subject to the following conditions:
34 *
35 * The above copyright notice and this permission notice shall be included in
36 * all copies or substantial portions of the Software.
37 *
38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
41 * CONECTIVA LINUX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
42 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
43 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SOFTWARE.
45 *
46 * Except as contained in this notice, the name of Conectiva Linux shall
47 * not be used in advertising or otherwise to promote the sale, use or other
48 * dealings in this Software without prior written authorization from
49 * Conectiva Linux.
50 *
51 * Authors: Paulo César Pereira de Andrade <[email protected]>
52 * David Dawes <[email protected]>
53 * Adam Jackson <[email protected]>
54 * Dave Airlie <[email protected]>
55 */
56
57#ifdef XORG_7X
58# include <stdlib.h>
59# include <string.h>
60#endif
61
62#include "xf86.h"
63#include "xf86_OSproc.h"
64#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
65# include "xf86Resources.h"
66#endif
67
68#ifndef PCIACCESS
69/* Drivers for PCI hardware need this */
70# include "xf86PciInfo.h"
71/* Drivers that need to access the PCI config space directly need this */
72# include "xf86Pci.h"
73#endif
74
75#include "fb.h"
76
77#include "vboxvideo.h"
78#include <VBox/VBoxGuest.h>
79#include "version-generated.h"
80#include "product-generated.h"
81#include <xf86.h>
82#include <misc.h>
83
84/* All drivers initialising the SW cursor need this */
85#include "mipointer.h"
86
87/* Colormap handling */
88#include "micmap.h"
89#include "xf86cmap.h"
90
91/* DPMS */
92/* #define DPMS_SERVER
93#include "extensions/dpms.h" */
94
95/* VGA hardware functions for setting and restoring text mode */
96#include "vgaHW.h"
97
98#ifdef VBOXVIDEO_13
99/* X.org 1.3+ mode setting */
100# define _HAVE_STRING_ARCH_strsep /* bits/string2.h, __strsep_1c. */
101# include "xf86Crtc.h"
102# include "xf86Modes.h"
103#endif
104
105/* For setting the root window property. */
106#include <X11/Xatom.h>
107#include "property.h"
108
109#ifdef VBOX_DRI
110# include "xf86drm.h"
111#endif
112
113/* Mandatory functions */
114
115static const OptionInfoRec * VBOXAvailableOptions(int chipid, int busid);
116static void VBOXIdentify(int flags);
117#ifndef PCIACCESS
118static Bool VBOXProbe(DriverPtr drv, int flags);
119#else
120static Bool VBOXPciProbe(DriverPtr drv, int entity_num,
121 struct pci_device *dev, intptr_t match_data);
122#endif
123static Bool VBOXPreInit(ScrnInfoPtr pScrn, int flags);
124static Bool VBOXScreenInit(ScreenPtr pScreen, int argc, char **argv);
125static Bool VBOXEnterVT(ScrnInfoPtr pScrn);
126static void VBOXLeaveVT(ScrnInfoPtr pScrn);
127static Bool VBOXCloseScreen(ScreenPtr pScreen);
128static Bool VBOXSaveScreen(ScreenPtr pScreen, int mode);
129static Bool VBOXSwitchMode(ScrnInfoPtr pScrn, DisplayModePtr pMode);
130static void VBOXAdjustFrame(ScrnInfoPtr pScrn, int x, int y);
131static void VBOXFreeScreen(ScrnInfoPtr pScrn);
132static void VBOXDisplayPowerManagementSet(ScrnInfoPtr pScrn, int mode,
133 int flags);
134
135/* locally used functions */
136static Bool VBOXMapVidMem(ScrnInfoPtr pScrn);
137static void VBOXUnmapVidMem(ScrnInfoPtr pScrn);
138static void VBOXSaveMode(ScrnInfoPtr pScrn);
139static void VBOXRestoreMode(ScrnInfoPtr pScrn);
140
141static inline void VBOXSetRec(ScrnInfoPtr pScrn)
142{
143 if (!pScrn->driverPrivate)
144 pScrn->driverPrivate = calloc(sizeof(VBOXRec), 1);
145}
146
147enum GenericTypes
148{
149 CHIP_VBOX_GENERIC
150};
151
152#ifdef PCIACCESS
153static const struct pci_id_match vbox_device_match[] = {
154 {
155 VBOX_VENDORID, VBOX_DEVICEID, PCI_MATCH_ANY, PCI_MATCH_ANY,
156 0, 0, 0
157 },
158
159 { 0, 0, 0 },
160};
161#endif
162
163/* Supported chipsets */
164static SymTabRec VBOXChipsets[] =
165{
166 {VBOX_DEVICEID, "vbox"},
167 {-1, NULL}
168};
169
170static PciChipsets VBOXPCIchipsets[] = {
171 { VBOX_DEVICEID, VBOX_DEVICEID, RES_SHARED_VGA },
172 { -1, -1, RES_UNDEFINED },
173};
174
175/*
176 * This contains the functions needed by the server after loading the
177 * driver module. It must be supplied, and gets added the driver list by
178 * the Module Setup function in the dynamic case. In the static case a
179 * reference to this is compiled in, and this requires that the name of
180 * this DriverRec be an upper-case version of the driver name.
181 */
182
183#ifdef XORG_7X
184_X_EXPORT
185#endif
186DriverRec VBOXVIDEO = {
187 VBOX_VERSION,
188 VBOX_DRIVER_NAME,
189 VBOXIdentify,
190#ifdef PCIACCESS
191 NULL,
192#else
193 VBOXProbe,
194#endif
195 VBOXAvailableOptions,
196 NULL,
197 0,
198#ifdef XORG_7X
199 NULL,
200#endif
201#ifdef PCIACCESS
202 vbox_device_match,
203 VBOXPciProbe
204#endif
205};
206
207/* No options for now */
208static const OptionInfoRec VBOXOptions[] = {
209 { -1, NULL, OPTV_NONE, {0}, FALSE }
210};
211
212#ifndef XORG_7X
213/*
214 * List of symbols from other modules that this module references. This
215 * list is used to tell the loader that it is OK for symbols here to be
216 * unresolved providing that it hasn't been told that they haven't been
217 * told that they are essential via a call to xf86LoaderReqSymbols() or
218 * xf86LoaderReqSymLists(). The purpose is this is to avoid warnings about
219 * unresolved symbols that are not required.
220 */
221static const char *fbSymbols[] = {
222 "fbPictureInit",
223 "fbScreenInit",
224 NULL
225};
226
227static const char *shadowfbSymbols[] = {
228 "ShadowFBInit2",
229 NULL
230};
231
232static const char *ramdacSymbols[] = {
233 "xf86InitCursor",
234 "xf86CreateCursorInfoRec",
235 NULL
236};
237
238static const char *vgahwSymbols[] = {
239 "vgaHWFreeHWRec",
240 "vgaHWGetHWRec",
241 "vgaHWGetIOBase",
242 "vgaHWGetIndex",
243 "vgaHWRestore",
244 "vgaHWSave",
245 "vgaHWSetStdFuncs",
246 NULL
247};
248#endif /* !XORG_7X */
249
250#ifdef VBOXVIDEO_13
251/* X.org 1.3+ mode-setting support ******************************************/
252
253/* For descriptions of these functions and structures, see
254 hw/xfree86/modes/xf86Crtc.h and hw/xfree86/modes/xf86Modes.h in the
255 X.Org source tree. */
256
257static Bool vbox_config_resize(ScrnInfoPtr pScrn, int cw, int ch)
258{
259 VBOXPtr pVBox = VBOXGetRec(pScrn);
260 TRACE_LOG("width=%d, height=%d\n", cw, ch);
261 /* Save the size in case we need to re-set it later. */
262 pVBox->FBSize.cx = cw;
263 pVBox->FBSize.cy = ch;
264 /* Don't fiddle with the hardware if we are switched
265 * to a virtual terminal. */
266 if (!pScrn->vtSema) {
267 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
268 "We do not own the active VT, exiting.\n");
269 return TRUE;
270 }
271 return VBOXAdjustScreenPixmap(pScrn, cw, ch);
272}
273
274static const xf86CrtcConfigFuncsRec VBOXCrtcConfigFuncs = {
275 vbox_config_resize
276};
277
278static void
279vbox_crtc_dpms(xf86CrtcPtr crtc, int mode)
280{
281 VBOXPtr pVBox = VBOXGetRec(crtc->scrn);
282 unsigned cDisplay = (uintptr_t)crtc->driver_private;
283 TRACE_LOG("cDisplay=%u, mode=%i\n", cDisplay, mode);
284 pVBox->afDisabled[cDisplay] = (mode != DPMSModeOn);
285 /* Don't fiddle with the hardware if we are switched
286 * to a virtual terminal. */
287 if (!crtc->scrn->vtSema) {
288 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
289 "We do not own the active VT, exiting.\n");
290 return;
291 }
292 if ( pVBox->aScreenLocation[cDisplay].cx
293 && pVBox->aScreenLocation[cDisplay].cy)
294 VBOXSetMode(crtc->scrn, cDisplay,
295 pVBox->aScreenLocation[cDisplay].cx,
296 pVBox->aScreenLocation[cDisplay].cy,
297 pVBox->aScreenLocation[cDisplay].x,
298 pVBox->aScreenLocation[cDisplay].y);
299}
300
301static Bool
302vbox_crtc_lock (xf86CrtcPtr crtc)
303{ (void) crtc; return FALSE; }
304
305
306/* We use this function to check whether the X server owns the active virtual
307 * terminal before attempting a mode switch, since the RandR extension isn't
308 * very dilligent here, which can mean crashes if we are unlucky. This is
309 * not the way it the function is intended - it is meant for reporting modes
310 * which the hardware can't handle. I hope that this won't confuse any clients
311 * connecting to us. */
312static Bool
313vbox_crtc_mode_fixup (xf86CrtcPtr crtc, DisplayModePtr mode,
314 DisplayModePtr adjusted_mode)
315{ (void) crtc; (void) mode; (void) adjusted_mode; return TRUE; }
316
317static void
318vbox_crtc_stub (xf86CrtcPtr crtc)
319{ (void) crtc; }
320
321static void
322vbox_crtc_mode_set (xf86CrtcPtr crtc, DisplayModePtr mode,
323 DisplayModePtr adjusted_mode, int x, int y)
324{
325 (void) mode;
326 VBOXPtr pVBox = VBOXGetRec(crtc->scrn);
327 unsigned cDisplay = (uintptr_t)crtc->driver_private;
328
329 TRACE_LOG("name=%s, HDisplay=%d, VDisplay=%d, x=%d, y=%d\n", adjusted_mode->name,
330 adjusted_mode->HDisplay, adjusted_mode->VDisplay, x, y);
331 pVBox->afDisabled[cDisplay] = false;
332 pVBox->aScreenLocation[cDisplay].cx = adjusted_mode->HDisplay;
333 pVBox->aScreenLocation[cDisplay].cy = adjusted_mode->VDisplay;
334 pVBox->aScreenLocation[cDisplay].x = x;
335 pVBox->aScreenLocation[cDisplay].y = y;
336 /* Don't remember any modes set while we are seamless, as they are
337 * just temporary. */
338 if (!vboxGuestIsSeamless(crtc->scrn))
339 vboxSaveVideoMode(crtc->scrn, adjusted_mode->HDisplay,
340 adjusted_mode->VDisplay, crtc->scrn->bitsPerPixel);
341 /* Don't fiddle with the hardware if we are switched
342 * to a virtual terminal. */
343 if (!crtc->scrn->vtSema)
344 {
345 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
346 "We do not own the active VT, exiting.\n");
347 return;
348 }
349 VBOXSetMode(crtc->scrn, cDisplay, adjusted_mode->HDisplay,
350 adjusted_mode->VDisplay, x, y);
351}
352
353static void
354vbox_crtc_gamma_set (xf86CrtcPtr crtc, CARD16 *red,
355 CARD16 *green, CARD16 *blue, int size)
356{ (void) crtc; (void) red; (void) green; (void) blue; (void) size; }
357
358static void *
359vbox_crtc_shadow_allocate (xf86CrtcPtr crtc, int width, int height)
360{ (void) crtc; (void) width; (void) height; return NULL; }
361
362static const xf86CrtcFuncsRec VBOXCrtcFuncs = {
363 .dpms = vbox_crtc_dpms,
364 .save = NULL, /* These two are never called by the server. */
365 .restore = NULL,
366 .lock = vbox_crtc_lock,
367 .unlock = NULL, /* This will not be invoked if lock returns FALSE. */
368 .mode_fixup = vbox_crtc_mode_fixup,
369 .prepare = vbox_crtc_stub,
370 .mode_set = vbox_crtc_mode_set,
371 .commit = vbox_crtc_stub,
372 .gamma_set = vbox_crtc_gamma_set,
373 .shadow_allocate = vbox_crtc_shadow_allocate,
374 .shadow_create = NULL, /* These two should not be invoked if allocate
375 returns NULL. */
376 .shadow_destroy = NULL,
377 .set_cursor_colors = NULL, /* We are still using the old cursor API. */
378 .set_cursor_position = NULL,
379 .show_cursor = NULL,
380 .hide_cursor = NULL,
381 .load_cursor_argb = NULL,
382 .destroy = vbox_crtc_stub
383};
384
385static void
386vbox_output_stub (xf86OutputPtr output)
387{ (void) output; }
388
389static void
390vbox_output_dpms (xf86OutputPtr output, int mode)
391{ (void) output; (void) mode; }
392
393static int
394vbox_output_mode_valid (xf86OutputPtr output, DisplayModePtr mode)
395{
396 ScrnInfoPtr pScrn = output->scrn;
397 int rc = MODE_OK;
398 TRACE_LOG("HDisplay=%d, VDisplay=%d\n", mode->HDisplay, mode->VDisplay);
399 /* We always like modes specified by the user in the configuration
400 * file and modes requested by the host, as doing otherwise is likely to
401 * annoy people. */
402 if ( !(mode->type & M_T_USERDEF)
403 && !(mode->type & M_T_PREFERRED)
404 && vbox_device_available(VBOXGetRec(pScrn))
405 && !vboxHostLikesVideoMode(pScrn, mode->HDisplay, mode->VDisplay,
406 pScrn->bitsPerPixel)
407 )
408 rc = MODE_BAD;
409 TRACE_LOG("returning %s\n", MODE_OK == rc ? "MODE_OK" : "MODE_BAD");
410 return rc;
411}
412
413static Bool
414vbox_output_mode_fixup (xf86OutputPtr output, DisplayModePtr mode,
415 DisplayModePtr adjusted_mode)
416{ (void) output; (void) mode; (void) adjusted_mode; return TRUE; }
417
418static void
419vbox_output_mode_set (xf86OutputPtr output, DisplayModePtr mode,
420 DisplayModePtr adjusted_mode)
421{ (void) output; (void) mode; (void) adjusted_mode; }
422
423/* A virtual monitor is always connected. */
424static xf86OutputStatus
425vbox_output_detect (xf86OutputPtr output)
426{
427 (void) output;
428 return XF86OutputStatusConnected;
429}
430
431static DisplayModePtr
432vbox_output_add_mode (VBOXPtr pVBox, DisplayModePtr *pModes,
433 const char *pszName, int x, int y,
434 Bool isPreferred, Bool isUserDef)
435{
436 TRACE_LOG("pszName=%s, x=%d, y=%d\n", pszName ? pszName : "(null)", x, y);
437 DisplayModePtr pMode = xnfcalloc(1, sizeof(DisplayModeRec));
438
439 pMode->status = MODE_OK;
440 /* We don't ask the host whether it likes user defined modes,
441 * as we assume that the user really wanted that mode. */
442 pMode->type = isUserDef ? M_T_USERDEF : M_T_BUILTIN;
443 if (isPreferred)
444 pMode->type |= M_T_PREFERRED;
445 /* Older versions of VBox only support screen widths which are a multiple
446 * of 8 */
447 if (pVBox->fAnyX)
448 pMode->HDisplay = x;
449 else
450 pMode->HDisplay = x & ~7;
451 pMode->HSyncStart = pMode->HDisplay + 2;
452 pMode->HSyncEnd = pMode->HDisplay + 4;
453 pMode->HTotal = pMode->HDisplay + 6;
454 pMode->VDisplay = y;
455 pMode->VSyncStart = pMode->VDisplay + 2;
456 pMode->VSyncEnd = pMode->VDisplay + 4;
457 pMode->VTotal = pMode->VDisplay + 6;
458 pMode->Clock = pMode->HTotal * pMode->VTotal * 60 / 1000; /* kHz */
459 if (NULL == pszName) {
460 xf86SetModeDefaultName(pMode);
461 } else {
462 pMode->name = xnfstrdup(pszName);
463 }
464 *pModes = xf86ModesAdd(*pModes, pMode);
465 return pMode;
466}
467
468static DisplayModePtr
469vbox_output_get_modes (xf86OutputPtr output)
470{
471 unsigned i, cIndex = 0;
472 DisplayModePtr pModes = NULL, pMode;
473 ScrnInfoPtr pScrn = output->scrn;
474 VBOXPtr pVBox = VBOXGetRec(pScrn);
475
476 TRACE_ENTRY();
477 uint32_t x, y, bpp, iScreen;
478 iScreen = (uintptr_t)output->driver_private;
479 vboxGetPreferredMode(pScrn, iScreen, &x, &y, &bpp);
480 pMode = vbox_output_add_mode(pVBox, &pModes, NULL, x, y, TRUE, FALSE);
481 VBOXEDIDSet(output, pMode);
482 /* Add standard modes supported by the host */
483 for ( ; ; )
484 {
485 cIndex = vboxNextStandardMode(pScrn, cIndex, &x, &y, NULL);
486 if (cIndex == 0)
487 break;
488 vbox_output_add_mode(pVBox, &pModes, NULL, x, y, FALSE, FALSE);
489 }
490
491 /* Also report any modes the user may have requested in the xorg.conf
492 * configuration file. */
493 for (i = 0; pScrn->display->modes[i] != NULL; i++)
494 {
495 if (2 == sscanf(pScrn->display->modes[i], "%ux%u", &x, &y))
496 vbox_output_add_mode(pVBox, &pModes, pScrn->display->modes[i], x, y,
497 FALSE, TRUE);
498 }
499 TRACE_EXIT();
500 return pModes;
501}
502
503#ifdef RANDR_12_INTERFACE
504static Atom
505vboxAtomVBoxMode(void)
506{
507 return MakeAtom("VBOX_MODE", sizeof("VBOX_MODE") - 1, TRUE);
508}
509
510static Atom
511vboxAtomEDID(void)
512{
513 return MakeAtom("EDID", sizeof("EDID") - 1, TRUE);
514}
515
516/** We use this for receiving information from clients for the purpose of
517 * dynamic resizing, and later possibly other things too.
518 */
519static Bool
520vbox_output_set_property(xf86OutputPtr output, Atom property,
521 RRPropertyValuePtr value)
522{
523 ScrnInfoPtr pScrn = output->scrn;
524 VBOXPtr pVBox = VBOXGetRec(pScrn);
525 TRACE_LOG("property=%d, value->type=%d, value->format=%d, value->size=%d\n",
526 (int)property, (int)value->type, (int)value->format, (int)value->size);
527 if (property == vboxAtomVBoxMode())
528 {
529 uint32_t cDisplay = (uintptr_t)output->driver_private;
530 int w, h;
531
532 if ( value->type != XA_INTEGER
533 || value->format != 32
534 || value->size != 1)
535 return FALSE;
536 w = (*(uint32_t *)value->data) >> 16;
537 h = (*(uint32_t *)value->data) & 0xffff;
538 TRACE_LOG("screen=%u, property value=%dx%d\n", cDisplay, w, h);
539 pVBox->aPreferredSize[cDisplay].cx = w;
540 pVBox->aPreferredSize[cDisplay].cy = h;
541 return TRUE;
542 }
543 if (property == vboxAtomEDID())
544 return TRUE;
545 return FALSE;
546}
547#endif
548
549static const xf86OutputFuncsRec VBOXOutputFuncs = {
550 .create_resources = vbox_output_stub,
551 .dpms = vbox_output_dpms,
552 .save = NULL, /* These two are never called by the server. */
553 .restore = NULL,
554 .mode_valid = vbox_output_mode_valid,
555 .mode_fixup = vbox_output_mode_fixup,
556 .prepare = vbox_output_stub,
557 .commit = vbox_output_stub,
558 .mode_set = vbox_output_mode_set,
559 .detect = vbox_output_detect,
560 .get_modes = vbox_output_get_modes,
561#ifdef RANDR_12_INTERFACE
562 .set_property = vbox_output_set_property,
563#endif
564 .destroy = vbox_output_stub
565};
566#endif /* VBOXVIDEO_13 */
567
568/* Module loader interface */
569static MODULESETUPPROTO(vboxSetup);
570
571static XF86ModuleVersionInfo vboxVersionRec =
572{
573 VBOX_DRIVER_NAME,
574 VBOX_VENDOR,
575 MODINFOSTRING1,
576 MODINFOSTRING2,
577#ifdef XORG_7X
578 XORG_VERSION_CURRENT,
579#else
580 XF86_VERSION_CURRENT,
581#endif
582 1, /* Module major version. Xorg-specific */
583 0, /* Module minor version. Xorg-specific */
584 1, /* Module patchlevel. Xorg-specific */
585 ABI_CLASS_VIDEODRV, /* This is a video driver */
586 ABI_VIDEODRV_VERSION,
587 MOD_CLASS_VIDEODRV,
588 {0, 0, 0, 0}
589};
590
591/*
592 * This data is accessed by the loader. The name must be the module name
593 * followed by "ModuleData".
594 */
595#ifdef XORG_7X
596_X_EXPORT
597#endif
598XF86ModuleData vboxvideoModuleData = { &vboxVersionRec, vboxSetup, NULL };
599
600static pointer
601vboxSetup(pointer Module, pointer Options, int *ErrorMajor, int *ErrorMinor)
602{
603 static Bool Initialised = FALSE;
604
605 if (!Initialised)
606 {
607 Initialised = TRUE;
608#ifdef PCIACCESS
609 xf86AddDriver(&VBOXVIDEO, Module, HaveDriverFuncs);
610#else
611 xf86AddDriver(&VBOXVIDEO, Module, 0);
612#endif
613#ifndef XORG_7X
614 LoaderRefSymLists(fbSymbols,
615 shadowfbSymbols,
616 ramdacSymbols,
617 vgahwSymbols,
618 NULL);
619#endif
620 xf86Msg(X_CONFIG, "Load address of symbol \"VBOXVIDEO\" is %p\n",
621 (void *)&VBOXVIDEO);
622 return (pointer)TRUE;
623 }
624
625 if (ErrorMajor)
626 *ErrorMajor = LDR_ONCEONLY;
627 return (NULL);
628}
629
630
631static const OptionInfoRec *
632VBOXAvailableOptions(int chipid, int busid)
633{
634 return (VBOXOptions);
635}
636
637static void
638VBOXIdentify(int flags)
639{
640 xf86PrintChipsets(VBOX_NAME, "guest driver for VirtualBox", VBOXChipsets);
641}
642
643#ifndef XF86_SCRN_INTERFACE
644# define xf86ScreenToScrn(pScreen) xf86Screens[(pScreen)->myNum]
645# define xf86ScrnToScreen(pScrn) screenInfo.screens[(pScrn)->scrnIndex]
646# define SCRNINDEXAPI(pfn) pfn ## Index
647static Bool VBOXScreenInitIndex(int scrnIndex, ScreenPtr pScreen, int argc,
648 char **argv)
649{ return VBOXScreenInit(pScreen, argc, argv); }
650
651static Bool VBOXEnterVTIndex(int scrnIndex, int flags)
652{ (void) flags; return VBOXEnterVT(xf86Screens[scrnIndex]); }
653
654static void VBOXLeaveVTIndex(int scrnIndex, int flags)
655{ (void) flags; VBOXLeaveVT(xf86Screens[scrnIndex]); }
656
657static Bool VBOXCloseScreenIndex(int scrnIndex, ScreenPtr pScreen)
658{ (void) scrnIndex; return VBOXCloseScreen(pScreen); }
659
660static Bool VBOXSwitchModeIndex(int scrnIndex, DisplayModePtr pMode, int flags)
661{ (void) flags; return VBOXSwitchMode(xf86Screens[scrnIndex], pMode); }
662
663static void VBOXAdjustFrameIndex(int scrnIndex, int x, int y, int flags)
664{ (void) flags; VBOXAdjustFrame(xf86Screens[scrnIndex], x, y); }
665
666static void VBOXFreeScreenIndex(int scrnIndex, int flags)
667{ (void) flags; VBOXFreeScreen(xf86Screens[scrnIndex]); }
668# else
669# define SCRNINDEXAPI(pfn) pfn
670#endif /* XF86_SCRN_INTERFACE */
671
672/** Helper to work round different ways of getting the root window in different
673 * server versions. */
674#if defined(XORG_VERSION_CURRENT) && XORG_VERSION_CURRENT < 700000000 \
675 && XORG_VERSION_CURRENT >= 100900000
676# define ROOT_WINDOW(pScrn) xf86ScrnToScreen(pScrn)->root
677#else
678# define ROOT_WINDOW(pScrn) WindowTable[xf86ScrnToScreen(pScrn)->myNum]
679#endif
680
681/** Helper to work round different prototypes for DeleteProperty in different
682 * server versions. */
683#if defined(XORG_VERSION_CURRENT) && XORG_VERSION_CURRENT < 700000000 \
684 && XORG_VERSION_CURRENT >= 100500000
685# define DELETE_PROPERTY(client, window, prop) \
686 DeleteProperty(client, window, prop)
687#else
688# define DELETE_PROPERTY(client, window, prop) \
689 DeleteProperty(window, prop)
690#endif
691
692static void setScreenFunctions(ScrnInfoPtr pScrn, xf86ProbeProc pfnProbe)
693{
694 pScrn->driverVersion = VBOX_VERSION;
695 pScrn->driverName = VBOX_DRIVER_NAME;
696 pScrn->name = VBOX_NAME;
697 pScrn->Probe = pfnProbe;
698 pScrn->PreInit = VBOXPreInit;
699 pScrn->ScreenInit = SCRNINDEXAPI(VBOXScreenInit);
700 pScrn->SwitchMode = SCRNINDEXAPI(VBOXSwitchMode);
701 pScrn->AdjustFrame = SCRNINDEXAPI(VBOXAdjustFrame);
702 pScrn->EnterVT = SCRNINDEXAPI(VBOXEnterVT);
703 pScrn->LeaveVT = SCRNINDEXAPI(VBOXLeaveVT);
704 pScrn->FreeScreen = SCRNINDEXAPI(VBOXFreeScreen);
705}
706
707/*
708 * One of these functions is called once, at the start of the first server
709 * generation to do a minimal probe for supported hardware.
710 */
711
712#ifdef PCIACCESS
713static Bool
714VBOXPciProbe(DriverPtr drv, int entity_num, struct pci_device *dev,
715 intptr_t match_data)
716{
717 ScrnInfoPtr pScrn;
718
719 TRACE_ENTRY();
720 pScrn = xf86ConfigPciEntity(NULL, 0, entity_num, VBOXPCIchipsets,
721 NULL, NULL, NULL, NULL, NULL);
722 if (pScrn != NULL) {
723 VBOXPtr pVBox;
724
725 VBOXSetRec(pScrn);
726 pVBox = VBOXGetRec(pScrn);
727 if (!pVBox)
728 return FALSE;
729 setScreenFunctions(pScrn, NULL);
730 pVBox->pciInfo = dev;
731 }
732
733 TRACE_LOG("returning %s\n", BOOL_STR(pScrn != NULL));
734 return (pScrn != NULL);
735}
736#endif
737
738#ifndef PCIACCESS
739static Bool
740VBOXProbe(DriverPtr drv, int flags)
741{
742 Bool foundScreen = FALSE;
743 int numDevSections;
744 GDevPtr *devSections;
745
746 /*
747 * Find the config file Device sections that match this
748 * driver, and return if there are none.
749 */
750 if ((numDevSections = xf86MatchDevice(VBOX_NAME,
751 &devSections)) <= 0)
752 return (FALSE);
753
754 /* PCI BUS */
755 if (xf86GetPciVideoInfo())
756 {
757 int numUsed;
758 int *usedChips;
759 int i;
760 numUsed = xf86MatchPciInstances(VBOX_NAME, VBOX_VENDORID,
761 VBOXChipsets, VBOXPCIchipsets,
762 devSections, numDevSections,
763 drv, &usedChips);
764 if (numUsed > 0)
765 {
766 if (flags & PROBE_DETECT)
767 foundScreen = TRUE;
768 else
769 for (i = 0; i < numUsed; i++)
770 {
771 ScrnInfoPtr pScrn = NULL;
772 /* Allocate a ScrnInfoRec */
773 if ((pScrn = xf86ConfigPciEntity(pScrn,0,usedChips[i],
774 VBOXPCIchipsets,NULL,
775 NULL,NULL,NULL,NULL)))
776 {
777 setScreenFunctions(pScrn, VBOXProbe);
778 foundScreen = TRUE;
779 }
780 }
781 free(usedChips);
782 }
783 }
784 free(devSections);
785 return (foundScreen);
786}
787#endif
788
789
790/*
791 * QUOTE from the XFree86 DESIGN document:
792 *
793 * The purpose of this function is to find out all the information
794 * required to determine if the configuration is usable, and to initialise
795 * those parts of the ScrnInfoRec that can be set once at the beginning of
796 * the first server generation.
797 *
798 * (...)
799 *
800 * This includes probing for video memory, clocks, ramdac, and all other
801 * HW info that is needed. It includes determining the depth/bpp/visual
802 * and related info. It includes validating and determining the set of
803 * video modes that will be used (and anything that is required to
804 * determine that).
805 *
806 * This information should be determined in the least intrusive way
807 * possible. The state of the HW must remain unchanged by this function.
808 * Although video memory (including MMIO) may be mapped within this
809 * function, it must be unmapped before returning.
810 *
811 * END QUOTE
812 */
813
814static Bool
815VBOXPreInit(ScrnInfoPtr pScrn, int flags)
816{
817 VBOXPtr pVBox;
818 Gamma gzeros = {0.0, 0.0, 0.0};
819 rgb rzeros = {0, 0, 0};
820 unsigned DispiId;
821
822 TRACE_ENTRY();
823 /* Are we really starting the server, or is this just a dummy run? */
824 if (flags & PROBE_DETECT)
825 return (FALSE);
826
827 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
828 "VirtualBox guest additions video driver version "
829 VBOX_VERSION_STRING "\n");
830
831 /* Get our private data from the ScrnInfoRec structure. */
832 VBOXSetRec(pScrn);
833 pVBox = VBOXGetRec(pScrn);
834 if (!pVBox)
835 return FALSE;
836
837 /* Initialise the guest library */
838 vbox_init(pScrn->scrnIndex, pVBox);
839
840 /* Entity information seems to mean bus information. */
841 pVBox->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
842
843 /* The ramdac module is needed for the hardware cursor. */
844 if (!xf86LoadSubModule(pScrn, "ramdac"))
845 return FALSE;
846
847 /* The framebuffer module. */
848 if (!xf86LoadSubModule(pScrn, "fb"))
849 return (FALSE);
850
851 if (!xf86LoadSubModule(pScrn, "shadowfb"))
852 return FALSE;
853
854 if (!xf86LoadSubModule(pScrn, "vgahw"))
855 return FALSE;
856
857#ifdef VBOX_DRI_OLD
858 /* Load the dri module. */
859 if (!xf86LoadSubModule(pScrn, "dri"))
860 return FALSE;
861#else
862# ifdef VBOX_DRI
863 /* Load the dri module. */
864 if (!xf86LoadSubModule(pScrn, "dri2"))
865 return FALSE;
866# endif
867#endif
868
869#ifndef PCIACCESS
870 if (pVBox->pEnt->location.type != BUS_PCI)
871 return FALSE;
872
873 pVBox->pciInfo = xf86GetPciInfoForEntity(pVBox->pEnt->index);
874 pVBox->pciTag = pciTag(pVBox->pciInfo->bus,
875 pVBox->pciInfo->device,
876 pVBox->pciInfo->func);
877#endif
878
879 /* Set up our ScrnInfoRec structure to describe our virtual
880 capabilities to X. */
881
882 pScrn->chipset = "vbox";
883 /** @note needed during colourmap initialisation */
884 pScrn->rgbBits = 8;
885
886 /* Let's create a nice, capable virtual monitor. */
887 pScrn->monitor = pScrn->confScreen->monitor;
888 pScrn->monitor->DDC = NULL;
889 pScrn->monitor->nHsync = 1;
890 pScrn->monitor->hsync[0].lo = 1;
891 pScrn->monitor->hsync[0].hi = 10000;
892 pScrn->monitor->nVrefresh = 1;
893 pScrn->monitor->vrefresh[0].lo = 1;
894 pScrn->monitor->vrefresh[0].hi = 100;
895
896 pScrn->progClock = TRUE;
897
898 /* Using the PCI information caused problems with non-powers-of-two
899 sized video RAM configurations */
900 pVBox->cbFBMax = VBoxVideoGetVRAMSize();
901 pScrn->videoRam = pVBox->cbFBMax / 1024;
902
903 /* Check if the chip restricts horizontal resolution or not. */
904 pVBox->fAnyX = VBoxVideoAnyWidthAllowed();
905
906 /* Set up clock information that will support all modes we need. */
907 pScrn->clockRanges = xnfcalloc(sizeof(ClockRange), 1);
908 pScrn->clockRanges->minClock = 1000;
909 pScrn->clockRanges->maxClock = 1000000000;
910 pScrn->clockRanges->clockIndex = -1;
911 pScrn->clockRanges->ClockMulFactor = 1;
912 pScrn->clockRanges->ClockDivFactor = 1;
913
914 /* Query the host for the preferred colour depth */
915 {
916 uint32_t cx = 0, cy = 0, cBits = 0;
917
918 vboxGetPreferredMode(pScrn, 0, &cx, &cy, &cBits);
919 /* We only support 16 and 24 bits depth (i.e. 16 and 32bpp) */
920 if (cBits != 16)
921 cBits = 24;
922 if (!xf86SetDepthBpp(pScrn, cBits, 0, 0, Support32bppFb))
923 return FALSE;
924 vboxAddModes(pScrn, cx, cy);
925 }
926 if (pScrn->bitsPerPixel != 32 && pScrn->bitsPerPixel != 16)
927 {
928 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
929 "The VBox additions only support 16 and 32bpp graphics modes\n");
930 return FALSE;
931 }
932 xf86PrintDepthBpp(pScrn);
933
934#ifdef VBOXVIDEO_13
935 /* Work around a bug in the original X server modesetting code, which
936 * took the first valid values set to these two as maxima over the
937 * server lifetime. */
938 pScrn->virtualX = 32000;
939 pScrn->virtualY = 32000;
940#else
941 /* We don't validate with xf86ValidateModes and xf86PruneModes as we
942 * already know what we like and what we don't. */
943
944 pScrn->currentMode = pScrn->modes;
945
946 /* Set the right virtual resolution. */
947 pScrn->virtualX = pScrn->currentMode->HDisplay;
948 pScrn->virtualY = pScrn->currentMode->VDisplay;
949
950#endif /* !VBOXVIDEO_13 */
951
952 /* Needed before we initialise DRI. */
953 pVBox->cbLine = vboxLineLength(pScrn, pScrn->virtualX);
954 pScrn->displayWidth = vboxDisplayPitch(pScrn, pVBox->cbLine);
955
956 xf86PrintModes(pScrn);
957
958 /* VGA hardware initialisation */
959 if (!vgaHWGetHWRec(pScrn))
960 return FALSE;
961 /* Must be called before any VGA registers are saved or restored */
962 vgaHWSetStdFuncs(VGAHWPTR(pScrn));
963 vgaHWGetIOBase(VGAHWPTR(pScrn));
964
965 /* Colour weight - we always call this, since we are always in
966 truecolour. */
967 if (!xf86SetWeight(pScrn, rzeros, rzeros))
968 return (FALSE);
969
970 /* visual init */
971 if (!xf86SetDefaultVisual(pScrn, -1))
972 return (FALSE);
973
974 xf86SetGamma(pScrn, gzeros);
975
976 /* Set the DPI. Perhaps we should read this from the host? */
977 xf86SetDpi(pScrn, 96, 96);
978
979 if (pScrn->memPhysBase == 0) {
980#ifdef PCIACCESS
981 pScrn->memPhysBase = pVBox->pciInfo->regions[0].base_addr;
982#else
983 pScrn->memPhysBase = pVBox->pciInfo->memBase[0];
984#endif
985 pScrn->fbOffset = 0;
986 }
987
988 TRACE_EXIT();
989 return (TRUE);
990}
991
992/**
993 * Dummy function for setting the colour palette, which we actually never
994 * touch. However, the server still requires us to provide this.
995 */
996static void
997vboxLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
998 LOCO *colors, VisualPtr pVisual)
999{
1000 (void)pScrn; (void) numColors; (void) indices; (void) colors;
1001 (void)pVisual;
1002}
1003
1004/*
1005 * QUOTE from the XFree86 DESIGN document:
1006 *
1007 * This is called at the start of each server generation.
1008 *
1009 * (...)
1010 *
1011 * Decide which operations need to be placed under resource access
1012 * control. (...) Map any video memory or other memory regions. (...)
1013 * Save the video card state. (...) Initialise the initial video
1014 * mode.
1015 *
1016 * End QUOTE.
1017 */
1018static Bool VBOXScreenInit(ScreenPtr pScreen, int argc, char **argv)
1019{
1020 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1021 VBOXPtr pVBox = VBOXGetRec(pScrn);
1022 VisualPtr visual;
1023 unsigned flags;
1024
1025 TRACE_ENTRY();
1026
1027 if (!VBOXMapVidMem(pScrn))
1028 return (FALSE);
1029
1030 /* save current video state */
1031 VBOXSaveMode(pScrn);
1032
1033 /* mi layer - reset the visual list (?)*/
1034 miClearVisualTypes();
1035 if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
1036 pScrn->rgbBits, TrueColor))
1037 return (FALSE);
1038 if (!miSetPixmapDepths())
1039 return (FALSE);
1040
1041#ifdef VBOX_DRI
1042 pVBox->useDRI = VBOXDRIScreenInit(pScrn, pScreen, pVBox);
1043# ifndef VBOX_DRI_OLD /* DRI2 */
1044 if (pVBox->drmFD >= 0)
1045 /* Tell the kernel driver, if present, that we are taking over. */
1046 drmIoctl(pVBox->drmFD, VBOXVIDEO_IOCTL_DISABLE_HGSMI, NULL);
1047# endif
1048#endif
1049
1050 if (!fbScreenInit(pScreen, pVBox->base,
1051 pScrn->virtualX, pScrn->virtualY,
1052 pScrn->xDpi, pScrn->yDpi,
1053 pScrn->displayWidth, pScrn->bitsPerPixel))
1054 return (FALSE);
1055
1056 /* Fixup RGB ordering */
1057 /** @note the X server uses this even in true colour. */
1058 visual = pScreen->visuals + pScreen->numVisuals;
1059 while (--visual >= pScreen->visuals) {
1060 if ((visual->class | DynamicClass) == DirectColor) {
1061 visual->offsetRed = pScrn->offset.red;
1062 visual->offsetGreen = pScrn->offset.green;
1063 visual->offsetBlue = pScrn->offset.blue;
1064 visual->redMask = pScrn->mask.red;
1065 visual->greenMask = pScrn->mask.green;
1066 visual->blueMask = pScrn->mask.blue;
1067 }
1068 }
1069
1070 /* must be after RGB ordering fixed */
1071 fbPictureInit(pScreen, 0, 0);
1072
1073 xf86SetBlackWhitePixels(pScreen);
1074 pScrn->vtSema = TRUE;
1075
1076 if (vbox_open (pScrn, pScreen, pVBox)) {
1077 vboxEnableVbva(pScrn);
1078 }
1079
1080#ifdef VBOXVIDEO_13
1081 /* Initialise CRTC and output configuration for use with randr1.2. */
1082 xf86CrtcConfigInit(pScrn, &VBOXCrtcConfigFuncs);
1083
1084 {
1085 uint32_t i;
1086
1087 for (i = 0; i < pVBox->cScreens; ++i)
1088 {
1089 char szOutput[256];
1090
1091 /* Setup our virtual CRTCs. */
1092 pVBox->paCrtcs[i] = xf86CrtcCreate(pScrn, &VBOXCrtcFuncs);
1093 pVBox->paCrtcs[i]->driver_private = (void *)(uintptr_t)i;
1094
1095 /* Set up our virtual outputs. */
1096 snprintf(szOutput, sizeof(szOutput), "VGA-%u", i);
1097 pVBox->paOutputs[i] = xf86OutputCreate(pScrn, &VBOXOutputFuncs,
1098 szOutput);
1099
1100 /* We are not interested in the monitor section in the
1101 * configuration file. */
1102 xf86OutputUseScreenMonitor(pVBox->paOutputs[i], FALSE);
1103 pVBox->paOutputs[i]->possible_crtcs = 1 << i;
1104 pVBox->paOutputs[i]->possible_clones = 0;
1105 pVBox->paOutputs[i]->driver_private = (void *)(uintptr_t)i;
1106 TRACE_LOG("Created crtc (%p) and output %s (%p)\n",
1107 (void *)pVBox->paCrtcs[i], szOutput,
1108 (void *)pVBox->paOutputs[i]);
1109 }
1110 }
1111
1112 /* Set a sane minimum and maximum mode size to match what the hardware
1113 * supports. */
1114 xf86CrtcSetSizeRange(pScrn, 64, 64, 16384, 16384);
1115
1116 /* Now create our initial CRTC/output configuration. */
1117 if (!xf86InitialConfiguration(pScrn, TRUE)) {
1118 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Initial CRTC configuration failed!\n");
1119 return (FALSE);
1120 }
1121
1122 /* Initialise randr 1.2 mode-setting functions and set first mode.
1123 * Note that the mode won't be usable until the server has resized the
1124 * framebuffer to something reasonable. */
1125 if (!xf86CrtcScreenInit(pScreen)) {
1126 return FALSE;
1127 }
1128
1129 /* Create our VBOX_MODE display properties. */
1130 {
1131 uint32_t i;
1132
1133 for (i = 0; i < pVBox->cScreens; ++i)
1134 {
1135 INT32 value = 0;
1136 RRChangeOutputProperty(pVBox->paOutputs[i]->randr_output,
1137 vboxAtomVBoxMode(), XA_INTEGER, 32,
1138 PropModeReplace, 1, &value, TRUE,
1139 FALSE);
1140
1141 }
1142 }
1143
1144 if (!xf86SetDesiredModes(pScrn)) {
1145 return FALSE;
1146 }
1147#else /* !VBOXVIDEO_13 */
1148 /* set first video mode */
1149 if (!VBOXSetMode(pScrn, 0, pScrn->currentMode->HDisplay,
1150 pScrn->currentMode->VDisplay, pScrn->frameX0,
1151 pScrn->frameY0))
1152 return FALSE;
1153 /* Save the size in case we need to re-set it later. */
1154 pVBox->FBSize.cx = pScrn->currentMode->HDisplay;
1155 pVBox->FBSize.cy = pScrn->currentMode->VDisplay;
1156 pVBox->aScreenLocation[0].cx = pScrn->currentMode->HDisplay;
1157 pVBox->aScreenLocation[0].cy = pScrn->currentMode->VDisplay;
1158 pVBox->aScreenLocation[0].x = pScrn->frameX0;
1159 pVBox->aScreenLocation[0].y = pScrn->frameY0;
1160 /* And make sure that a non-current dynamic mode is at the front of the
1161 * list */
1162 vboxWriteHostModes(pScrn, pScrn->currentMode);
1163#endif /* !VBOXVIDEO_13 */
1164
1165 /* software cursor */
1166 miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
1167
1168 /* colourmap code */
1169 if (!miCreateDefColormap(pScreen))
1170 return (FALSE);
1171
1172 if(!xf86HandleColormaps(pScreen, 256, 8, vboxLoadPalette, NULL, 0))
1173 return (FALSE);
1174
1175 pVBox->CloseScreen = pScreen->CloseScreen;
1176 pScreen->CloseScreen = SCRNINDEXAPI(VBOXCloseScreen);
1177#ifdef VBOXVIDEO_13
1178 pScreen->SaveScreen = xf86SaveScreen;
1179#else
1180 pScreen->SaveScreen = VBOXSaveScreen;
1181#endif
1182
1183#ifdef VBOXVIDEO_13
1184 xf86DPMSInit(pScreen, xf86DPMSSet, 0);
1185#else
1186 /* We probably do want to support power management - even if we just use
1187 a dummy function. */
1188 xf86DPMSInit(pScreen, VBOXDisplayPowerManagementSet, 0);
1189#endif
1190
1191 /* Report any unused options (only for the first generation) */
1192 if (serverGeneration == 1)
1193 xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
1194
1195 if (vbox_cursor_init(pScreen) != TRUE)
1196 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1197 "Unable to start the VirtualBox mouse pointer integration with the host system.\n");
1198
1199#ifdef VBOX_DRI_OLD
1200 if (pVBox->useDRI)
1201 pVBox->useDRI = VBOXDRIFinishScreenInit(pScreen);
1202#endif
1203 return (TRUE);
1204}
1205
1206/** FEATURE: Set a property on the root window if the server VT is currently
1207 * switched out. This should be called from the EnterVT/LeaveVT hooks.
1208 * TESTING: the feature works if seamless mode and dynamic resizing are
1209 * disabled on the host when a VT switch to a text console is done on an old
1210 * X11 guest. When the vboxvideo DDX is running the property created should
1211 * exist exactly when the X server is switched out. */
1212static Bool setVTRootProperty(ScrnInfoPtr pScrn, BOOL fLeave)
1213{
1214 Atom propertyName = MakeAtom("XFree86_NO_VT", sizeof("XFree86_NO_VT") - 1,
1215 TRUE);
1216 int32_t cValue = 0;
1217 int rc;
1218
1219 if (propertyName == BAD_RESOURCE)
1220 return FALSE;
1221 rc = fLeave
1222 ? ChangeWindowProperty(ROOT_WINDOW(pScrn), propertyName, XA_INTEGER,
1223 32, PropModeReplace, 1, &cValue, TRUE)
1224 : DELETE_PROPERTY(serverClient, ROOT_WINDOW(pScrn), propertyName);
1225 return rc == Success;
1226}
1227
1228static Bool VBOXEnterVT(ScrnInfoPtr pScrn)
1229{
1230 VBOXPtr pVBox = VBOXGetRec(pScrn);
1231
1232 TRACE_ENTRY();
1233 vboxClearVRAM(pScrn, 0, 0);
1234#ifdef VBOX_DRI_OLD
1235 if (pVBox->useDRI)
1236 DRIUnlock(xf86ScrnToScreen(pScrn));
1237#elif defined(VBOX_DRI) /* DRI2 */
1238 if (pVBox->drmFD >= 0)
1239 /* Tell the kernel driver, if present, that we are taking over. */
1240 drmIoctl(pVBox->drmFD, VBOXVIDEO_IOCTL_DISABLE_HGSMI, NULL);
1241#endif
1242 if (pVBox->fHaveHGSMI)
1243 vboxEnableVbva(pScrn);
1244 /* Re-assert this in case we had a change request while switched out. */
1245 if (pVBox->FBSize.cx && pVBox->FBSize.cy)
1246 VBOXAdjustScreenPixmap(pScrn, pVBox->FBSize.cx, pVBox->FBSize.cy);
1247#ifdef VBOXVIDEO_13
1248 if (!xf86SetDesiredModes(pScrn))
1249 return FALSE;
1250#else
1251 if (!VBOXSetMode(pScrn, 0, pScrn->currentMode->HDisplay,
1252 pScrn->currentMode->VDisplay, pScrn->frameX0,
1253 pScrn->frameY0))
1254 return FALSE;
1255#endif
1256 setVTRootProperty(pScrn, FALSE);
1257 return TRUE;
1258}
1259
1260static void VBOXLeaveVT(ScrnInfoPtr pScrn)
1261{
1262 VBOXPtr pVBox = VBOXGetRec(pScrn);
1263
1264 TRACE_ENTRY();
1265 if (pVBox->fHaveHGSMI)
1266 vboxDisableVbva(pScrn);
1267 vboxClearVRAM(pScrn, 0, 0);
1268#ifdef VBOX_DRI_OLD
1269 if (pVBox->useDRI)
1270 DRILock(xf86ScrnToScreen(pScrn), 0);
1271#elif defined(VBOX_DRI) /* DRI2 */
1272 if ( pVBox->drmFD < 0
1273 /* Tell the kernel driver, if present, that it can use the framebuffer
1274 * driver again. */
1275 || drmIoctl(pVBox->drmFD, VBOXVIDEO_IOCTL_ENABLE_HGSMI, NULL) < 0)
1276#endif
1277 VBOXRestoreMode(pScrn);
1278 setVTRootProperty(pScrn, TRUE);
1279 TRACE_EXIT();
1280}
1281
1282static Bool VBOXCloseScreen(ScreenPtr pScreen)
1283{
1284 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1285 VBOXPtr pVBox = VBOXGetRec(pScrn);
1286#if defined(VBOX_DRI) && !defined(VBOX_DRI_OLD) /* DRI2 */
1287 BOOL fRestore = TRUE;
1288#endif
1289 if (pScrn->vtSema)
1290 {
1291 if (pVBox->fHaveHGSMI)
1292 vboxDisableVbva(pScrn);
1293 vboxClearVRAM(pScrn, 0, 0);
1294 }
1295#ifdef VBOX_DRI
1296# ifndef VBOX_DRI_OLD /* DRI2 */
1297 if ( pVBox->drmFD >= 0
1298 /* Tell the kernel driver, if present, that we are going away. */
1299 && drmIoctl(pVBox->drmFD, VBOXVIDEO_IOCTL_ENABLE_HGSMI, NULL) >= 0)
1300 fRestore = false;
1301# endif
1302 if (pVBox->useDRI)
1303 VBOXDRICloseScreen(pScreen, pVBox);
1304 pVBox->useDRI = false;
1305#endif
1306#if defined(VBOX_DRI) && !defined(VBOX_DRI_OLD) /* DRI2 */
1307 if (fRestore)
1308#endif
1309 if (pScrn->vtSema)
1310 VBOXRestoreMode(pScrn);
1311 if (pScrn->vtSema)
1312 VBOXUnmapVidMem(pScrn);
1313 pScrn->vtSema = FALSE;
1314
1315 /* Do additional bits which are separate for historical reasons */
1316 vbox_close(pScrn, pVBox);
1317
1318 pScreen->CloseScreen = pVBox->CloseScreen;
1319#ifndef XF86_SCRN_INTERFACE
1320 return pScreen->CloseScreen(pScreen->myNum, pScreen);
1321#else
1322 return pScreen->CloseScreen(pScreen);
1323#endif
1324}
1325
1326static Bool VBOXSwitchMode(ScrnInfoPtr pScrn, DisplayModePtr pMode)
1327{
1328 VBOXPtr pVBox;
1329 Bool rc;
1330
1331 TRACE_LOG("HDisplay=%d, VDisplay=%d\n", pMode->HDisplay, pMode->VDisplay);
1332#ifndef VBOXVIDEO_13
1333 pVBox = VBOXGetRec(pScrn);
1334 /* Save the size in case we need to re-set it later. */
1335 pVBox->FBSize.cx = pMode->HDisplay;
1336 pVBox->FBSize.cy = pMode->VDisplay;
1337 pVBox->aScreenLocation[0].cx = pMode->HDisplay;
1338 pVBox->aScreenLocation[0].cy = pMode->VDisplay;
1339 pVBox->aScreenLocation[0].x = pScrn->frameX0;
1340 pVBox->aScreenLocation[0].y = pScrn->frameY0;
1341#endif
1342 if (!pScrn->vtSema)
1343 {
1344 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1345 "We do not own the active VT, exiting.\n");
1346 return TRUE;
1347 }
1348#ifdef VBOXVIDEO_13
1349 rc = xf86SetSingleMode(pScrn, pMode, 0);
1350#else
1351 VBOXAdjustScreenPixmap(pScrn, pMode->HDisplay, pMode->VDisplay);
1352 rc = VBOXSetMode(pScrn, 0, pMode->HDisplay, pMode->VDisplay,
1353 pScrn->frameX0, pScrn->frameY0);
1354 if (rc)
1355 {
1356 vboxWriteHostModes(pScrn, pMode);
1357 xf86PrintModes(pScrn);
1358 }
1359 if (rc && !vboxGuestIsSeamless(pScrn))
1360 vboxSaveVideoMode(pScrn, pMode->HDisplay, pMode->VDisplay,
1361 pScrn->bitsPerPixel);
1362#endif
1363 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1364 return rc;
1365}
1366
1367static void VBOXAdjustFrame(ScrnInfoPtr pScrn, int x, int y)
1368{
1369 VBOXPtr pVBox = VBOXGetRec(pScrn);
1370
1371 TRACE_ENTRY();
1372 pVBox->aScreenLocation[0].x = x;
1373 pVBox->aScreenLocation[0].y = y;
1374 /* Don't fiddle with the hardware if we are switched
1375 * to a virtual terminal. */
1376 if (!pScrn->vtSema)
1377 {
1378 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1379 "We do not own the active VT, exiting.\n");
1380 return;
1381 }
1382 VBOXSetMode(pScrn, 0, pVBox->aScreenLocation[0].cx,
1383 pVBox->aScreenLocation[0].cy, x, y);
1384 TRACE_EXIT();
1385}
1386
1387static void VBOXFreeScreen(ScrnInfoPtr pScrn)
1388{
1389 /* Destroy the VGA hardware record */
1390 vgaHWFreeHWRec(pScrn);
1391 /* And our private record */
1392 free(pScrn->driverPrivate);
1393 pScrn->driverPrivate = NULL;
1394}
1395
1396static Bool
1397VBOXMapVidMem(ScrnInfoPtr pScrn)
1398{
1399 VBOXPtr pVBox = VBOXGetRec(pScrn);
1400 Bool rc = TRUE;
1401
1402 TRACE_ENTRY();
1403 if (!pVBox->base)
1404 {
1405#ifdef PCIACCESS
1406 (void) pci_device_map_range(pVBox->pciInfo,
1407 pScrn->memPhysBase,
1408 pScrn->videoRam * 1024,
1409 PCI_DEV_MAP_FLAG_WRITABLE,
1410 & pVBox->base);
1411#else
1412 pVBox->base = xf86MapPciMem(pScrn->scrnIndex,
1413 VIDMEM_FRAMEBUFFER,
1414 pVBox->pciTag, pScrn->memPhysBase,
1415 (unsigned) pScrn->videoRam * 1024);
1416#endif
1417 if (!pVBox->base)
1418 rc = FALSE;
1419 }
1420 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1421 return rc;
1422}
1423
1424static void
1425VBOXUnmapVidMem(ScrnInfoPtr pScrn)
1426{
1427 VBOXPtr pVBox = VBOXGetRec(pScrn);
1428
1429 TRACE_ENTRY();
1430 if (pVBox->base == NULL)
1431 return;
1432
1433#ifdef PCIACCESS
1434 (void) pci_device_unmap_range(pVBox->pciInfo,
1435 pVBox->base,
1436 pScrn->videoRam * 1024);
1437#else
1438 xf86UnMapVidMem(pScrn->scrnIndex, pVBox->base,
1439 (unsigned) pScrn->videoRam * 1024);
1440#endif
1441 pVBox->base = NULL;
1442 TRACE_EXIT();
1443}
1444
1445static Bool
1446VBOXSaveScreen(ScreenPtr pScreen, int mode)
1447{
1448 (void)pScreen; (void)mode;
1449 return TRUE;
1450}
1451
1452void
1453VBOXSaveMode(ScrnInfoPtr pScrn)
1454{
1455 VBOXPtr pVBox = VBOXGetRec(pScrn);
1456 vgaRegPtr vgaReg;
1457
1458 TRACE_ENTRY();
1459 vgaReg = &VGAHWPTR(pScrn)->SavedReg;
1460 vgaHWSave(pScrn, vgaReg, VGA_SR_ALL);
1461 pVBox->fSavedVBEMode = VBoxVideoGetModeRegisters(&pVBox->cSavedWidth,
1462 &pVBox->cSavedHeight,
1463 &pVBox->cSavedPitch,
1464 &pVBox->cSavedBPP,
1465 &pVBox->fSavedFlags);
1466}
1467
1468void
1469VBOXRestoreMode(ScrnInfoPtr pScrn)
1470{
1471 VBOXPtr pVBox = VBOXGetRec(pScrn);
1472 vgaRegPtr vgaReg;
1473
1474 TRACE_ENTRY();
1475 vgaReg = &VGAHWPTR(pScrn)->SavedReg;
1476 vgaHWRestore(pScrn, vgaReg, VGA_SR_ALL);
1477 if (pVBox->fSavedVBEMode)
1478 VBoxVideoSetModeRegisters(pVBox->cSavedWidth, pVBox->cSavedHeight,
1479 pVBox->cSavedPitch, pVBox->cSavedBPP,
1480 pVBox->fSavedFlags, 0, 0);
1481 else
1482 VBoxVideoDisableVBE();
1483}
1484
1485static void
1486VBOXDisplayPowerManagementSet(ScrnInfoPtr pScrn, int mode,
1487 int flags)
1488{
1489 (void)pScrn; (void)mode; (void) flags;
1490}
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