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