1357bfbb2Smrg/*
2cba5e21cSmrg * Copyright © 2001-2012 Matthieu Herrb
3357bfbb2Smrg * All rights reserved.
4357bfbb2Smrg *
5357bfbb2Smrg * Redistribution and use in source and binary forms, with or without
6357bfbb2Smrg * modification, are permitted provided that the following conditions
7357bfbb2Smrg * are met:
8357bfbb2Smrg *
9357bfbb2Smrg *    - Redistributions of source code must retain the above copyright
10357bfbb2Smrg *      notice, this list of conditions and the following disclaimer.
11357bfbb2Smrg *    - Redistributions in binary form must reproduce the above
12357bfbb2Smrg *      copyright notice, this list of conditions and the following
13357bfbb2Smrg *      disclaimer in the documentation and/or other materials provided
14357bfbb2Smrg *      with the distribution.
15357bfbb2Smrg *
16357bfbb2Smrg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17357bfbb2Smrg * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18357bfbb2Smrg * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19357bfbb2Smrg * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20357bfbb2Smrg * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21357bfbb2Smrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22357bfbb2Smrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23357bfbb2Smrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24357bfbb2Smrg * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25357bfbb2Smrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26357bfbb2Smrg * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27357bfbb2Smrg * POSSIBILITY OF SUCH DAMAGE.
28357bfbb2Smrg *
29357bfbb2Smrg */
30357bfbb2Smrg
31357bfbb2Smrg/*
32357bfbb2Smrg * Based on fbdev.c written by:
33357bfbb2Smrg *
34357bfbb2Smrg * Authors:  Alan Hourihane, <alanh@fairlite.demon.co.uk>
35357bfbb2Smrg *	     Michel Dänzer, <michdaen@iiic.ethz.ch>
36357bfbb2Smrg */
37357bfbb2Smrg
38357bfbb2Smrg#ifdef HAVE_CONFIG_H
39357bfbb2Smrg#include "config.h"
40357bfbb2Smrg#endif
41357bfbb2Smrg
4264f08a18Smrg#include <errno.h>
43357bfbb2Smrg#include <fcntl.h>
44cba5e21cSmrg#include <unistd.h>
45cba5e21cSmrg#include <sys/ioctl.h>
46357bfbb2Smrg#include <sys/types.h>
4764f08a18Smrg#include <sys/mman.h>
48357bfbb2Smrg#include <sys/time.h>
492b2b17b3Smrg#include <errno.h>
50357bfbb2Smrg#include <dev/wscons/wsconsio.h>
51357bfbb2Smrg
5264f08a18Smrg/* All drivers need this. */
53357bfbb2Smrg#include "xf86.h"
54357bfbb2Smrg#include "xf86_OSproc.h"
55357bfbb2Smrg
56357bfbb2Smrg#include "mipointer.h"
57357bfbb2Smrg#include "micmap.h"
58357bfbb2Smrg#include "colormapst.h"
59357bfbb2Smrg#include "xf86cmap.h"
60357bfbb2Smrg#include "shadow.h"
61357bfbb2Smrg#include "dgaproc.h"
62357bfbb2Smrg
6364f08a18Smrg/* For visuals */
64357bfbb2Smrg#include "fb.h"
65357bfbb2Smrg
6664f08a18Smrg#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
67357bfbb2Smrg#include "xf86Resources.h"
68357bfbb2Smrg#include "xf86RAC.h"
6964f08a18Smrg#endif
70357bfbb2Smrg
71357bfbb2Smrg#ifdef XvExtension
72357bfbb2Smrg#include "xf86xv.h"
73357bfbb2Smrg#endif
74357bfbb2Smrg
755d492fd2Smacallan#include "wsfb.h"
765d492fd2Smacallan
7764f08a18Smrg#ifdef X_PRIVSEP
78357bfbb2Smrgextern int priv_open_device(const char *);
79357bfbb2Smrg#else
80357bfbb2Smrg#define priv_open_device(n)    open(n,O_RDWR|O_NONBLOCK|O_EXCL)
81357bfbb2Smrg#endif
82357bfbb2Smrg
83357bfbb2Smrg#if defined(__NetBSD__)
84357bfbb2Smrg#define WSFB_DEFAULT_DEV "/dev/ttyE0"
85357bfbb2Smrg#else
86357bfbb2Smrg#define WSFB_DEFAULT_DEV "/dev/ttyC0"
87357bfbb2Smrg#endif
88357bfbb2Smrg
89357bfbb2Smrg#define DEBUG 0
90357bfbb2Smrg
91357bfbb2Smrg#if DEBUG
92357bfbb2Smrg# define TRACE_ENTER(str)       ErrorF("wsfb: " str " %d\n",pScrn->scrnIndex)
93357bfbb2Smrg# define TRACE_EXIT(str)        ErrorF("wsfb: " str " done\n")
94357bfbb2Smrg# define TRACE(str)             ErrorF("wsfb trace: " str "\n")
95357bfbb2Smrg#else
96357bfbb2Smrg# define TRACE_ENTER(str)
97357bfbb2Smrg# define TRACE_EXIT(str)
98357bfbb2Smrg# define TRACE(str)
99357bfbb2Smrg#endif
100357bfbb2Smrg
101357bfbb2Smrg/* Prototypes */
102357bfbb2Smrgstatic pointer WsfbSetup(pointer, pointer, int *, int *);
103357bfbb2Smrgstatic Bool WsfbGetRec(ScrnInfoPtr);
104357bfbb2Smrgstatic void WsfbFreeRec(ScrnInfoPtr);
105357bfbb2Smrgstatic const OptionInfoRec * WsfbAvailableOptions(int, int);
106357bfbb2Smrgstatic void WsfbIdentify(int);
107357bfbb2Smrgstatic Bool WsfbProbe(DriverPtr, int);
108357bfbb2Smrgstatic Bool WsfbPreInit(ScrnInfoPtr, int);
1099974adabSmrgstatic Bool WsfbScreenInit(SCREEN_INIT_ARGS_DECL);
1109974adabSmrgstatic Bool WsfbCloseScreen(CLOSE_SCREEN_ARGS_DECL);
111357bfbb2Smrgstatic void *WsfbWindowLinear(ScreenPtr, CARD32, CARD32, int, CARD32 *,
112357bfbb2Smrg			      void *);
113dc9c99a5Stsutsui#ifdef HAVE_SHADOW_AFB
1143306a6a4Sjandbergstatic void *WsfbWindowAfb(ScreenPtr, CARD32, CARD32, int, CARD32 *,
1153306a6a4Sjandberg			      void *);
116dc9c99a5Stsutsui#endif
1179974adabSmrgstatic void WsfbPointerMoved(SCRN_ARG_TYPE, int, int);
1189974adabSmrgstatic Bool WsfbEnterVT(VT_FUNC_ARGS_DECL);
1199974adabSmrgstatic void WsfbLeaveVT(VT_FUNC_ARGS_DECL);
1209974adabSmrgstatic Bool WsfbSwitchMode(SWITCH_MODE_ARGS_DECL);
1219974adabSmrgstatic int WsfbValidMode(SCRN_ARG_TYPE, DisplayModePtr, Bool, int);
122357bfbb2Smrgstatic void WsfbLoadPalette(ScrnInfoPtr, int, int *, LOCO *, VisualPtr);
123357bfbb2Smrgstatic Bool WsfbSaveScreen(ScreenPtr, int);
124357bfbb2Smrgstatic void WsfbSave(ScrnInfoPtr);
125357bfbb2Smrgstatic void WsfbRestore(ScrnInfoPtr);
126357bfbb2Smrg
12764f08a18Smrg/* DGA stuff */
128357bfbb2Smrg#ifdef XFreeXDGA
129357bfbb2Smrgstatic Bool WsfbDGAOpenFramebuffer(ScrnInfoPtr, char **, unsigned char **,
130357bfbb2Smrg				   int *, int *, int *);
131357bfbb2Smrgstatic Bool WsfbDGASetMode(ScrnInfoPtr, DGAModePtr);
132357bfbb2Smrgstatic void WsfbDGASetViewport(ScrnInfoPtr, int, int, int);
133357bfbb2Smrgstatic Bool WsfbDGAInit(ScrnInfoPtr, ScreenPtr);
134357bfbb2Smrg#endif
1350ef55ed1Smacallan
136bff82b7eSjmcneillstatic void WsfbShadowUpdateRGB16ToYUY2(ScreenPtr, shadowBufPtr);
1370ef55ed1Smacallanstatic void WsfbShadowUpdateSwap32(ScreenPtr, shadowBufPtr);
138cbc9a668Smacallanstatic void WsfbShadowUpdateSplit(ScreenPtr, shadowBufPtr);
1390ef55ed1Smacallan
1400ef55ed1Smacallanstatic Bool WsfbDriverFunc(ScrnInfoPtr, xorgDriverFuncOp, pointer);
141357bfbb2Smrg
14264f08a18Smrg/* Helper functions */
143cba5e21cSmrgstatic int wsfb_open(const char *);
144357bfbb2Smrgstatic pointer wsfb_mmap(size_t, off_t, int);
145357bfbb2Smrg
146357bfbb2Smrgenum { WSFB_ROTATE_NONE = 0,
147357bfbb2Smrg       WSFB_ROTATE_CCW = 90,
148357bfbb2Smrg       WSFB_ROTATE_UD = 180,
149357bfbb2Smrg       WSFB_ROTATE_CW = 270
150357bfbb2Smrg};
151357bfbb2Smrg
152357bfbb2Smrg/*
153cba5e21cSmrg * This is intentionally screen-independent.
15464f08a18Smrg * It indicates the binding choice made in the first PreInit.
155357bfbb2Smrg */
156357bfbb2Smrgstatic int pix24bpp = 0;
157357bfbb2Smrg
158bff82b7eSjmcneill/*
159bff82b7eSjmcneill * Screen-independent lookup table for RGB16 to YUV conversions.
160bff82b7eSjmcneill */
161bff82b7eSjmcneillstatic unsigned char *mapRGB16ToY = NULL;
162bff82b7eSjmcneillstatic unsigned char *mapRGB16ToU = NULL;
163bff82b7eSjmcneillstatic unsigned char *mapRGB16ToV = NULL;
164bff82b7eSjmcneill
1658a4fa5cdStsutsui#define WSFB_VERSION		4000
166357bfbb2Smrg#define WSFB_NAME		"wsfb"
167357bfbb2Smrg#define WSFB_DRIVER_NAME	"wsfb"
168357bfbb2Smrg
169357bfbb2Smrg_X_EXPORT DriverRec WSFB = {
170357bfbb2Smrg	WSFB_VERSION,
1718a4fa5cdStsutsui	(char *)WSFB_DRIVER_NAME,
172357bfbb2Smrg	WsfbIdentify,
173357bfbb2Smrg	WsfbProbe,
174357bfbb2Smrg	WsfbAvailableOptions,
175357bfbb2Smrg	NULL,
176357bfbb2Smrg	0,
177357bfbb2Smrg	WsfbDriverFunc
178357bfbb2Smrg};
179357bfbb2Smrg
180357bfbb2Smrg/* Supported "chipsets" */
181357bfbb2Smrgstatic SymTabRec WsfbChipsets[] = {
182357bfbb2Smrg	{ 0, "wsfb" },
183357bfbb2Smrg	{ -1, NULL }
184357bfbb2Smrg};
185357bfbb2Smrg
186357bfbb2Smrg/* Supported options */
187357bfbb2Smrgtypedef enum {
188357bfbb2Smrg	OPTION_SHADOW_FB,
1895d492fd2Smacallan	OPTION_ROTATE,
1905d492fd2Smacallan	OPTION_HW_CURSOR,
1915d492fd2Smacallan	OPTION_SW_CURSOR
192357bfbb2Smrg} WsfbOpts;
193357bfbb2Smrg
194357bfbb2Smrgstatic const OptionInfoRec WsfbOptions[] = {
195357bfbb2Smrg	{ OPTION_SHADOW_FB, "ShadowFB", OPTV_BOOLEAN, {0}, FALSE},
196357bfbb2Smrg	{ OPTION_ROTATE, "Rotate", OPTV_STRING, {0}, FALSE},
197b6e55117Smacallan	{ OPTION_HW_CURSOR, "HWCursor", OPTV_BOOLEAN, {1}, FALSE},
198357bfbb2Smrg	{ -1, NULL, OPTV_NONE, {0}, FALSE}
199357bfbb2Smrg};
200357bfbb2Smrg
201357bfbb2Smrgstatic XF86ModuleVersionInfo WsfbVersRec = {
202357bfbb2Smrg	"wsfb",
203357bfbb2Smrg	MODULEVENDORSTRING,
204357bfbb2Smrg	MODINFOSTRING1,
205357bfbb2Smrg	MODINFOSTRING2,
206357bfbb2Smrg	XORG_VERSION_CURRENT,
207cba5e21cSmrg	PACKAGE_VERSION_MAJOR,
208cba5e21cSmrg	PACKAGE_VERSION_MINOR,
209357bfbb2Smrg	PACKAGE_VERSION_PATCHLEVEL,
210357bfbb2Smrg	ABI_CLASS_VIDEODRV,
211357bfbb2Smrg	ABI_VIDEODRV_VERSION,
212357bfbb2Smrg	NULL,
213357bfbb2Smrg	{0, 0, 0, 0}
214357bfbb2Smrg};
215357bfbb2Smrg
216357bfbb2Smrg_X_EXPORT XF86ModuleData wsfbModuleData = { &WsfbVersRec, WsfbSetup, NULL };
217357bfbb2Smrg
218357bfbb2Smrgstatic pointer
219357bfbb2SmrgWsfbSetup(pointer module, pointer opts, int *errmaj, int *errmin)
220357bfbb2Smrg{
221357bfbb2Smrg	static Bool setupDone = FALSE;
222357bfbb2Smrg
2238a4fa5cdStsutsui#if !defined(__OpenBSD__) && !defined(__NetBSD__)
22472ee27a2Smrg	return NULL;
22572ee27a2Smrg#endif
2268a4fa5cdStsutsui
227357bfbb2Smrg	if (!setupDone) {
228357bfbb2Smrg		setupDone = TRUE;
229357bfbb2Smrg		xf86AddDriver(&WSFB, module, HaveDriverFuncs);
230357bfbb2Smrg		return (pointer)1;
231357bfbb2Smrg	} else {
232357bfbb2Smrg		if (errmaj != NULL)
233357bfbb2Smrg			*errmaj = LDR_ONCEONLY;
234357bfbb2Smrg		return NULL;
235357bfbb2Smrg	}
236357bfbb2Smrg}
237357bfbb2Smrg
238357bfbb2Smrgstatic Bool
239357bfbb2SmrgWsfbGetRec(ScrnInfoPtr pScrn)
240357bfbb2Smrg{
241357bfbb2Smrg
242357bfbb2Smrg	if (pScrn->driverPrivate != NULL)
243357bfbb2Smrg		return TRUE;
244357bfbb2Smrg
245357bfbb2Smrg	pScrn->driverPrivate = xnfcalloc(sizeof(WsfbRec), 1);
246357bfbb2Smrg	return TRUE;
247357bfbb2Smrg}
248357bfbb2Smrg
249357bfbb2Smrgstatic void
250357bfbb2SmrgWsfbFreeRec(ScrnInfoPtr pScrn)
251357bfbb2Smrg{
252357bfbb2Smrg
253357bfbb2Smrg	if (pScrn->driverPrivate == NULL)
254357bfbb2Smrg		return;
255cba5e21cSmrg	free(pScrn->driverPrivate);
256357bfbb2Smrg	pScrn->driverPrivate = NULL;
257357bfbb2Smrg}
258357bfbb2Smrg
259357bfbb2Smrgstatic const OptionInfoRec *
260357bfbb2SmrgWsfbAvailableOptions(int chipid, int busid)
261357bfbb2Smrg{
262357bfbb2Smrg	return WsfbOptions;
263357bfbb2Smrg}
264357bfbb2Smrg
265357bfbb2Smrgstatic void
266357bfbb2SmrgWsfbIdentify(int flags)
267357bfbb2Smrg{
268357bfbb2Smrg	xf86PrintChipsets(WSFB_NAME, "driver for wsdisplay framebuffer",
269357bfbb2Smrg			  WsfbChipsets);
270357bfbb2Smrg}
271357bfbb2Smrg
27264f08a18Smrg/* Open the framebuffer device. */
273357bfbb2Smrgstatic int
274cba5e21cSmrgwsfb_open(const char *dev)
275357bfbb2Smrg{
276357bfbb2Smrg	int fd = -1;
277357bfbb2Smrg
278cba5e21cSmrg	/* Try argument from xorg.conf first. */
279357bfbb2Smrg	if (dev == NULL || ((fd = priv_open_device(dev)) == -1)) {
28064f08a18Smrg		/* Second: environment variable. */
281357bfbb2Smrg		dev = getenv("XDEVICE");
282357bfbb2Smrg		if (dev == NULL || ((fd = priv_open_device(dev)) == -1)) {
28364f08a18Smrg			/* Last try: default device. */
284357bfbb2Smrg			dev = WSFB_DEFAULT_DEV;
285357bfbb2Smrg			if ((fd = priv_open_device(dev)) == -1) {
286357bfbb2Smrg				return -1;
287357bfbb2Smrg			}
288357bfbb2Smrg		}
289357bfbb2Smrg	}
290357bfbb2Smrg	return fd;
291357bfbb2Smrg}
292357bfbb2Smrg
29364f08a18Smrg/* Map the framebuffer's memory. */
294357bfbb2Smrgstatic pointer
295357bfbb2Smrgwsfb_mmap(size_t len, off_t off, int fd)
296357bfbb2Smrg{
297357bfbb2Smrg	int pagemask, mapsize;
298357bfbb2Smrg	caddr_t addr;
299357bfbb2Smrg	pointer mapaddr;
300357bfbb2Smrg
301357bfbb2Smrg	pagemask = getpagesize() - 1;
302357bfbb2Smrg	mapsize = ((int) len + pagemask) & ~pagemask;
303357bfbb2Smrg	addr = 0;
304357bfbb2Smrg
305357bfbb2Smrg	/*
30664f08a18Smrg	 * Try and make it private first, that way once we get it, an
307357bfbb2Smrg	 * interloper, e.g. another server, can't get this frame buffer,
308357bfbb2Smrg	 * and if another server already has it, this one won't.
309357bfbb2Smrg	 */
310357bfbb2Smrg	mapaddr = (pointer) mmap(addr, mapsize,
311357bfbb2Smrg				 PROT_READ | PROT_WRITE, MAP_SHARED,
312357bfbb2Smrg				 fd, off);
3135d492fd2Smacallan	if (mapaddr == MAP_FAILED) {
314357bfbb2Smrg		mapaddr = NULL;
315357bfbb2Smrg	}
316357bfbb2Smrg#if DEBUG
317357bfbb2Smrg	ErrorF("mmap returns: addr %p len 0x%x\n", mapaddr, mapsize);
318357bfbb2Smrg#endif
319357bfbb2Smrg	return mapaddr;
320357bfbb2Smrg}
321357bfbb2Smrg
322357bfbb2Smrgstatic Bool
323357bfbb2SmrgWsfbProbe(DriverPtr drv, int flags)
324357bfbb2Smrg{
325357bfbb2Smrg	int i, fd, entity;
3268a4fa5cdStsutsui	GDevPtr *devSections;
327357bfbb2Smrg	int numDevSections;
328cba5e21cSmrg	const char *dev;
329357bfbb2Smrg	Bool foundScreen = FALSE;
330357bfbb2Smrg
331357bfbb2Smrg	TRACE("probe start");
332357bfbb2Smrg
333357bfbb2Smrg	/* For now, just bail out for PROBE_DETECT. */
334357bfbb2Smrg	if (flags & PROBE_DETECT)
335357bfbb2Smrg		return FALSE;
336357bfbb2Smrg
337357bfbb2Smrg	if ((numDevSections = xf86MatchDevice(WSFB_DRIVER_NAME,
338357bfbb2Smrg					      &devSections)) <= 0)
339357bfbb2Smrg		return FALSE;
340357bfbb2Smrg
341357bfbb2Smrg	for (i = 0; i < numDevSections; i++) {
342357bfbb2Smrg		ScrnInfoPtr pScrn = NULL;
343357bfbb2Smrg
344357bfbb2Smrg		dev = xf86FindOptionValue(devSections[i]->options, "device");
345357bfbb2Smrg		if ((fd = wsfb_open(dev)) >= 0) {
346357bfbb2Smrg			entity = xf86ClaimFbSlot(drv, 0, devSections[i], TRUE);
347357bfbb2Smrg			pScrn = xf86ConfigFbEntity(NULL,0,entity,
348357bfbb2Smrg						   NULL,NULL,NULL,NULL);
349357bfbb2Smrg			if (pScrn != NULL) {
350357bfbb2Smrg				foundScreen = TRUE;
351357bfbb2Smrg				pScrn->driverVersion = WSFB_VERSION;
3528a4fa5cdStsutsui				pScrn->driverName = (char *)WSFB_DRIVER_NAME;
3538a4fa5cdStsutsui				pScrn->name = (char *)WSFB_NAME;
354357bfbb2Smrg				pScrn->Probe = WsfbProbe;
355357bfbb2Smrg				pScrn->PreInit = WsfbPreInit;
356357bfbb2Smrg				pScrn->ScreenInit = WsfbScreenInit;
357357bfbb2Smrg				pScrn->SwitchMode = WsfbSwitchMode;
358357bfbb2Smrg				pScrn->AdjustFrame = NULL;
359357bfbb2Smrg				pScrn->EnterVT = WsfbEnterVT;
360357bfbb2Smrg				pScrn->LeaveVT = WsfbLeaveVT;
361357bfbb2Smrg				pScrn->ValidMode = WsfbValidMode;
362357bfbb2Smrg
363357bfbb2Smrg				xf86DrvMsg(pScrn->scrnIndex, X_INFO,
364357bfbb2Smrg				    "using %s\n", dev != NULL ? dev :
365357bfbb2Smrg				    "default device");
366357bfbb2Smrg			}
367357bfbb2Smrg		}
368357bfbb2Smrg	}
369cba5e21cSmrg	free(devSections);
370357bfbb2Smrg	TRACE("probe done");
371357bfbb2Smrg	return foundScreen;
372357bfbb2Smrg}
373357bfbb2Smrg
374357bfbb2Smrgstatic Bool
375357bfbb2SmrgWsfbPreInit(ScrnInfoPtr pScrn, int flags)
376357bfbb2Smrg{
377357bfbb2Smrg	WsfbPtr fPtr;
3787b613f18Stsutsui	int default_depth, bitsperpixel, wstype;
3798a4fa5cdStsutsui	const char *dev;
3808a4fa5cdStsutsui	const char *s;
381357bfbb2Smrg	Gamma zeros = {0.0, 0.0, 0.0};
382357bfbb2Smrg	DisplayModePtr mode;
3835d492fd2Smacallan	MessageType from;
384357bfbb2Smrg
385357bfbb2Smrg	if (flags & PROBE_DETECT) return FALSE;
386357bfbb2Smrg
387357bfbb2Smrg	TRACE_ENTER("PreInit");
388357bfbb2Smrg
389357bfbb2Smrg	if (pScrn->numEntities != 1) return FALSE;
390357bfbb2Smrg
391357bfbb2Smrg	pScrn->monitor = pScrn->confScreen->monitor;
392357bfbb2Smrg
393357bfbb2Smrg	WsfbGetRec(pScrn);
394357bfbb2Smrg	fPtr = WSFBPTR(pScrn);
395357bfbb2Smrg
396357bfbb2Smrg	fPtr->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
397357bfbb2Smrg
39864f08a18Smrg#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
399357bfbb2Smrg	pScrn->racMemFlags = RAC_FB | RAC_COLORMAP | RAC_CURSOR | RAC_VIEWPORT;
400357bfbb2Smrg	pScrn->racIoFlags = pScrn->racMemFlags;
40164f08a18Smrg#endif
402357bfbb2Smrg
403357bfbb2Smrg	dev = xf86FindOptionValue(fPtr->pEnt->device->options, "device");
404357bfbb2Smrg	fPtr->fd = wsfb_open(dev);
405357bfbb2Smrg	if (fPtr->fd == -1) {
406357bfbb2Smrg		return FALSE;
407357bfbb2Smrg	}
408357bfbb2Smrg
4093306a6a4Sjandberg	if (ioctl(fPtr->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
4103306a6a4Sjandberg		xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
4113306a6a4Sjandberg			   "ioctl WSDISPLAY_GTYPE: %s\n",
4123306a6a4Sjandberg			   strerror(errno));
4133306a6a4Sjandberg		wstype = WSDISPLAY_TYPE_UNKNOWN;
4143306a6a4Sjandberg	}
4153306a6a4Sjandberg
4163788fa92Smacallan	if (ioctl(fPtr->fd, WSDISPLAYIO_GET_FBINFO, &fPtr->fbi) != 0) {
4173788fa92Smacallan		struct wsdisplay_fbinfo info;
4183788fa92Smacallan		struct wsdisplayio_fbinfo *fbi = &fPtr->fbi;
4193788fa92Smacallan		int lb;
4203788fa92Smacallan
4213788fa92Smacallan		xf86Msg(X_WARNING, "ioctl(WSDISPLAYIO_GET_FBINFO) failed, " \
4223788fa92Smacallan			"falling back to old method\n");
4233788fa92Smacallan		if (ioctl(fPtr->fd, WSDISPLAYIO_GINFO, &info) == -1) {
4243788fa92Smacallan			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4253788fa92Smacallan				   "ioctl WSDISPLAY_GINFO: %s\n",
4263788fa92Smacallan				   strerror(errno));
4273788fa92Smacallan			return FALSE;
4283788fa92Smacallan		}
4293788fa92Smacallan		if (ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES, &lb) == -1) {
4303788fa92Smacallan			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4313788fa92Smacallan				   "ioctl WSDISPLAYIO_LINEBYTES: %s\n",
4323788fa92Smacallan				   strerror(errno));
4333788fa92Smacallan			return FALSE;
4343788fa92Smacallan		}
4353788fa92Smacallan		/* ok, fake up a new style fbinfo */
4363788fa92Smacallan		fbi->fbi_width = info.width;
4373788fa92Smacallan		fbi->fbi_height = info.height;
4383788fa92Smacallan		fbi->fbi_stride = lb;
4393788fa92Smacallan		fbi->fbi_bitsperpixel = info.depth;
4403788fa92Smacallan		if (info.depth > 16) {
4413788fa92Smacallan			fbi->fbi_pixeltype = WSFB_RGB;
4423788fa92Smacallan			if (wstype == WSDISPLAY_TYPE_SUN24 ||
4433788fa92Smacallan			    wstype == WSDISPLAY_TYPE_SUNCG12 ||
4443788fa92Smacallan			    wstype == WSDISPLAY_TYPE_SUNCG14 ||
4453788fa92Smacallan			    wstype == WSDISPLAY_TYPE_SUNTCX ||
4463788fa92Smacallan			    wstype == WSDISPLAY_TYPE_SUNFFB ||
4473788fa92Smacallan			    wstype == WSDISPLAY_TYPE_XVR1000 ||
4483788fa92Smacallan			    wstype == WSDISPLAY_TYPE_VC4) {
4493788fa92Smacallan				fbi->fbi_subtype.fbi_rgbmasks.red_offset = 0;
4503788fa92Smacallan				fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
4513788fa92Smacallan				fbi->fbi_subtype.fbi_rgbmasks.green_offset = 8;
4523788fa92Smacallan				fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
4533788fa92Smacallan				fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 16;
4543788fa92Smacallan				fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
4553788fa92Smacallan			} else {
4563788fa92Smacallan				fbi->fbi_subtype.fbi_rgbmasks.red_offset = 16;
4573788fa92Smacallan				fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
4583788fa92Smacallan				fbi->fbi_subtype.fbi_rgbmasks.green_offset = 8;
4593788fa92Smacallan				fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
4603788fa92Smacallan				fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 0;
4613788fa92Smacallan				fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
4623788fa92Smacallan			}
4633788fa92Smacallan			fbi->fbi_subtype.fbi_rgbmasks.alpha_offset = 0;
4643788fa92Smacallan			fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 0;
4653788fa92Smacallan		} else if (info.depth <= 8) {
4663788fa92Smacallan			fbi->fbi_pixeltype = WSFB_CI;
4673788fa92Smacallan			fbi->fbi_subtype.fbi_cmapinfo.cmap_entries = info.cmsize;
4683788fa92Smacallan		}
4693788fa92Smacallan		fbi->fbi_flags = 0;
4700d003323Stsutsui		fbi->fbi_fbsize = lb * info.height;
4718fbcba3dSrin#ifdef	WSDISPLAY_TYPE_LUNA
4728fbcba3dSrin		if (wstype == WSDISPLAY_TYPE_LUNA) {
4738fbcba3dSrin			/*
4748fbcba3dSrin			 * XXX
4758fbcba3dSrin			 * LUNA's FB seems to have 64 dot (8 byte) offset.
4768fbcba3dSrin			 * This might be able to be changed in kernel
4778fbcba3dSrin			 * lunafb driver, but current setting was pulled
4788fbcba3dSrin			 * from 4.4BSD-Lite2/luna68k.
4798fbcba3dSrin			 */
4808fbcba3dSrin			fbi->fbi_fboffset = 8;
4818fbcba3dSrin		} else
4828fbcba3dSrin#endif
4838fbcba3dSrin			fbi->fbi_fboffset = 0;
484357bfbb2Smrg	}
4858fbcba3dSrin	xf86Msg(X_INFO, "fboffset %x\n", (int)fPtr->fbi.fbi_fboffset);
486357bfbb2Smrg	/*
48764f08a18Smrg	 * Allocate room for saving the colormap.
488357bfbb2Smrg	 */
489b8199fcbStsutsui	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
490b8199fcbStsutsui	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
491357bfbb2Smrg		fPtr->saved_cmap.red =
492cba5e21cSmrg		    (unsigned char *)malloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
493357bfbb2Smrg		if (fPtr->saved_cmap.red == NULL) {
494357bfbb2Smrg			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4953788fa92Smacallan			    "Cannot malloc %d bytes\n",
4963788fa92Smacallan			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
497357bfbb2Smrg			return FALSE;
498357bfbb2Smrg		}
499357bfbb2Smrg		fPtr->saved_cmap.green =
500cba5e21cSmrg		    (unsigned char *)malloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
501357bfbb2Smrg		if (fPtr->saved_cmap.green == NULL) {
502357bfbb2Smrg			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
5033788fa92Smacallan			    "Cannot malloc %d bytes\n",
5043788fa92Smacallan			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
505cba5e21cSmrg			free(fPtr->saved_cmap.red);
506357bfbb2Smrg			return FALSE;
507357bfbb2Smrg		}
508357bfbb2Smrg		fPtr->saved_cmap.blue =
509cba5e21cSmrg		    (unsigned char *)malloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
510357bfbb2Smrg		if (fPtr->saved_cmap.blue == NULL) {
511357bfbb2Smrg			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
5123788fa92Smacallan			    "Cannot malloc %d bytes\n",
5133788fa92Smacallan			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
514cba5e21cSmrg			free(fPtr->saved_cmap.red);
515cba5e21cSmrg			free(fPtr->saved_cmap.green);
516357bfbb2Smrg			return FALSE;
517357bfbb2Smrg		}
518357bfbb2Smrg	}
519357bfbb2Smrg
520357bfbb2Smrg	/* Handle depth */
5213788fa92Smacallan	default_depth = fPtr->fbi.fbi_bitsperpixel <= 24 ? fPtr->fbi.fbi_bitsperpixel : 24;
522e686e132Srin	bitsperpixel = fPtr->fbi.fbi_bitsperpixel == 15 ? 16 : fPtr->fbi.fbi_bitsperpixel;
5237b613f18Stsutsui#if defined(__NetBSD__) && defined(WSDISPLAY_TYPE_LUNA)
5247b613f18Stsutsui	if (wstype == WSDISPLAY_TYPE_LUNA) {
5257b613f18Stsutsui		/*
5267b613f18Stsutsui		 * LUNA's color framebuffers support 4bpp or 8bpp
5277b613f18Stsutsui		 * but they have multiple 1bpp VRAM planes like ancient VGA.
5287b613f18Stsutsui		 */
529c2056409Stsutsui#ifdef HAVE_SHADOW_AFB
530c2056409Stsutsui		if (bitsperpixel == 8) {
531c2056409Stsutsui			/*
532c2056409Stsutsui			 * For 8bpp one, we can use the bitplane ops with
533c2056409Stsutsui			 * shadow update proc as amiga.
534c2056409Stsutsui			 */
535c2056409Stsutsui			fPtr->planarAfb = TRUE;
536c2056409Stsutsui		} else
537c2056409Stsutsui#endif
538c2056409Stsutsui		{
539c2056409Stsutsui			/*
540c2056409Stsutsui			 * For 4bpp one (or there is no planar support),
541c2056409Stsutsui			 * just use only the first one plane
542c2056409Stsutsui			 * as 1bpp monochrome server.
543c2056409Stsutsui			 *
544c2056409Stsutsui			 * Note OpenBSD/luna88k workarounds this by
545c2056409Stsutsui			 * switching depth and palette settings by
546c2056409Stsutsui			 * WSDISPLAYIO_SETGFXMODE ioctl.
547c2056409Stsutsui			 */
548c2056409Stsutsui			default_depth = 1;
549c2056409Stsutsui			bitsperpixel = 1;
550c2056409Stsutsui		}
5517b613f18Stsutsui	}
5527b613f18Stsutsui#endif
553b4fe077eSrin#ifdef WSDISPLAY_TYPE_AMIGACC
554bb2f4479Srin	if (wstype == WSDISPLAY_TYPE_AMIGACC) {
5553306a6a4Sjandberg		/*
5563306a6a4Sjandberg		 * Video memory is organized in bitplanes.
55701260a01Sjandberg		 * 8bpp, 4bpp, and 1bpp supported in this driver.
55801260a01Sjandberg		 * With 8bpp/4bpp conversion to bitplane format
55901260a01Sjandberg		 * is done in shadow update proc. In both cases
56001260a01Sjandberg		 * shadow fb uses 8bpp memory layout and shadow
56101260a01Sjandberg		 * update proc ignores the possible extra bits.
5623306a6a4Sjandberg		 * With 1bpp no conversion needed.
5633306a6a4Sjandberg		 */
564dc9c99a5Stsutsui#ifdef HAVE_SHADOW_AFB
5653306a6a4Sjandberg		if (bitsperpixel == 8) {
5663306a6a4Sjandberg			fPtr->planarAfb = TRUE;
56701260a01Sjandberg		} else if (bitsperpixel == 4) {
56801260a01Sjandberg			fPtr->planarAfb = TRUE;
56901260a01Sjandberg			default_depth = 4;
57001260a01Sjandberg			bitsperpixel = 8;
5710bb1e2adSrin		} else
572dc9c99a5Stsutsui#endif
5730bb1e2adSrin		{
5743306a6a4Sjandberg			default_depth = 1;
5753306a6a4Sjandberg			bitsperpixel = 1;
5763306a6a4Sjandberg		}
5773306a6a4Sjandberg	}
5783306a6a4Sjandberg#endif
5793306a6a4Sjandberg
580357bfbb2Smrg	if (!xf86SetDepthBpp(pScrn, default_depth, default_depth,
5817b613f18Stsutsui		bitsperpixel,
5827b613f18Stsutsui		bitsperpixel >= 24 ? Support24bppFb|Support32bppFb : 0))
583357bfbb2Smrg		return FALSE;
584357bfbb2Smrg
58564f08a18Smrg	/* Check consistency. */
5867b613f18Stsutsui	if (pScrn->bitsPerPixel != bitsperpixel) {
587357bfbb2Smrg		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
588357bfbb2Smrg		    "specified depth (%d) or bpp (%d) doesn't match "
589cba5e21cSmrg		    "framebuffer depth (%d)\n", pScrn->depth,
5907b613f18Stsutsui		    pScrn->bitsPerPixel, bitsperpixel);
591357bfbb2Smrg		return FALSE;
592357bfbb2Smrg	}
593357bfbb2Smrg	xf86PrintDepthBpp(pScrn);
594357bfbb2Smrg
59564f08a18Smrg	/* Get the depth24 pixmap format. */
596357bfbb2Smrg	if (pScrn->depth == 24 && pix24bpp == 0)
597357bfbb2Smrg		pix24bpp = xf86GetBppFromDepth(pScrn, 24);
598357bfbb2Smrg
5990c2341a9Stsutsui#ifdef	WSDISPLAY_TYPE_VAX_MONO
6000c2341a9Stsutsui	if (wstype == WSDISPLAY_TYPE_VAX_MONO) {
6010c2341a9Stsutsui		pScrn->bitmapBitOrder = LSBFirst;
6020c2341a9Stsutsui	}
6030c2341a9Stsutsui#endif
6040c2341a9Stsutsui
6050ef55ed1Smacallan	/* Handle options. */
6060ef55ed1Smacallan	xf86CollectOptions(pScrn, NULL);
6070ef55ed1Smacallan	fPtr->Options = (OptionInfoRec *)malloc(sizeof(WsfbOptions));
6080ef55ed1Smacallan	if (fPtr->Options == NULL)
6090ef55ed1Smacallan		return FALSE;
6100ef55ed1Smacallan	memcpy(fPtr->Options, WsfbOptions, sizeof(WsfbOptions));
6110ef55ed1Smacallan	xf86ProcessOptions(pScrn->scrnIndex, fPtr->pEnt->device->options,
6120ef55ed1Smacallan			   fPtr->Options);
6130ef55ed1Smacallan
6140ef55ed1Smacallan	/* Use shadow framebuffer by default, on depth >= 8 */
6150ef55ed1Smacallan	xf86Msg(X_INFO, "fbi_flags: %x\n", fPtr->fbi.fbi_flags);
6160ef55ed1Smacallan	if ((pScrn->depth >= 8) &&
6170ef55ed1Smacallan	   ((fPtr->fbi.fbi_flags & WSFB_VRAM_IS_RAM) == 0)) {
6180ef55ed1Smacallan		fPtr->shadowFB = xf86ReturnOptValBool(fPtr->Options,
6190ef55ed1Smacallan						      OPTION_SHADOW_FB, TRUE);
6200ef55ed1Smacallan	} else
6210ef55ed1Smacallan		if (xf86ReturnOptValBool(fPtr->Options,
6220ef55ed1Smacallan					 OPTION_SHADOW_FB, FALSE)) {
6230ef55ed1Smacallan			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
624b3c77fccStsutsui				   "Shadow FB option ignored on depth < 8\n");
6250ef55ed1Smacallan		}
626cbc9a668Smacallan	if (fPtr->fbi.fbi_flags & WSFB_VRAM_IS_SPLIT) {
627cbc9a668Smacallan		if (!fPtr->shadowFB) {
628cbc9a668Smacallan			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
629b3c77fccStsutsui				   "Shadow FB forced on for split framebuffer\n");
630cbc9a668Smacallan			fPtr->shadowFB = TRUE;
631cbc9a668Smacallan		}
632cbc9a668Smacallan	}
633bff82b7eSjmcneill
634bff82b7eSjmcneill	fPtr->useRGB16ToYUY2 = FALSE;
635bff82b7eSjmcneill#ifdef WSDISPLAY_TYPE_HOLLYWOOD
636bff82b7eSjmcneill	if (wstype == WSDISPLAY_TYPE_HOLLYWOOD) {
637bff82b7eSjmcneill		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
638bff82b7eSjmcneill			   "Enabling RGB16->YUY2 conversion for Hollywood\n");
639bff82b7eSjmcneill		fPtr->useRGB16ToYUY2 = TRUE;
640bff82b7eSjmcneill		if (!fPtr->shadowFB) {
641bff82b7eSjmcneill			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
642bff82b7eSjmcneill				   "Shadow FB forced on for RGB16->YUY2 conversion\n");
643bff82b7eSjmcneill			fPtr->shadowFB = TRUE;
644bff82b7eSjmcneill		}
645bff82b7eSjmcneill		/*
646bff82b7eSjmcneill		 * Hollywood has a YUY2 framebuffer, but we treat it as
647bff82b7eSjmcneill		 * RGB565 and convert with a custom shadowproc.
648bff82b7eSjmcneill		 */
649bff82b7eSjmcneill		fPtr->fbi.fbi_pixeltype = WSFB_RGB;
650bff82b7eSjmcneill	}
65101260a01Sjandberg#endif
65201260a01Sjandberg#ifdef HAVE_SHADOW_AFB
65301260a01Sjandberg	if (fPtr->planarAfb)
65401260a01Sjandberg	{
65501260a01Sjandberg		if (!fPtr->shadowFB) {
65601260a01Sjandberg			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
65701260a01Sjandberg				   "Shadow FB forced on for planar framebuffer\n");
65801260a01Sjandberg			fPtr->shadowFB = TRUE;
65901260a01Sjandberg		}
66001260a01Sjandberg	}
661bff82b7eSjmcneill#endif
6620ef55ed1Smacallan	/* Rotation */
6630ef55ed1Smacallan	fPtr->rotate = WSFB_ROTATE_NONE;
6640ef55ed1Smacallan	if ((s = xf86GetOptValString(fPtr->Options, OPTION_ROTATE))) {
6650ef55ed1Smacallan		if (pScrn->depth >= 8) {
6660ef55ed1Smacallan			if (!xf86NameCmp(s, "CW")) {
6670ef55ed1Smacallan				fPtr->shadowFB = TRUE;
6680ef55ed1Smacallan				fPtr->rotate = WSFB_ROTATE_CW;
6690ef55ed1Smacallan				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
6700ef55ed1Smacallan				    "Rotating screen clockwise\n");
6710ef55ed1Smacallan			} else if (!xf86NameCmp(s, "CCW")) {
6720ef55ed1Smacallan				fPtr->shadowFB = TRUE;
6730ef55ed1Smacallan				fPtr->rotate = WSFB_ROTATE_CCW;
6740ef55ed1Smacallan				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
6750ef55ed1Smacallan				    "Rotating screen counter clockwise\n");
6760ef55ed1Smacallan			} else if (!xf86NameCmp(s, "UD")) {
6770ef55ed1Smacallan				fPtr->shadowFB = TRUE;
6780ef55ed1Smacallan				fPtr->rotate = WSFB_ROTATE_UD;
6790ef55ed1Smacallan				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
6800ef55ed1Smacallan				    "Rotating screen upside down\n");
6810ef55ed1Smacallan			} else {
6820ef55ed1Smacallan				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
6830ef55ed1Smacallan				    "\"%s\" is not a valid value for Option "
6840ef55ed1Smacallan				    "\"Rotate\"\n", s);
6850ef55ed1Smacallan				xf86DrvMsg(pScrn->scrnIndex, X_INFO,
6860ef55ed1Smacallan				    "Valid options are \"CW\", \"CCW\","
6870ef55ed1Smacallan				    " or \"UD\"\n");
6880ef55ed1Smacallan			}
6890ef55ed1Smacallan		} else {
6900ef55ed1Smacallan			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
691b3c77fccStsutsui			    "Option \"Rotate\" ignored on depth < 8\n");
6920ef55ed1Smacallan		}
6930ef55ed1Smacallan	}
6940ef55ed1Smacallan
6950ef55ed1Smacallan
6960ef55ed1Smacallan	fPtr->useSwap32 = FALSE;
69764f08a18Smrg	/* Color weight */
6983788fa92Smacallan	if (fPtr->fbi.fbi_pixeltype == WSFB_RGB) {
699357bfbb2Smrg		rgb zeros = { 0, 0, 0 }, masks;
700357bfbb2Smrg
7013788fa92Smacallan		if (fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_size > 0) {
7023788fa92Smacallan			uint32_t msk;
7033788fa92Smacallan
7040ef55ed1Smacallan			/*
7050ef55ed1Smacallan			 * see if we need to byte-swap pixels
7060ef55ed1Smacallan			 * XXX this requires a shadow FB and is incompatible
7070ef55ed1Smacallan			 * (for now ) with rotation
7080ef55ed1Smacallan			 */
7090ef55ed1Smacallan			if ((fPtr->fbi.fbi_bitsperpixel == 32) &&
7100ef55ed1Smacallan			    (fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset == 24) &&
7110ef55ed1Smacallan			    (fPtr->rotate == WSFB_ROTATE_NONE) &&
7120ef55ed1Smacallan			    (fPtr->shadowFB == TRUE)) {
7130ef55ed1Smacallan			    	/*
7140ef55ed1Smacallan			    	 * looks like BGRA - set the swap flag and flip
7150ef55ed1Smacallan			    	 * the offsets
7160ef55ed1Smacallan			    	 */
7170ef55ed1Smacallan			    	xf86Msg(X_INFO, "endian-flipped RGB framebuffer "
7180ef55ed1Smacallan			    			"detected, using WsfbShadowUpdateSwap32()\n");
7190ef55ed1Smacallan			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset = 0;
7200ef55ed1Smacallan			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_offset = 8;
7210ef55ed1Smacallan			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_offset = 16;
7220ef55ed1Smacallan			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.alpha_offset = 24;
7230ef55ed1Smacallan				fPtr->useSwap32 = TRUE;
7240ef55ed1Smacallan			}
7250ef55ed1Smacallan
7263788fa92Smacallan			msk = 0xffffffff;
7273788fa92Smacallan			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_size;
7283788fa92Smacallan			msk = ~msk;
7293788fa92Smacallan			masks.red = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_offset;
7303788fa92Smacallan
7313788fa92Smacallan			msk = 0xffffffff;
7323788fa92Smacallan			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_size;
7333788fa92Smacallan			msk = ~msk;
7343788fa92Smacallan			masks.green = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_offset;
7353788fa92Smacallan
7363788fa92Smacallan			msk = 0xffffffff;
7373788fa92Smacallan			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_size;
7383788fa92Smacallan			msk = ~msk;
7393788fa92Smacallan			masks.blue = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset;
74053d96324Sapb			xf86Msg(X_INFO, "masks generated: %08lx %08lx %08lx\n",
74153d96324Sapb			    (unsigned long)masks.red,
74253d96324Sapb			    (unsigned long)masks.green,
74353d96324Sapb			    (unsigned long)masks.blue);
744357bfbb2Smrg		} else {
745357bfbb2Smrg			masks.red = 0;
746357bfbb2Smrg			masks.green = 0;
747357bfbb2Smrg			masks.blue = 0;
748357bfbb2Smrg		}
749357bfbb2Smrg
750357bfbb2Smrg		if (!xf86SetWeight(pScrn, zeros, masks))
751357bfbb2Smrg			return FALSE;
752357bfbb2Smrg	}
753357bfbb2Smrg
75464f08a18Smrg	/* Visual init */
755357bfbb2Smrg	if (!xf86SetDefaultVisual(pScrn, -1))
756357bfbb2Smrg		return FALSE;
757357bfbb2Smrg
75864f08a18Smrg	/* We don't currently support DirectColor at > 8bpp . */
759357bfbb2Smrg	if (pScrn->depth > 8 && pScrn->defaultVisual != TrueColor) {
760357bfbb2Smrg		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Given default visual"
761357bfbb2Smrg			   " (%s) is not supported at depth %d\n",
762357bfbb2Smrg			   xf86GetVisualName(pScrn->defaultVisual),
763357bfbb2Smrg			   pScrn->depth);
764357bfbb2Smrg		return FALSE;
765357bfbb2Smrg	}
766357bfbb2Smrg
767357bfbb2Smrg	xf86SetGamma(pScrn,zeros);
768357bfbb2Smrg
769357bfbb2Smrg	pScrn->progClock = TRUE;
7708fc0431cStsutsui	pScrn->rgbBits   = (pScrn->depth >= 8) ? 8 : pScrn->depth;
7718a4fa5cdStsutsui	pScrn->chipset   = (char *)"wsfb";
7723788fa92Smacallan	pScrn->videoRam  = fPtr->fbi.fbi_fbsize;
773357bfbb2Smrg
774357bfbb2Smrg	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Vidmem: %dk\n",
775357bfbb2Smrg		   pScrn->videoRam/1024);
776357bfbb2Smrg
77764f08a18Smrg	/* Fake video mode struct. */
778cba5e21cSmrg	mode = (DisplayModePtr)malloc(sizeof(DisplayModeRec));
779357bfbb2Smrg	mode->prev = mode;
780357bfbb2Smrg	mode->next = mode;
7818a4fa5cdStsutsui	mode->name = (char *)"wsfb current mode";
782357bfbb2Smrg	mode->status = MODE_OK;
783357bfbb2Smrg	mode->type = M_T_BUILTIN;
784357bfbb2Smrg	mode->Clock = 0;
7853788fa92Smacallan	mode->HDisplay = fPtr->fbi.fbi_width;
786357bfbb2Smrg	mode->HSyncStart = 0;
787357bfbb2Smrg	mode->HSyncEnd = 0;
788357bfbb2Smrg	mode->HTotal = 0;
789357bfbb2Smrg	mode->HSkew = 0;
7903788fa92Smacallan	mode->VDisplay = fPtr->fbi.fbi_height;
791357bfbb2Smrg	mode->VSyncStart = 0;
792357bfbb2Smrg	mode->VSyncEnd = 0;
793357bfbb2Smrg	mode->VTotal = 0;
794357bfbb2Smrg	mode->VScan = 0;
795357bfbb2Smrg	mode->Flags = 0;
796357bfbb2Smrg	if (pScrn->modes != NULL) {
797357bfbb2Smrg		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
798357bfbb2Smrg		   "Ignoring mode specification from screen section\n");
799357bfbb2Smrg	}
800357bfbb2Smrg	pScrn->currentMode = pScrn->modes = mode;
8013788fa92Smacallan	pScrn->virtualX = fPtr->fbi.fbi_width;
8023788fa92Smacallan	pScrn->virtualY = fPtr->fbi.fbi_height;
803357bfbb2Smrg	pScrn->displayWidth = pScrn->virtualX;
804357bfbb2Smrg
80564f08a18Smrg	/* Set the display resolution. */
806357bfbb2Smrg	xf86SetDpi(pScrn, 0, 0);
807357bfbb2Smrg
8085d492fd2Smacallan	from = X_DEFAULT;
8095d492fd2Smacallan	fPtr->HWCursor = TRUE;
8105d492fd2Smacallan	if (xf86GetOptValBool(fPtr->Options, OPTION_HW_CURSOR, &fPtr->HWCursor))
8115d492fd2Smacallan		from = X_CONFIG;
8125d492fd2Smacallan	if (xf86ReturnOptValBool(fPtr->Options, OPTION_SW_CURSOR, FALSE)) {
8135d492fd2Smacallan		from = X_CONFIG;
8145d492fd2Smacallan		fPtr->HWCursor = FALSE;
8155d492fd2Smacallan	}
8165d492fd2Smacallan	xf86DrvMsg(pScrn->scrnIndex, from, "Using %s cursor\n",
8175d492fd2Smacallan		fPtr->HWCursor ? "HW" : "SW");
8185d492fd2Smacallan
819357bfbb2Smrg
82064f08a18Smrg	/* Load shadow if needed. */
821357bfbb2Smrg	if (fPtr->shadowFB) {
822357bfbb2Smrg		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
823357bfbb2Smrg			   "Using \"Shadow Framebuffer\"\n");
824357bfbb2Smrg		if (xf86LoadSubModule(pScrn, "shadow") == NULL) {
825357bfbb2Smrg			WsfbFreeRec(pScrn);
826357bfbb2Smrg			return FALSE;
827357bfbb2Smrg		}
828357bfbb2Smrg	}
8298a4fa5cdStsutsui	if (xf86LoadSubModule(pScrn, "fb") == NULL) {
830357bfbb2Smrg		WsfbFreeRec(pScrn);
831357bfbb2Smrg		return FALSE;
832357bfbb2Smrg	}
8335d492fd2Smacallan
8345d492fd2Smacallan	if (xf86LoadSubModule(pScrn, "ramdac") == NULL) {
8355d492fd2Smacallan		WsfbFreeRec(pScrn);
8365d492fd2Smacallan		return FALSE;
8375d492fd2Smacallan        }
8385d492fd2Smacallan
839357bfbb2Smrg	TRACE_EXIT("PreInit");
840357bfbb2Smrg	return TRUE;
841357bfbb2Smrg}
842357bfbb2Smrg
8438a4fa5cdStsutsuistatic void
8448a4fa5cdStsutsuiwsfbUpdateRotatePacked(ScreenPtr pScreen, shadowBufPtr pBuf)
8458a4fa5cdStsutsui{
8468a4fa5cdStsutsui    shadowUpdateRotatePacked(pScreen, pBuf);
8478a4fa5cdStsutsui}
8488a4fa5cdStsutsui
8498a4fa5cdStsutsuistatic void
8508a4fa5cdStsutsuiwsfbUpdatePacked(ScreenPtr pScreen, shadowBufPtr pBuf)
8518a4fa5cdStsutsui{
8528a4fa5cdStsutsui    shadowUpdatePacked(pScreen, pBuf);
8538a4fa5cdStsutsui}
8548a4fa5cdStsutsui
855357bfbb2Smrgstatic Bool
856357bfbb2SmrgWsfbCreateScreenResources(ScreenPtr pScreen)
857357bfbb2Smrg{
8588a4fa5cdStsutsui	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
859357bfbb2Smrg	WsfbPtr fPtr = WSFBPTR(pScrn);
860357bfbb2Smrg	PixmapPtr pPixmap;
861357bfbb2Smrg	Bool ret;
862cbc9a668Smacallan	void (*shadowproc)(ScreenPtr, shadowBufPtr);
8633306a6a4Sjandberg	ShadowWindowProc windowproc = WsfbWindowLinear;
864357bfbb2Smrg
865357bfbb2Smrg	pScreen->CreateScreenResources = fPtr->CreateScreenResources;
866357bfbb2Smrg	ret = pScreen->CreateScreenResources(pScreen);
867357bfbb2Smrg	pScreen->CreateScreenResources = WsfbCreateScreenResources;
868357bfbb2Smrg
869357bfbb2Smrg	if (!ret)
870357bfbb2Smrg		return FALSE;
871357bfbb2Smrg
872357bfbb2Smrg	pPixmap = pScreen->GetScreenPixmap(pScreen);
873cbc9a668Smacallan	if (fPtr->fbi.fbi_flags & WSFB_VRAM_IS_SPLIT) {
874cbc9a668Smacallan		shadowproc = WsfbShadowUpdateSplit;
875bff82b7eSjmcneill	} else if (fPtr->useRGB16ToYUY2) {
876bff82b7eSjmcneill		/* Build RGB16 to Y, U, and V lookup tables */
877bff82b7eSjmcneill		if (mapRGB16ToY == NULL) {
878bff82b7eSjmcneill			mapRGB16ToY = malloc(0x30000);
879bff82b7eSjmcneill			if (mapRGB16ToY == NULL) {
880bff82b7eSjmcneill				xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
881bff82b7eSjmcneill				    "Cannot malloc %d bytes for RGB16->YUY2\n",
882bff82b7eSjmcneill				    0x30000);
883bff82b7eSjmcneill				return FALSE;
884bff82b7eSjmcneill			}
885bff82b7eSjmcneill			mapRGB16ToU = mapRGB16ToY + 0x10000;
886bff82b7eSjmcneill			mapRGB16ToV = mapRGB16ToY + 0x20000;
887bff82b7eSjmcneill        		for (unsigned int n = 0; n < 0x10000; n++) {
888bff82b7eSjmcneill                		/* RGB565 values, scaled to 8 bits */
889bff82b7eSjmcneill                		const double R = (((n >> 11) & 0x1f) * 255) / 31;
890bff82b7eSjmcneill                		const double G = (((n >> 5) & 0x3f) * 255) / 63;
891bff82b7eSjmcneill                		const double B = (((n >> 0) & 0x1f) * 255) / 31;
892bff82b7eSjmcneill
893bff82b7eSjmcneill				/* Convert to YUV */
894bff82b7eSjmcneill	                	mapRGB16ToY[n] =
895bff82b7eSjmcneill				    0.257 * R + 0.504 * G + 0.098 * B +  16;
896bff82b7eSjmcneill	                	mapRGB16ToU[n] =
897bff82b7eSjmcneill				   -0.148 * R - 0.291 * G + 0.439 * B + 128;
898bff82b7eSjmcneill	                	mapRGB16ToV[n] =
899bff82b7eSjmcneill				    0.439 * R - 0.368 * G - 0.071 * B + 128;
900bff82b7eSjmcneill			}
901bff82b7eSjmcneill		}
902bff82b7eSjmcneill
903bff82b7eSjmcneill		shadowproc = WsfbShadowUpdateRGB16ToYUY2;
904cbc9a668Smacallan	} else if (fPtr->useSwap32) {
905cbc9a668Smacallan		shadowproc = WsfbShadowUpdateSwap32;
906cbc9a668Smacallan	} else if (fPtr->rotate) {
9078a4fa5cdStsutsui		shadowproc = wsfbUpdateRotatePacked;
908dc9c99a5Stsutsui	} else
909dc9c99a5Stsutsui#ifdef HAVE_SHADOW_AFB
91001260a01Sjandberg	if (fPtr->planarAfb && fPtr->fbi.fbi_bitsperpixel == 8) {
9113306a6a4Sjandberg		shadowproc = shadowUpdateAfb8;
9123306a6a4Sjandberg		windowproc = WsfbWindowAfb;
91301260a01Sjandberg	} else if (fPtr->planarAfb && fPtr->fbi.fbi_bitsperpixel == 4) {
91401260a01Sjandberg		shadowproc = shadowUpdateAfb4x8;
91501260a01Sjandberg		windowproc = WsfbWindowAfb;
916cbc9a668Smacallan	} else
917dc9c99a5Stsutsui#endif
918dc9c99a5Stsutsui	{
9198a4fa5cdStsutsui		shadowproc = wsfbUpdatePacked;
920dc9c99a5Stsutsui	}
921cbc9a668Smacallan
922cbc9a668Smacallan	if (!shadowAdd(pScreen, pPixmap, shadowproc,
9233306a6a4Sjandberg		windowproc, fPtr->rotate, NULL)) {
924357bfbb2Smrg		return FALSE;
925357bfbb2Smrg	}
926357bfbb2Smrg	return TRUE;
927357bfbb2Smrg}
928357bfbb2Smrg
929357bfbb2Smrg
930357bfbb2Smrgstatic Bool
931357bfbb2SmrgWsfbShadowInit(ScreenPtr pScreen)
932357bfbb2Smrg{
9338a4fa5cdStsutsui	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
934357bfbb2Smrg	WsfbPtr fPtr = WSFBPTR(pScrn);
935357bfbb2Smrg
936357bfbb2Smrg	if (!shadowSetup(pScreen))
937357bfbb2Smrg		return FALSE;
938357bfbb2Smrg	fPtr->CreateScreenResources = pScreen->CreateScreenResources;
939357bfbb2Smrg	pScreen->CreateScreenResources = WsfbCreateScreenResources;
940357bfbb2Smrg
941357bfbb2Smrg	return TRUE;
942357bfbb2Smrg}
943357bfbb2Smrg
944357bfbb2Smrgstatic Bool
9459974adabSmrgWsfbScreenInit(SCREEN_INIT_ARGS_DECL)
946357bfbb2Smrg{
9478a4fa5cdStsutsui	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
948357bfbb2Smrg	WsfbPtr fPtr = WSFBPTR(pScrn);
949357bfbb2Smrg	VisualPtr visual;
950357bfbb2Smrg	int ret, flags, ncolors;
951357bfbb2Smrg	int wsmode = WSDISPLAYIO_MODE_DUMBFB;
9528fc0431cStsutsui	int wstype;
953fe5fcf53Stsutsui	int width;
954357bfbb2Smrg	size_t len;
955357bfbb2Smrg
956357bfbb2Smrg	TRACE_ENTER("WsfbScreenInit");
957357bfbb2Smrg#if DEBUG
958357bfbb2Smrg	ErrorF("\tbitsPerPixel=%d, depth=%d, defaultVisual=%s\n"
959357bfbb2Smrg	       "\tmask: %x,%x,%x, offset: %u,%u,%u\n",
960357bfbb2Smrg	       pScrn->bitsPerPixel,
961357bfbb2Smrg	       pScrn->depth,
962357bfbb2Smrg	       xf86GetVisualName(pScrn->defaultVisual),
963357bfbb2Smrg	       pScrn->mask.red,pScrn->mask.green,pScrn->mask.blue,
964357bfbb2Smrg	       pScrn->offset.red,pScrn->offset.green,pScrn->offset.blue);
965357bfbb2Smrg#endif
9663788fa92Smacallan	switch (fPtr->fbi.fbi_bitsperpixel) {
967357bfbb2Smrg	case 1:
968357bfbb2Smrg	case 4:
969357bfbb2Smrg	case 8:
9703788fa92Smacallan		len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
9713e8a0de0Stsutsui#ifdef HAVE_SHADOW_AFB
9723e8a0de0Stsutsui		if (fPtr->planarAfb) {
9733e8a0de0Stsutsui			/*
9743e8a0de0Stsutsui			 * stride is "bytes per line" for each plane so
9753e8a0de0Stsutsui			 * we need a number of planes to mmap in planar case.
9763e8a0de0Stsutsui			 */
9773e8a0de0Stsutsui			len *= fPtr->fbi.fbi_bitsperpixel;
9783e8a0de0Stsutsui		}
9793e8a0de0Stsutsui#endif
9803e8a0de0Stsutsui
981357bfbb2Smrg		break;
982e686e132Srin	case 15:
983357bfbb2Smrg	case 16:
9843788fa92Smacallan		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
9853788fa92Smacallan			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * sizeof(short);
986357bfbb2Smrg		} else {
9873788fa92Smacallan			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
988357bfbb2Smrg		}
989357bfbb2Smrg		break;
990357bfbb2Smrg	case 24:
9913788fa92Smacallan		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
9923788fa92Smacallan			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * 3;
993357bfbb2Smrg		} else {
9943788fa92Smacallan			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
995357bfbb2Smrg		}
996357bfbb2Smrg		break;
997357bfbb2Smrg	case 32:
9983788fa92Smacallan		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
9993788fa92Smacallan			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * sizeof(int);
1000357bfbb2Smrg		} else {
10013788fa92Smacallan			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
1002357bfbb2Smrg		}
1003357bfbb2Smrg		break;
1004357bfbb2Smrg	default:
1005357bfbb2Smrg		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
10063788fa92Smacallan			   "unsupported depth %d\n", fPtr->fbi.fbi_bitsperpixel);
1007357bfbb2Smrg		return FALSE;
1008357bfbb2Smrg	}
100964f08a18Smrg	/* Switch to graphics mode - required before mmap. */
1010357bfbb2Smrg	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &wsmode) == -1) {
1011357bfbb2Smrg		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1012357bfbb2Smrg			   "ioctl WSDISPLAYIO_SMODE: %s\n",
1013357bfbb2Smrg			   strerror(errno));
1014357bfbb2Smrg		return FALSE;
1015357bfbb2Smrg	}
10168fc0431cStsutsui	/* Get wsdisplay type to handle quirks */
10178fc0431cStsutsui	if (ioctl(fPtr->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
10188fc0431cStsutsui		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
10198fc0431cStsutsui			   "ioctl WSDISPLAY_GTYPE: %s\n",
10208fc0431cStsutsui			   strerror(errno));
10218fc0431cStsutsui		return FALSE;
10228fc0431cStsutsui	}
1023cbc9a668Smacallan	len = max(len, fPtr->fbi.fbi_fbsize);
10248fbcba3dSrin	fPtr->fbmem = wsfb_mmap(len + fPtr->fbi.fbi_fboffset, 0, fPtr->fd);
1025357bfbb2Smrg
1026357bfbb2Smrg	if (fPtr->fbmem == NULL) {
1027357bfbb2Smrg		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1028357bfbb2Smrg			   "wsfb_mmap: %s\n", strerror(errno));
1029357bfbb2Smrg		return FALSE;
1030357bfbb2Smrg	}
1031357bfbb2Smrg	fPtr->fbmem_len = len;
1032357bfbb2Smrg
1033357bfbb2Smrg	WsfbSave(pScrn);
1034357bfbb2Smrg	pScrn->vtSema = TRUE;
1035357bfbb2Smrg
103664f08a18Smrg	/* MI layer */
1037357bfbb2Smrg	miClearVisualTypes();
1038357bfbb2Smrg	if (pScrn->bitsPerPixel > 8) {
1039357bfbb2Smrg		if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
1040357bfbb2Smrg				      pScrn->rgbBits, TrueColor))
1041357bfbb2Smrg			return FALSE;
1042357bfbb2Smrg	} else {
1043357bfbb2Smrg		if (!miSetVisualTypes(pScrn->depth,
1044357bfbb2Smrg				      miGetDefaultVisualMask(pScrn->depth),
1045357bfbb2Smrg				      pScrn->rgbBits, pScrn->defaultVisual))
1046357bfbb2Smrg			return FALSE;
1047357bfbb2Smrg	}
1048357bfbb2Smrg	if (!miSetPixmapDepths())
1049357bfbb2Smrg		return FALSE;
1050357bfbb2Smrg
1051cba5e21cSmrg	if (fPtr->rotate == WSFB_ROTATE_CW
1052357bfbb2Smrg	    || fPtr->rotate == WSFB_ROTATE_CCW) {
1053357bfbb2Smrg		int tmp = pScrn->virtualX;
1054357bfbb2Smrg		pScrn->virtualX = pScrn->displayWidth = pScrn->virtualY;
1055357bfbb2Smrg		pScrn->virtualY = tmp;
1056357bfbb2Smrg	}
1057357bfbb2Smrg	if (fPtr->rotate && !fPtr->PointerMoved) {
1058357bfbb2Smrg		fPtr->PointerMoved = pScrn->PointerMoved;
1059357bfbb2Smrg		pScrn->PointerMoved = WsfbPointerMoved;
1060357bfbb2Smrg	}
1061357bfbb2Smrg
106214f0c695Smacallan	fPtr->fbstart = fPtr->fbmem + fPtr->fbi.fbi_fboffset;
1063357bfbb2Smrg
106464f08a18Smrg	if (fPtr->shadowFB) {
1065fe5fcf53Stsutsui		if (fPtr->rotate) {
1066fe5fcf53Stsutsui			/*
1067fe5fcf53Stsutsui			 * Note Rotate and Shadow FB options are valid
1068fe5fcf53Stsutsui			 * only on depth >= 8.
1069fe5fcf53Stsutsui			 */
1070fe5fcf53Stsutsui			len = pScrn->virtualX * pScrn->virtualY *
1071fe5fcf53Stsutsui			    (pScrn->bitsPerPixel >> 3);
1072dc9c99a5Stsutsui		} else
1073dc9c99a5Stsutsui#ifdef HAVE_SHADOW_AFB
1074dc9c99a5Stsutsui		if (fPtr->planarAfb) {
10753306a6a4Sjandberg			/* always 8bpp */
10763306a6a4Sjandberg			len = pScrn->virtualX * pScrn->virtualY;
1077dc9c99a5Stsutsui		} else
1078dc9c99a5Stsutsui#endif
1079dc9c99a5Stsutsui		{
1080fe5fcf53Stsutsui			len = fPtr->fbi.fbi_stride * pScrn->virtualY;
1081fe5fcf53Stsutsui		}
1082fe5fcf53Stsutsui		fPtr->shadow = calloc(1, len);
1083cba5e21cSmrg
108464f08a18Smrg		if (!fPtr->shadow) {
108564f08a18Smrg			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
108664f08a18Smrg			    "Failed to allocate shadow framebuffer\n");
108764f08a18Smrg			return FALSE;
108864f08a18Smrg		}
108964f08a18Smrg	}
109064f08a18Smrg
1091fe5fcf53Stsutsui	/*
1092fe5fcf53Stsutsui	 * fbScreenInit() seems to require "pixel width of frame buffer"
1093fe5fcf53Stsutsui	 * but it is actually "stride in pixel" of frame buffer,
1094fe5fcf53Stsutsui	 * per xorg/xserver/tree/fb/fbscreen.c.
1095fe5fcf53Stsutsui	 */
1096fe5fcf53Stsutsui	if (fPtr->rotate) {
1097fe5fcf53Stsutsui		width = pScrn->displayWidth;
1098dc9c99a5Stsutsui	} else
1099dc9c99a5Stsutsui#ifdef HAVE_SHADOW_AFB
1100dc9c99a5Stsutsui	if (fPtr->planarAfb) {
11013306a6a4Sjandberg		width = pScrn->displayWidth;
1102dc9c99a5Stsutsui	} else
1103dc9c99a5Stsutsui#endif
1104dc9c99a5Stsutsui	{
1105fe5fcf53Stsutsui		if (pScrn->bitsPerPixel > 8) {
1106fe5fcf53Stsutsui			width =
1107fe5fcf53Stsutsui			    fPtr->fbi.fbi_stride / (pScrn->bitsPerPixel >> 3);
1108fe5fcf53Stsutsui		} else {
1109fe5fcf53Stsutsui			width =
1110fe5fcf53Stsutsui			    fPtr->fbi.fbi_stride * (8 / pScrn->bitsPerPixel);
1111fe5fcf53Stsutsui		}
1112fe5fcf53Stsutsui	}
1113357bfbb2Smrg	switch (pScrn->bitsPerPixel) {
111464f08a18Smrg	case 1:
11158fc0431cStsutsui		ret = fbScreenInit(pScreen,
11168fc0431cStsutsui		    fPtr->fbstart,
11178fc0431cStsutsui		    pScrn->virtualX, pScrn->virtualY,
11188fc0431cStsutsui		    pScrn->xDpi, pScrn->yDpi,
1119fe5fcf53Stsutsui		    width, pScrn->bitsPerPixel);
11208fc0431cStsutsui		break;
112164f08a18Smrg	case 4:
1122357bfbb2Smrg	case 8:
1123357bfbb2Smrg	case 16:
1124357bfbb2Smrg	case 24:
1125357bfbb2Smrg	case 32:
1126357bfbb2Smrg		ret = fbScreenInit(pScreen,
1127b603b469Snonaka		    fPtr->shadowFB ? fPtr->shadow : fPtr->fbstart,
1128357bfbb2Smrg		    pScrn->virtualX, pScrn->virtualY,
1129357bfbb2Smrg		    pScrn->xDpi, pScrn->yDpi,
1130fe5fcf53Stsutsui		    width,
1131cbc9a668Smacallan		    pScrn->bitsPerPixel);
1132357bfbb2Smrg		break;
1133357bfbb2Smrg	default:
1134357bfbb2Smrg		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1135b3c77fccStsutsui			   "Unsupported bpp: %d\n", pScrn->bitsPerPixel);
1136357bfbb2Smrg		return FALSE;
1137357bfbb2Smrg	} /* case */
1138357bfbb2Smrg
1139357bfbb2Smrg	if (!ret)
1140357bfbb2Smrg		return FALSE;
1141357bfbb2Smrg
1142357bfbb2Smrg	if (pScrn->bitsPerPixel > 8) {
114364f08a18Smrg		/* Fixup RGB ordering. */
1144357bfbb2Smrg		visual = pScreen->visuals + pScreen->numVisuals;
1145357bfbb2Smrg		while (--visual >= pScreen->visuals) {
1146357bfbb2Smrg			if ((visual->class | DynamicClass) == DirectColor) {
1147357bfbb2Smrg				visual->offsetRed   = pScrn->offset.red;
1148357bfbb2Smrg				visual->offsetGreen = pScrn->offset.green;
1149357bfbb2Smrg				visual->offsetBlue  = pScrn->offset.blue;
1150357bfbb2Smrg				visual->redMask     = pScrn->mask.red;
1151357bfbb2Smrg				visual->greenMask   = pScrn->mask.green;
1152357bfbb2Smrg				visual->blueMask    = pScrn->mask.blue;
1153357bfbb2Smrg			}
1154357bfbb2Smrg		}
1155357bfbb2Smrg	}
1156357bfbb2Smrg
1157357bfbb2Smrg	if (pScrn->bitsPerPixel >= 8) {
1158357bfbb2Smrg		if (!fbPictureInit(pScreen, NULL, 0))
1159357bfbb2Smrg			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
1160b3c77fccStsutsui				   "RENDER extension initialisation failed.\n");
1161357bfbb2Smrg	}
1162357bfbb2Smrg	if (fPtr->shadowFB && !WsfbShadowInit(pScreen)) {
11639974adabSmrg		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1164357bfbb2Smrg		    "shadow framebuffer initialization failed\n");
1165357bfbb2Smrg		return FALSE;
1166357bfbb2Smrg	}
1167357bfbb2Smrg
1168357bfbb2Smrg#ifdef XFreeXDGA
1169cba5e21cSmrg	if (!fPtr->rotate)
1170357bfbb2Smrg		WsfbDGAInit(pScrn, pScreen);
1171cba5e21cSmrg	else
11729974adabSmrg		xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Rotated display, "
1173357bfbb2Smrg		    "disabling DGA\n");
1174357bfbb2Smrg#endif
1175357bfbb2Smrg	if (fPtr->rotate) {
11768a4fa5cdStsutsui		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
11778a4fa5cdStsutsui		    "Enabling Driver Rotation, " "disabling RandR\n");
11785a609870Stsutsui#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 24
1179357bfbb2Smrg		xf86DisableRandR();
118072ee27a2Smrg#endif
1181cba5e21cSmrg		if (pScrn->bitsPerPixel == 24)
11829974adabSmrg			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
1183357bfbb2Smrg			    "Rotation might be broken in 24 bpp\n");
1184357bfbb2Smrg	}
1185357bfbb2Smrg
1186357bfbb2Smrg	xf86SetBlackWhitePixels(pScreen);
1187357bfbb2Smrg	xf86SetBackingStore(pScreen);
1188357bfbb2Smrg
118964f08a18Smrg	/* Software cursor. */
1190357bfbb2Smrg	miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
1191357bfbb2Smrg
11925d492fd2Smacallan	/* check for hardware cursor support */
11935d492fd2Smacallan	if (fPtr->HWCursor)
11945d492fd2Smacallan		WsfbSetupCursor(pScreen);
11955d492fd2Smacallan
119664f08a18Smrg	/*
119764f08a18Smrg	 * Colormap
119864f08a18Smrg	 *
119964f08a18Smrg	 * Note that, even on less than 8 bit depth frame buffers, we
120064f08a18Smrg	 * expect the colormap to be programmable with 8 bit values.
120164f08a18Smrg	 * As of now, this is indeed the case on all OpenBSD supported
120264f08a18Smrg	 * graphics hardware.
120364f08a18Smrg	 */
1204357bfbb2Smrg	if (!miCreateDefColormap(pScreen))
1205357bfbb2Smrg		return FALSE;
1206357bfbb2Smrg	flags = CMAP_RELOAD_ON_MODE_SWITCH;
12073788fa92Smacallan
12083788fa92Smacallan	ncolors = 0;
12093788fa92Smacallan	if (fPtr->fbi.fbi_pixeltype == WSFB_CI) {
12103788fa92Smacallan		ncolors = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
12113788fa92Smacallan	}
12123788fa92Smacallan
121364f08a18Smrg	/* On StaticGray visuals, fake a 256 entries colormap. */
1214357bfbb2Smrg	if (ncolors == 0)
1215357bfbb2Smrg		ncolors = 256;
1216357bfbb2Smrg	if(!xf86HandleColormaps(pScreen, ncolors, 8, WsfbLoadPalette,
1217357bfbb2Smrg				NULL, flags))
1218357bfbb2Smrg		return FALSE;
1219357bfbb2Smrg
12207b613f18Stsutsui#if defined(__NetBSD__) && defined(WSDISPLAY_TYPE_LUNA)
12217b613f18Stsutsui	if (wstype == WSDISPLAY_TYPE_LUNA) {
12227b613f18Stsutsui		ncolors = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1223c2056409Stsutsui		if (ncolors > 0
1224c2056409Stsutsui#ifdef HAVE_SHADOW_AFB
1225c2056409Stsutsui		    && !fPtr->planarAfb
1226c2056409Stsutsui#endif
1227c2056409Stsutsui		) {
12287b613f18Stsutsui			/*
12297b613f18Stsutsui			 * Override palette to use 4bpp/8bpp framebuffers as
12307b613f18Stsutsui			 * monochrome server by using only the first plane.
12317b613f18Stsutsui			 * See also comment in WsfbPreInit().
12327b613f18Stsutsui			 */
12337b613f18Stsutsui			struct wsdisplay_cmap cmap;
12347b613f18Stsutsui			uint8_t r[256], g[256], b[256];
12357b613f18Stsutsui			int p;
12367b613f18Stsutsui
12377b613f18Stsutsui			for (p = 0; p < ncolors; p++)
12387b613f18Stsutsui				r[p] = g[p] = b[p] = (p & 1) ? 0xff : 0;
12397b613f18Stsutsui			cmap.index = 0;
12407b613f18Stsutsui			cmap.count = ncolors;
12417b613f18Stsutsui			cmap.red   = r;
12427b613f18Stsutsui			cmap.green = g;
12437b613f18Stsutsui			cmap.blue  = b;
12447b613f18Stsutsui			if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP, &cmap) == -1) {
12457b613f18Stsutsui				xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
12467b613f18Stsutsui				   "ioctl WSDISPLAYIO_PUTCMAP: %s\n",
12477b613f18Stsutsui				   strerror(errno));
12487b613f18Stsutsui			}
12497b613f18Stsutsui		}
12507b613f18Stsutsui	}
12517b613f18Stsutsui#endif
12527b613f18Stsutsui
1253357bfbb2Smrg	pScreen->SaveScreen = WsfbSaveScreen;
1254357bfbb2Smrg
1255357bfbb2Smrg#ifdef XvExtension
1256357bfbb2Smrg	{
1257357bfbb2Smrg		XF86VideoAdaptorPtr *ptr;
1258357bfbb2Smrg
1259357bfbb2Smrg		int n = xf86XVListGenericAdaptors(pScrn,&ptr);
1260357bfbb2Smrg		if (n) {
1261357bfbb2Smrg			xf86XVScreenInit(pScreen,ptr,n);
1262357bfbb2Smrg		}
1263357bfbb2Smrg	}
1264357bfbb2Smrg#endif
1265357bfbb2Smrg
126664f08a18Smrg	/* Wrap the current CloseScreen function. */
1267357bfbb2Smrg	fPtr->CloseScreen = pScreen->CloseScreen;
1268357bfbb2Smrg	pScreen->CloseScreen = WsfbCloseScreen;
1269357bfbb2Smrg
1270357bfbb2Smrg	TRACE_EXIT("WsfbScreenInit");
1271357bfbb2Smrg	return TRUE;
1272357bfbb2Smrg}
1273357bfbb2Smrg
1274357bfbb2Smrgstatic Bool
12759974adabSmrgWsfbCloseScreen(CLOSE_SCREEN_ARGS_DECL)
1276357bfbb2Smrg{
12779974adabSmrg	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
127864f08a18Smrg	PixmapPtr pPixmap;
1279357bfbb2Smrg	WsfbPtr fPtr = WSFBPTR(pScrn);
1280357bfbb2Smrg
128164f08a18Smrg
1282357bfbb2Smrg	TRACE_ENTER("WsfbCloseScreen");
1283357bfbb2Smrg
128464f08a18Smrg	pPixmap = pScreen->GetScreenPixmap(pScreen);
1285141baaa7Smlelstv	if (fPtr->shadowFB)
1286141baaa7Smlelstv		shadowRemove(pScreen, pPixmap);
128764f08a18Smrg
1288357bfbb2Smrg	if (pScrn->vtSema) {
1289357bfbb2Smrg		WsfbRestore(pScrn);
12908fbcba3dSrin		if (munmap(fPtr->fbmem, fPtr->fbmem_len + fPtr->fbi.fbi_fboffset) == -1) {
1291357bfbb2Smrg			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1292357bfbb2Smrg				   "munmap: %s\n", strerror(errno));
1293357bfbb2Smrg		}
1294357bfbb2Smrg
1295357bfbb2Smrg		fPtr->fbmem = NULL;
1296357bfbb2Smrg	}
1297357bfbb2Smrg#ifdef XFreeXDGA
1298357bfbb2Smrg	if (fPtr->pDGAMode) {
1299cba5e21cSmrg		free(fPtr->pDGAMode);
1300357bfbb2Smrg		fPtr->pDGAMode = NULL;
1301357bfbb2Smrg		fPtr->nDGAMode = 0;
1302357bfbb2Smrg	}
1303357bfbb2Smrg#endif
1304357bfbb2Smrg	pScrn->vtSema = FALSE;
1305357bfbb2Smrg
130664f08a18Smrg	/* Unwrap CloseScreen. */
1307357bfbb2Smrg	pScreen->CloseScreen = fPtr->CloseScreen;
130864f08a18Smrg	TRACE_EXIT("WsfbCloseScreen");
13099974adabSmrg	return (*pScreen->CloseScreen)(CLOSE_SCREEN_ARGS);
1310357bfbb2Smrg}
1311357bfbb2Smrg
1312357bfbb2Smrgstatic void *
1313357bfbb2SmrgWsfbWindowLinear(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
1314357bfbb2Smrg		CARD32 *size, void *closure)
1315357bfbb2Smrg{
13168a4fa5cdStsutsui	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1317357bfbb2Smrg	WsfbPtr fPtr = WSFBPTR(pScrn);
1318357bfbb2Smrg
13193788fa92Smacallan	if (fPtr->fbi.fbi_stride)
13203788fa92Smacallan		*size = fPtr->fbi.fbi_stride;
1321357bfbb2Smrg	else {
1322357bfbb2Smrg		if (ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES, size) == -1)
1323357bfbb2Smrg			return NULL;
13243788fa92Smacallan		fPtr->fbi.fbi_stride = *size;
1325357bfbb2Smrg	}
13268fbcba3dSrin	return ((CARD8 *)fPtr->fbstart + row * fPtr->fbi.fbi_stride + offset);
1327357bfbb2Smrg}
1328357bfbb2Smrg
1329dc9c99a5Stsutsui#ifdef HAVE_SHADOW_AFB
1330bb2f4479Srin/*
13313306a6a4Sjandberg * For use with shadowUpdateAfb8
13323306a6a4Sjandberg *
13333306a6a4Sjandberg * For video memory layout with non-interleaved bitplanes.
13343306a6a4Sjandberg */
13353306a6a4Sjandbergstatic void *
13363306a6a4SjandbergWsfbWindowAfb(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
13373306a6a4Sjandberg		CARD32 *size, void *closure)
13383306a6a4Sjandberg{
13398a4fa5cdStsutsui	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
13403306a6a4Sjandberg	WsfbPtr fPtr = WSFBPTR(pScrn);
13413306a6a4Sjandberg
13423306a6a4Sjandberg	/* size is offset from start of bitplane to next bitplane */
13433306a6a4Sjandberg	*size = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
13443306a6a4Sjandberg	return ((CARD8 *)fPtr->fbstart + row * fPtr->fbi.fbi_stride + offset);
13453306a6a4Sjandberg}
1346dc9c99a5Stsutsui#endif
13473306a6a4Sjandberg
1348357bfbb2Smrgstatic void
13499974adabSmrgWsfbPointerMoved(SCRN_ARG_TYPE arg, int x, int y)
1350357bfbb2Smrg{
13519974adabSmrg    SCRN_INFO_PTR(arg);
1352357bfbb2Smrg    WsfbPtr fPtr = WSFBPTR(pScrn);
1353357bfbb2Smrg    int newX, newY;
1354357bfbb2Smrg
1355357bfbb2Smrg    switch (fPtr->rotate)
1356357bfbb2Smrg    {
1357357bfbb2Smrg    case WSFB_ROTATE_CW:
1358357bfbb2Smrg	/* 90 degrees CW rotation. */
1359357bfbb2Smrg	newX = pScrn->pScreen->height - y - 1;
1360357bfbb2Smrg	newY = x;
1361357bfbb2Smrg	break;
1362357bfbb2Smrg
1363357bfbb2Smrg    case WSFB_ROTATE_CCW:
1364357bfbb2Smrg	/* 90 degrees CCW rotation. */
1365357bfbb2Smrg	newX = y;
1366357bfbb2Smrg	newY = pScrn->pScreen->width - x - 1;
1367357bfbb2Smrg	break;
1368357bfbb2Smrg
1369357bfbb2Smrg    case WSFB_ROTATE_UD:
1370357bfbb2Smrg	/* 180 degrees UD rotation. */
1371357bfbb2Smrg	newX = pScrn->pScreen->width - x - 1;
1372357bfbb2Smrg	newY = pScrn->pScreen->height - y - 1;
1373357bfbb2Smrg	break;
1374357bfbb2Smrg
1375357bfbb2Smrg    default:
1376357bfbb2Smrg	/* No rotation. */
1377357bfbb2Smrg	newX = x;
1378357bfbb2Smrg	newY = y;
1379357bfbb2Smrg	break;
1380357bfbb2Smrg    }
1381357bfbb2Smrg
1382357bfbb2Smrg    /* Pass adjusted pointer coordinates to wrapped PointerMoved function. */
13839974adabSmrg    (*fPtr->PointerMoved)(arg, newX, newY);
1384357bfbb2Smrg}
1385357bfbb2Smrg
1386357bfbb2Smrgstatic Bool
13879974adabSmrgWsfbEnterVT(VT_FUNC_ARGS_DECL)
1388357bfbb2Smrg{
13899974adabSmrg	SCRN_INFO_PTR(arg);
1390b83350a8Smacallan	WsfbPtr fPtr = WSFBPTR(pScrn);
1391b83350a8Smacallan	int mode;
1392357bfbb2Smrg
1393357bfbb2Smrg	TRACE_ENTER("EnterVT");
1394357bfbb2Smrg	pScrn->vtSema = TRUE;
1395b83350a8Smacallan
1396b83350a8Smacallan	/* Restore the graphics mode. */
1397b83350a8Smacallan	mode = WSDISPLAYIO_MODE_DUMBFB;
1398b83350a8Smacallan	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1399b83350a8Smacallan		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1400b83350a8Smacallan			   "error setting graphics mode %s\n", strerror(errno));
1401b83350a8Smacallan	}
1402b83350a8Smacallan
140364f08a18Smrg	TRACE_EXIT("EnterVT");
1404357bfbb2Smrg	return TRUE;
1405357bfbb2Smrg}
1406357bfbb2Smrg
1407357bfbb2Smrgstatic void
14089974adabSmrgWsfbLeaveVT(VT_FUNC_ARGS_DECL)
1409357bfbb2Smrg{
14109974adabSmrg	SCRN_INFO_PTR(arg);
1411b83350a8Smacallan	WsfbPtr fPtr = WSFBPTR(pScrn);
1412b83350a8Smacallan	int mode;
1413357bfbb2Smrg
1414357bfbb2Smrg	TRACE_ENTER("LeaveVT");
1415b83350a8Smacallan
1416b83350a8Smacallan	/*
1417b83350a8Smacallan	 * stuff to do:
1418b83350a8Smacallan	 * - turn off hw cursor
1419b83350a8Smacallan	 * - restore colour map if WSFB_CI
1420b83350a8Smacallan	 * - ioctl(WSDISPLAYIO_MODE_EMUL) to notify the kernel driver that
1421b83350a8Smacallan	 *   we're backing off
1422b83350a8Smacallan	 */
1423b83350a8Smacallan
1424371b4ab3Stsutsui	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
1425371b4ab3Stsutsui	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
1426b83350a8Smacallan		/* reset colormap for text mode */
1427b83350a8Smacallan		if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP,
1428b83350a8Smacallan			  &(fPtr->saved_cmap)) == -1) {
1429b83350a8Smacallan			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1430b83350a8Smacallan				   "error restoring colormap %s\n",
1431b83350a8Smacallan				   strerror(errno));
1432b83350a8Smacallan		}
1433b83350a8Smacallan	}
1434b83350a8Smacallan
1435b83350a8Smacallan	/* Restore the text mode. */
1436b83350a8Smacallan	mode = WSDISPLAYIO_MODE_EMUL;
1437b83350a8Smacallan	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1438b83350a8Smacallan		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1439b83350a8Smacallan			   "error setting text mode %s\n", strerror(errno));
1440b83350a8Smacallan	}
1441b83350a8Smacallan
1442b83350a8Smacallan	pScrn->vtSema = FALSE;
1443b83350a8Smacallan	TRACE_EXIT("LeaveVT");
1444357bfbb2Smrg}
1445357bfbb2Smrg
1446357bfbb2Smrgstatic Bool
14479974adabSmrgWsfbSwitchMode(SWITCH_MODE_ARGS_DECL)
1448357bfbb2Smrg{
14498a4fa5cdStsutsui#if DEBUG
14508a4fa5cdStsutsui	SCRN_INFO_PTR(arg);
14518a4fa5cdStsutsui#endif
14528a4fa5cdStsutsui
1453357bfbb2Smrg	TRACE_ENTER("SwitchMode");
145464f08a18Smrg	/* Nothing else to do. */
1455357bfbb2Smrg	return TRUE;
1456357bfbb2Smrg}
1457357bfbb2Smrg
1458357bfbb2Smrgstatic int
14599974adabSmrgWsfbValidMode(SCRN_ARG_TYPE arg, DisplayModePtr mode, Bool verbose, int flags)
1460357bfbb2Smrg{
14618a4fa5cdStsutsui#if DEBUG
14628a4fa5cdStsutsui	SCRN_INFO_PTR(arg);
14638a4fa5cdStsutsui#endif
14648a4fa5cdStsutsui
1465357bfbb2Smrg	TRACE_ENTER("ValidMode");
1466357bfbb2Smrg	return MODE_OK;
1467357bfbb2Smrg}
1468357bfbb2Smrg
1469357bfbb2Smrgstatic void
1470357bfbb2SmrgWsfbLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
1471357bfbb2Smrg	       LOCO *colors, VisualPtr pVisual)
1472357bfbb2Smrg{
1473357bfbb2Smrg	WsfbPtr fPtr = WSFBPTR(pScrn);
1474357bfbb2Smrg	struct wsdisplay_cmap cmap;
1475357bfbb2Smrg	unsigned char red[256],green[256],blue[256];
1476357bfbb2Smrg	int i, indexMin=256, indexMax=0;
1477357bfbb2Smrg
1478357bfbb2Smrg	TRACE_ENTER("LoadPalette");
1479357bfbb2Smrg
1480b8199fcbStsutsui	/* nothing to do if there is no color palette support */
1481b8199fcbStsutsui	if (fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries == 0)
1482b8199fcbStsutsui		return;
1483b8199fcbStsutsui
1484357bfbb2Smrg	cmap.count   = 1;
1485357bfbb2Smrg	cmap.red   = red;
1486357bfbb2Smrg	cmap.green = green;
1487357bfbb2Smrg	cmap.blue  = blue;
1488357bfbb2Smrg
1489357bfbb2Smrg	if (numColors == 1) {
1490357bfbb2Smrg		/* Optimisation */
1491357bfbb2Smrg		cmap.index = indices[0];
1492357bfbb2Smrg		red[0]   = colors[indices[0]].red;
1493357bfbb2Smrg		green[0] = colors[indices[0]].green;
1494357bfbb2Smrg		blue[0]  = colors[indices[0]].blue;
1495357bfbb2Smrg		if (ioctl(fPtr->fd,WSDISPLAYIO_PUTCMAP, &cmap) == -1)
1496357bfbb2Smrg			ErrorF("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
1497357bfbb2Smrg	} else {
149864f08a18Smrg		/*
149964f08a18Smrg		 * Change all colors in 2 ioctls
15008a4fa5cdStsutsui		 * and limit the data to be transferred.
150164f08a18Smrg		 */
1502357bfbb2Smrg		for (i = 0; i < numColors; i++) {
1503357bfbb2Smrg			if (indices[i] < indexMin)
1504357bfbb2Smrg				indexMin = indices[i];
1505357bfbb2Smrg			if (indices[i] > indexMax)
1506357bfbb2Smrg				indexMax = indices[i];
1507357bfbb2Smrg		}
1508357bfbb2Smrg		cmap.index = indexMin;
1509357bfbb2Smrg		cmap.count = indexMax - indexMin + 1;
1510357bfbb2Smrg		cmap.red = &red[indexMin];
1511357bfbb2Smrg		cmap.green = &green[indexMin];
1512357bfbb2Smrg		cmap.blue = &blue[indexMin];
151364f08a18Smrg		/* Get current map. */
1514357bfbb2Smrg		if (ioctl(fPtr->fd, WSDISPLAYIO_GETCMAP, &cmap) == -1)
1515357bfbb2Smrg			ErrorF("ioctl FBIOGETCMAP: %s\n", strerror(errno));
151664f08a18Smrg		/* Change the colors that require updating. */
1517357bfbb2Smrg		for (i = 0; i < numColors; i++) {
1518357bfbb2Smrg			red[indices[i]]   = colors[indices[i]].red;
1519357bfbb2Smrg			green[indices[i]] = colors[indices[i]].green;
1520357bfbb2Smrg			blue[indices[i]]  = colors[indices[i]].blue;
1521357bfbb2Smrg		}
152264f08a18Smrg		/* Write the colormap back. */
1523357bfbb2Smrg		if (ioctl(fPtr->fd,WSDISPLAYIO_PUTCMAP, &cmap) == -1)
1524357bfbb2Smrg			ErrorF("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
1525357bfbb2Smrg	}
1526357bfbb2Smrg	TRACE_EXIT("LoadPalette");
1527357bfbb2Smrg}
1528357bfbb2Smrg
1529357bfbb2Smrgstatic Bool
1530357bfbb2SmrgWsfbSaveScreen(ScreenPtr pScreen, int mode)
1531357bfbb2Smrg{
15328a4fa5cdStsutsui	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1533357bfbb2Smrg	WsfbPtr fPtr = WSFBPTR(pScrn);
1534357bfbb2Smrg	int state;
1535357bfbb2Smrg
1536357bfbb2Smrg	TRACE_ENTER("SaveScreen");
1537357bfbb2Smrg
1538357bfbb2Smrg	if (!pScrn->vtSema)
1539357bfbb2Smrg		return TRUE;
1540357bfbb2Smrg
1541357bfbb2Smrg	if (mode != SCREEN_SAVER_FORCER) {
1542357bfbb2Smrg		state = xf86IsUnblank(mode)?WSDISPLAYIO_VIDEO_ON:
15438a4fa5cdStsutsui					    WSDISPLAYIO_VIDEO_OFF;
1544357bfbb2Smrg		ioctl(fPtr->fd,
1545357bfbb2Smrg		      WSDISPLAYIO_SVIDEO, &state);
1546357bfbb2Smrg	}
154764f08a18Smrg	TRACE_EXIT("SaveScreen");
1548357bfbb2Smrg	return TRUE;
1549357bfbb2Smrg}
1550357bfbb2Smrg
1551357bfbb2Smrg
1552357bfbb2Smrgstatic void
1553357bfbb2SmrgWsfbSave(ScrnInfoPtr pScrn)
1554357bfbb2Smrg{
1555357bfbb2Smrg	WsfbPtr fPtr = WSFBPTR(pScrn);
1556357bfbb2Smrg
1557357bfbb2Smrg	TRACE_ENTER("WsfbSave");
1558357bfbb2Smrg
15593788fa92Smacallan	/* nothing to save if we don't run in colour-indexed mode */
15603788fa92Smacallan	if (fPtr->fbi.fbi_pixeltype != WSFB_CI)
1561357bfbb2Smrg		return;
1562357bfbb2Smrg
1563b8199fcbStsutsui	/* nothing to do if no color palette support */
1564b8199fcbStsutsui	if (fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries == 0)
1565b8199fcbStsutsui		return;
1566b8199fcbStsutsui
1567357bfbb2Smrg	fPtr->saved_cmap.index = 0;
15683788fa92Smacallan	fPtr->saved_cmap.count = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1569357bfbb2Smrg	if (ioctl(fPtr->fd, WSDISPLAYIO_GETCMAP,
1570357bfbb2Smrg		  &(fPtr->saved_cmap)) == -1) {
1571357bfbb2Smrg		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1572357bfbb2Smrg			   "error saving colormap %s\n", strerror(errno));
1573357bfbb2Smrg	}
157464f08a18Smrg	TRACE_EXIT("WsfbSave");
1575357bfbb2Smrg
1576357bfbb2Smrg}
1577357bfbb2Smrg
1578357bfbb2Smrgstatic void
1579357bfbb2SmrgWsfbRestore(ScrnInfoPtr pScrn)
1580357bfbb2Smrg{
1581357bfbb2Smrg	WsfbPtr fPtr = WSFBPTR(pScrn);
1582357bfbb2Smrg	int mode;
1583357bfbb2Smrg
1584357bfbb2Smrg	TRACE_ENTER("WsfbRestore");
1585357bfbb2Smrg
1586b8199fcbStsutsui	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
1587b8199fcbStsutsui	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
1588357bfbb2Smrg		/* reset colormap for text mode */
1589357bfbb2Smrg		if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP,
1590357bfbb2Smrg			  &(fPtr->saved_cmap)) == -1) {
1591357bfbb2Smrg			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1592357bfbb2Smrg				   "error restoring colormap %s\n",
1593357bfbb2Smrg				   strerror(errno));
1594357bfbb2Smrg		}
1595357bfbb2Smrg	}
1596357bfbb2Smrg
159764f08a18Smrg	/* Clear the screen. */
15988fbcba3dSrin	memset(fPtr->fbstart, 0, fPtr->fbmem_len);
1599357bfbb2Smrg
160064f08a18Smrg	/* Restore the text mode. */
1601357bfbb2Smrg	mode = WSDISPLAYIO_MODE_EMUL;
1602357bfbb2Smrg	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1603357bfbb2Smrg		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1604357bfbb2Smrg			   "error setting text mode %s\n", strerror(errno));
1605357bfbb2Smrg	}
1606357bfbb2Smrg	TRACE_EXIT("WsfbRestore");
1607357bfbb2Smrg}
1608357bfbb2Smrg
1609357bfbb2Smrg#ifdef XFreeXDGA
1610357bfbb2Smrg/***********************************************************************
1611357bfbb2Smrg * DGA stuff
1612357bfbb2Smrg ***********************************************************************/
1613357bfbb2Smrg
1614357bfbb2Smrgstatic Bool
1615357bfbb2SmrgWsfbDGAOpenFramebuffer(ScrnInfoPtr pScrn, char **DeviceName,
1616357bfbb2Smrg		       unsigned char **ApertureBase, int *ApertureSize,
1617357bfbb2Smrg		       int *ApertureOffset, int *flags)
1618357bfbb2Smrg{
1619357bfbb2Smrg	*DeviceName = NULL;		/* No special device */
1620357bfbb2Smrg	*ApertureBase = (unsigned char *)(pScrn->memPhysBase);
1621357bfbb2Smrg	*ApertureSize = pScrn->videoRam;
1622357bfbb2Smrg	*ApertureOffset = pScrn->fbOffset;
1623357bfbb2Smrg	*flags = 0;
1624357bfbb2Smrg
1625357bfbb2Smrg	return TRUE;
1626357bfbb2Smrg}
1627357bfbb2Smrg
1628357bfbb2Smrgstatic Bool
1629357bfbb2SmrgWsfbDGASetMode(ScrnInfoPtr pScrn, DGAModePtr pDGAMode)
1630357bfbb2Smrg{
1631357bfbb2Smrg	DisplayModePtr pMode;
1632357bfbb2Smrg	int frameX0, frameY0;
1633357bfbb2Smrg
1634357bfbb2Smrg	if (pDGAMode) {
1635357bfbb2Smrg		pMode = pDGAMode->mode;
1636357bfbb2Smrg		frameX0 = frameY0 = 0;
1637357bfbb2Smrg	} else {
1638357bfbb2Smrg		if (!(pMode = pScrn->currentMode))
1639357bfbb2Smrg			return TRUE;
1640357bfbb2Smrg
1641357bfbb2Smrg		frameX0 = pScrn->frameX0;
1642357bfbb2Smrg		frameY0 = pScrn->frameY0;
1643357bfbb2Smrg	}
1644357bfbb2Smrg
16459974adabSmrg	if (!(*pScrn->SwitchMode)(SWITCH_MODE_ARGS(pScrn, pMode)))
1646357bfbb2Smrg		return FALSE;
16479974adabSmrg	(*pScrn->AdjustFrame)(ADJUST_FRAME_ARGS(pScrn, frameX0, frameY0));
1648357bfbb2Smrg
1649357bfbb2Smrg	return TRUE;
1650357bfbb2Smrg}
1651357bfbb2Smrg
1652357bfbb2Smrgstatic void
1653357bfbb2SmrgWsfbDGASetViewport(ScrnInfoPtr pScrn, int x, int y, int flags)
1654357bfbb2Smrg{
16559974adabSmrg	(*pScrn->AdjustFrame)(ADJUST_FRAME_ARGS(pScrn, x, y));
1656357bfbb2Smrg}
1657357bfbb2Smrg
1658357bfbb2Smrgstatic int
1659357bfbb2SmrgWsfbDGAGetViewport(ScrnInfoPtr pScrn)
1660357bfbb2Smrg{
1661357bfbb2Smrg	return (0);
1662357bfbb2Smrg}
1663357bfbb2Smrg
1664357bfbb2Smrgstatic DGAFunctionRec WsfbDGAFunctions =
1665357bfbb2Smrg{
1666357bfbb2Smrg	WsfbDGAOpenFramebuffer,
1667357bfbb2Smrg	NULL,       /* CloseFramebuffer */
1668357bfbb2Smrg	WsfbDGASetMode,
1669357bfbb2Smrg	WsfbDGASetViewport,
1670357bfbb2Smrg	WsfbDGAGetViewport,
1671357bfbb2Smrg	NULL,       /* Sync */
1672357bfbb2Smrg	NULL,       /* FillRect */
1673357bfbb2Smrg	NULL,       /* BlitRect */
1674357bfbb2Smrg	NULL,       /* BlitTransRect */
1675357bfbb2Smrg};
1676357bfbb2Smrg
1677357bfbb2Smrgstatic void
1678357bfbb2SmrgWsfbDGAAddModes(ScrnInfoPtr pScrn)
1679357bfbb2Smrg{
1680357bfbb2Smrg	WsfbPtr fPtr = WSFBPTR(pScrn);
1681357bfbb2Smrg	DisplayModePtr pMode = pScrn->modes;
1682357bfbb2Smrg	DGAModePtr pDGAMode;
1683357bfbb2Smrg
1684357bfbb2Smrg	do {
1685cba5e21cSmrg		pDGAMode = realloc(fPtr->pDGAMode,
1686357bfbb2Smrg				    (fPtr->nDGAMode + 1) * sizeof(DGAModeRec));
1687357bfbb2Smrg		if (!pDGAMode)
1688357bfbb2Smrg			break;
1689357bfbb2Smrg
1690357bfbb2Smrg		fPtr->pDGAMode = pDGAMode;
1691357bfbb2Smrg		pDGAMode += fPtr->nDGAMode;
1692357bfbb2Smrg		(void)memset(pDGAMode, 0, sizeof(DGAModeRec));
1693357bfbb2Smrg
1694357bfbb2Smrg		++fPtr->nDGAMode;
1695357bfbb2Smrg		pDGAMode->mode = pMode;
1696357bfbb2Smrg		pDGAMode->flags = DGA_CONCURRENT_ACCESS | DGA_PIXMAP_AVAILABLE;
1697357bfbb2Smrg		pDGAMode->byteOrder = pScrn->imageByteOrder;
1698357bfbb2Smrg		pDGAMode->depth = pScrn->depth;
1699357bfbb2Smrg		pDGAMode->bitsPerPixel = pScrn->bitsPerPixel;
1700357bfbb2Smrg		pDGAMode->red_mask = pScrn->mask.red;
1701357bfbb2Smrg		pDGAMode->green_mask = pScrn->mask.green;
1702357bfbb2Smrg		pDGAMode->blue_mask = pScrn->mask.blue;
1703357bfbb2Smrg		pDGAMode->visualClass = pScrn->bitsPerPixel > 8 ?
1704357bfbb2Smrg			TrueColor : PseudoColor;
1705357bfbb2Smrg		pDGAMode->xViewportStep = 1;
1706357bfbb2Smrg		pDGAMode->yViewportStep = 1;
1707357bfbb2Smrg		pDGAMode->viewportWidth = pMode->HDisplay;
1708357bfbb2Smrg		pDGAMode->viewportHeight = pMode->VDisplay;
1709357bfbb2Smrg
17103788fa92Smacallan		if (fPtr->fbi.fbi_stride)
17113788fa92Smacallan			pDGAMode->bytesPerScanline = fPtr->fbi.fbi_stride;
1712357bfbb2Smrg		else {
1713357bfbb2Smrg			ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES,
17143788fa92Smacallan			      &fPtr->fbi.fbi_stride);
17153788fa92Smacallan			pDGAMode->bytesPerScanline = fPtr->fbi.fbi_stride;
1716357bfbb2Smrg		}
1717357bfbb2Smrg
1718357bfbb2Smrg		pDGAMode->imageWidth = pMode->HDisplay;
1719357bfbb2Smrg		pDGAMode->imageHeight =  pMode->VDisplay;
1720357bfbb2Smrg		pDGAMode->pixmapWidth = pDGAMode->imageWidth;
1721357bfbb2Smrg		pDGAMode->pixmapHeight = pDGAMode->imageHeight;
1722357bfbb2Smrg		pDGAMode->maxViewportX = pScrn->virtualX -
1723357bfbb2Smrg			pDGAMode->viewportWidth;
1724357bfbb2Smrg		pDGAMode->maxViewportY = pScrn->virtualY -
1725357bfbb2Smrg			pDGAMode->viewportHeight;
1726357bfbb2Smrg
1727357bfbb2Smrg		pDGAMode->address = fPtr->fbstart;
1728357bfbb2Smrg
1729357bfbb2Smrg		pMode = pMode->next;
1730357bfbb2Smrg	} while (pMode != pScrn->modes);
1731357bfbb2Smrg}
1732357bfbb2Smrg
1733357bfbb2Smrgstatic Bool
1734357bfbb2SmrgWsfbDGAInit(ScrnInfoPtr pScrn, ScreenPtr pScreen)
1735357bfbb2Smrg{
1736357bfbb2Smrg	WsfbPtr fPtr = WSFBPTR(pScrn);
1737357bfbb2Smrg
1738357bfbb2Smrg	if (pScrn->depth < 8)
1739357bfbb2Smrg		return FALSE;
1740357bfbb2Smrg
1741357bfbb2Smrg	if (!fPtr->nDGAMode)
1742357bfbb2Smrg		WsfbDGAAddModes(pScrn);
1743357bfbb2Smrg
1744357bfbb2Smrg	return (DGAInit(pScreen, &WsfbDGAFunctions,
1745357bfbb2Smrg			fPtr->pDGAMode, fPtr->nDGAMode));
1746357bfbb2Smrg}
1747357bfbb2Smrg#endif
1748357bfbb2Smrg
1749357bfbb2Smrgstatic Bool
1750357bfbb2SmrgWsfbDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op,
1751357bfbb2Smrg    pointer ptr)
1752357bfbb2Smrg{
1753357bfbb2Smrg	xorgHWFlags *flag;
1754cba5e21cSmrg
1755357bfbb2Smrg	switch (op) {
1756357bfbb2Smrg	case GET_REQUIRED_HW_INTERFACES:
1757357bfbb2Smrg		flag = (CARD32*)ptr;
1758357bfbb2Smrg		(*flag) = 0;
1759357bfbb2Smrg		return TRUE;
1760357bfbb2Smrg	default:
1761357bfbb2Smrg		return FALSE;
1762357bfbb2Smrg	}
1763357bfbb2Smrg}
1764357bfbb2Smrg
1765bff82b7eSjmcneillstatic inline void
1766bff82b7eSjmcneillWsfbCopyRGB16ToYUY2(void *dest, void *src, int len)
1767bff82b7eSjmcneill{
1768bff82b7eSjmcneill	uint16_t *src16 = src;
1769bff82b7eSjmcneill	uint32_t *dest32 = dest;
1770bff82b7eSjmcneill
1771bff82b7eSjmcneill	while (len > 0) {
1772bff82b7eSjmcneill		const uint16_t rgb0 = src16[0];
1773bff82b7eSjmcneill		const uint16_t rgb1 = src16[1];
1774bff82b7eSjmcneill		const uint16_t rgb = ((rgb0 >> 1) & ~0x8410) +
1775bff82b7eSjmcneill				     ((rgb1 >> 1) & ~0x8410) +
1776bff82b7eSjmcneill				     ((rgb0 & rgb1) & 0x0841);
1777bff82b7eSjmcneill		const uint32_t y0 = mapRGB16ToY[rgb0];
1778bff82b7eSjmcneill		const uint32_t y1 = mapRGB16ToY[rgb1];
1779bff82b7eSjmcneill		const uint32_t u = mapRGB16ToU[rgb];
1780bff82b7eSjmcneill		const uint32_t v = mapRGB16ToV[rgb];
1781bff82b7eSjmcneill
1782bff82b7eSjmcneill		*dest32 = (y0 << 24) | (u << 16) | (y1 << 8) | v;
1783bff82b7eSjmcneill
1784bff82b7eSjmcneill		dest32++;
1785bff82b7eSjmcneill		src16 += 2;
1786bff82b7eSjmcneill		len -= 4;
1787bff82b7eSjmcneill	}
1788bff82b7eSjmcneill}
1789bff82b7eSjmcneill
1790bff82b7eSjmcneillvoid
1791bff82b7eSjmcneillWsfbShadowUpdateRGB16ToYUY2(ScreenPtr pScreen, shadowBufPtr pBuf)
1792bff82b7eSjmcneill{
1793bff82b7eSjmcneill    RegionPtr	damage = DamageRegion (pBuf->pDamage);
1794bff82b7eSjmcneill    PixmapPtr	pShadow = pBuf->pPixmap;
1795bff82b7eSjmcneill    int		nbox = RegionNumRects (damage);
1796bff82b7eSjmcneill    BoxPtr	pbox = RegionRects (damage);
1797bff82b7eSjmcneill    FbBits	*shaBase, *shaLine, *sha;
1798bff82b7eSjmcneill    FbStride	shaStride;
1799bff82b7eSjmcneill    int		scrBase, scrLine, scr;
1800bff82b7eSjmcneill    int		shaBpp;
1801bff82b7eSjmcneill    int		shaXoff, shaYoff; /* XXX assumed to be zero */
1802bff82b7eSjmcneill    int		x, y, w, h, width;
1803bff82b7eSjmcneill    int         i;
1804bff82b7eSjmcneill    FbBits	*winBase = NULL, *win;
1805bff82b7eSjmcneill    CARD32      winSize;
1806bff82b7eSjmcneill
1807bff82b7eSjmcneill    fbGetDrawable (&pShadow->drawable, shaBase, shaStride, shaBpp, shaXoff, shaYoff);
1808bff82b7eSjmcneill    while (nbox--)
1809bff82b7eSjmcneill    {
1810bff82b7eSjmcneill	x = pbox->x1 * shaBpp;
1811bff82b7eSjmcneill	y = pbox->y1;
1812bff82b7eSjmcneill	w = (pbox->x2 - pbox->x1) * shaBpp;
1813bff82b7eSjmcneill	h = pbox->y2 - pbox->y1;
1814bff82b7eSjmcneill
1815bff82b7eSjmcneill	scrLine = (x >> FB_SHIFT);
1816bff82b7eSjmcneill	shaLine = shaBase + y * shaStride + (x >> FB_SHIFT);
1817bff82b7eSjmcneill
1818bff82b7eSjmcneill	x &= FB_MASK;
1819bff82b7eSjmcneill	w = (w + x + FB_MASK) >> FB_SHIFT;
1820bff82b7eSjmcneill
1821bff82b7eSjmcneill	while (h--)
1822bff82b7eSjmcneill	{
1823bff82b7eSjmcneill	    winSize = 0;
1824bff82b7eSjmcneill	    scrBase = 0;
1825bff82b7eSjmcneill	    width = w;
1826bff82b7eSjmcneill	    scr = scrLine;
1827bff82b7eSjmcneill	    sha = shaLine;
1828bff82b7eSjmcneill	    while (width) {
1829bff82b7eSjmcneill		/* how much remains in this window */
1830bff82b7eSjmcneill		i = scrBase + winSize - scr;
1831bff82b7eSjmcneill		if (i <= 0 || scr < scrBase)
1832bff82b7eSjmcneill		{
1833bff82b7eSjmcneill		    winBase = (FbBits *) (*pBuf->window) (pScreen,
1834bff82b7eSjmcneill							  y,
1835bff82b7eSjmcneill							  scr * sizeof (FbBits),
1836bff82b7eSjmcneill							  SHADOW_WINDOW_WRITE,
1837bff82b7eSjmcneill							  &winSize,
1838bff82b7eSjmcneill							  pBuf->closure);
1839bff82b7eSjmcneill		    if(!winBase)
1840bff82b7eSjmcneill			return;
1841bff82b7eSjmcneill		    scrBase = scr;
1842bff82b7eSjmcneill		    winSize /= sizeof (FbBits);
1843bff82b7eSjmcneill		    i = winSize;
1844bff82b7eSjmcneill		}
1845bff82b7eSjmcneill		win = winBase + (scr - scrBase);
1846bff82b7eSjmcneill		if (i > width)
1847bff82b7eSjmcneill		    i = width;
1848bff82b7eSjmcneill		width -= i;
1849bff82b7eSjmcneill		scr += i;
1850bff82b7eSjmcneill		WsfbCopyRGB16ToYUY2(win, sha, i * sizeof(FbBits));
1851bff82b7eSjmcneill		sha += i;
1852bff82b7eSjmcneill	    }
1853bff82b7eSjmcneill	    shaLine += shaStride;
1854bff82b7eSjmcneill	    y++;
1855bff82b7eSjmcneill	}
1856bff82b7eSjmcneill	pbox++;
1857bff82b7eSjmcneill    }
1858bff82b7eSjmcneill}
1859bff82b7eSjmcneill
18600ef55ed1Smacallanstatic inline void
18610ef55ed1Smacallanmemcpy32sw(void *dest, void *src, int len)
18620ef55ed1Smacallan{
18630ef55ed1Smacallan	uint32_t *d = dest, *s = src;
18640ef55ed1Smacallan
18650ef55ed1Smacallan#if DEBUG
18660ef55ed1Smacallan	if ((((long)dest & 3) + ((long)src & 3) + (len & 3)) != 0) {
18670ef55ed1Smacallan		xf86Msg(X_ERROR, "unaligned %s\n", __func__);
18680ef55ed1Smacallan		return;
18690ef55ed1Smacallan	}
18700ef55ed1Smacallan#endif
18710ef55ed1Smacallan	while (len > 0) {
18720ef55ed1Smacallan		*d = bswap32(*s);
18730ef55ed1Smacallan		d++;
18740ef55ed1Smacallan		s++;
18750ef55ed1Smacallan		len -= 4;
18760ef55ed1Smacallan	}
18770ef55ed1Smacallan}
18780ef55ed1Smacallan
18790ef55ed1Smacallan/* adapted from miext/shadow/shpacked.c::shadowUpdatePacked() */
18800ef55ed1Smacallanvoid
18810ef55ed1SmacallanWsfbShadowUpdateSwap32(ScreenPtr pScreen, shadowBufPtr pBuf)
18820ef55ed1Smacallan{
188372ee27a2Smrg    RegionPtr	damage = DamageRegion (pBuf->pDamage);
18840ef55ed1Smacallan    PixmapPtr	pShadow = pBuf->pPixmap;
18850ef55ed1Smacallan    int		nbox = RegionNumRects (damage);
18860ef55ed1Smacallan    BoxPtr	pbox = RegionRects (damage);
18870ef55ed1Smacallan    FbBits	*shaBase, *shaLine, *sha;
18880ef55ed1Smacallan    FbStride	shaStride;
18890ef55ed1Smacallan    int		scrBase, scrLine, scr;
18900ef55ed1Smacallan    int		shaBpp;
18910ef55ed1Smacallan    int		shaXoff, shaYoff; /* XXX assumed to be zero */
18920ef55ed1Smacallan    int		x, y, w, h, width;
18930ef55ed1Smacallan    int         i;
18940ef55ed1Smacallan    FbBits	*winBase = NULL, *win;
18950ef55ed1Smacallan    CARD32      winSize;
18960ef55ed1Smacallan
18970ef55ed1Smacallan    fbGetDrawable (&pShadow->drawable, shaBase, shaStride, shaBpp, shaXoff, shaYoff);
18980ef55ed1Smacallan    while (nbox--)
18990ef55ed1Smacallan    {
19000ef55ed1Smacallan	x = pbox->x1 * shaBpp;
19010ef55ed1Smacallan	y = pbox->y1;
19020ef55ed1Smacallan	w = (pbox->x2 - pbox->x1) * shaBpp;
19030ef55ed1Smacallan	h = pbox->y2 - pbox->y1;
19040ef55ed1Smacallan
19050ef55ed1Smacallan	scrLine = (x >> FB_SHIFT);
19060ef55ed1Smacallan	shaLine = shaBase + y * shaStride + (x >> FB_SHIFT);
19070ef55ed1Smacallan
19080ef55ed1Smacallan	x &= FB_MASK;
19090ef55ed1Smacallan	w = (w + x + FB_MASK) >> FB_SHIFT;
19100ef55ed1Smacallan
19110ef55ed1Smacallan	while (h--)
19120ef55ed1Smacallan	{
19130ef55ed1Smacallan	    winSize = 0;
19140ef55ed1Smacallan	    scrBase = 0;
19150ef55ed1Smacallan	    width = w;
19160ef55ed1Smacallan	    scr = scrLine;
19170ef55ed1Smacallan	    sha = shaLine;
19180ef55ed1Smacallan	    while (width) {
19190ef55ed1Smacallan		/* how much remains in this window */
19200ef55ed1Smacallan		i = scrBase + winSize - scr;
19210ef55ed1Smacallan		if (i <= 0 || scr < scrBase)
19220ef55ed1Smacallan		{
19230ef55ed1Smacallan		    winBase = (FbBits *) (*pBuf->window) (pScreen,
19240ef55ed1Smacallan							  y,
19250ef55ed1Smacallan							  scr * sizeof (FbBits),
19260ef55ed1Smacallan							  SHADOW_WINDOW_WRITE,
19270ef55ed1Smacallan							  &winSize,
19280ef55ed1Smacallan							  pBuf->closure);
19290ef55ed1Smacallan		    if(!winBase)
19300ef55ed1Smacallan			return;
19310ef55ed1Smacallan		    scrBase = scr;
19320ef55ed1Smacallan		    winSize /= sizeof (FbBits);
19330ef55ed1Smacallan		    i = winSize;
19340ef55ed1Smacallan		}
19350ef55ed1Smacallan		win = winBase + (scr - scrBase);
19360ef55ed1Smacallan		if (i > width)
19370ef55ed1Smacallan		    i = width;
19380ef55ed1Smacallan		width -= i;
19390ef55ed1Smacallan		scr += i;
19400ef55ed1Smacallan		memcpy32sw(win, sha, i * sizeof(FbBits));
19410ef55ed1Smacallan		sha += i;
19420ef55ed1Smacallan	    }
19430ef55ed1Smacallan	    shaLine += shaStride;
19440ef55ed1Smacallan	    y++;
19450ef55ed1Smacallan	}
19460ef55ed1Smacallan	pbox++;
19470ef55ed1Smacallan    }
19480ef55ed1Smacallan}
19490ef55ed1Smacallan
1950cbc9a668Smacallanvoid
1951cbc9a668SmacallanWsfbShadowUpdateSplit(ScreenPtr pScreen, shadowBufPtr pBuf)
1952cbc9a668Smacallan{
1953cbc9a668Smacallan    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1954cbc9a668Smacallan    WsfbPtr 	fPtr = WSFBPTR(pScrn);
195572ee27a2Smrg    RegionPtr	damage = DamageRegion (pBuf->pDamage);
1956cbc9a668Smacallan    PixmapPtr	pShadow = pBuf->pPixmap;
1957cbc9a668Smacallan    int		nbox = RegionNumRects (damage);
1958cbc9a668Smacallan    BoxPtr	pbox = RegionRects (damage);
1959cbc9a668Smacallan    FbBits	*shaBase, *shaLine, *sha;
1960cbc9a668Smacallan    FbStride	shaStride;
1961cbc9a668Smacallan    int		scrBase, scrLine, scr;
1962cbc9a668Smacallan    int		shaBpp;
1963cbc9a668Smacallan    int		shaXoff, shaYoff; /* XXX assumed to be zero */
1964cbc9a668Smacallan    int		x, y, w, h, width;
1965cbc9a668Smacallan    int         i;
1966cbc9a668Smacallan    FbBits	*winBase = NULL, *win, *win2;
1967cbc9a668Smacallan    unsigned long split = fPtr->fbi.fbi_fbsize / 2;
1968cbc9a668Smacallan    CARD32      winSize;
1969cbc9a668Smacallan
1970cbc9a668Smacallan    fbGetDrawable (&pShadow->drawable, shaBase, shaStride, shaBpp, shaXoff, shaYoff);
1971cbc9a668Smacallan    while (nbox--)
1972cbc9a668Smacallan    {
1973cbc9a668Smacallan	x = pbox->x1 * shaBpp;
1974cbc9a668Smacallan	y = pbox->y1;
1975cbc9a668Smacallan	w = (pbox->x2 - pbox->x1) * shaBpp;
1976cbc9a668Smacallan	h = pbox->y2 - pbox->y1;
1977cbc9a668Smacallan
1978cbc9a668Smacallan	scrLine = (x >> FB_SHIFT);
1979cbc9a668Smacallan	shaLine = shaBase + y * shaStride + (x >> FB_SHIFT);
1980cbc9a668Smacallan
1981cbc9a668Smacallan	x &= FB_MASK;
1982cbc9a668Smacallan	w = (w + x + FB_MASK) >> FB_SHIFT;
1983cbc9a668Smacallan
1984cbc9a668Smacallan	while (h--)
1985cbc9a668Smacallan	{
1986cbc9a668Smacallan	    winSize = 0;
1987cbc9a668Smacallan	    scrBase = 0;
1988cbc9a668Smacallan	    width = w;
1989cbc9a668Smacallan	    scr = scrLine;
1990cbc9a668Smacallan	    sha = shaLine;
1991cbc9a668Smacallan	    while (width) {
1992cbc9a668Smacallan		/* how much remains in this window */
1993cbc9a668Smacallan		i = scrBase + winSize - scr;
1994cbc9a668Smacallan		if (i <= 0 || scr < scrBase)
1995cbc9a668Smacallan		{
1996cbc9a668Smacallan		    winBase = (FbBits *) (*pBuf->window) (pScreen,
1997cbc9a668Smacallan							  y,
1998cbc9a668Smacallan							  scr * sizeof (FbBits),
1999cbc9a668Smacallan							  SHADOW_WINDOW_WRITE,
2000cbc9a668Smacallan							  &winSize,
2001cbc9a668Smacallan							  pBuf->closure);
2002cbc9a668Smacallan		    if(!winBase)
2003cbc9a668Smacallan			return;
2004cbc9a668Smacallan		    scrBase = scr;
2005cbc9a668Smacallan		    winSize /= sizeof (FbBits);
2006cbc9a668Smacallan		    i = winSize;
2007cbc9a668Smacallan		}
2008cbc9a668Smacallan		win = winBase + (scr - scrBase);
2009cbc9a668Smacallan		win2 = (FbBits *)(split + (unsigned long)win);
2010cbc9a668Smacallan		if (i > width)
2011cbc9a668Smacallan		    i = width;
2012cbc9a668Smacallan		width -= i;
2013cbc9a668Smacallan		scr += i;
2014cbc9a668Smacallan		memcpy(win, sha, i * sizeof(FbBits));
2015cbc9a668Smacallan		memcpy(win2, sha, i * sizeof(FbBits));
2016cbc9a668Smacallan		sha += i;
2017cbc9a668Smacallan	    }
2018cbc9a668Smacallan	    shaLine += shaStride;
2019cbc9a668Smacallan	    y++;
2020cbc9a668Smacallan	}
2021cbc9a668Smacallan	pbox++;
2022cbc9a668Smacallan    }
2023cbc9a668Smacallan}
2024