VirtualBox

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

Last change on this file since 53781 was 53544, checked in by vboxsync, 10 years ago

Additions/x11/vboxvideo: properly distinguish low power states and monitor disconnected.

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