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