VirtualBox

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

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

Additions/x11/vboxvideo: report monitor connected status to X11, based on the enabled state in the last mode hint.

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