wsfb_driver.c revision 3306a6a4
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#if defined(__NetBSD__) && defined(WSDISPLAY_TYPE_AMIGACC)
581	if (wstype == WSDISPLAY_TYPE_AMIGACC)
582	{
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			default_depth = 1;
594			bitsperpixel = 1;
595		}
596	}
597#endif
598
599	if (!xf86SetDepthBpp(pScrn, default_depth, default_depth,
600		bitsperpixel,
601		bitsperpixel >= 24 ? Support24bppFb|Support32bppFb : 0))
602		return FALSE;
603
604	/* Check consistency. */
605	if (pScrn->bitsPerPixel != bitsperpixel) {
606		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
607		    "specified depth (%d) or bpp (%d) doesn't match "
608		    "framebuffer depth (%d)\n", pScrn->depth,
609		    pScrn->bitsPerPixel, bitsperpixel);
610		return FALSE;
611	}
612	xf86PrintDepthBpp(pScrn);
613
614	/* Get the depth24 pixmap format. */
615	if (pScrn->depth == 24 && pix24bpp == 0)
616		pix24bpp = xf86GetBppFromDepth(pScrn, 24);
617
618	/* Handle options. */
619	xf86CollectOptions(pScrn, NULL);
620	fPtr->Options = (OptionInfoRec *)malloc(sizeof(WsfbOptions));
621	if (fPtr->Options == NULL)
622		return FALSE;
623	memcpy(fPtr->Options, WsfbOptions, sizeof(WsfbOptions));
624	xf86ProcessOptions(pScrn->scrnIndex, fPtr->pEnt->device->options,
625			   fPtr->Options);
626
627	/* Use shadow framebuffer by default, on depth >= 8 */
628	xf86Msg(X_INFO, "fbi_flags: %x\n", fPtr->fbi.fbi_flags);
629	if ((pScrn->depth >= 8) &&
630	   ((fPtr->fbi.fbi_flags & WSFB_VRAM_IS_RAM) == 0)) {
631		fPtr->shadowFB = xf86ReturnOptValBool(fPtr->Options,
632						      OPTION_SHADOW_FB, TRUE);
633	} else
634		if (xf86ReturnOptValBool(fPtr->Options,
635					 OPTION_SHADOW_FB, FALSE)) {
636			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
637				   "Shadow FB option ignored on depth < 8\n");
638		}
639	if (fPtr->fbi.fbi_flags & WSFB_VRAM_IS_SPLIT) {
640		if (!fPtr->shadowFB) {
641			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
642				   "Shadow FB forced on for split framebuffer\n");
643			fPtr->shadowFB = TRUE;
644		}
645	}
646	/* Rotation */
647	fPtr->rotate = WSFB_ROTATE_NONE;
648	if ((s = xf86GetOptValString(fPtr->Options, OPTION_ROTATE))) {
649		if (pScrn->depth >= 8) {
650			if (!xf86NameCmp(s, "CW")) {
651				fPtr->shadowFB = TRUE;
652				fPtr->rotate = WSFB_ROTATE_CW;
653				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
654				    "Rotating screen clockwise\n");
655			} else if (!xf86NameCmp(s, "CCW")) {
656				fPtr->shadowFB = TRUE;
657				fPtr->rotate = WSFB_ROTATE_CCW;
658				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
659				    "Rotating screen counter clockwise\n");
660			} else if (!xf86NameCmp(s, "UD")) {
661				fPtr->shadowFB = TRUE;
662				fPtr->rotate = WSFB_ROTATE_UD;
663				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
664				    "Rotating screen upside down\n");
665			} else {
666				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
667				    "\"%s\" is not a valid value for Option "
668				    "\"Rotate\"\n", s);
669				xf86DrvMsg(pScrn->scrnIndex, X_INFO,
670				    "Valid options are \"CW\", \"CCW\","
671				    " or \"UD\"\n");
672			}
673		} else {
674			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
675			    "Option \"Rotate\" ignored on depth < 8\n");
676		}
677	}
678
679
680	fPtr->useSwap32 = FALSE;
681	/* Color weight */
682	if (fPtr->fbi.fbi_pixeltype == WSFB_RGB) {
683		rgb zeros = { 0, 0, 0 }, masks;
684
685		if (fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_size > 0) {
686			uint32_t msk;
687
688			/*
689			 * see if we need to byte-swap pixels
690			 * XXX this requires a shadow FB and is incompatible
691			 * (for now ) with rotation
692			 */
693			if ((fPtr->fbi.fbi_bitsperpixel == 32) &&
694			    (fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset == 24) &&
695			    (fPtr->rotate == WSFB_ROTATE_NONE) &&
696			    (fPtr->shadowFB == TRUE)) {
697			    	/*
698			    	 * looks like BGRA - set the swap flag and flip
699			    	 * the offsets
700			    	 */
701			    	xf86Msg(X_INFO, "endian-flipped RGB framebuffer "
702			    			"detected, using WsfbShadowUpdateSwap32()\n");
703			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset = 0;
704			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_offset = 8;
705			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_offset = 16;
706			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.alpha_offset = 24;
707				fPtr->useSwap32 = TRUE;
708			}
709
710			msk = 0xffffffff;
711			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_size;
712			msk = ~msk;
713			masks.red = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_offset;
714
715			msk = 0xffffffff;
716			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_size;
717			msk = ~msk;
718			masks.green = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_offset;
719
720			msk = 0xffffffff;
721			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_size;
722			msk = ~msk;
723			masks.blue = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset;
724			xf86Msg(X_INFO, "masks generated: %08lx %08lx %08lx\n",
725			    (unsigned long)masks.red,
726			    (unsigned long)masks.green,
727			    (unsigned long)masks.blue);
728		} else {
729			masks.red = 0;
730			masks.green = 0;
731			masks.blue = 0;
732		}
733
734		if (!xf86SetWeight(pScrn, zeros, masks))
735			return FALSE;
736	}
737
738	/* Visual init */
739	if (!xf86SetDefaultVisual(pScrn, -1))
740		return FALSE;
741
742	/* We don't currently support DirectColor at > 8bpp . */
743	if (pScrn->depth > 8 && pScrn->defaultVisual != TrueColor) {
744		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Given default visual"
745			   " (%s) is not supported at depth %d\n",
746			   xf86GetVisualName(pScrn->defaultVisual),
747			   pScrn->depth);
748		return FALSE;
749	}
750
751	xf86SetGamma(pScrn,zeros);
752
753	pScrn->progClock = TRUE;
754	pScrn->rgbBits   = (pScrn->depth >= 8) ? 8 : pScrn->depth;
755	pScrn->chipset   = "wsfb";
756	pScrn->videoRam  = fPtr->fbi.fbi_fbsize;
757
758	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Vidmem: %dk\n",
759		   pScrn->videoRam/1024);
760
761	/* Fake video mode struct. */
762	mode = (DisplayModePtr)malloc(sizeof(DisplayModeRec));
763	mode->prev = mode;
764	mode->next = mode;
765	mode->name = "wsfb current mode";
766	mode->status = MODE_OK;
767	mode->type = M_T_BUILTIN;
768	mode->Clock = 0;
769	mode->HDisplay = fPtr->fbi.fbi_width;
770	mode->HSyncStart = 0;
771	mode->HSyncEnd = 0;
772	mode->HTotal = 0;
773	mode->HSkew = 0;
774	mode->VDisplay = fPtr->fbi.fbi_height;
775	mode->VSyncStart = 0;
776	mode->VSyncEnd = 0;
777	mode->VTotal = 0;
778	mode->VScan = 0;
779	mode->Flags = 0;
780	if (pScrn->modes != NULL) {
781		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
782		   "Ignoring mode specification from screen section\n");
783	}
784	pScrn->currentMode = pScrn->modes = mode;
785	pScrn->virtualX = fPtr->fbi.fbi_width;
786	pScrn->virtualY = fPtr->fbi.fbi_height;
787	pScrn->displayWidth = pScrn->virtualX;
788
789	/* Set the display resolution. */
790	xf86SetDpi(pScrn, 0, 0);
791
792	from = X_DEFAULT;
793	fPtr->HWCursor = TRUE;
794	if (xf86GetOptValBool(fPtr->Options, OPTION_HW_CURSOR, &fPtr->HWCursor))
795		from = X_CONFIG;
796	if (xf86ReturnOptValBool(fPtr->Options, OPTION_SW_CURSOR, FALSE)) {
797		from = X_CONFIG;
798		fPtr->HWCursor = FALSE;
799	}
800	xf86DrvMsg(pScrn->scrnIndex, from, "Using %s cursor\n",
801		fPtr->HWCursor ? "HW" : "SW");
802
803	/* Load bpp-specific modules. */
804	switch(pScrn->bitsPerPixel) {
805	case 1:
806	case 4:
807	default:
808		mod = "fb";
809		break;
810	}
811
812
813	/* Load shadow if needed. */
814	if (fPtr->shadowFB) {
815		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
816			   "Using \"Shadow Framebuffer\"\n");
817		if (xf86LoadSubModule(pScrn, "shadow") == NULL) {
818			WsfbFreeRec(pScrn);
819			return FALSE;
820		}
821	}
822
823	if (mod && xf86LoadSubModule(pScrn, mod) == NULL) {
824		WsfbFreeRec(pScrn);
825		return FALSE;
826	}
827
828	if (xf86LoadSubModule(pScrn, "ramdac") == NULL) {
829		WsfbFreeRec(pScrn);
830		return FALSE;
831        }
832
833	if (mod) {
834		if (reqSym) {
835			xf86LoaderReqSymbols(reqSym, NULL);
836		} else {
837			xf86LoaderReqSymLists(fbSymbols, NULL);
838		}
839	}
840	TRACE_EXIT("PreInit");
841	return TRUE;
842}
843
844static Bool
845WsfbCreateScreenResources(ScreenPtr pScreen)
846{
847	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
848	WsfbPtr fPtr = WSFBPTR(pScrn);
849	PixmapPtr pPixmap;
850	Bool ret;
851	void (*shadowproc)(ScreenPtr, shadowBufPtr);
852	ShadowWindowProc windowproc = WsfbWindowLinear;
853
854	pScreen->CreateScreenResources = fPtr->CreateScreenResources;
855	ret = pScreen->CreateScreenResources(pScreen);
856	pScreen->CreateScreenResources = WsfbCreateScreenResources;
857
858	if (!ret)
859		return FALSE;
860
861	pPixmap = pScreen->GetScreenPixmap(pScreen);
862	if (fPtr->fbi.fbi_flags & WSFB_VRAM_IS_SPLIT) {
863		shadowproc = WsfbShadowUpdateSplit;
864	} else if (fPtr->useSwap32) {
865		shadowproc = WsfbShadowUpdateSwap32;
866	} else if (fPtr->rotate) {
867		shadowproc = shadowUpdateRotatePacked;
868	} else if (fPtr->planarAfb) {
869		shadowproc = shadowUpdateAfb8;
870		windowproc = WsfbWindowAfb;
871	} else
872		shadowproc = shadowUpdatePacked;
873
874	if (!shadowAdd(pScreen, pPixmap, shadowproc,
875		windowproc, fPtr->rotate, NULL)) {
876		return FALSE;
877	}
878	return TRUE;
879}
880
881
882static Bool
883WsfbShadowInit(ScreenPtr pScreen)
884{
885	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
886	WsfbPtr fPtr = WSFBPTR(pScrn);
887
888	if (!shadowSetup(pScreen))
889		return FALSE;
890	fPtr->CreateScreenResources = pScreen->CreateScreenResources;
891	pScreen->CreateScreenResources = WsfbCreateScreenResources;
892
893	return TRUE;
894}
895
896static Bool
897WsfbScreenInit(SCREEN_INIT_ARGS_DECL)
898{
899	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
900	WsfbPtr fPtr = WSFBPTR(pScrn);
901	VisualPtr visual;
902	int ret, flags, ncolors;
903	int wsmode = WSDISPLAYIO_MODE_DUMBFB;
904	int wstype;
905	int width;
906	size_t len;
907
908	TRACE_ENTER("WsfbScreenInit");
909#if DEBUG
910	ErrorF("\tbitsPerPixel=%d, depth=%d, defaultVisual=%s\n"
911	       "\tmask: %x,%x,%x, offset: %u,%u,%u\n",
912	       pScrn->bitsPerPixel,
913	       pScrn->depth,
914	       xf86GetVisualName(pScrn->defaultVisual),
915	       pScrn->mask.red,pScrn->mask.green,pScrn->mask.blue,
916	       pScrn->offset.red,pScrn->offset.green,pScrn->offset.blue);
917#endif
918	switch (fPtr->fbi.fbi_bitsperpixel) {
919	case 1:
920	case 4:
921	case 8:
922		len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
923		break;
924	case 15:
925	case 16:
926		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
927			xf86Msg(X_ERROR, "Bogus stride == width in 16bit colour\n");
928			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * sizeof(short);
929		} else {
930			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
931		}
932		break;
933	case 24:
934		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
935			xf86Msg(X_ERROR, "Bogus stride == width in 24bit colour\n");
936			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * 3;
937		} else {
938			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
939		}
940		break;
941	case 32:
942		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
943			xf86Msg(X_ERROR, "Bogus stride == width in 32bit colour\n");
944			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * sizeof(int);
945		} else {
946			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
947		}
948		break;
949	default:
950		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
951			   "unsupported depth %d\n", fPtr->fbi.fbi_bitsperpixel);
952		return FALSE;
953	}
954	/* Switch to graphics mode - required before mmap. */
955	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &wsmode) == -1) {
956		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
957			   "ioctl WSDISPLAYIO_SMODE: %s\n",
958			   strerror(errno));
959		return FALSE;
960	}
961	/* Get wsdisplay type to handle quirks */
962	if (ioctl(fPtr->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
963		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
964			   "ioctl WSDISPLAY_GTYPE: %s\n",
965			   strerror(errno));
966		return FALSE;
967	}
968	len = max(len, fPtr->fbi.fbi_fbsize);
969	fPtr->fbmem = wsfb_mmap(len + fPtr->fbi.fbi_fboffset, 0, fPtr->fd);
970
971	if (fPtr->fbmem == NULL) {
972		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
973			   "wsfb_mmap: %s\n", strerror(errno));
974		return FALSE;
975	}
976	fPtr->fbmem_len = len;
977
978	WsfbSave(pScrn);
979	pScrn->vtSema = TRUE;
980
981	/* MI layer */
982	miClearVisualTypes();
983	if (pScrn->bitsPerPixel > 8) {
984		if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
985				      pScrn->rgbBits, TrueColor))
986			return FALSE;
987	} else {
988		if (!miSetVisualTypes(pScrn->depth,
989				      miGetDefaultVisualMask(pScrn->depth),
990				      pScrn->rgbBits, pScrn->defaultVisual))
991			return FALSE;
992	}
993	if (!miSetPixmapDepths())
994		return FALSE;
995
996	if (fPtr->rotate == WSFB_ROTATE_CW
997	    || fPtr->rotate == WSFB_ROTATE_CCW) {
998		int tmp = pScrn->virtualX;
999		pScrn->virtualX = pScrn->displayWidth = pScrn->virtualY;
1000		pScrn->virtualY = tmp;
1001	}
1002	if (fPtr->rotate && !fPtr->PointerMoved) {
1003		fPtr->PointerMoved = pScrn->PointerMoved;
1004		pScrn->PointerMoved = WsfbPointerMoved;
1005	}
1006
1007	fPtr->fbstart = fPtr->fbmem + fPtr->fbi.fbi_fboffset;
1008
1009	if (fPtr->shadowFB) {
1010		if (fPtr->rotate) {
1011			/*
1012			 * Note Rotate and Shadow FB options are valid
1013			 * only on depth >= 8.
1014			 */
1015			len = pScrn->virtualX * pScrn->virtualY *
1016			    (pScrn->bitsPerPixel >> 3);
1017		} else if (fPtr->planarAfb) {
1018			/* always 8bpp */
1019			len = pScrn->virtualX * pScrn->virtualY;
1020		} else {
1021			len = fPtr->fbi.fbi_stride * pScrn->virtualY;
1022		}
1023		fPtr->shadow = calloc(1, len);
1024
1025		if (!fPtr->shadow) {
1026			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1027			    "Failed to allocate shadow framebuffer\n");
1028			return FALSE;
1029		}
1030	}
1031
1032	/*
1033	 * fbScreenInit() seems to require "pixel width of frame buffer"
1034	 * but it is actually "stride in pixel" of frame buffer,
1035	 * per xorg/xserver/tree/fb/fbscreen.c.
1036	 */
1037	if (fPtr->rotate) {
1038		width = pScrn->displayWidth;
1039	} else if (fPtr->planarAfb) {
1040		width = pScrn->displayWidth;
1041	} else {
1042		if (pScrn->bitsPerPixel > 8) {
1043			width =
1044			    fPtr->fbi.fbi_stride / (pScrn->bitsPerPixel >> 3);
1045		} else {
1046			width =
1047			    fPtr->fbi.fbi_stride * (8 / pScrn->bitsPerPixel);
1048		}
1049	}
1050	switch (pScrn->bitsPerPixel) {
1051	case 1:
1052		ret = fbScreenInit(pScreen,
1053		    fPtr->fbstart,
1054		    pScrn->virtualX, pScrn->virtualY,
1055		    pScrn->xDpi, pScrn->yDpi,
1056		    width, pScrn->bitsPerPixel);
1057		break;
1058	case 4:
1059	case 8:
1060	case 16:
1061	case 24:
1062	case 32:
1063		ret = fbScreenInit(pScreen,
1064		    fPtr->shadowFB ? fPtr->shadow : fPtr->fbstart,
1065		    pScrn->virtualX, pScrn->virtualY,
1066		    pScrn->xDpi, pScrn->yDpi,
1067		    width,
1068		    pScrn->bitsPerPixel);
1069		break;
1070	default:
1071		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1072			   "Unsupported bpp: %d\n", pScrn->bitsPerPixel);
1073		return FALSE;
1074	} /* case */
1075
1076	if (!ret)
1077		return FALSE;
1078
1079	if (pScrn->bitsPerPixel > 8) {
1080		/* Fixup RGB ordering. */
1081		visual = pScreen->visuals + pScreen->numVisuals;
1082		while (--visual >= pScreen->visuals) {
1083			if ((visual->class | DynamicClass) == DirectColor) {
1084				visual->offsetRed   = pScrn->offset.red;
1085				visual->offsetGreen = pScrn->offset.green;
1086				visual->offsetBlue  = pScrn->offset.blue;
1087				visual->redMask     = pScrn->mask.red;
1088				visual->greenMask   = pScrn->mask.green;
1089				visual->blueMask    = pScrn->mask.blue;
1090			}
1091		}
1092	}
1093
1094	if (pScrn->bitsPerPixel >= 8) {
1095		if (!fbPictureInit(pScreen, NULL, 0))
1096			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
1097				   "RENDER extension initialisation failed.\n");
1098	}
1099	if (fPtr->shadowFB && !WsfbShadowInit(pScreen)) {
1100		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1101		    "shadow framebuffer initialization failed\n");
1102		return FALSE;
1103	}
1104
1105#ifdef XFreeXDGA
1106	if (!fPtr->rotate)
1107		WsfbDGAInit(pScrn, pScreen);
1108	else
1109		xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Rotated display, "
1110		    "disabling DGA\n");
1111#endif
1112	if (fPtr->rotate) {
1113		xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Enabling Driver Rotation, "
1114		    "disabling RandR\n");
1115#if 0
1116		xf86DisableRandR();
1117#endif
1118		if (pScrn->bitsPerPixel == 24)
1119			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
1120			    "Rotation might be broken in 24 bpp\n");
1121	}
1122
1123	xf86SetBlackWhitePixels(pScreen);
1124	xf86SetBackingStore(pScreen);
1125
1126	/* Software cursor. */
1127	miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
1128
1129	/* check for hardware cursor support */
1130	if (fPtr->HWCursor)
1131		WsfbSetupCursor(pScreen);
1132
1133	/*
1134	 * Colormap
1135	 *
1136	 * Note that, even on less than 8 bit depth frame buffers, we
1137	 * expect the colormap to be programmable with 8 bit values.
1138	 * As of now, this is indeed the case on all OpenBSD supported
1139	 * graphics hardware.
1140	 */
1141	if (!miCreateDefColormap(pScreen))
1142		return FALSE;
1143	flags = CMAP_RELOAD_ON_MODE_SWITCH;
1144
1145	ncolors = 0;
1146	if (fPtr->fbi.fbi_pixeltype == WSFB_CI) {
1147		ncolors = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1148	}
1149
1150	/* On StaticGray visuals, fake a 256 entries colormap. */
1151	if (ncolors == 0)
1152		ncolors = 256;
1153
1154	if(!xf86HandleColormaps(pScreen, ncolors, 8, WsfbLoadPalette,
1155				NULL, flags))
1156		return FALSE;
1157
1158#if defined(__NetBSD__) && defined(WSDISPLAY_TYPE_LUNA)
1159	if (wstype == WSDISPLAY_TYPE_LUNA) {
1160		ncolors = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1161		if (ncolors > 0) {
1162			/*
1163			 * Override palette to use 4bpp/8bpp framebuffers as
1164			 * monochrome server by using only the first plane.
1165			 * See also comment in WsfbPreInit().
1166			 */
1167			struct wsdisplay_cmap cmap;
1168			uint8_t r[256], g[256], b[256];
1169			int p;
1170
1171			for (p = 0; p < ncolors; p++)
1172				r[p] = g[p] = b[p] = (p & 1) ? 0xff : 0;
1173			cmap.index = 0;
1174			cmap.count = ncolors;
1175			cmap.red   = r;
1176			cmap.green = g;
1177			cmap.blue  = b;
1178			if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP, &cmap) == -1) {
1179				xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1180				   "ioctl WSDISPLAYIO_PUTCMAP: %s\n",
1181				   strerror(errno));
1182			}
1183		}
1184	}
1185#endif
1186
1187	pScreen->SaveScreen = WsfbSaveScreen;
1188
1189#ifdef XvExtension
1190	{
1191		XF86VideoAdaptorPtr *ptr;
1192
1193		int n = xf86XVListGenericAdaptors(pScrn,&ptr);
1194		if (n) {
1195			xf86XVScreenInit(pScreen,ptr,n);
1196		}
1197	}
1198#endif
1199
1200	/* Wrap the current CloseScreen function. */
1201	fPtr->CloseScreen = pScreen->CloseScreen;
1202	pScreen->CloseScreen = WsfbCloseScreen;
1203
1204	TRACE_EXIT("WsfbScreenInit");
1205	return TRUE;
1206}
1207
1208static Bool
1209WsfbCloseScreen(CLOSE_SCREEN_ARGS_DECL)
1210{
1211	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1212	PixmapPtr pPixmap;
1213	WsfbPtr fPtr = WSFBPTR(pScrn);
1214
1215
1216	TRACE_ENTER("WsfbCloseScreen");
1217
1218	pPixmap = pScreen->GetScreenPixmap(pScreen);
1219	if (fPtr->shadowFB)
1220		shadowRemove(pScreen, pPixmap);
1221
1222	if (pScrn->vtSema) {
1223		WsfbRestore(pScrn);
1224		if (munmap(fPtr->fbmem, fPtr->fbmem_len + fPtr->fbi.fbi_fboffset) == -1) {
1225			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1226				   "munmap: %s\n", strerror(errno));
1227		}
1228
1229		fPtr->fbmem = NULL;
1230	}
1231#ifdef XFreeXDGA
1232	if (fPtr->pDGAMode) {
1233		free(fPtr->pDGAMode);
1234		fPtr->pDGAMode = NULL;
1235		fPtr->nDGAMode = 0;
1236	}
1237#endif
1238	pScrn->vtSema = FALSE;
1239
1240	/* Unwrap CloseScreen. */
1241	pScreen->CloseScreen = fPtr->CloseScreen;
1242	TRACE_EXIT("WsfbCloseScreen");
1243	return (*pScreen->CloseScreen)(CLOSE_SCREEN_ARGS);
1244}
1245
1246static void *
1247WsfbWindowLinear(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
1248		CARD32 *size, void *closure)
1249{
1250	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1251	WsfbPtr fPtr = WSFBPTR(pScrn);
1252
1253	/*
1254	 * XXX
1255	 * This should never happen. Is it really necessary?
1256	 */
1257	if (fPtr->fbi.fbi_stride)
1258		*size = fPtr->fbi.fbi_stride;
1259	else {
1260		if (ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES, size) == -1)
1261			return NULL;
1262		fPtr->fbi.fbi_stride = *size;
1263	}
1264	return ((CARD8 *)fPtr->fbstart + row * fPtr->fbi.fbi_stride + offset);
1265}
1266
1267/**
1268 * For use with shadowUpdateAfb8
1269 *
1270 * For video memory layout with non-interleaved bitplanes.
1271 */
1272static void *
1273WsfbWindowAfb(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
1274		CARD32 *size, void *closure)
1275{
1276	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1277	WsfbPtr fPtr = WSFBPTR(pScrn);
1278
1279	/* size is offset from start of bitplane to next bitplane */
1280	*size = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
1281	return ((CARD8 *)fPtr->fbstart + row * fPtr->fbi.fbi_stride + offset);
1282}
1283
1284static void
1285WsfbPointerMoved(SCRN_ARG_TYPE arg, int x, int y)
1286{
1287    SCRN_INFO_PTR(arg);
1288    WsfbPtr fPtr = WSFBPTR(pScrn);
1289    int newX, newY;
1290
1291    switch (fPtr->rotate)
1292    {
1293    case WSFB_ROTATE_CW:
1294	/* 90 degrees CW rotation. */
1295	newX = pScrn->pScreen->height - y - 1;
1296	newY = x;
1297	break;
1298
1299    case WSFB_ROTATE_CCW:
1300	/* 90 degrees CCW rotation. */
1301	newX = y;
1302	newY = pScrn->pScreen->width - x - 1;
1303	break;
1304
1305    case WSFB_ROTATE_UD:
1306	/* 180 degrees UD rotation. */
1307	newX = pScrn->pScreen->width - x - 1;
1308	newY = pScrn->pScreen->height - y - 1;
1309	break;
1310
1311    default:
1312	/* No rotation. */
1313	newX = x;
1314	newY = y;
1315	break;
1316    }
1317
1318    /* Pass adjusted pointer coordinates to wrapped PointerMoved function. */
1319    (*fPtr->PointerMoved)(arg, newX, newY);
1320}
1321
1322static Bool
1323WsfbEnterVT(VT_FUNC_ARGS_DECL)
1324{
1325	SCRN_INFO_PTR(arg);
1326	WsfbPtr fPtr = WSFBPTR(pScrn);
1327	int mode;
1328
1329	TRACE_ENTER("EnterVT");
1330	pScrn->vtSema = TRUE;
1331
1332	/* Restore the graphics mode. */
1333	mode = WSDISPLAYIO_MODE_DUMBFB;
1334	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1335		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1336			   "error setting graphics mode %s\n", strerror(errno));
1337	}
1338
1339	TRACE_EXIT("EnterVT");
1340	return TRUE;
1341}
1342
1343static void
1344WsfbLeaveVT(VT_FUNC_ARGS_DECL)
1345{
1346	SCRN_INFO_PTR(arg);
1347	WsfbPtr fPtr = WSFBPTR(pScrn);
1348	int mode;
1349
1350	TRACE_ENTER("LeaveVT");
1351
1352	/*
1353	 * stuff to do:
1354	 * - turn off hw cursor
1355	 * - restore colour map if WSFB_CI
1356	 * - ioctl(WSDISPLAYIO_MODE_EMUL) to notify the kernel driver that
1357	 *   we're backing off
1358	 */
1359
1360	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
1361	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
1362		/* reset colormap for text mode */
1363		if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP,
1364			  &(fPtr->saved_cmap)) == -1) {
1365			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1366				   "error restoring colormap %s\n",
1367				   strerror(errno));
1368		}
1369	}
1370
1371	/* Restore the text mode. */
1372	mode = WSDISPLAYIO_MODE_EMUL;
1373	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1374		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1375			   "error setting text mode %s\n", strerror(errno));
1376	}
1377
1378	pScrn->vtSema = FALSE;
1379	TRACE_EXIT("LeaveVT");
1380}
1381
1382static Bool
1383WsfbSwitchMode(SWITCH_MODE_ARGS_DECL)
1384{
1385	TRACE_ENTER("SwitchMode");
1386	/* Nothing else to do. */
1387	return TRUE;
1388}
1389
1390static int
1391WsfbValidMode(SCRN_ARG_TYPE arg, DisplayModePtr mode, Bool verbose, int flags)
1392{
1393	TRACE_ENTER("ValidMode");
1394	return MODE_OK;
1395}
1396
1397static void
1398WsfbLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
1399	       LOCO *colors, VisualPtr pVisual)
1400{
1401	WsfbPtr fPtr = WSFBPTR(pScrn);
1402	struct wsdisplay_cmap cmap;
1403	unsigned char red[256],green[256],blue[256];
1404	int i, indexMin=256, indexMax=0;
1405
1406	TRACE_ENTER("LoadPalette");
1407
1408	/* nothing to do if there is no color palette support */
1409	if (fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries == 0)
1410		return;
1411
1412	cmap.count   = 1;
1413	cmap.red   = red;
1414	cmap.green = green;
1415	cmap.blue  = blue;
1416
1417	if (numColors == 1) {
1418		/* Optimisation */
1419		cmap.index = indices[0];
1420		red[0]   = colors[indices[0]].red;
1421		green[0] = colors[indices[0]].green;
1422		blue[0]  = colors[indices[0]].blue;
1423		if (ioctl(fPtr->fd,WSDISPLAYIO_PUTCMAP, &cmap) == -1)
1424			ErrorF("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
1425	} else {
1426		/*
1427		 * Change all colors in 2 ioctls
1428		 * and limit the data to be transfered.
1429		 */
1430		for (i = 0; i < numColors; i++) {
1431			if (indices[i] < indexMin)
1432				indexMin = indices[i];
1433			if (indices[i] > indexMax)
1434				indexMax = indices[i];
1435		}
1436		cmap.index = indexMin;
1437		cmap.count = indexMax - indexMin + 1;
1438		cmap.red = &red[indexMin];
1439		cmap.green = &green[indexMin];
1440		cmap.blue = &blue[indexMin];
1441		/* Get current map. */
1442		if (ioctl(fPtr->fd, WSDISPLAYIO_GETCMAP, &cmap) == -1)
1443			ErrorF("ioctl FBIOGETCMAP: %s\n", strerror(errno));
1444		/* Change the colors that require updating. */
1445		for (i = 0; i < numColors; i++) {
1446			red[indices[i]]   = colors[indices[i]].red;
1447			green[indices[i]] = colors[indices[i]].green;
1448			blue[indices[i]]  = colors[indices[i]].blue;
1449		}
1450		/* Write the colormap back. */
1451		if (ioctl(fPtr->fd,WSDISPLAYIO_PUTCMAP, &cmap) == -1)
1452			ErrorF("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
1453	}
1454	TRACE_EXIT("LoadPalette");
1455}
1456
1457static Bool
1458WsfbSaveScreen(ScreenPtr pScreen, int mode)
1459{
1460	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1461	WsfbPtr fPtr = WSFBPTR(pScrn);
1462	int state;
1463
1464	TRACE_ENTER("SaveScreen");
1465
1466	if (!pScrn->vtSema)
1467		return TRUE;
1468
1469	if (mode != SCREEN_SAVER_FORCER) {
1470		state = xf86IsUnblank(mode)?WSDISPLAYIO_VIDEO_ON:
1471		                            WSDISPLAYIO_VIDEO_OFF;
1472		ioctl(fPtr->fd,
1473		      WSDISPLAYIO_SVIDEO, &state);
1474	}
1475	TRACE_EXIT("SaveScreen");
1476	return TRUE;
1477}
1478
1479
1480static void
1481WsfbSave(ScrnInfoPtr pScrn)
1482{
1483	WsfbPtr fPtr = WSFBPTR(pScrn);
1484
1485	TRACE_ENTER("WsfbSave");
1486
1487	/* nothing to save if we don't run in colour-indexed mode */
1488	if (fPtr->fbi.fbi_pixeltype != WSFB_CI)
1489		return;
1490
1491	/* nothing to do if no color palette support */
1492	if (fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries == 0)
1493		return;
1494
1495	fPtr->saved_cmap.index = 0;
1496	fPtr->saved_cmap.count = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1497	if (ioctl(fPtr->fd, WSDISPLAYIO_GETCMAP,
1498		  &(fPtr->saved_cmap)) == -1) {
1499		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1500			   "error saving colormap %s\n", strerror(errno));
1501	}
1502	TRACE_EXIT("WsfbSave");
1503
1504}
1505
1506static void
1507WsfbRestore(ScrnInfoPtr pScrn)
1508{
1509	WsfbPtr fPtr = WSFBPTR(pScrn);
1510	int mode;
1511
1512	TRACE_ENTER("WsfbRestore");
1513
1514	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
1515	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
1516		/* reset colormap for text mode */
1517		if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP,
1518			  &(fPtr->saved_cmap)) == -1) {
1519			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1520				   "error restoring colormap %s\n",
1521				   strerror(errno));
1522		}
1523	}
1524
1525	/* Clear the screen. */
1526	memset(fPtr->fbstart, 0, fPtr->fbmem_len);
1527
1528	/* Restore the text mode. */
1529	mode = WSDISPLAYIO_MODE_EMUL;
1530	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1531		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1532			   "error setting text mode %s\n", strerror(errno));
1533	}
1534	TRACE_EXIT("WsfbRestore");
1535}
1536
1537#ifdef XFreeXDGA
1538/***********************************************************************
1539 * DGA stuff
1540 ***********************************************************************/
1541
1542static Bool
1543WsfbDGAOpenFramebuffer(ScrnInfoPtr pScrn, char **DeviceName,
1544		       unsigned char **ApertureBase, int *ApertureSize,
1545		       int *ApertureOffset, int *flags)
1546{
1547	*DeviceName = NULL;		/* No special device */
1548	*ApertureBase = (unsigned char *)(pScrn->memPhysBase);
1549	*ApertureSize = pScrn->videoRam;
1550	*ApertureOffset = pScrn->fbOffset;
1551	*flags = 0;
1552
1553	return TRUE;
1554}
1555
1556static Bool
1557WsfbDGASetMode(ScrnInfoPtr pScrn, DGAModePtr pDGAMode)
1558{
1559	DisplayModePtr pMode;
1560	int scrnIdx = pScrn->pScreen->myNum;
1561	int frameX0, frameY0;
1562
1563	if (pDGAMode) {
1564		pMode = pDGAMode->mode;
1565		frameX0 = frameY0 = 0;
1566	} else {
1567		if (!(pMode = pScrn->currentMode))
1568			return TRUE;
1569
1570		frameX0 = pScrn->frameX0;
1571		frameY0 = pScrn->frameY0;
1572	}
1573
1574	if (!(*pScrn->SwitchMode)(SWITCH_MODE_ARGS(pScrn, pMode)))
1575		return FALSE;
1576	(*pScrn->AdjustFrame)(ADJUST_FRAME_ARGS(pScrn, frameX0, frameY0));
1577
1578	return TRUE;
1579}
1580
1581static void
1582WsfbDGASetViewport(ScrnInfoPtr pScrn, int x, int y, int flags)
1583{
1584	(*pScrn->AdjustFrame)(ADJUST_FRAME_ARGS(pScrn, x, y));
1585}
1586
1587static int
1588WsfbDGAGetViewport(ScrnInfoPtr pScrn)
1589{
1590	return (0);
1591}
1592
1593static DGAFunctionRec WsfbDGAFunctions =
1594{
1595	WsfbDGAOpenFramebuffer,
1596	NULL,       /* CloseFramebuffer */
1597	WsfbDGASetMode,
1598	WsfbDGASetViewport,
1599	WsfbDGAGetViewport,
1600	NULL,       /* Sync */
1601	NULL,       /* FillRect */
1602	NULL,       /* BlitRect */
1603	NULL,       /* BlitTransRect */
1604};
1605
1606static void
1607WsfbDGAAddModes(ScrnInfoPtr pScrn)
1608{
1609	WsfbPtr fPtr = WSFBPTR(pScrn);
1610	DisplayModePtr pMode = pScrn->modes;
1611	DGAModePtr pDGAMode;
1612
1613	do {
1614		pDGAMode = realloc(fPtr->pDGAMode,
1615				    (fPtr->nDGAMode + 1) * sizeof(DGAModeRec));
1616		if (!pDGAMode)
1617			break;
1618
1619		fPtr->pDGAMode = pDGAMode;
1620		pDGAMode += fPtr->nDGAMode;
1621		(void)memset(pDGAMode, 0, sizeof(DGAModeRec));
1622
1623		++fPtr->nDGAMode;
1624		pDGAMode->mode = pMode;
1625		pDGAMode->flags = DGA_CONCURRENT_ACCESS | DGA_PIXMAP_AVAILABLE;
1626		pDGAMode->byteOrder = pScrn->imageByteOrder;
1627		pDGAMode->depth = pScrn->depth;
1628		pDGAMode->bitsPerPixel = pScrn->bitsPerPixel;
1629		pDGAMode->red_mask = pScrn->mask.red;
1630		pDGAMode->green_mask = pScrn->mask.green;
1631		pDGAMode->blue_mask = pScrn->mask.blue;
1632		pDGAMode->visualClass = pScrn->bitsPerPixel > 8 ?
1633			TrueColor : PseudoColor;
1634		pDGAMode->xViewportStep = 1;
1635		pDGAMode->yViewportStep = 1;
1636		pDGAMode->viewportWidth = pMode->HDisplay;
1637		pDGAMode->viewportHeight = pMode->VDisplay;
1638
1639		if (fPtr->fbi.fbi_stride)
1640			pDGAMode->bytesPerScanline = fPtr->fbi.fbi_stride;
1641		else {
1642			ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES,
1643			      &fPtr->fbi.fbi_stride);
1644			pDGAMode->bytesPerScanline = fPtr->fbi.fbi_stride;
1645		}
1646
1647		pDGAMode->imageWidth = pMode->HDisplay;
1648		pDGAMode->imageHeight =  pMode->VDisplay;
1649		pDGAMode->pixmapWidth = pDGAMode->imageWidth;
1650		pDGAMode->pixmapHeight = pDGAMode->imageHeight;
1651		pDGAMode->maxViewportX = pScrn->virtualX -
1652			pDGAMode->viewportWidth;
1653		pDGAMode->maxViewportY = pScrn->virtualY -
1654			pDGAMode->viewportHeight;
1655
1656		pDGAMode->address = fPtr->fbstart;
1657
1658		pMode = pMode->next;
1659	} while (pMode != pScrn->modes);
1660}
1661
1662static Bool
1663WsfbDGAInit(ScrnInfoPtr pScrn, ScreenPtr pScreen)
1664{
1665	WsfbPtr fPtr = WSFBPTR(pScrn);
1666
1667	if (pScrn->depth < 8)
1668		return FALSE;
1669
1670	if (!fPtr->nDGAMode)
1671		WsfbDGAAddModes(pScrn);
1672
1673	return (DGAInit(pScreen, &WsfbDGAFunctions,
1674			fPtr->pDGAMode, fPtr->nDGAMode));
1675}
1676#endif
1677
1678static Bool
1679WsfbDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op,
1680    pointer ptr)
1681{
1682	xorgHWFlags *flag;
1683
1684	switch (op) {
1685	case GET_REQUIRED_HW_INTERFACES:
1686		flag = (CARD32*)ptr;
1687		(*flag) = 0;
1688		return TRUE;
1689	default:
1690		return FALSE;
1691	}
1692}
1693
1694static inline void
1695memcpy32sw(void *dest, void *src, int len)
1696{
1697	uint32_t *d = dest, *s = src;
1698
1699#if DEBUG
1700	if ((((long)dest & 3) + ((long)src & 3) + (len & 3)) != 0) {
1701		xf86Msg(X_ERROR, "unaligned %s\n", __func__);
1702		return;
1703	}
1704#endif
1705	while (len > 0) {
1706		*d = bswap32(*s);
1707		d++;
1708		s++;
1709		len -= 4;
1710	}
1711}
1712
1713/* adapted from miext/shadow/shpacked.c::shadowUpdatePacked() */
1714void
1715WsfbShadowUpdateSwap32(ScreenPtr pScreen, shadowBufPtr pBuf)
1716{
1717    RegionPtr	damage = DamageRegion (pBuf->pDamage);
1718    PixmapPtr	pShadow = pBuf->pPixmap;
1719    int		nbox = RegionNumRects (damage);
1720    BoxPtr	pbox = RegionRects (damage);
1721    FbBits	*shaBase, *shaLine, *sha;
1722    FbStride	shaStride;
1723    int		scrBase, scrLine, scr;
1724    int		shaBpp;
1725    int		shaXoff, shaYoff; /* XXX assumed to be zero */
1726    int		x, y, w, h, width;
1727    int         i;
1728    FbBits	*winBase = NULL, *win;
1729    CARD32      winSize;
1730
1731    fbGetDrawable (&pShadow->drawable, shaBase, shaStride, shaBpp, shaXoff, shaYoff);
1732    while (nbox--)
1733    {
1734	x = pbox->x1 * shaBpp;
1735	y = pbox->y1;
1736	w = (pbox->x2 - pbox->x1) * shaBpp;
1737	h = pbox->y2 - pbox->y1;
1738
1739	scrLine = (x >> FB_SHIFT);
1740	shaLine = shaBase + y * shaStride + (x >> FB_SHIFT);
1741
1742	x &= FB_MASK;
1743	w = (w + x + FB_MASK) >> FB_SHIFT;
1744
1745	while (h--)
1746	{
1747	    winSize = 0;
1748	    scrBase = 0;
1749	    width = w;
1750	    scr = scrLine;
1751	    sha = shaLine;
1752	    while (width) {
1753		/* how much remains in this window */
1754		i = scrBase + winSize - scr;
1755		if (i <= 0 || scr < scrBase)
1756		{
1757		    winBase = (FbBits *) (*pBuf->window) (pScreen,
1758							  y,
1759							  scr * sizeof (FbBits),
1760							  SHADOW_WINDOW_WRITE,
1761							  &winSize,
1762							  pBuf->closure);
1763		    if(!winBase)
1764			return;
1765		    scrBase = scr;
1766		    winSize /= sizeof (FbBits);
1767		    i = winSize;
1768		}
1769		win = winBase + (scr - scrBase);
1770		if (i > width)
1771		    i = width;
1772		width -= i;
1773		scr += i;
1774		memcpy32sw(win, sha, i * sizeof(FbBits));
1775		sha += i;
1776	    }
1777	    shaLine += shaStride;
1778	    y++;
1779	}
1780	pbox++;
1781    }
1782}
1783
1784void
1785WsfbShadowUpdateSplit(ScreenPtr pScreen, shadowBufPtr pBuf)
1786{
1787    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1788    WsfbPtr 	fPtr = WSFBPTR(pScrn);
1789    RegionPtr	damage = DamageRegion (pBuf->pDamage);
1790    PixmapPtr	pShadow = pBuf->pPixmap;
1791    int		nbox = RegionNumRects (damage);
1792    BoxPtr	pbox = RegionRects (damage);
1793    FbBits	*shaBase, *shaLine, *sha;
1794    FbStride	shaStride;
1795    int		scrBase, scrLine, scr;
1796    int		shaBpp;
1797    int		shaXoff, shaYoff; /* XXX assumed to be zero */
1798    int		x, y, w, h, width;
1799    int         i;
1800    FbBits	*winBase = NULL, *win, *win2;
1801    unsigned long split = fPtr->fbi.fbi_fbsize / 2;
1802    CARD32      winSize;
1803
1804    fbGetDrawable (&pShadow->drawable, shaBase, shaStride, shaBpp, shaXoff, shaYoff);
1805    while (nbox--)
1806    {
1807	x = pbox->x1 * shaBpp;
1808	y = pbox->y1;
1809	w = (pbox->x2 - pbox->x1) * shaBpp;
1810	h = pbox->y2 - pbox->y1;
1811
1812	scrLine = (x >> FB_SHIFT);
1813	shaLine = shaBase + y * shaStride + (x >> FB_SHIFT);
1814
1815	x &= FB_MASK;
1816	w = (w + x + FB_MASK) >> FB_SHIFT;
1817
1818	while (h--)
1819	{
1820	    winSize = 0;
1821	    scrBase = 0;
1822	    width = w;
1823	    scr = scrLine;
1824	    sha = shaLine;
1825	    while (width) {
1826		/* how much remains in this window */
1827		i = scrBase + winSize - scr;
1828		if (i <= 0 || scr < scrBase)
1829		{
1830		    winBase = (FbBits *) (*pBuf->window) (pScreen,
1831							  y,
1832							  scr * sizeof (FbBits),
1833							  SHADOW_WINDOW_WRITE,
1834							  &winSize,
1835							  pBuf->closure);
1836		    if(!winBase)
1837			return;
1838		    scrBase = scr;
1839		    winSize /= sizeof (FbBits);
1840		    i = winSize;
1841		}
1842		win = winBase + (scr - scrBase);
1843		win2 = (FbBits *)(split + (unsigned long)win);
1844		if (i > width)
1845		    i = width;
1846		width -= i;
1847		scr += i;
1848		memcpy(win, sha, i * sizeof(FbBits));
1849		memcpy(win2, sha, i * sizeof(FbBits));
1850		sha += i;
1851	    }
1852	    shaLine += shaStride;
1853	    y++;
1854	}
1855	pbox++;
1856    }
1857}
1858