if (xf86IsEntityShared(pScrn->entityList[0])) {
if (pMga->DualHeadEnabled) {
MGAScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv){    ScrnInfoPtr pScrn;    vgaHWPtr hwp;    MGAPtr pMga;    MGARamdacPtr MGAdac;    int ret;    VisualPtr visual;    unsigned char *FBStart;    int width, height, displayWidth;    MGAEntPtr pMgaEnt = NULL;    int f;#ifdef XF86DRI    MessageType driFrom = X_DEFAULT;#endif    /*     * First get the ScrnInfoRec     */    pScrn = xf86Screens[pScreen->myNum];    hwp = VGAHWPTR(pScrn);    pMga = MGAPTR(pScrn);    MGAdac = &pMga->Dac;    /* Map the MGA memory and MMIO areas */    if (pMga->FBDev) {	if (!MGAMapMemFBDev(pScrn))	    return FALSE;    } else {	if (!MGAMapMem(pScrn))	    return FALSE;    }    if ((pMga->Chipset == PCI_CHIP_MGAG100)	|| (pMga->Chipset == PCI_CHIP_MGAG100_PCI))        MGAG100BlackMagic(pMga);    if (xf86IsEntityShared(pScrn->entityList[0])) {       DevUnion *pPriv;       pPriv = xf86GetEntityPrivate(pScrn->entityList[0], MGAEntityIndex);       pMgaEnt = pPriv->ptr;       pMgaEnt->refCount++;#ifdef USEMGAHAL       MGA_HAL(       if(pMgaEnt->refCount == 1) {	  pMga->pBoard = xalloc(sizeof(CLIENTDATA) + MGAGetBOARDHANDLESize());	  pMga->pClientStruct = xalloc(sizeof(CLIENTDATA));	  pMga->pClientStruct->pMga = (MGAPtr) pMga;	  MGAOpenLibrary(pMga->pBoard,pMga->pClientStruct,sizeof(CLIENTDATA));	  pMga->pMgaHwInfo = xalloc(sizeof(MGAHWINFO));	  MGAGetHardwareInfo(pMga->pBoard,pMga->pMgaHwInfo);	  /* Detecting for type of display */	  if (pMga->pMgaHwInfo->ulCapsSecondOutput & MGAHWINFOCAPS_OUTPUT_TV) {	  	xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "TV detected\n");	  }	  if (pMga->pMgaHwInfo->ulCapsFirstOutput &			MGAHWINFOCAPS_OUTPUT_DIGITAL) {	  	xf86DrvMsg(pScrn->scrnIndex, X_PROBED,				"Digital Screen detected\n");	  }	  if (pMga->pMgaHwInfo->ulCapsSecondOutput &			MGAHWINFOCAPS_OUTPUT_DIGITAL) {	  	xf86DrvMsg(pScrn->scrnIndex, X_PROBED,				"Digital Screen detected\n");	  }	  /* Now copy these to the entitystructure */	  pMgaEnt->pClientStruct = pMga->pClientStruct;	  pMgaEnt->pBoard = pMga->pBoard;	  pMgaEnt->pMgaHwInfo = pMga->pMgaHwInfo;       } else { /* Ref count is 2 */	  pMga->pClientStruct = pMgaEnt->pClientStruct;	  pMga->pBoard = pMgaEnt->pBoard;	  pMga->pMgaHwInfo = pMgaEnt->pMgaHwInfo;       }       );	/* MGA_HAL */#endif    } else {#ifdef USEMGAHAL	  MGA_HAL(	  pMga->pBoard = xalloc(sizeof(CLIENTDATA) + MGAGetBOARDHANDLESize());	  pMga->pClientStruct = xalloc(sizeof(CLIENTDATA));	  pMga->pClientStruct->pMga = (MGAPtr) pMga;	  MGAOpenLibrary(pMga->pBoard,pMga->pClientStruct,sizeof(CLIENTDATA));	  pMga->pMgaHwInfo = xalloc(sizeof(MGAHWINFO));	  MGAGetHardwareInfo(pMga->pBoard,pMga->pMgaHwInfo);	  );	/* MGA_HAL */#endif    }#ifdef USEMGAHAL    MGA_HAL(	/* There is a problem in the HALlib: set soft reset bit */	/* MATROX: hint, hint */	if (!pMga->Primary && !pMga->FBDev &&	    (pMga->PciInfo->subsysCard == PCI_CARD_MILL_G200_SG) ) {	    OUTREG(MGAREG_Reset, 1);	    usleep(200);	    OUTREG(MGAREG_Reset, 0);	}    );	/* MGA_HAL */#endif    /* Initialise the MMIO vgahw functions */    vgaHWSetMmioFuncs(hwp, pMga->IOBase, PORT_OFFSET);    vgaHWGetIOBase(hwp);    /* Map the VGA memory when the primary video */    if (pMga->Primary && !pMga->FBDev) {	hwp->MapSize = 0x10000;	if (!vgaHWMapMem(pScrn))	    return FALSE;    }    if (pMga->FBDev) {	fbdevHWSave(pScrn);	/* Disable VGA core, and leave memory access on */	pciSetBitsLong(pMga->PciTag, PCI_OPTION_REG, 0x100, 0x000);	if (!fbdevHWModeInit(pScrn, pScrn->currentMode))	    return FALSE;	if(pMga->SecondCrtc == FALSE && pMga->HWCursor == TRUE) {	    switch (pMga->Chipset) {	    case PCI_CHIP_MGA1064:	    case PCI_CHIP_MGAG100:	    case PCI_CHIP_MGAG100_PCI:	    case PCI_CHIP_MGAG200:	    case PCI_CHIP_MGAG200_PCI:	    case PCI_CHIP_MGAG400:	    case PCI_CHIP_MGAG550:		outMGAdac(MGA1064_CURSOR_BASE_ADR_LOW, pMga->FbCursorOffset >> 10);		outMGAdac(MGA1064_CURSOR_BASE_ADR_HI, pMga->FbCursorOffset >> 18);		break;	    default:		break;	    }	}	MGAStormEngineInit(pScrn);    } else {	/* Save the current state */	MGASave(pScrn);	/* Initialise the first mode */	if (!MGAModeInit(pScrn, pScrn->currentMode))	    return FALSE;    }    /* Darken the screen for aesthetic reasons and set the viewport */    if (pMga->SecondCrtc == TRUE) {        MGASaveScreenCrtc2(pScreen, SCREEN_SAVER_ON);    } else {        MGASaveScreen(pScreen, SCREEN_SAVER_ON);    }    pScrn->AdjustFrame(scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);    /*     * The next step is to setup the screen's visuals, and initialise the     * framebuffer code.  In cases where the framebuffer's default     * choices for things like visual layouts and bits per RGB are OK,     * this may be as simple as calling the framebuffer's ScreenInit()     * function.  If not, the visuals will need to be setup before calling     * a fb ScreenInit() function and fixed up after.     *     * For most PC hardware at depths >= 8, the defaults that cfb uses     * are not appropriate.  In this driver, we fixup the visuals after.     */    /*     * Reset the visual list.     */    miClearVisualTypes();    /* Setup the visuals we support. */    /* All MGA support DirectColor and can do overlays in 32bpp */    if(pMga->Overlay8Plus24 && (pScrn->bitsPerPixel == 32)) {	if (!miSetVisualTypes(8, PseudoColorMask | GrayScaleMask,			      pScrn->rgbBits, PseudoColor))		return FALSE;	if (!miSetVisualTypes(24, TrueColorMask, pScrn->rgbBits, TrueColor))		return FALSE;    } else if (pMga->SecondCrtc) {	/* No DirectColor on the second head */	if (!miSetVisualTypes(pScrn->depth, TrueColorMask, pScrn->rgbBits,			      TrueColor))		return FALSE;	if (!miSetPixmapDepths ())	    return FALSE;    } else {	if (!xf86SetDefaultVisual(pScrn, -1))	    return FALSE;	if (!miSetVisualTypes(pScrn->depth,			      miGetDefaultVisualMask(pScrn->depth),			      pScrn->rgbBits, pScrn->defaultVisual))	    return FALSE;	if (!miSetPixmapDepths ())	    return FALSE;    }    /*     * Call the framebuffer layer's ScreenInit function, and fill in other     * pScreen fields.     */    width = pScrn->virtualX;    height = pScrn->virtualY;    displayWidth = pScrn->displayWidth;    if(pMga->Rotate) {	height = pScrn->virtualX;	width = pScrn->virtualY;    }    if(pMga->ShadowFB) { 	pMga->ShadowPitch = BitmapBytePad(pScrn->bitsPerPixel * width);	pMga->ShadowPtr = xalloc(pMga->ShadowPitch * height);	displayWidth = pMga->ShadowPitch / (pScrn->bitsPerPixel >> 3);        FBStart = pMga->ShadowPtr;    } else {	pMga->ShadowPtr = NULL;	FBStart = pMga->FbStart;    }#ifdef XF86DRI     /*      * Setup DRI after visuals have been established, but before cfbScreenInit      * is called.   cfbScreenInit will eventually call into the drivers      * InitGLXVisuals call back.      * The DRI does not work when textured video is enabled at this time.      */    if (!xf86ReturnOptValBool(pMga->Options, OPTION_DRI, TRUE)) {	driFrom = X_CONFIG;    } else if ( pMga->NoAccel ) {       xf86DrvMsg( pScrn->scrnIndex, X_ERROR,		   "Acceleration disabled, not initializing the DRI\n" );       pMga->directRenderingEnabled = FALSE;       driFrom = X_CONFIG;    }    else if ( pMga->TexturedVideo == TRUE ) {       xf86DrvMsg( pScrn->scrnIndex, X_ERROR,		   "Textured video enabled, not initializing the DRI\n" );       pMga->directRenderingEnabled = FALSE;       driFrom = X_CONFIG;    }    else if (pMga->SecondCrtc == TRUE) {       xf86DrvMsg( pScrn->scrnIndex, X_ERROR,		   "Not initializing the DRI on the second head\n" );       pMga->directRenderingEnabled = FALSE;    }    else if ((pMga->FbMapSize /	       (width * (pScrn->bitsPerPixel >> 3))) <= height * 3) {       xf86DrvMsg(pScrn->scrnIndex, X_ERROR,	  "Static buffer allocation failed, not initializing the DRI\n");       xf86DrvMsg(pScrn->scrnIndex, X_ERROR,	  "Need at least %d kB video memory at this resolution, bit depth\n",	  (3 * displayWidth * height * (pScrn->bitsPerPixel >> 3)) / 1024 );       pMga->directRenderingEnabled = FALSE;       driFrom = X_PROBED;    }    else {       pMga->directRenderingEnabled = MGADRIScreenInit(pScreen);    }#endif    if (pMga->Overlay8Plus24) {	ret = cfb8_32ScreenInit(pScreen, FBStart,			width, height,			pScrn->xDpi, pScrn->yDpi,			displayWidth);    } else {	ret = fbScreenInit(pScreen, FBStart, width, height,			   pScrn->xDpi, pScrn->yDpi,			   displayWidth, pScrn->bitsPerPixel);    }    if (!ret)	return FALSE;    if (pScrn->bitsPerPixel > 8) {        /* Fixup RGB ordering */        visual = pScreen->visuals + pScreen->numVisuals;        while (--visual >= pScreen->visuals) {	    if ((visual->class | DynamicClass) == DirectColor) {		visual->offsetRed = pScrn->offset.red;		visual->offsetGreen = pScrn->offset.green;		visual->offsetBlue = pScrn->offset.blue;		visual->redMask = pScrn->mask.red;		visual->greenMask = pScrn->mask.green;		visual->blueMask = pScrn->mask.blue;	    }	}    }    /* must be after RGB ordering fixed */    if (!pMga->Overlay8Plus24)	fbPictureInit (pScreen, 0, 0);        xf86SetBlackWhitePixels(pScreen);    pMga->BlockHandler = pScreen->BlockHandler;    pScreen->BlockHandler = MGABlockHandler;    if(!pMga->ShadowFB) /* hardware cursor needs to wrap this layer */	MGADGAInit(pScreen);    if (!pMga->NoAccel)	MGAStormAccelInit(pScreen);    miInitializeBackingStore(pScreen);    xf86SetBackingStore(pScreen);    xf86SetSilkenMouse(pScreen);    /* Initialize software cursor.	Must precede creation of the default colormap */    miDCInitialize(pScreen, xf86GetPointerScreenFuncs());    /* Initialize HW cursor layer.	Must follow software cursor initialization*/    if (pMga->HWCursor) {	if(!MGAHWCursorInit(pScreen))	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,		"Hardware cursor initialization failed\n");    }    /* Initialise default colourmap */    if (!miCreateDefColormap(pScreen))	return FALSE;    /* Initialize colormap layer.	Must follow initialization of the default colormap */    if (!pMga->SecondCrtc)	f = CMAP_PALETTED_TRUECOLOR | CMAP_RELOAD_ON_MODE_SWITCH;    else	f = CMAP_RELOAD_ON_MODE_SWITCH;    if(!xf86HandleColormaps(pScreen, 256, 8,	(pMga->FBDev ? fbdevHWLoadPalette : MGAdac->LoadPalette), NULL, f))	return FALSE;    if(pMga->Overlay8Plus24) { /* Must come after colormap initialization */	if(!xf86Overlay8Plus32Init(pScreen))	    return FALSE;    }    if(pMga->ShadowFB) {	RefreshAreaFuncPtr refreshArea = MGARefreshArea;	if(pMga->Rotate) {	    if (!pMga->PointerMoved) {	    pMga->PointerMoved = pScrn->PointerMoved;	    pScrn->PointerMoved = MGAPointerMoved;	    }	   switch(pScrn->bitsPerPixel) {	   case 8:	refreshArea = MGARefreshArea8;	break;	   case 16:	refreshArea = MGARefreshArea16;	break;	   case 24:	refreshArea = MGARefreshArea24;	break;	   case 32:	refreshArea = MGARefreshArea32;	break;	   }	}	ShadowFBInit(pScreen, refreshArea);    }    if(pMga->SecondCrtc == TRUE) {        xf86DPMSInit(pScreen, MGADisplayPowerManagementSetCrtc2, 0);    } else {        xf86DPMSInit(pScreen, MGADisplayPowerManagementSet, 0);    }    pScrn->memPhysBase = pMga->FbAddress;    pScrn->fbOffset = pMga->YDstOrg * (pScrn->bitsPerPixel / 8);    if(pMga->SecondCrtc == TRUE) {	pScreen->SaveScreen = MGASaveScreenCrtc2;    } else {	pScreen->SaveScreen = MGASaveScreen;    }    MGAInitVideo(pScreen);#ifdef XF86DRI    if (pMga->directRenderingEnabled) {       /* Now that mi, cfb, drm and others have done their thing,	* complete the DRI setup.	*/        pMga->directRenderingEnabled = MGADRIFinishScreenInit(pScreen);    }    if (pMga->directRenderingEnabled) {        xf86DrvMsg(pScrn->scrnIndex, driFrom, "Direct rendering enabled\n");    } else {        xf86DrvMsg(pScrn->scrnIndex, driFrom, "Direct rendering disabled\n");    }    if (xf86IsEntityShared(pScrn->entityList[0]) && pMga->SecondCrtc == FALSE)	pMgaEnt->directRenderingEnabled = pMga->directRenderingEnabled;    pMga->haveQuiescense = 1;#endif    /* Wrap the current CloseScreen function */    pMga->CloseScreen = pScreen->CloseScreen;    pScreen->CloseScreen = MGACloseScreen;    /* Report any unused options (only for the first generation) */    if (serverGeneration == 1) {	xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);    }    /* For the second head, work around display problem. */    if (pMga->SecondCrtc) {	MGACrtc2FillStrip(pScrn);    }    /* Done */    return TRUE;}