if (xf86IsEntityShared(pScrn->entityList[0])) {
if (pMga->DualHeadEnabled) {
MGACloseScreen(int scrnIndex, ScreenPtr pScreen){    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];    vgaHWPtr hwp = VGAHWPTR(pScrn);    MGAPtr pMga = MGAPTR(pScrn);    MGAEntPtr pMgaEnt = NULL;#ifdef USEMGAHAL        MGA_HAL( RESTORE_TEXTMODE_ON_DVI(pMga); );#endif    if (pScrn->vtSema) {	if (pMga->FBDev) {	    fbdevHWRestore(pScrn);	    MGAUnmapMemFBDev(pScrn);        } else {	    MGARestore(pScrn);	    vgaHWLock(hwp);	    MGAUnmapMem(pScrn);	    vgaHWUnmapMem(pScrn);	}    }#ifdef XF86DRI   if (pMga->directRenderingEnabled) {       MGADRICloseScreen(pScreen);       pMga->directRenderingEnabled=FALSE;   }#endif   if (xf86IsEntityShared(pScrn->entityList[0])) {       DevUnion *pPriv;       pPriv = xf86GetEntityPrivate(pScrn->entityList[0], MGAEntityIndex);       pMgaEnt = pPriv->ptr;       pMgaEnt->refCount--;   }#ifdef USEMGAHAL   MGA_HAL(   if(xf86IsEntityShared(pScrn->entityList[0])) {      if(pMgaEnt->refCount == 0) {	 /* Both boards have closed there screen */	 MGACloseLibrary(pMga->pBoard);	 if (pMga->pBoard)	   xfree(pMga->pBoard);	 if (pMga->pClientStruct)	   xfree(pMga->pClientStruct);	 if (pMga->pMgaModeInfo)	   xfree(pMga->pMgaModeInfo);	 if (pMga->pMgaHwInfo)	   xfree(pMga->pMgaHwInfo);      }   } else {      MGACloseLibrary(pMga->pBoard);      if (pMga->pBoard)	xfree(pMga->pBoard);      if (pMga->pClientStruct)	xfree(pMga->pClientStruct);      if (pMga->pMgaModeInfo)	xfree(pMga->pMgaModeInfo);      if (pMga->pMgaHwInfo)	xfree(pMga->pMgaHwInfo);   }   );	/* MGA_HAL */#endif    if (pMga->AccelInfoRec)	XAADestroyInfoRec(pMga->AccelInfoRec);    if (pMga->CursorInfoRec)    	xf86DestroyCursorInfoRec(pMga->CursorInfoRec);    if (pMga->ShadowPtr)	xfree(pMga->ShadowPtr);    if (pMga->DGAModes)	xfree(pMga->DGAModes);    if (pMga->adaptor)	xfree(pMga->adaptor);    if (pMga->portPrivate)	xfree(pMga->portPrivate);    if (pMga->ScratchBuffer)	xfree(pMga->ScratchBuffer);    pScrn->vtSema = FALSE;    if (xf86IsPc98())	outb(0xfac, 0x00);    xf86ClearPrimInitDone(pScrn->entityList[0]);    if(pMga->BlockHandler)	pScreen->BlockHandler = pMga->BlockHandler;    pScreen->CloseScreen = pMga->CloseScreen;    return (*pScreen->CloseScreen)(scrnIndex, pScreen);}