1b9325ec5Stsutsui
2b9325ec5Stsutsui/* $Xorg: sunFbs.c,v 1.4 2001/02/09 02:04:43 xorgcvs Exp $ */
3b9325ec5Stsutsui
4b9325ec5Stsutsui/*
5b9325ec5StsutsuiCopyright 1990, 1993, 1998  The Open Group
6b9325ec5Stsutsui
7b9325ec5StsutsuiPermission to use, copy, modify, distribute, and sell this software and its
8b9325ec5Stsutsuidocumentation for any purpose is hereby granted without fee, provided that
9b9325ec5Stsutsuithe above copyright notice appear in all copies and that both that
10b9325ec5Stsutsuicopyright notice and this permission notice appear in supporting
11b9325ec5Stsutsuidocumentation.
12b9325ec5Stsutsui
13b9325ec5StsutsuiThe above copyright notice and this permission notice shall be included in
14b9325ec5Stsutsuiall copies or substantial portions of the Software.
15b9325ec5Stsutsui
16b9325ec5StsutsuiTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17b9325ec5StsutsuiIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18b9325ec5StsutsuiFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19b9325ec5StsutsuiOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20b9325ec5StsutsuiAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21b9325ec5StsutsuiCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22b9325ec5Stsutsui
23b9325ec5StsutsuiExcept as contained in this notice, the name of The Open Group shall not be
24b9325ec5Stsutsuiused in advertising or otherwise to promote the sale, use or other dealings
25b9325ec5Stsutsuiin this Software without prior written authorization from The Open Group.
26b9325ec5Stsutsui */
27b9325ec5Stsutsui
28b9325ec5Stsutsui/************************************************************
29b9325ec5StsutsuiCopyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
30b9325ec5Stsutsui
31b9325ec5Stsutsui                    All Rights Reserved
32b9325ec5Stsutsui
33b9325ec5StsutsuiPermission  to  use,  copy,  modify,  and  distribute   this
34b9325ec5Stsutsuisoftware  and  its documentation for any purpose and without
35b9325ec5Stsutsuifee is hereby granted, provided that the above copyright no-
36b9325ec5Stsutsuitice  appear  in all copies and that both that copyright no-
37b9325ec5Stsutsuitice and this permission notice appear in  supporting  docu-
38b9325ec5Stsutsuimentation,  and  that the names of Sun or The Open Group
39b9325ec5Stsutsuinot be used in advertising or publicity pertaining to
40b9325ec5Stsutsuidistribution  of  the software  without specific prior
41b9325ec5Stsutsuiwritten permission. Sun and The Open Group make no
42b9325ec5Stsutsuirepresentations about the suitability of this software for
43b9325ec5Stsutsuiany purpose. It is provided "as is" without any express or
44b9325ec5Stsutsuiimplied warranty.
45b9325ec5Stsutsui
46b9325ec5StsutsuiSUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
47b9325ec5StsutsuiINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
48b9325ec5StsutsuiNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
49b9325ec5StsutsuiABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
50b9325ec5StsutsuiANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
51b9325ec5StsutsuiPROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
52b9325ec5StsutsuiOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
53b9325ec5StsutsuiTHE USE OR PERFORMANCE OF THIS SOFTWARE.
54b9325ec5Stsutsui
55b9325ec5Stsutsui********************************************************/
56b9325ec5Stsutsui
57b9325ec5Stsutsui/*
58b9325ec5Stsutsui * Copyright 1987 by the Regents of the University of California
59b9325ec5Stsutsui * Copyright 1987 by Adam de Boor, UC Berkeley
60b9325ec5Stsutsui *
61b9325ec5Stsutsui * Permission to use, copy, modify, and distribute this
62b9325ec5Stsutsui * software and its documentation for any purpose and without
63b9325ec5Stsutsui * fee is hereby granted, provided that the above copyright
64b9325ec5Stsutsui * notice appear in all copies.  The University of California
65b9325ec5Stsutsui * makes no representations about the suitability of this
66b9325ec5Stsutsui * software for any purpose.  It is provided "as is" without
67b9325ec5Stsutsui * express or implied warranty.
68b9325ec5Stsutsui */
69b9325ec5Stsutsui
70b9325ec5Stsutsui/* $XFree86: xc/programs/Xserver/hw/sun/sunFbs.c,v 1.8 2003/11/17 22:20:36 dawes Exp $ */
71b9325ec5Stsutsui
72b9325ec5Stsutsui/****************************************************************/
73b9325ec5Stsutsui/* Modified from  sunCG4C.c for X11R3 by Tom Jarmolowski	*/
74b9325ec5Stsutsui/****************************************************************/
75b9325ec5Stsutsui
76b9325ec5Stsutsui#include "sun.h"
77b9325ec5Stsutsui#include <sys/mman.h>
78b9325ec5Stsutsui
79b9325ec5Stsutsuistatic Bool closeScreen(int, ScreenPtr pScreen);
80b9325ec5Stsutsui
81b9325ec5Stsutsuiint sunScreenIndex;
82b9325ec5Stsutsui
83b9325ec5StsutsuiDevPrivateKeyRec sunScreenPrivateKeyRec;
84b9325ec5Stsutsui
85b9325ec5Stsutsuipointer
86b9325ec5StsutsuisunMemoryMap(size_t len, off_t off, int fd)
87b9325ec5Stsutsui{
88b9325ec5Stsutsui    int		pagemask, mapsize;
89b9325ec5Stsutsui    caddr_t	addr;
90b9325ec5Stsutsui    pointer	mapaddr;
91b9325ec5Stsutsui
92b9325ec5Stsutsui#ifdef SVR4
93b9325ec5Stsutsui    pagemask = sysconf(_SC_PAGESIZE) - 1;
94b9325ec5Stsutsui#else
95b9325ec5Stsutsui    pagemask = getpagesize() - 1;
96b9325ec5Stsutsui#endif
97b9325ec5Stsutsui    mapsize = ((int) len + pagemask) & ~pagemask;
98b9325ec5Stsutsui    addr = 0;
99b9325ec5Stsutsui
100b9325ec5Stsutsui#if !defined(__bsdi__) && !defined(_MAP_NEW) && !defined(__NetBSD__) && !defined(__OpenBSD__)
101b9325ec5Stsutsui    if ((addr = (caddr_t) valloc (mapsize)) == NULL) {
102b9325ec5Stsutsui	Error ("Couldn't allocate frame buffer memory");
103b9325ec5Stsutsui	(void) close (fd);
104b9325ec5Stsutsui	return NULL;
105b9325ec5Stsutsui    }
106b9325ec5Stsutsui#endif
107b9325ec5Stsutsui
108b9325ec5Stsutsui#if !defined(__NetBSD__) && !defined(__OpenBSD__)
109b9325ec5Stsutsui    /*
110b9325ec5Stsutsui     * try and make it private first, that way once we get it, an
111b9325ec5Stsutsui     * interloper, e.g. another server, can't get this frame buffer,
112b9325ec5Stsutsui     * and if another server already has it, this one won't.
113b9325ec5Stsutsui     */
114b9325ec5Stsutsui    if ((int)(mapaddr = (pointer) mmap (addr,
115b9325ec5Stsutsui		mapsize,
116b9325ec5Stsutsui		PROT_READ | PROT_WRITE, MAP_PRIVATE,
117b9325ec5Stsutsui		fd, off)) == -1)
118b9325ec5Stsutsui#endif
119b9325ec5Stsutsui	mapaddr = (pointer) mmap (addr,
120b9325ec5Stsutsui		    mapsize,
121b9325ec5Stsutsui		    PROT_READ | PROT_WRITE, MAP_SHARED,
122b9325ec5Stsutsui		    fd, off);
123b9325ec5Stsutsui    if (mapaddr == (pointer) -1) {
124b9325ec5Stsutsui	Error ("mapping frame buffer memory");
125b9325ec5Stsutsui	(void) close (fd);
126b9325ec5Stsutsui	mapaddr = (pointer) NULL;
127b9325ec5Stsutsui    }
128b9325ec5Stsutsui    return mapaddr;
129b9325ec5Stsutsui}
130b9325ec5Stsutsui
131b9325ec5StsutsuiBool
132b9325ec5StsutsuisunScreenAllocate(ScreenPtr pScreen)
133b9325ec5Stsutsui{
134b9325ec5Stsutsui    sunScreenPtr    pPrivate;
135b9325ec5Stsutsui
136b9325ec5Stsutsui    if (!dixRegisterPrivateKey(&sunScreenPrivateKeyRec, PRIVATE_SCREEN, 0)) {
137b9325ec5Stsutsui	Error("dixRegisterPrivateKey failed");
138b9325ec5Stsutsui	return FALSE;
139b9325ec5Stsutsui    }
140cb17d216Stsutsui    pPrivate = calloc(1, sizeof (sunScreenRec));
141b9325ec5Stsutsui    if (!pPrivate)
142b9325ec5Stsutsui	return FALSE;
143b9325ec5Stsutsui
144cb17d216Stsutsui    pPrivate->origColormapValid = FALSE;
145b9325ec5Stsutsui    sunSetScreenPrivate(pScreen, pPrivate);
146b9325ec5Stsutsui    return TRUE;
147b9325ec5Stsutsui}
148b9325ec5Stsutsui
149b9325ec5StsutsuiBool
150b9325ec5StsutsuisunSaveScreen(ScreenPtr pScreen, int on)
151b9325ec5Stsutsui{
152b9325ec5Stsutsui    int		state;
153b9325ec5Stsutsui
154b9325ec5Stsutsui    if (on != SCREEN_SAVER_FORCER)
155b9325ec5Stsutsui    {
156b9325ec5Stsutsui	if (on == SCREEN_SAVER_ON)
157b9325ec5Stsutsui	    state = 0;
158b9325ec5Stsutsui	else
159b9325ec5Stsutsui	    state = 1;
160b9325ec5Stsutsui	(void) ioctl(sunFbs[pScreen->myNum].fd, FBIOSVIDEO, &state);
161b9325ec5Stsutsui    }
162b9325ec5Stsutsui    return( TRUE );
163b9325ec5Stsutsui}
164b9325ec5Stsutsui
165b9325ec5Stsutsuistatic Bool
166b9325ec5StsutsuicloseScreen(int i, ScreenPtr pScreen)
167b9325ec5Stsutsui{
168b9325ec5Stsutsui    sunScreenPtr pPrivate = sunGetScreenPrivate(pScreen);
169b9325ec5Stsutsui    Bool    ret;
170b9325ec5Stsutsui
171b9325ec5Stsutsui    (void) OsSignal (SIGIO, SIG_IGN);
172b9325ec5Stsutsui#if 0	/* XXX GX is disabled for now */
173b9325ec5Stsutsui    sunDisableCursor (pScreen);
174b9325ec5Stsutsui#endif
175cb17d216Stsutsui    if (pPrivate->origColormapValid)
176cb17d216Stsutsui	(*pPrivate->RestoreColormap)(pScreen);
177b9325ec5Stsutsui    pScreen->CloseScreen = pPrivate->CloseScreen;
178b9325ec5Stsutsui    ret = (*pScreen->CloseScreen) (i, pScreen);
179b9325ec5Stsutsui    (void) (*pScreen->SaveScreen) (pScreen, SCREEN_SAVER_OFF);
180cb17d216Stsutsui    free ((pointer) pPrivate);
181b9325ec5Stsutsui    return ret;
182b9325ec5Stsutsui}
183b9325ec5Stsutsui
184b9325ec5StsutsuiBool
185b9325ec5StsutsuisunScreenInit(ScreenPtr pScreen)
186b9325ec5Stsutsui{
187b9325ec5Stsutsui    sunScreenPtr pPrivate = sunGetScreenPrivate(pScreen);
188b9325ec5Stsutsui
189b9325ec5Stsutsui    pPrivate->installedMap = 0;
190b9325ec5Stsutsui    pPrivate->CloseScreen = pScreen->CloseScreen;
191b9325ec5Stsutsui    pScreen->CloseScreen = closeScreen;
192b9325ec5Stsutsui    pScreen->SaveScreen = sunSaveScreen;
193b9325ec5Stsutsui#if 0	/* XXX GX is disabled for now */
194b9325ec5Stsutsui    if (!sunCursorInitialize (pScreen))
195b9325ec5Stsutsui#endif
196b9325ec5Stsutsui	miDCInitialize (pScreen, &sunPointerScreenFuncs);
197b9325ec5Stsutsui    return TRUE;
198b9325ec5Stsutsui}
199b9325ec5Stsutsui
200b9325ec5StsutsuiBool
201b9325ec5StsutsuisunInitCommon(
202b9325ec5Stsutsui    int		scrn,
203b9325ec5Stsutsui    ScreenPtr	pScrn,
204b9325ec5Stsutsui    off_t	offset,
205b9325ec5Stsutsui    Bool	(*init1)(ScreenPtr, pointer, int, int, int, int, int, int),
206b9325ec5Stsutsui    void	(*init2)(ScreenPtr),
207b9325ec5Stsutsui    Bool	(*cr_cm)(ScreenPtr),
208b9325ec5Stsutsui    Bool	(*save)(ScreenPtr, int),
209b9325ec5Stsutsui    int		fb_off
210b9325ec5Stsutsui)
211b9325ec5Stsutsui{
212b9325ec5Stsutsui    unsigned char*	fb = sunFbs[scrn].fb;
213b9325ec5Stsutsui
214b9325ec5Stsutsui    if (!sunScreenAllocate (pScrn))
215b9325ec5Stsutsui	return FALSE;
216b9325ec5Stsutsui    if (!fb) {
217b9325ec5Stsutsui	if ((fb = sunMemoryMap ((size_t) sunFbs[scrn].info.fb_size,
218b9325ec5Stsutsui			     offset,
219b9325ec5Stsutsui			     sunFbs[scrn].fd)) == NULL)
220b9325ec5Stsutsui	    return FALSE;
221b9325ec5Stsutsui	sunFbs[scrn].fb = fb;
222b9325ec5Stsutsui    }
223b9325ec5Stsutsui    /* mfbScreenInit() or cfbScreenInit() */
224b9325ec5Stsutsui    if (!(*init1)(pScrn, fb + fb_off,
225b9325ec5Stsutsui	    sunFbs[scrn].info.fb_width,
226b9325ec5Stsutsui	    sunFbs[scrn].info.fb_height,
227b9325ec5Stsutsui	    monitorResolution, monitorResolution,
228b9325ec5Stsutsui	    sunFbs[scrn].info.fb_width,
229b9325ec5Stsutsui	    sunFbs[scrn].info.fb_depth))
230b9325ec5Stsutsui	    return FALSE;
231b9325ec5Stsutsui    /* sunCGScreenInit() if cfb... */
232b9325ec5Stsutsui    if (init2)
233b9325ec5Stsutsui	(*init2)(pScrn);
234b9325ec5Stsutsui    if (!sunScreenInit(pScrn))
235b9325ec5Stsutsui	return FALSE;
236b9325ec5Stsutsui    (void) (*save) (pScrn, SCREEN_SAVER_OFF);
237b9325ec5Stsutsui    return (*cr_cm)(pScrn);
238b9325ec5Stsutsui}
239b9325ec5Stsutsui
240