wsfb_driver.c revision 0bb1e2ad
1/*
2 * Copyright © 2001-2012 Matthieu Herrb
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 *    - Redistributions of source code must retain the above copyright
10 *      notice, this list of conditions and the following disclaimer.
11 *    - Redistributions in binary form must reproduce the above
12 *      copyright notice, this list of conditions and the following
13 *      disclaimer in the documentation and/or other materials provided
14 *      with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31/*
32 * Based on fbdev.c written by:
33 *
34 * Authors:  Alan Hourihane, <alanh@fairlite.demon.co.uk>
35 *	     Michel Dänzer, <michdaen@iiic.ethz.ch>
36 */
37
38#ifdef HAVE_CONFIG_H
39#include "config.h"
40#endif
41
42#include <errno.h>
43#include <fcntl.h>
44#include <unistd.h>
45#include <sys/ioctl.h>
46#include <sys/types.h>
47#include <sys/mman.h>
48#include <sys/time.h>
49#include <errno.h>
50#include <dev/wscons/wsconsio.h>
51
52/* All drivers need this. */
53#include "xf86.h"
54#include "xf86_OSproc.h"
55#include "xf86_OSlib.h"
56
57#include "mipointer.h"
58#include "micmap.h"
59#include "colormapst.h"
60#include "xf86cmap.h"
61#include "shadow.h"
62#include "dgaproc.h"
63
64/* For visuals */
65#include "fb.h"
66
67#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
68#include "xf86Resources.h"
69#include "xf86RAC.h"
70#endif
71
72#ifdef XvExtension
73#include "xf86xv.h"
74#endif
75
76#include "wsfb.h"
77
78/* #include "wsconsio.h" */
79
80#include <sys/mman.h>
81
82#ifdef X_PRIVSEP
83extern int priv_open_device(const char *);
84#else
85#define priv_open_device(n)    open(n,O_RDWR|O_NONBLOCK|O_EXCL)
86#endif
87
88#if defined(__NetBSD__)
89#define WSFB_DEFAULT_DEV "/dev/ttyE0"
90#else
91#define WSFB_DEFAULT_DEV "/dev/ttyC0"
92#endif
93
94#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) > 6
95#define xf86LoaderReqSymLists(...) do {} while (0)
96#define LoaderRefSymLists(...) do {} while (0)
97#define xf86LoaderReqSymbols(...) do {} while (0)
98#endif
99
100#define DEBUG 0
101
102#if DEBUG
103# define TRACE_ENTER(str)       ErrorF("wsfb: " str " %d\n",pScrn->scrnIndex)
104# define TRACE_EXIT(str)        ErrorF("wsfb: " str " done\n")
105# define TRACE(str)             ErrorF("wsfb trace: " str "\n")
106#else
107# define TRACE_ENTER(str)
108# define TRACE_EXIT(str)
109# define TRACE(str)
110#endif
111
112/* Prototypes */
113static pointer WsfbSetup(pointer, pointer, int *, int *);
114static Bool WsfbGetRec(ScrnInfoPtr);
115static void WsfbFreeRec(ScrnInfoPtr);
116static const OptionInfoRec * WsfbAvailableOptions(int, int);
117static void WsfbIdentify(int);
118static Bool WsfbProbe(DriverPtr, int);
119static Bool WsfbPreInit(ScrnInfoPtr, int);
120static Bool WsfbScreenInit(SCREEN_INIT_ARGS_DECL);
121static Bool WsfbCloseScreen(CLOSE_SCREEN_ARGS_DECL);
122static void *WsfbWindowLinear(ScreenPtr, CARD32, CARD32, int, CARD32 *,
123			      void *);
124static void *WsfbWindowAfb(ScreenPtr, CARD32, CARD32, int, CARD32 *,
125			      void *);
126static void WsfbPointerMoved(SCRN_ARG_TYPE, int, int);
127static Bool WsfbEnterVT(VT_FUNC_ARGS_DECL);
128static void WsfbLeaveVT(VT_FUNC_ARGS_DECL);
129static Bool WsfbSwitchMode(SWITCH_MODE_ARGS_DECL);
130static int WsfbValidMode(SCRN_ARG_TYPE, DisplayModePtr, Bool, int);
131static void WsfbLoadPalette(ScrnInfoPtr, int, int *, LOCO *, VisualPtr);
132static Bool WsfbSaveScreen(ScreenPtr, int);
133static void WsfbSave(ScrnInfoPtr);
134static void WsfbRestore(ScrnInfoPtr);
135
136/* DGA stuff */
137#ifdef XFreeXDGA
138static Bool WsfbDGAOpenFramebuffer(ScrnInfoPtr, char **, unsigned char **,
139				   int *, int *, int *);
140static Bool WsfbDGASetMode(ScrnInfoPtr, DGAModePtr);
141static void WsfbDGASetViewport(ScrnInfoPtr, int, int, int);
142static Bool WsfbDGAInit(ScrnInfoPtr, ScreenPtr);
143#endif
144
145static void WsfbShadowUpdateSwap32(ScreenPtr, shadowBufPtr);
146static void WsfbShadowUpdateSplit(ScreenPtr, shadowBufPtr);
147
148static Bool WsfbDriverFunc(ScrnInfoPtr, xorgDriverFuncOp, pointer);
149
150/* Helper functions */
151static int wsfb_open(const char *);
152static pointer wsfb_mmap(size_t, off_t, int);
153
154enum { WSFB_ROTATE_NONE = 0,
155       WSFB_ROTATE_CCW = 90,
156       WSFB_ROTATE_UD = 180,
157       WSFB_ROTATE_CW = 270
158};
159
160/*
161 * This is intentionally screen-independent.
162 * It indicates the binding choice made in the first PreInit.
163 */
164static int pix24bpp = 0;
165
166#define WSFB_VERSION 		4000
167#define WSFB_NAME		"wsfb"
168#define WSFB_DRIVER_NAME	"wsfb"
169
170_X_EXPORT DriverRec WSFB = {
171	WSFB_VERSION,
172	WSFB_DRIVER_NAME,
173	WsfbIdentify,
174	WsfbProbe,
175	WsfbAvailableOptions,
176	NULL,
177	0,
178	WsfbDriverFunc
179};
180
181/* Supported "chipsets" */
182static SymTabRec WsfbChipsets[] = {
183	{ 0, "wsfb" },
184	{ -1, NULL }
185};
186
187/* Supported options */
188typedef enum {
189	OPTION_SHADOW_FB,
190	OPTION_ROTATE,
191	OPTION_HW_CURSOR,
192	OPTION_SW_CURSOR
193} WsfbOpts;
194
195static const OptionInfoRec WsfbOptions[] = {
196	{ OPTION_SHADOW_FB, "ShadowFB", OPTV_BOOLEAN, {0}, FALSE},
197	{ OPTION_ROTATE, "Rotate", OPTV_STRING, {0}, FALSE},
198	{ OPTION_HW_CURSOR, "HWCursor", OPTV_BOOLEAN, {1}, FALSE},
199	{ -1, NULL, OPTV_NONE, {0}, FALSE}
200};
201
202#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) <= 6
203/* Symbols needed from other modules */
204static const char *fbSymbols[] = {
205	"fbPictureInit",
206	"fbScreenInit",
207	NULL
208};
209static const char *shadowSymbols[] = {
210	"shadowAdd",
211	"shadowSetup",
212	"shadowUpdatePacked",
213	"shadowUpdatePackedWeak",
214	"shadowUpdateRotatePacked",
215	"shadowUpdateRotatePackedWeak",
216	"shadowUpdateAfb8",
217	NULL
218};
219
220static const char *ramdacSymbols[] = {
221	"xf86CreateCursorInfoRec",
222	"xf86DestroyCursorInfoRec",
223	"xf86InitCursor",
224	NULL
225};
226#endif
227
228static XF86ModuleVersionInfo WsfbVersRec = {
229	"wsfb",
230	MODULEVENDORSTRING,
231	MODINFOSTRING1,
232	MODINFOSTRING2,
233	XORG_VERSION_CURRENT,
234	PACKAGE_VERSION_MAJOR,
235	PACKAGE_VERSION_MINOR,
236	PACKAGE_VERSION_PATCHLEVEL,
237	ABI_CLASS_VIDEODRV,
238	ABI_VIDEODRV_VERSION,
239	NULL,
240	{0, 0, 0, 0}
241};
242
243_X_EXPORT XF86ModuleData wsfbModuleData = { &WsfbVersRec, WsfbSetup, NULL };
244
245static pointer
246WsfbSetup(pointer module, pointer opts, int *errmaj, int *errmin)
247{
248	static Bool setupDone = FALSE;
249
250#if !(defined __NetBSD__ || defined __OpenBSD__)
251	/* Check that we're being loaded on a OpenBSD or NetBSD system. */
252	if (errmaj)
253		*errmaj = LDR_BADOS;
254	if (errmin)
255		*errmin = 0;
256	return NULL;
257#endif
258	if (!setupDone) {
259		setupDone = TRUE;
260		xf86AddDriver(&WSFB, module, HaveDriverFuncs);
261		LoaderRefSymLists(fbSymbols, shadowSymbols, ramdacSymbols,
262		    NULL);
263		return (pointer)1;
264	} else {
265		if (errmaj != NULL)
266			*errmaj = LDR_ONCEONLY;
267		return NULL;
268	}
269}
270
271static Bool
272WsfbGetRec(ScrnInfoPtr pScrn)
273{
274
275	if (pScrn->driverPrivate != NULL)
276		return TRUE;
277
278	pScrn->driverPrivate = xnfcalloc(sizeof(WsfbRec), 1);
279	return TRUE;
280}
281
282static void
283WsfbFreeRec(ScrnInfoPtr pScrn)
284{
285
286	if (pScrn->driverPrivate == NULL)
287		return;
288	free(pScrn->driverPrivate);
289	pScrn->driverPrivate = NULL;
290}
291
292static const OptionInfoRec *
293WsfbAvailableOptions(int chipid, int busid)
294{
295	return WsfbOptions;
296}
297
298static void
299WsfbIdentify(int flags)
300{
301	xf86PrintChipsets(WSFB_NAME, "driver for wsdisplay framebuffer",
302			  WsfbChipsets);
303}
304
305/* Open the framebuffer device. */
306static int
307wsfb_open(const char *dev)
308{
309	int fd = -1;
310
311	/* Try argument from xorg.conf first. */
312	if (dev == NULL || ((fd = priv_open_device(dev)) == -1)) {
313		/* Second: environment variable. */
314		dev = getenv("XDEVICE");
315		if (dev == NULL || ((fd = priv_open_device(dev)) == -1)) {
316			/* Last try: default device. */
317			dev = WSFB_DEFAULT_DEV;
318			if ((fd = priv_open_device(dev)) == -1) {
319				return -1;
320			}
321		}
322	}
323	return fd;
324}
325
326/* Map the framebuffer's memory. */
327static pointer
328wsfb_mmap(size_t len, off_t off, int fd)
329{
330	int pagemask, mapsize;
331	caddr_t addr;
332	pointer mapaddr;
333
334	pagemask = getpagesize() - 1;
335	mapsize = ((int) len + pagemask) & ~pagemask;
336	addr = 0;
337
338	/*
339	 * Try and make it private first, that way once we get it, an
340	 * interloper, e.g. another server, can't get this frame buffer,
341	 * and if another server already has it, this one won't.
342	 */
343	mapaddr = (pointer) mmap(addr, mapsize,
344				 PROT_READ | PROT_WRITE, MAP_SHARED,
345				 fd, off);
346	if (mapaddr == MAP_FAILED) {
347		mapaddr = NULL;
348	}
349#if DEBUG
350	ErrorF("mmap returns: addr %p len 0x%x\n", mapaddr, mapsize);
351#endif
352	return mapaddr;
353}
354
355static Bool
356WsfbProbe(DriverPtr drv, int flags)
357{
358	int i, fd, entity;
359       	GDevPtr *devSections;
360	int numDevSections;
361	const char *dev;
362	Bool foundScreen = FALSE;
363
364	TRACE("probe start");
365
366	/* For now, just bail out for PROBE_DETECT. */
367	if (flags & PROBE_DETECT)
368		return FALSE;
369
370	if ((numDevSections = xf86MatchDevice(WSFB_DRIVER_NAME,
371					      &devSections)) <= 0)
372		return FALSE;
373
374	/* Do not attach if the modesetting driver is active */
375	if (fbSlotClaimed == TRUE) {
376		DriverPtr fbSlotDrv = xf86GetEntityInfo(0)->driver;
377		if (strcmp(fbSlotDrv->driverName, "modesetting") == 0)
378			return FALSE;
379	}
380
381	for (i = 0; i < numDevSections; i++) {
382		ScrnInfoPtr pScrn = NULL;
383
384		dev = xf86FindOptionValue(devSections[i]->options, "device");
385		if ((fd = wsfb_open(dev)) >= 0) {
386			entity = xf86ClaimFbSlot(drv, 0, devSections[i], TRUE);
387			pScrn = xf86ConfigFbEntity(NULL,0,entity,
388						   NULL,NULL,NULL,NULL);
389			if (pScrn != NULL) {
390				foundScreen = TRUE;
391				pScrn->driverVersion = WSFB_VERSION;
392				pScrn->driverName = WSFB_DRIVER_NAME;
393				pScrn->name = WSFB_NAME;
394				pScrn->Probe = WsfbProbe;
395				pScrn->PreInit = WsfbPreInit;
396				pScrn->ScreenInit = WsfbScreenInit;
397				pScrn->SwitchMode = WsfbSwitchMode;
398				pScrn->AdjustFrame = NULL;
399				pScrn->EnterVT = WsfbEnterVT;
400				pScrn->LeaveVT = WsfbLeaveVT;
401				pScrn->ValidMode = WsfbValidMode;
402
403				xf86DrvMsg(pScrn->scrnIndex, X_INFO,
404				    "using %s\n", dev != NULL ? dev :
405				    "default device");
406			}
407		}
408	}
409	free(devSections);
410	TRACE("probe done");
411	return foundScreen;
412}
413
414static Bool
415WsfbPreInit(ScrnInfoPtr pScrn, int flags)
416{
417	WsfbPtr fPtr;
418	int default_depth, bitsperpixel, wstype;
419	const char *dev, *s;
420	char *mod = NULL;
421	const char *reqSym = NULL;
422	Gamma zeros = {0.0, 0.0, 0.0};
423	DisplayModePtr mode;
424	MessageType from;
425
426	if (flags & PROBE_DETECT) return FALSE;
427
428	TRACE_ENTER("PreInit");
429
430	if (pScrn->numEntities != 1) return FALSE;
431
432	pScrn->monitor = pScrn->confScreen->monitor;
433
434	WsfbGetRec(pScrn);
435	fPtr = WSFBPTR(pScrn);
436
437	fPtr->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
438
439#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
440	pScrn->racMemFlags = RAC_FB | RAC_COLORMAP | RAC_CURSOR | RAC_VIEWPORT;
441	pScrn->racIoFlags = pScrn->racMemFlags;
442#endif
443
444	dev = xf86FindOptionValue(fPtr->pEnt->device->options, "device");
445	fPtr->fd = wsfb_open(dev);
446	if (fPtr->fd == -1) {
447		return FALSE;
448	}
449
450	if (ioctl(fPtr->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
451		xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
452			   "ioctl WSDISPLAY_GTYPE: %s\n",
453			   strerror(errno));
454		wstype = WSDISPLAY_TYPE_UNKNOWN;
455	}
456
457	if (ioctl(fPtr->fd, WSDISPLAYIO_GET_FBINFO, &fPtr->fbi) != 0) {
458		struct wsdisplay_fbinfo info;
459		struct wsdisplayio_fbinfo *fbi = &fPtr->fbi;
460		int lb;
461
462		xf86Msg(X_WARNING, "ioctl(WSDISPLAYIO_GET_FBINFO) failed, " \
463			"falling back to old method\n");
464		if (ioctl(fPtr->fd, WSDISPLAYIO_GINFO, &info) == -1) {
465			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
466				   "ioctl WSDISPLAY_GINFO: %s\n",
467				   strerror(errno));
468			return FALSE;
469		}
470		if (ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES, &lb) == -1) {
471			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
472				   "ioctl WSDISPLAYIO_LINEBYTES: %s\n",
473				   strerror(errno));
474			return FALSE;
475		}
476		/* ok, fake up a new style fbinfo */
477		fbi->fbi_width = info.width;
478		fbi->fbi_height = info.height;
479		fbi->fbi_stride = lb;
480		fbi->fbi_bitsperpixel = info.depth;
481		if (info.depth > 16) {
482			fbi->fbi_pixeltype = WSFB_RGB;
483			if (wstype == WSDISPLAY_TYPE_SUN24 ||
484			    wstype == WSDISPLAY_TYPE_SUNCG12 ||
485			    wstype == WSDISPLAY_TYPE_SUNCG14 ||
486			    wstype == WSDISPLAY_TYPE_SUNTCX ||
487			    wstype == WSDISPLAY_TYPE_SUNFFB ||
488			    wstype == WSDISPLAY_TYPE_XVR1000 ||
489			    wstype == WSDISPLAY_TYPE_VC4) {
490				fbi->fbi_subtype.fbi_rgbmasks.red_offset = 0;
491				fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
492				fbi->fbi_subtype.fbi_rgbmasks.green_offset = 8;
493				fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
494				fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 16;
495				fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
496			} else {
497				fbi->fbi_subtype.fbi_rgbmasks.red_offset = 16;
498				fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
499				fbi->fbi_subtype.fbi_rgbmasks.green_offset = 8;
500				fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
501				fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 0;
502				fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
503			}
504			fbi->fbi_subtype.fbi_rgbmasks.alpha_offset = 0;
505			fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 0;
506		} else if (info.depth <= 8) {
507			fbi->fbi_pixeltype = WSFB_CI;
508			fbi->fbi_subtype.fbi_cmapinfo.cmap_entries = info.cmsize;
509		}
510		fbi->fbi_flags = 0;
511		fbi->fbi_fbsize = lb * info.height;
512#ifdef	WSDISPLAY_TYPE_LUNA
513		if (wstype == WSDISPLAY_TYPE_LUNA) {
514			/*
515			 * XXX
516			 * LUNA's FB seems to have 64 dot (8 byte) offset.
517			 * This might be able to be changed in kernel
518			 * lunafb driver, but current setting was pulled
519			 * from 4.4BSD-Lite2/luna68k.
520			 */
521			fbi->fbi_fboffset = 8;
522		} else
523#endif
524			fbi->fbi_fboffset = 0;
525	}
526	xf86Msg(X_INFO, "fboffset %x\n", (int)fPtr->fbi.fbi_fboffset);
527	/*
528	 * Allocate room for saving the colormap.
529	 */
530	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
531	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
532		fPtr->saved_cmap.red =
533		    (unsigned char *)malloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
534		if (fPtr->saved_cmap.red == NULL) {
535			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
536			    "Cannot malloc %d bytes\n",
537			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
538			return FALSE;
539		}
540		fPtr->saved_cmap.green =
541		    (unsigned char *)malloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
542		if (fPtr->saved_cmap.green == NULL) {
543			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
544			    "Cannot malloc %d bytes\n",
545			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
546			free(fPtr->saved_cmap.red);
547			return FALSE;
548		}
549		fPtr->saved_cmap.blue =
550		    (unsigned char *)malloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
551		if (fPtr->saved_cmap.blue == NULL) {
552			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
553			    "Cannot malloc %d bytes\n",
554			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
555			free(fPtr->saved_cmap.red);
556			free(fPtr->saved_cmap.green);
557			return FALSE;
558		}
559	}
560
561	/* Handle depth */
562	default_depth = fPtr->fbi.fbi_bitsperpixel <= 24 ? fPtr->fbi.fbi_bitsperpixel : 24;
563	bitsperpixel = fPtr->fbi.fbi_bitsperpixel == 15 ? 16 : fPtr->fbi.fbi_bitsperpixel;
564#if defined(__NetBSD__) && defined(WSDISPLAY_TYPE_LUNA)
565	if (wstype == WSDISPLAY_TYPE_LUNA) {
566		/*
567		 * XXX
568		 * LUNA's color framebuffers support 4bpp or 8bpp
569		 * but they have multiple 1bpp VRAM planes like ancient VGA.
570		 * For now, Xorg server supports only the first one plane
571		 * as 1bpp monochrome server.
572		 *
573		 * Note OpenBSD/luna88k workarounds this by switching depth
574		 * and palette settings by WSDISPLAYIO_SETGFXMODE ioctl.
575		 */
576		default_depth = 1;
577		bitsperpixel = 1;
578	}
579#endif
580#ifdef WSDISPLAY_TYPE_AMIGACC
581	if (wstype == WSDISPLAY_TYPE_AMIGACC) {
582#  if XORG_VERSION_CURRENT >= (1) * 10000000 + (20) * 100000
583		/*
584		 * Video memory is organized in bitplanes.
585		 * 8bpp or 1bpp supported in this driver.
586		 * With 8bpp conversion to bitplane format
587		 * is done in shadow update proc.
588		 * With 1bpp no conversion needed.
589		 */
590		if (bitsperpixel == 8) {
591			fPtr->planarAfb = TRUE;
592		} else
593#  endif
594		{
595			default_depth = 1;
596			bitsperpixel = 1;
597		}
598	}
599#endif
600
601	if (!xf86SetDepthBpp(pScrn, default_depth, default_depth,
602		bitsperpixel,
603		bitsperpixel >= 24 ? Support24bppFb|Support32bppFb : 0))
604		return FALSE;
605
606	/* Check consistency. */
607	if (pScrn->bitsPerPixel != bitsperpixel) {
608		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
609		    "specified depth (%d) or bpp (%d) doesn't match "
610		    "framebuffer depth (%d)\n", pScrn->depth,
611		    pScrn->bitsPerPixel, bitsperpixel);
612		return FALSE;
613	}
614	xf86PrintDepthBpp(pScrn);
615
616	/* Get the depth24 pixmap format. */
617	if (pScrn->depth == 24 && pix24bpp == 0)
618		pix24bpp = xf86GetBppFromDepth(pScrn, 24);
619
620	/* Handle options. */
621	xf86CollectOptions(pScrn, NULL);
622	fPtr->Options = (OptionInfoRec *)malloc(sizeof(WsfbOptions));
623	if (fPtr->Options == NULL)
624		return FALSE;
625	memcpy(fPtr->Options, WsfbOptions, sizeof(WsfbOptions));
626	xf86ProcessOptions(pScrn->scrnIndex, fPtr->pEnt->device->options,
627			   fPtr->Options);
628
629	/* Use shadow framebuffer by default, on depth >= 8 */
630	xf86Msg(X_INFO, "fbi_flags: %x\n", fPtr->fbi.fbi_flags);
631	if ((pScrn->depth >= 8) &&
632	   ((fPtr->fbi.fbi_flags & WSFB_VRAM_IS_RAM) == 0)) {
633		fPtr->shadowFB = xf86ReturnOptValBool(fPtr->Options,
634						      OPTION_SHADOW_FB, TRUE);
635	} else
636		if (xf86ReturnOptValBool(fPtr->Options,
637					 OPTION_SHADOW_FB, FALSE)) {
638			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
639				   "Shadow FB option ignored on depth < 8\n");
640		}
641	if (fPtr->fbi.fbi_flags & WSFB_VRAM_IS_SPLIT) {
642		if (!fPtr->shadowFB) {
643			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
644				   "Shadow FB forced on for split framebuffer\n");
645			fPtr->shadowFB = TRUE;
646		}
647	}
648	/* Rotation */
649	fPtr->rotate = WSFB_ROTATE_NONE;
650	if ((s = xf86GetOptValString(fPtr->Options, OPTION_ROTATE))) {
651		if (pScrn->depth >= 8) {
652			if (!xf86NameCmp(s, "CW")) {
653				fPtr->shadowFB = TRUE;
654				fPtr->rotate = WSFB_ROTATE_CW;
655				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
656				    "Rotating screen clockwise\n");
657			} else if (!xf86NameCmp(s, "CCW")) {
658				fPtr->shadowFB = TRUE;
659				fPtr->rotate = WSFB_ROTATE_CCW;
660				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
661				    "Rotating screen counter clockwise\n");
662			} else if (!xf86NameCmp(s, "UD")) {
663				fPtr->shadowFB = TRUE;
664				fPtr->rotate = WSFB_ROTATE_UD;
665				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
666				    "Rotating screen upside down\n");
667			} else {
668				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
669				    "\"%s\" is not a valid value for Option "
670				    "\"Rotate\"\n", s);
671				xf86DrvMsg(pScrn->scrnIndex, X_INFO,
672				    "Valid options are \"CW\", \"CCW\","
673				    " or \"UD\"\n");
674			}
675		} else {
676			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
677			    "Option \"Rotate\" ignored on depth < 8\n");
678		}
679	}
680
681
682	fPtr->useSwap32 = FALSE;
683	/* Color weight */
684	if (fPtr->fbi.fbi_pixeltype == WSFB_RGB) {
685		rgb zeros = { 0, 0, 0 }, masks;
686
687		if (fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_size > 0) {
688			uint32_t msk;
689
690			/*
691			 * see if we need to byte-swap pixels
692			 * XXX this requires a shadow FB and is incompatible
693			 * (for now ) with rotation
694			 */
695			if ((fPtr->fbi.fbi_bitsperpixel == 32) &&
696			    (fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset == 24) &&
697			    (fPtr->rotate == WSFB_ROTATE_NONE) &&
698			    (fPtr->shadowFB == TRUE)) {
699			    	/*
700			    	 * looks like BGRA - set the swap flag and flip
701			    	 * the offsets
702			    	 */
703			    	xf86Msg(X_INFO, "endian-flipped RGB framebuffer "
704			    			"detected, using WsfbShadowUpdateSwap32()\n");
705			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset = 0;
706			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_offset = 8;
707			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_offset = 16;
708			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.alpha_offset = 24;
709				fPtr->useSwap32 = TRUE;
710			}
711
712			msk = 0xffffffff;
713			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_size;
714			msk = ~msk;
715			masks.red = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_offset;
716
717			msk = 0xffffffff;
718			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_size;
719			msk = ~msk;
720			masks.green = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_offset;
721
722			msk = 0xffffffff;
723			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_size;
724			msk = ~msk;
725			masks.blue = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset;
726			xf86Msg(X_INFO, "masks generated: %08lx %08lx %08lx\n",
727			    (unsigned long)masks.red,
728			    (unsigned long)masks.green,
729			    (unsigned long)masks.blue);
730		} else {
731			masks.red = 0;
732			masks.green = 0;
733			masks.blue = 0;
734		}
735
736		if (!xf86SetWeight(pScrn, zeros, masks))
737			return FALSE;
738	}
739
740	/* Visual init */
741	if (!xf86SetDefaultVisual(pScrn, -1))
742		return FALSE;
743
744	/* We don't currently support DirectColor at > 8bpp . */
745	if (pScrn->depth > 8 && pScrn->defaultVisual != TrueColor) {
746		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Given default visual"
747			   " (%s) is not supported at depth %d\n",
748			   xf86GetVisualName(pScrn->defaultVisual),
749			   pScrn->depth);
750		return FALSE;
751	}
752
753	xf86SetGamma(pScrn,zeros);
754
755	pScrn->progClock = TRUE;
756	pScrn->rgbBits   = (pScrn->depth >= 8) ? 8 : pScrn->depth;
757	pScrn->chipset   = "wsfb";
758	pScrn->videoRam  = fPtr->fbi.fbi_fbsize;
759
760	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Vidmem: %dk\n",
761		   pScrn->videoRam/1024);
762
763	/* Fake video mode struct. */
764	mode = (DisplayModePtr)malloc(sizeof(DisplayModeRec));
765	mode->prev = mode;
766	mode->next = mode;
767	mode->name = "wsfb current mode";
768	mode->status = MODE_OK;
769	mode->type = M_T_BUILTIN;
770	mode->Clock = 0;
771	mode->HDisplay = fPtr->fbi.fbi_width;
772	mode->HSyncStart = 0;
773	mode->HSyncEnd = 0;
774	mode->HTotal = 0;
775	mode->HSkew = 0;
776	mode->VDisplay = fPtr->fbi.fbi_height;
777	mode->VSyncStart = 0;
778	mode->VSyncEnd = 0;
779	mode->VTotal = 0;
780	mode->VScan = 0;
781	mode->Flags = 0;
782	if (pScrn->modes != NULL) {
783		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
784		   "Ignoring mode specification from screen section\n");
785	}
786	pScrn->currentMode = pScrn->modes = mode;
787	pScrn->virtualX = fPtr->fbi.fbi_width;
788	pScrn->virtualY = fPtr->fbi.fbi_height;
789	pScrn->displayWidth = pScrn->virtualX;
790
791	/* Set the display resolution. */
792	xf86SetDpi(pScrn, 0, 0);
793
794	from = X_DEFAULT;
795	fPtr->HWCursor = TRUE;
796	if (xf86GetOptValBool(fPtr->Options, OPTION_HW_CURSOR, &fPtr->HWCursor))
797		from = X_CONFIG;
798	if (xf86ReturnOptValBool(fPtr->Options, OPTION_SW_CURSOR, FALSE)) {
799		from = X_CONFIG;
800		fPtr->HWCursor = FALSE;
801	}
802	xf86DrvMsg(pScrn->scrnIndex, from, "Using %s cursor\n",
803		fPtr->HWCursor ? "HW" : "SW");
804
805	/* Load bpp-specific modules. */
806	switch(pScrn->bitsPerPixel) {
807	case 1:
808	case 4:
809	default:
810		mod = "fb";
811		break;
812	}
813
814
815	/* Load shadow if needed. */
816	if (fPtr->shadowFB) {
817		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
818			   "Using \"Shadow Framebuffer\"\n");
819		if (xf86LoadSubModule(pScrn, "shadow") == NULL) {
820			WsfbFreeRec(pScrn);
821			return FALSE;
822		}
823	}
824
825	if (mod && xf86LoadSubModule(pScrn, mod) == NULL) {
826		WsfbFreeRec(pScrn);
827		return FALSE;
828	}
829
830	if (xf86LoadSubModule(pScrn, "ramdac") == NULL) {
831		WsfbFreeRec(pScrn);
832		return FALSE;
833        }
834
835	if (mod) {
836		if (reqSym) {
837			xf86LoaderReqSymbols(reqSym, NULL);
838		} else {
839			xf86LoaderReqSymLists(fbSymbols, NULL);
840		}
841	}
842	TRACE_EXIT("PreInit");
843	return TRUE;
844}
845
846static Bool
847WsfbCreateScreenResources(ScreenPtr pScreen)
848{
849	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
850	WsfbPtr fPtr = WSFBPTR(pScrn);
851	PixmapPtr pPixmap;
852	Bool ret;
853	void (*shadowproc)(ScreenPtr, shadowBufPtr);
854	ShadowWindowProc windowproc = WsfbWindowLinear;
855
856	pScreen->CreateScreenResources = fPtr->CreateScreenResources;
857	ret = pScreen->CreateScreenResources(pScreen);
858	pScreen->CreateScreenResources = WsfbCreateScreenResources;
859
860	if (!ret)
861		return FALSE;
862
863	pPixmap = pScreen->GetScreenPixmap(pScreen);
864	if (fPtr->fbi.fbi_flags & WSFB_VRAM_IS_SPLIT) {
865		shadowproc = WsfbShadowUpdateSplit;
866	} else if (fPtr->useSwap32) {
867		shadowproc = WsfbShadowUpdateSwap32;
868	} else if (fPtr->rotate) {
869		shadowproc = shadowUpdateRotatePacked;
870	} else if (fPtr->planarAfb) {
871#if XORG_VERSION_CURRENT >= (1) * 10000000 + (20) * 100000
872		shadowproc = shadowUpdateAfb8;
873#else
874		xf86Msg(X_ERROR,
875		    "Planar fb requires xorg-server 1.20 or higher.");
876		return FALSE;
877#endif
878		windowproc = WsfbWindowAfb;
879	} else
880		shadowproc = shadowUpdatePacked;
881
882	if (!shadowAdd(pScreen, pPixmap, shadowproc,
883		windowproc, fPtr->rotate, NULL)) {
884		return FALSE;
885	}
886	return TRUE;
887}
888
889
890static Bool
891WsfbShadowInit(ScreenPtr pScreen)
892{
893	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
894	WsfbPtr fPtr = WSFBPTR(pScrn);
895
896	if (!shadowSetup(pScreen))
897		return FALSE;
898	fPtr->CreateScreenResources = pScreen->CreateScreenResources;
899	pScreen->CreateScreenResources = WsfbCreateScreenResources;
900
901	return TRUE;
902}
903
904static Bool
905WsfbScreenInit(SCREEN_INIT_ARGS_DECL)
906{
907	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
908	WsfbPtr fPtr = WSFBPTR(pScrn);
909	VisualPtr visual;
910	int ret, flags, ncolors;
911	int wsmode = WSDISPLAYIO_MODE_DUMBFB;
912	int wstype;
913	int width;
914	size_t len;
915
916	TRACE_ENTER("WsfbScreenInit");
917#if DEBUG
918	ErrorF("\tbitsPerPixel=%d, depth=%d, defaultVisual=%s\n"
919	       "\tmask: %x,%x,%x, offset: %u,%u,%u\n",
920	       pScrn->bitsPerPixel,
921	       pScrn->depth,
922	       xf86GetVisualName(pScrn->defaultVisual),
923	       pScrn->mask.red,pScrn->mask.green,pScrn->mask.blue,
924	       pScrn->offset.red,pScrn->offset.green,pScrn->offset.blue);
925#endif
926	switch (fPtr->fbi.fbi_bitsperpixel) {
927	case 1:
928	case 4:
929	case 8:
930		len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
931		break;
932	case 15:
933	case 16:
934		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
935			xf86Msg(X_ERROR, "Bogus stride == width in 16bit colour\n");
936			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * sizeof(short);
937		} else {
938			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
939		}
940		break;
941	case 24:
942		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
943			xf86Msg(X_ERROR, "Bogus stride == width in 24bit colour\n");
944			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * 3;
945		} else {
946			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
947		}
948		break;
949	case 32:
950		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
951			xf86Msg(X_ERROR, "Bogus stride == width in 32bit colour\n");
952			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * sizeof(int);
953		} else {
954			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
955		}
956		break;
957	default:
958		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
959			   "unsupported depth %d\n", fPtr->fbi.fbi_bitsperpixel);
960		return FALSE;
961	}
962	/* Switch to graphics mode - required before mmap. */
963	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &wsmode) == -1) {
964		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
965			   "ioctl WSDISPLAYIO_SMODE: %s\n",
966			   strerror(errno));
967		return FALSE;
968	}
969	/* Get wsdisplay type to handle quirks */
970	if (ioctl(fPtr->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
971		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
972			   "ioctl WSDISPLAY_GTYPE: %s\n",
973			   strerror(errno));
974		return FALSE;
975	}
976	len = max(len, fPtr->fbi.fbi_fbsize);
977	fPtr->fbmem = wsfb_mmap(len + fPtr->fbi.fbi_fboffset, 0, fPtr->fd);
978
979	if (fPtr->fbmem == NULL) {
980		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
981			   "wsfb_mmap: %s\n", strerror(errno));
982		return FALSE;
983	}
984	fPtr->fbmem_len = len;
985
986	WsfbSave(pScrn);
987	pScrn->vtSema = TRUE;
988
989	/* MI layer */
990	miClearVisualTypes();
991	if (pScrn->bitsPerPixel > 8) {
992		if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
993				      pScrn->rgbBits, TrueColor))
994			return FALSE;
995	} else {
996		if (!miSetVisualTypes(pScrn->depth,
997				      miGetDefaultVisualMask(pScrn->depth),
998				      pScrn->rgbBits, pScrn->defaultVisual))
999			return FALSE;
1000	}
1001	if (!miSetPixmapDepths())
1002		return FALSE;
1003
1004	if (fPtr->rotate == WSFB_ROTATE_CW
1005	    || fPtr->rotate == WSFB_ROTATE_CCW) {
1006		int tmp = pScrn->virtualX;
1007		pScrn->virtualX = pScrn->displayWidth = pScrn->virtualY;
1008		pScrn->virtualY = tmp;
1009	}
1010	if (fPtr->rotate && !fPtr->PointerMoved) {
1011		fPtr->PointerMoved = pScrn->PointerMoved;
1012		pScrn->PointerMoved = WsfbPointerMoved;
1013	}
1014
1015	fPtr->fbstart = fPtr->fbmem + fPtr->fbi.fbi_fboffset;
1016
1017	if (fPtr->shadowFB) {
1018		if (fPtr->rotate) {
1019			/*
1020			 * Note Rotate and Shadow FB options are valid
1021			 * only on depth >= 8.
1022			 */
1023			len = pScrn->virtualX * pScrn->virtualY *
1024			    (pScrn->bitsPerPixel >> 3);
1025		} else if (fPtr->planarAfb) {
1026			/* always 8bpp */
1027			len = pScrn->virtualX * pScrn->virtualY;
1028		} else {
1029			len = fPtr->fbi.fbi_stride * pScrn->virtualY;
1030		}
1031		fPtr->shadow = calloc(1, len);
1032
1033		if (!fPtr->shadow) {
1034			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1035			    "Failed to allocate shadow framebuffer\n");
1036			return FALSE;
1037		}
1038	}
1039
1040	/*
1041	 * fbScreenInit() seems to require "pixel width of frame buffer"
1042	 * but it is actually "stride in pixel" of frame buffer,
1043	 * per xorg/xserver/tree/fb/fbscreen.c.
1044	 */
1045	if (fPtr->rotate) {
1046		width = pScrn->displayWidth;
1047	} else if (fPtr->planarAfb) {
1048		width = pScrn->displayWidth;
1049	} else {
1050		if (pScrn->bitsPerPixel > 8) {
1051			width =
1052			    fPtr->fbi.fbi_stride / (pScrn->bitsPerPixel >> 3);
1053		} else {
1054			width =
1055			    fPtr->fbi.fbi_stride * (8 / pScrn->bitsPerPixel);
1056		}
1057	}
1058	switch (pScrn->bitsPerPixel) {
1059	case 1:
1060		ret = fbScreenInit(pScreen,
1061		    fPtr->fbstart,
1062		    pScrn->virtualX, pScrn->virtualY,
1063		    pScrn->xDpi, pScrn->yDpi,
1064		    width, pScrn->bitsPerPixel);
1065		break;
1066	case 4:
1067	case 8:
1068	case 16:
1069	case 24:
1070	case 32:
1071		ret = fbScreenInit(pScreen,
1072		    fPtr->shadowFB ? fPtr->shadow : fPtr->fbstart,
1073		    pScrn->virtualX, pScrn->virtualY,
1074		    pScrn->xDpi, pScrn->yDpi,
1075		    width,
1076		    pScrn->bitsPerPixel);
1077		break;
1078	default:
1079		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1080			   "Unsupported bpp: %d\n", pScrn->bitsPerPixel);
1081		return FALSE;
1082	} /* case */
1083
1084	if (!ret)
1085		return FALSE;
1086
1087	if (pScrn->bitsPerPixel > 8) {
1088		/* Fixup RGB ordering. */
1089		visual = pScreen->visuals + pScreen->numVisuals;
1090		while (--visual >= pScreen->visuals) {
1091			if ((visual->class | DynamicClass) == DirectColor) {
1092				visual->offsetRed   = pScrn->offset.red;
1093				visual->offsetGreen = pScrn->offset.green;
1094				visual->offsetBlue  = pScrn->offset.blue;
1095				visual->redMask     = pScrn->mask.red;
1096				visual->greenMask   = pScrn->mask.green;
1097				visual->blueMask    = pScrn->mask.blue;
1098			}
1099		}
1100	}
1101
1102	if (pScrn->bitsPerPixel >= 8) {
1103		if (!fbPictureInit(pScreen, NULL, 0))
1104			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
1105				   "RENDER extension initialisation failed.\n");
1106	}
1107	if (fPtr->shadowFB && !WsfbShadowInit(pScreen)) {
1108		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1109		    "shadow framebuffer initialization failed\n");
1110		return FALSE;
1111	}
1112
1113#ifdef XFreeXDGA
1114	if (!fPtr->rotate)
1115		WsfbDGAInit(pScrn, pScreen);
1116	else
1117		xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Rotated display, "
1118		    "disabling DGA\n");
1119#endif
1120	if (fPtr->rotate) {
1121		xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Enabling Driver Rotation, "
1122		    "disabling RandR\n");
1123#if 0
1124		xf86DisableRandR();
1125#endif
1126		if (pScrn->bitsPerPixel == 24)
1127			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
1128			    "Rotation might be broken in 24 bpp\n");
1129	}
1130
1131	xf86SetBlackWhitePixels(pScreen);
1132	xf86SetBackingStore(pScreen);
1133
1134	/* Software cursor. */
1135	miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
1136
1137	/* check for hardware cursor support */
1138	if (fPtr->HWCursor)
1139		WsfbSetupCursor(pScreen);
1140
1141	/*
1142	 * Colormap
1143	 *
1144	 * Note that, even on less than 8 bit depth frame buffers, we
1145	 * expect the colormap to be programmable with 8 bit values.
1146	 * As of now, this is indeed the case on all OpenBSD supported
1147	 * graphics hardware.
1148	 */
1149	if (!miCreateDefColormap(pScreen))
1150		return FALSE;
1151	flags = CMAP_RELOAD_ON_MODE_SWITCH;
1152
1153	ncolors = 0;
1154	if (fPtr->fbi.fbi_pixeltype == WSFB_CI) {
1155		ncolors = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1156	}
1157
1158	/* On StaticGray visuals, fake a 256 entries colormap. */
1159	if (ncolors == 0)
1160		ncolors = 256;
1161
1162	if(!xf86HandleColormaps(pScreen, ncolors, 8, WsfbLoadPalette,
1163				NULL, flags))
1164		return FALSE;
1165
1166#if defined(__NetBSD__) && defined(WSDISPLAY_TYPE_LUNA)
1167	if (wstype == WSDISPLAY_TYPE_LUNA) {
1168		ncolors = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1169		if (ncolors > 0) {
1170			/*
1171			 * Override palette to use 4bpp/8bpp framebuffers as
1172			 * monochrome server by using only the first plane.
1173			 * See also comment in WsfbPreInit().
1174			 */
1175			struct wsdisplay_cmap cmap;
1176			uint8_t r[256], g[256], b[256];
1177			int p;
1178
1179			for (p = 0; p < ncolors; p++)
1180				r[p] = g[p] = b[p] = (p & 1) ? 0xff : 0;
1181			cmap.index = 0;
1182			cmap.count = ncolors;
1183			cmap.red   = r;
1184			cmap.green = g;
1185			cmap.blue  = b;
1186			if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP, &cmap) == -1) {
1187				xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1188				   "ioctl WSDISPLAYIO_PUTCMAP: %s\n",
1189				   strerror(errno));
1190			}
1191		}
1192	}
1193#endif
1194
1195	pScreen->SaveScreen = WsfbSaveScreen;
1196
1197#ifdef XvExtension
1198	{
1199		XF86VideoAdaptorPtr *ptr;
1200
1201		int n = xf86XVListGenericAdaptors(pScrn,&ptr);
1202		if (n) {
1203			xf86XVScreenInit(pScreen,ptr,n);
1204		}
1205	}
1206#endif
1207
1208	/* Wrap the current CloseScreen function. */
1209	fPtr->CloseScreen = pScreen->CloseScreen;
1210	pScreen->CloseScreen = WsfbCloseScreen;
1211
1212	TRACE_EXIT("WsfbScreenInit");
1213	return TRUE;
1214}
1215
1216static Bool
1217WsfbCloseScreen(CLOSE_SCREEN_ARGS_DECL)
1218{
1219	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1220	PixmapPtr pPixmap;
1221	WsfbPtr fPtr = WSFBPTR(pScrn);
1222
1223
1224	TRACE_ENTER("WsfbCloseScreen");
1225
1226	pPixmap = pScreen->GetScreenPixmap(pScreen);
1227	if (fPtr->shadowFB)
1228		shadowRemove(pScreen, pPixmap);
1229
1230	if (pScrn->vtSema) {
1231		WsfbRestore(pScrn);
1232		if (munmap(fPtr->fbmem, fPtr->fbmem_len + fPtr->fbi.fbi_fboffset) == -1) {
1233			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1234				   "munmap: %s\n", strerror(errno));
1235		}
1236
1237		fPtr->fbmem = NULL;
1238	}
1239#ifdef XFreeXDGA
1240	if (fPtr->pDGAMode) {
1241		free(fPtr->pDGAMode);
1242		fPtr->pDGAMode = NULL;
1243		fPtr->nDGAMode = 0;
1244	}
1245#endif
1246	pScrn->vtSema = FALSE;
1247
1248	/* Unwrap CloseScreen. */
1249	pScreen->CloseScreen = fPtr->CloseScreen;
1250	TRACE_EXIT("WsfbCloseScreen");
1251	return (*pScreen->CloseScreen)(CLOSE_SCREEN_ARGS);
1252}
1253
1254static void *
1255WsfbWindowLinear(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
1256		CARD32 *size, void *closure)
1257{
1258	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1259	WsfbPtr fPtr = WSFBPTR(pScrn);
1260
1261	/*
1262	 * XXX
1263	 * This should never happen. Is it really necessary?
1264	 */
1265	if (fPtr->fbi.fbi_stride)
1266		*size = fPtr->fbi.fbi_stride;
1267	else {
1268		if (ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES, size) == -1)
1269			return NULL;
1270		fPtr->fbi.fbi_stride = *size;
1271	}
1272	return ((CARD8 *)fPtr->fbstart + row * fPtr->fbi.fbi_stride + offset);
1273}
1274
1275/*
1276 * For use with shadowUpdateAfb8
1277 *
1278 * For video memory layout with non-interleaved bitplanes.
1279 */
1280static void *
1281WsfbWindowAfb(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
1282		CARD32 *size, void *closure)
1283{
1284	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1285	WsfbPtr fPtr = WSFBPTR(pScrn);
1286
1287	/* size is offset from start of bitplane to next bitplane */
1288	*size = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
1289	return ((CARD8 *)fPtr->fbstart + row * fPtr->fbi.fbi_stride + offset);
1290}
1291
1292static void
1293WsfbPointerMoved(SCRN_ARG_TYPE arg, int x, int y)
1294{
1295    SCRN_INFO_PTR(arg);
1296    WsfbPtr fPtr = WSFBPTR(pScrn);
1297    int newX, newY;
1298
1299    switch (fPtr->rotate)
1300    {
1301    case WSFB_ROTATE_CW:
1302	/* 90 degrees CW rotation. */
1303	newX = pScrn->pScreen->height - y - 1;
1304	newY = x;
1305	break;
1306
1307    case WSFB_ROTATE_CCW:
1308	/* 90 degrees CCW rotation. */
1309	newX = y;
1310	newY = pScrn->pScreen->width - x - 1;
1311	break;
1312
1313    case WSFB_ROTATE_UD:
1314	/* 180 degrees UD rotation. */
1315	newX = pScrn->pScreen->width - x - 1;
1316	newY = pScrn->pScreen->height - y - 1;
1317	break;
1318
1319    default:
1320	/* No rotation. */
1321	newX = x;
1322	newY = y;
1323	break;
1324    }
1325
1326    /* Pass adjusted pointer coordinates to wrapped PointerMoved function. */
1327    (*fPtr->PointerMoved)(arg, newX, newY);
1328}
1329
1330static Bool
1331WsfbEnterVT(VT_FUNC_ARGS_DECL)
1332{
1333	SCRN_INFO_PTR(arg);
1334	WsfbPtr fPtr = WSFBPTR(pScrn);
1335	int mode;
1336
1337	TRACE_ENTER("EnterVT");
1338	pScrn->vtSema = TRUE;
1339
1340	/* Restore the graphics mode. */
1341	mode = WSDISPLAYIO_MODE_DUMBFB;
1342	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1343		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1344			   "error setting graphics mode %s\n", strerror(errno));
1345	}
1346
1347	TRACE_EXIT("EnterVT");
1348	return TRUE;
1349}
1350
1351static void
1352WsfbLeaveVT(VT_FUNC_ARGS_DECL)
1353{
1354	SCRN_INFO_PTR(arg);
1355	WsfbPtr fPtr = WSFBPTR(pScrn);
1356	int mode;
1357
1358	TRACE_ENTER("LeaveVT");
1359
1360	/*
1361	 * stuff to do:
1362	 * - turn off hw cursor
1363	 * - restore colour map if WSFB_CI
1364	 * - ioctl(WSDISPLAYIO_MODE_EMUL) to notify the kernel driver that
1365	 *   we're backing off
1366	 */
1367
1368	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
1369	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
1370		/* reset colormap for text mode */
1371		if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP,
1372			  &(fPtr->saved_cmap)) == -1) {
1373			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1374				   "error restoring colormap %s\n",
1375				   strerror(errno));
1376		}
1377	}
1378
1379	/* Restore the text mode. */
1380	mode = WSDISPLAYIO_MODE_EMUL;
1381	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1382		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1383			   "error setting text mode %s\n", strerror(errno));
1384	}
1385
1386	pScrn->vtSema = FALSE;
1387	TRACE_EXIT("LeaveVT");
1388}
1389
1390static Bool
1391WsfbSwitchMode(SWITCH_MODE_ARGS_DECL)
1392{
1393	TRACE_ENTER("SwitchMode");
1394	/* Nothing else to do. */
1395	return TRUE;
1396}
1397
1398static int
1399WsfbValidMode(SCRN_ARG_TYPE arg, DisplayModePtr mode, Bool verbose, int flags)
1400{
1401	TRACE_ENTER("ValidMode");
1402	return MODE_OK;
1403}
1404
1405static void
1406WsfbLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
1407	       LOCO *colors, VisualPtr pVisual)
1408{
1409	WsfbPtr fPtr = WSFBPTR(pScrn);
1410	struct wsdisplay_cmap cmap;
1411	unsigned char red[256],green[256],blue[256];
1412	int i, indexMin=256, indexMax=0;
1413
1414	TRACE_ENTER("LoadPalette");
1415
1416	/* nothing to do if there is no color palette support */
1417	if (fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries == 0)
1418		return;
1419
1420	cmap.count   = 1;
1421	cmap.red   = red;
1422	cmap.green = green;
1423	cmap.blue  = blue;
1424
1425	if (numColors == 1) {
1426		/* Optimisation */
1427		cmap.index = indices[0];
1428		red[0]   = colors[indices[0]].red;
1429		green[0] = colors[indices[0]].green;
1430		blue[0]  = colors[indices[0]].blue;
1431		if (ioctl(fPtr->fd,WSDISPLAYIO_PUTCMAP, &cmap) == -1)
1432			ErrorF("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
1433	} else {
1434		/*
1435		 * Change all colors in 2 ioctls
1436		 * and limit the data to be transfered.
1437		 */
1438		for (i = 0; i < numColors; i++) {
1439			if (indices[i] < indexMin)
1440				indexMin = indices[i];
1441			if (indices[i] > indexMax)
1442				indexMax = indices[i];
1443		}
1444		cmap.index = indexMin;
1445		cmap.count = indexMax - indexMin + 1;
1446		cmap.red = &red[indexMin];
1447		cmap.green = &green[indexMin];
1448		cmap.blue = &blue[indexMin];
1449		/* Get current map. */
1450		if (ioctl(fPtr->fd, WSDISPLAYIO_GETCMAP, &cmap) == -1)
1451			ErrorF("ioctl FBIOGETCMAP: %s\n", strerror(errno));
1452		/* Change the colors that require updating. */
1453		for (i = 0; i < numColors; i++) {
1454			red[indices[i]]   = colors[indices[i]].red;
1455			green[indices[i]] = colors[indices[i]].green;
1456			blue[indices[i]]  = colors[indices[i]].blue;
1457		}
1458		/* Write the colormap back. */
1459		if (ioctl(fPtr->fd,WSDISPLAYIO_PUTCMAP, &cmap) == -1)
1460			ErrorF("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
1461	}
1462	TRACE_EXIT("LoadPalette");
1463}
1464
1465static Bool
1466WsfbSaveScreen(ScreenPtr pScreen, int mode)
1467{
1468	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1469	WsfbPtr fPtr = WSFBPTR(pScrn);
1470	int state;
1471
1472	TRACE_ENTER("SaveScreen");
1473
1474	if (!pScrn->vtSema)
1475		return TRUE;
1476
1477	if (mode != SCREEN_SAVER_FORCER) {
1478		state = xf86IsUnblank(mode)?WSDISPLAYIO_VIDEO_ON:
1479		                            WSDISPLAYIO_VIDEO_OFF;
1480		ioctl(fPtr->fd,
1481		      WSDISPLAYIO_SVIDEO, &state);
1482	}
1483	TRACE_EXIT("SaveScreen");
1484	return TRUE;
1485}
1486
1487
1488static void
1489WsfbSave(ScrnInfoPtr pScrn)
1490{
1491	WsfbPtr fPtr = WSFBPTR(pScrn);
1492
1493	TRACE_ENTER("WsfbSave");
1494
1495	/* nothing to save if we don't run in colour-indexed mode */
1496	if (fPtr->fbi.fbi_pixeltype != WSFB_CI)
1497		return;
1498
1499	/* nothing to do if no color palette support */
1500	if (fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries == 0)
1501		return;
1502
1503	fPtr->saved_cmap.index = 0;
1504	fPtr->saved_cmap.count = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1505	if (ioctl(fPtr->fd, WSDISPLAYIO_GETCMAP,
1506		  &(fPtr->saved_cmap)) == -1) {
1507		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1508			   "error saving colormap %s\n", strerror(errno));
1509	}
1510	TRACE_EXIT("WsfbSave");
1511
1512}
1513
1514static void
1515WsfbRestore(ScrnInfoPtr pScrn)
1516{
1517	WsfbPtr fPtr = WSFBPTR(pScrn);
1518	int mode;
1519
1520	TRACE_ENTER("WsfbRestore");
1521
1522	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
1523	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
1524		/* reset colormap for text mode */
1525		if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP,
1526			  &(fPtr->saved_cmap)) == -1) {
1527			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1528				   "error restoring colormap %s\n",
1529				   strerror(errno));
1530		}
1531	}
1532
1533	/* Clear the screen. */
1534	memset(fPtr->fbstart, 0, fPtr->fbmem_len);
1535
1536	/* Restore the text mode. */
1537	mode = WSDISPLAYIO_MODE_EMUL;
1538	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1539		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1540			   "error setting text mode %s\n", strerror(errno));
1541	}
1542	TRACE_EXIT("WsfbRestore");
1543}
1544
1545#ifdef XFreeXDGA
1546/***********************************************************************
1547 * DGA stuff
1548 ***********************************************************************/
1549
1550static Bool
1551WsfbDGAOpenFramebuffer(ScrnInfoPtr pScrn, char **DeviceName,
1552		       unsigned char **ApertureBase, int *ApertureSize,
1553		       int *ApertureOffset, int *flags)
1554{
1555	*DeviceName = NULL;		/* No special device */
1556	*ApertureBase = (unsigned char *)(pScrn->memPhysBase);
1557	*ApertureSize = pScrn->videoRam;
1558	*ApertureOffset = pScrn->fbOffset;
1559	*flags = 0;
1560
1561	return TRUE;
1562}
1563
1564static Bool
1565WsfbDGASetMode(ScrnInfoPtr pScrn, DGAModePtr pDGAMode)
1566{
1567	DisplayModePtr pMode;
1568	int scrnIdx = pScrn->pScreen->myNum;
1569	int frameX0, frameY0;
1570
1571	if (pDGAMode) {
1572		pMode = pDGAMode->mode;
1573		frameX0 = frameY0 = 0;
1574	} else {
1575		if (!(pMode = pScrn->currentMode))
1576			return TRUE;
1577
1578		frameX0 = pScrn->frameX0;
1579		frameY0 = pScrn->frameY0;
1580	}
1581
1582	if (!(*pScrn->SwitchMode)(SWITCH_MODE_ARGS(pScrn, pMode)))
1583		return FALSE;
1584	(*pScrn->AdjustFrame)(ADJUST_FRAME_ARGS(pScrn, frameX0, frameY0));
1585
1586	return TRUE;
1587}
1588
1589static void
1590WsfbDGASetViewport(ScrnInfoPtr pScrn, int x, int y, int flags)
1591{
1592	(*pScrn->AdjustFrame)(ADJUST_FRAME_ARGS(pScrn, x, y));
1593}
1594
1595static int
1596WsfbDGAGetViewport(ScrnInfoPtr pScrn)
1597{
1598	return (0);
1599}
1600
1601static DGAFunctionRec WsfbDGAFunctions =
1602{
1603	WsfbDGAOpenFramebuffer,
1604	NULL,       /* CloseFramebuffer */
1605	WsfbDGASetMode,
1606	WsfbDGASetViewport,
1607	WsfbDGAGetViewport,
1608	NULL,       /* Sync */
1609	NULL,       /* FillRect */
1610	NULL,       /* BlitRect */
1611	NULL,       /* BlitTransRect */
1612};
1613
1614static void
1615WsfbDGAAddModes(ScrnInfoPtr pScrn)
1616{
1617	WsfbPtr fPtr = WSFBPTR(pScrn);
1618	DisplayModePtr pMode = pScrn->modes;
1619	DGAModePtr pDGAMode;
1620
1621	do {
1622		pDGAMode = realloc(fPtr->pDGAMode,
1623				    (fPtr->nDGAMode + 1) * sizeof(DGAModeRec));
1624		if (!pDGAMode)
1625			break;
1626
1627		fPtr->pDGAMode = pDGAMode;
1628		pDGAMode += fPtr->nDGAMode;
1629		(void)memset(pDGAMode, 0, sizeof(DGAModeRec));
1630
1631		++fPtr->nDGAMode;
1632		pDGAMode->mode = pMode;
1633		pDGAMode->flags = DGA_CONCURRENT_ACCESS | DGA_PIXMAP_AVAILABLE;
1634		pDGAMode->byteOrder = pScrn->imageByteOrder;
1635		pDGAMode->depth = pScrn->depth;
1636		pDGAMode->bitsPerPixel = pScrn->bitsPerPixel;
1637		pDGAMode->red_mask = pScrn->mask.red;
1638		pDGAMode->green_mask = pScrn->mask.green;
1639		pDGAMode->blue_mask = pScrn->mask.blue;
1640		pDGAMode->visualClass = pScrn->bitsPerPixel > 8 ?
1641			TrueColor : PseudoColor;
1642		pDGAMode->xViewportStep = 1;
1643		pDGAMode->yViewportStep = 1;
1644		pDGAMode->viewportWidth = pMode->HDisplay;
1645		pDGAMode->viewportHeight = pMode->VDisplay;
1646
1647		if (fPtr->fbi.fbi_stride)
1648			pDGAMode->bytesPerScanline = fPtr->fbi.fbi_stride;
1649		else {
1650			ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES,
1651			      &fPtr->fbi.fbi_stride);
1652			pDGAMode->bytesPerScanline = fPtr->fbi.fbi_stride;
1653		}
1654
1655		pDGAMode->imageWidth = pMode->HDisplay;
1656		pDGAMode->imageHeight =  pMode->VDisplay;
1657		pDGAMode->pixmapWidth = pDGAMode->imageWidth;
1658		pDGAMode->pixmapHeight = pDGAMode->imageHeight;
1659		pDGAMode->maxViewportX = pScrn->virtualX -
1660			pDGAMode->viewportWidth;
1661		pDGAMode->maxViewportY = pScrn->virtualY -
1662			pDGAMode->viewportHeight;
1663
1664		pDGAMode->address = fPtr->fbstart;
1665
1666		pMode = pMode->next;
1667	} while (pMode != pScrn->modes);
1668}
1669
1670static Bool
1671WsfbDGAInit(ScrnInfoPtr pScrn, ScreenPtr pScreen)
1672{
1673	WsfbPtr fPtr = WSFBPTR(pScrn);
1674
1675	if (pScrn->depth < 8)
1676		return FALSE;
1677
1678	if (!fPtr->nDGAMode)
1679		WsfbDGAAddModes(pScrn);
1680
1681	return (DGAInit(pScreen, &WsfbDGAFunctions,
1682			fPtr->pDGAMode, fPtr->nDGAMode));
1683}
1684#endif
1685
1686static Bool
1687WsfbDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op,
1688    pointer ptr)
1689{
1690	xorgHWFlags *flag;
1691
1692	switch (op) {
1693	case GET_REQUIRED_HW_INTERFACES:
1694		flag = (CARD32*)ptr;
1695		(*flag) = 0;
1696		return TRUE;
1697	default:
1698		return FALSE;
1699	}
1700}
1701
1702static inline void
1703memcpy32sw(void *dest, void *src, int len)
1704{
1705	uint32_t *d = dest, *s = src;
1706
1707#if DEBUG
1708	if ((((long)dest & 3) + ((long)src & 3) + (len & 3)) != 0) {
1709		xf86Msg(X_ERROR, "unaligned %s\n", __func__);
1710		return;
1711	}
1712#endif
1713	while (len > 0) {
1714		*d = bswap32(*s);
1715		d++;
1716		s++;
1717		len -= 4;
1718	}
1719}
1720
1721/* adapted from miext/shadow/shpacked.c::shadowUpdatePacked() */
1722void
1723WsfbShadowUpdateSwap32(ScreenPtr pScreen, shadowBufPtr pBuf)
1724{
1725    RegionPtr	damage = DamageRegion (pBuf->pDamage);
1726    PixmapPtr	pShadow = pBuf->pPixmap;
1727    int		nbox = RegionNumRects (damage);
1728    BoxPtr	pbox = RegionRects (damage);
1729    FbBits	*shaBase, *shaLine, *sha;
1730    FbStride	shaStride;
1731    int		scrBase, scrLine, scr;
1732    int		shaBpp;
1733    int		shaXoff, shaYoff; /* XXX assumed to be zero */
1734    int		x, y, w, h, width;
1735    int         i;
1736    FbBits	*winBase = NULL, *win;
1737    CARD32      winSize;
1738
1739    fbGetDrawable (&pShadow->drawable, shaBase, shaStride, shaBpp, shaXoff, shaYoff);
1740    while (nbox--)
1741    {
1742	x = pbox->x1 * shaBpp;
1743	y = pbox->y1;
1744	w = (pbox->x2 - pbox->x1) * shaBpp;
1745	h = pbox->y2 - pbox->y1;
1746
1747	scrLine = (x >> FB_SHIFT);
1748	shaLine = shaBase + y * shaStride + (x >> FB_SHIFT);
1749
1750	x &= FB_MASK;
1751	w = (w + x + FB_MASK) >> FB_SHIFT;
1752
1753	while (h--)
1754	{
1755	    winSize = 0;
1756	    scrBase = 0;
1757	    width = w;
1758	    scr = scrLine;
1759	    sha = shaLine;
1760	    while (width) {
1761		/* how much remains in this window */
1762		i = scrBase + winSize - scr;
1763		if (i <= 0 || scr < scrBase)
1764		{
1765		    winBase = (FbBits *) (*pBuf->window) (pScreen,
1766							  y,
1767							  scr * sizeof (FbBits),
1768							  SHADOW_WINDOW_WRITE,
1769							  &winSize,
1770							  pBuf->closure);
1771		    if(!winBase)
1772			return;
1773		    scrBase = scr;
1774		    winSize /= sizeof (FbBits);
1775		    i = winSize;
1776		}
1777		win = winBase + (scr - scrBase);
1778		if (i > width)
1779		    i = width;
1780		width -= i;
1781		scr += i;
1782		memcpy32sw(win, sha, i * sizeof(FbBits));
1783		sha += i;
1784	    }
1785	    shaLine += shaStride;
1786	    y++;
1787	}
1788	pbox++;
1789    }
1790}
1791
1792void
1793WsfbShadowUpdateSplit(ScreenPtr pScreen, shadowBufPtr pBuf)
1794{
1795    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1796    WsfbPtr 	fPtr = WSFBPTR(pScrn);
1797    RegionPtr	damage = DamageRegion (pBuf->pDamage);
1798    PixmapPtr	pShadow = pBuf->pPixmap;
1799    int		nbox = RegionNumRects (damage);
1800    BoxPtr	pbox = RegionRects (damage);
1801    FbBits	*shaBase, *shaLine, *sha;
1802    FbStride	shaStride;
1803    int		scrBase, scrLine, scr;
1804    int		shaBpp;
1805    int		shaXoff, shaYoff; /* XXX assumed to be zero */
1806    int		x, y, w, h, width;
1807    int         i;
1808    FbBits	*winBase = NULL, *win, *win2;
1809    unsigned long split = fPtr->fbi.fbi_fbsize / 2;
1810    CARD32      winSize;
1811
1812    fbGetDrawable (&pShadow->drawable, shaBase, shaStride, shaBpp, shaXoff, shaYoff);
1813    while (nbox--)
1814    {
1815	x = pbox->x1 * shaBpp;
1816	y = pbox->y1;
1817	w = (pbox->x2 - pbox->x1) * shaBpp;
1818	h = pbox->y2 - pbox->y1;
1819
1820	scrLine = (x >> FB_SHIFT);
1821	shaLine = shaBase + y * shaStride + (x >> FB_SHIFT);
1822
1823	x &= FB_MASK;
1824	w = (w + x + FB_MASK) >> FB_SHIFT;
1825
1826	while (h--)
1827	{
1828	    winSize = 0;
1829	    scrBase = 0;
1830	    width = w;
1831	    scr = scrLine;
1832	    sha = shaLine;
1833	    while (width) {
1834		/* how much remains in this window */
1835		i = scrBase + winSize - scr;
1836		if (i <= 0 || scr < scrBase)
1837		{
1838		    winBase = (FbBits *) (*pBuf->window) (pScreen,
1839							  y,
1840							  scr * sizeof (FbBits),
1841							  SHADOW_WINDOW_WRITE,
1842							  &winSize,
1843							  pBuf->closure);
1844		    if(!winBase)
1845			return;
1846		    scrBase = scr;
1847		    winSize /= sizeof (FbBits);
1848		    i = winSize;
1849		}
1850		win = winBase + (scr - scrBase);
1851		win2 = (FbBits *)(split + (unsigned long)win);
1852		if (i > width)
1853		    i = width;
1854		width -= i;
1855		scr += i;
1856		memcpy(win, sha, i * sizeof(FbBits));
1857		memcpy(win2, sha, i * sizeof(FbBits));
1858		sha += i;
1859	    }
1860	    shaLine += shaStride;
1861	    y++;
1862	}
1863	pbox++;
1864    }
1865}
1866