wsfb_driver.c revision 3788fa92
1/* $OpenBSD: wsfb_driver.c,v 1.16 2009/09/13 19:33:49 matthieu Exp $ */
2/*
3 * Copyright (c) 2001 Matthieu Herrb
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 *    - Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    - Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials provided
15 *      with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 *
30 */
31
32/*
33 * Based on fbdev.c written by:
34 *
35 * Authors:  Alan Hourihane, <alanh@fairlite.demon.co.uk>
36 *	     Michel Dänzer, <michdaen@iiic.ethz.ch>
37 */
38
39#ifdef HAVE_CONFIG_H
40#include "config.h"
41#endif
42
43#include <errno.h>
44#include <fcntl.h>
45#include <sys/types.h>
46#include <sys/mman.h>
47#include <sys/time.h>
48#include <errno.h>
49#include <dev/wscons/wsconsio.h>
50
51/* All drivers need this. */
52#include "xf86.h"
53#include "xf86_OSproc.h"
54#include "xf86_OSlib.h"
55
56#include "mipointer.h"
57#include "mibstore.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 */
113#ifdef XFree86LOADER
114static pointer WsfbSetup(pointer, pointer, int *, int *);
115#endif
116static Bool WsfbGetRec(ScrnInfoPtr);
117static void WsfbFreeRec(ScrnInfoPtr);
118static const OptionInfoRec * WsfbAvailableOptions(int, int);
119static void WsfbIdentify(int);
120static Bool WsfbProbe(DriverPtr, int);
121static Bool WsfbPreInit(ScrnInfoPtr, int);
122static Bool WsfbScreenInit(int, ScreenPtr, int, char **);
123static Bool WsfbCloseScreen(int, ScreenPtr);
124static void *WsfbWindowLinear(ScreenPtr, CARD32, CARD32, int, CARD32 *,
125			      void *);
126static void WsfbPointerMoved(int, int, int);
127static Bool WsfbEnterVT(int, int);
128static void WsfbLeaveVT(int, int);
129static Bool WsfbSwitchMode(int, DisplayModePtr, int);
130static int WsfbValidMode(int, 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
144static Bool WsfbDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op,
145				pointer ptr);
146
147/* Helper functions */
148static int wsfb_open(char *);
149static pointer wsfb_mmap(size_t, off_t, int);
150
151enum { WSFB_ROTATE_NONE = 0,
152       WSFB_ROTATE_CCW = 90,
153       WSFB_ROTATE_UD = 180,
154       WSFB_ROTATE_CW = 270
155};
156
157/*
158 * This is intentionally screen-independent.
159 * It indicates the binding choice made in the first PreInit.
160 */
161static int pix24bpp = 0;
162
163#define WSFB_VERSION 		4000
164#define WSFB_NAME		"wsfb"
165#define WSFB_DRIVER_NAME	"wsfb"
166
167_X_EXPORT DriverRec WSFB = {
168	WSFB_VERSION,
169	WSFB_DRIVER_NAME,
170	WsfbIdentify,
171	WsfbProbe,
172	WsfbAvailableOptions,
173	NULL,
174	0,
175	WsfbDriverFunc
176};
177
178/* Supported "chipsets" */
179static SymTabRec WsfbChipsets[] = {
180	{ 0, "wsfb" },
181	{ -1, NULL }
182};
183
184/* Supported options */
185typedef enum {
186	OPTION_SHADOW_FB,
187	OPTION_ROTATE,
188	OPTION_HW_CURSOR,
189	OPTION_SW_CURSOR
190} WsfbOpts;
191
192static const OptionInfoRec WsfbOptions[] = {
193	{ OPTION_SHADOW_FB, "ShadowFB", OPTV_BOOLEAN, {0}, FALSE},
194	{ OPTION_ROTATE, "Rotate", OPTV_STRING, {0}, FALSE},
195	{ -1, NULL, OPTV_NONE, {0}, FALSE}
196};
197
198/* Symbols needed from other modules */
199static const char *fbSymbols[] = {
200	"fbPictureInit",
201	"fbScreenInit",
202	NULL
203};
204static const char *shadowSymbols[] = {
205	"shadowAdd",
206	"shadowSetup",
207	"shadowUpdatePacked",
208	"shadowUpdatePackedWeak",
209	"shadowUpdateRotatePacked",
210	"shadowUpdateRotatePackedWeak",
211	NULL
212};
213
214static const char *ramdacSymbols[] = {
215	"xf86CreateCursorInfoRec",
216	"xf86DestroyCursorInfoRec",
217	"xf86InitCursor",
218	NULL
219};
220
221#ifdef XFree86LOADER
222static XF86ModuleVersionInfo WsfbVersRec = {
223	"wsfb",
224	MODULEVENDORSTRING,
225	MODINFOSTRING1,
226	MODINFOSTRING2,
227	XORG_VERSION_CURRENT,
228	PACKAGE_VERSION_MAJOR,
229	PACKAGE_VERSION_MINOR,
230	PACKAGE_VERSION_PATCHLEVEL,
231	ABI_CLASS_VIDEODRV,
232	ABI_VIDEODRV_VERSION,
233	NULL,
234	{0, 0, 0, 0}
235};
236
237_X_EXPORT XF86ModuleData wsfbModuleData = { &WsfbVersRec, WsfbSetup, NULL };
238
239static pointer
240WsfbSetup(pointer module, pointer opts, int *errmaj, int *errmin)
241{
242	static Bool setupDone = FALSE;
243	const char *osname;
244
245	/* Check that we're being loaded on a OpenBSD or NetBSD system. */
246	LoaderGetOS(&osname, NULL, NULL, NULL);
247	if (!osname || (strcmp(osname, "openbsd") != 0 &&
248	                strcmp(osname, "netbsd") != 0)) {
249		if (errmaj)
250			*errmaj = LDR_BADOS;
251		if (errmin)
252			*errmin = 0;
253		return NULL;
254	}
255	if (!setupDone) {
256		setupDone = TRUE;
257		xf86AddDriver(&WSFB, module, HaveDriverFuncs);
258		LoaderRefSymLists(fbSymbols, shadowSymbols, ramdacSymbols,
259		    NULL);
260		return (pointer)1;
261	} else {
262		if (errmaj != NULL)
263			*errmaj = LDR_ONCEONLY;
264		return NULL;
265	}
266}
267#endif /* XFree86LOADER */
268
269static Bool
270WsfbGetRec(ScrnInfoPtr pScrn)
271{
272
273	if (pScrn->driverPrivate != NULL)
274		return TRUE;
275
276	pScrn->driverPrivate = xnfcalloc(sizeof(WsfbRec), 1);
277	return TRUE;
278}
279
280static void
281WsfbFreeRec(ScrnInfoPtr pScrn)
282{
283
284	if (pScrn->driverPrivate == NULL)
285		return;
286	xfree(pScrn->driverPrivate);
287	pScrn->driverPrivate = NULL;
288}
289
290static const OptionInfoRec *
291WsfbAvailableOptions(int chipid, int busid)
292{
293	return WsfbOptions;
294}
295
296static void
297WsfbIdentify(int flags)
298{
299	xf86PrintChipsets(WSFB_NAME, "driver for wsdisplay framebuffer",
300			  WsfbChipsets);
301}
302
303/* Open the framebuffer device. */
304static int
305wsfb_open(char *dev)
306{
307	int fd = -1;
308
309	/* Try argument from XF86Config first. */
310	if (dev == NULL || ((fd = priv_open_device(dev)) == -1)) {
311		/* Second: environment variable. */
312		dev = getenv("XDEVICE");
313		if (dev == NULL || ((fd = priv_open_device(dev)) == -1)) {
314			/* Last try: default device. */
315			dev = WSFB_DEFAULT_DEV;
316			if ((fd = priv_open_device(dev)) == -1) {
317				return -1;
318			}
319		}
320	}
321	return fd;
322}
323
324/* Map the framebuffer's memory. */
325static pointer
326wsfb_mmap(size_t len, off_t off, int fd)
327{
328	int pagemask, mapsize;
329	caddr_t addr;
330	pointer mapaddr;
331
332	pagemask = getpagesize() - 1;
333	mapsize = ((int) len + pagemask) & ~pagemask;
334	addr = 0;
335
336	/*
337	 * Try and make it private first, that way once we get it, an
338	 * interloper, e.g. another server, can't get this frame buffer,
339	 * and if another server already has it, this one won't.
340	 */
341	mapaddr = (pointer) mmap(addr, mapsize,
342				 PROT_READ | PROT_WRITE, MAP_SHARED,
343				 fd, off);
344	if (mapaddr == MAP_FAILED) {
345		mapaddr = NULL;
346	}
347#if DEBUG
348	ErrorF("mmap returns: addr %p len 0x%x\n", mapaddr, mapsize);
349#endif
350	return mapaddr;
351}
352
353static Bool
354WsfbProbe(DriverPtr drv, int flags)
355{
356	int i, fd, entity;
357       	GDevPtr *devSections;
358	int numDevSections;
359	char *dev;
360	Bool foundScreen = FALSE;
361
362	TRACE("probe start");
363
364	/* For now, just bail out for PROBE_DETECT. */
365	if (flags & PROBE_DETECT)
366		return FALSE;
367
368	if ((numDevSections = xf86MatchDevice(WSFB_DRIVER_NAME,
369					      &devSections)) <= 0)
370		return FALSE;
371
372	for (i = 0; i < numDevSections; i++) {
373		ScrnInfoPtr pScrn = NULL;
374
375		dev = xf86FindOptionValue(devSections[i]->options, "device");
376		if ((fd = wsfb_open(dev)) >= 0) {
377			entity = xf86ClaimFbSlot(drv, 0, devSections[i], TRUE);
378			pScrn = xf86ConfigFbEntity(NULL,0,entity,
379						   NULL,NULL,NULL,NULL);
380			if (pScrn != NULL) {
381				foundScreen = TRUE;
382				pScrn->driverVersion = WSFB_VERSION;
383				pScrn->driverName = WSFB_DRIVER_NAME;
384				pScrn->name = WSFB_NAME;
385				pScrn->Probe = WsfbProbe;
386				pScrn->PreInit = WsfbPreInit;
387				pScrn->ScreenInit = WsfbScreenInit;
388				pScrn->SwitchMode = WsfbSwitchMode;
389				pScrn->AdjustFrame = NULL;
390				pScrn->EnterVT = WsfbEnterVT;
391				pScrn->LeaveVT = WsfbLeaveVT;
392				pScrn->ValidMode = WsfbValidMode;
393
394				xf86DrvMsg(pScrn->scrnIndex, X_INFO,
395				    "using %s\n", dev != NULL ? dev :
396				    "default device");
397			}
398		}
399	}
400	xfree(devSections);
401	TRACE("probe done");
402	return foundScreen;
403}
404
405static Bool
406WsfbPreInit(ScrnInfoPtr pScrn, int flags)
407{
408	WsfbPtr fPtr;
409	int default_depth, wstype;
410	char *dev, *s;
411	char *mod = NULL;
412	const char *reqSym = NULL;
413	Gamma zeros = {0.0, 0.0, 0.0};
414	DisplayModePtr mode;
415	MessageType from;
416
417	if (flags & PROBE_DETECT) return FALSE;
418
419	TRACE_ENTER("PreInit");
420
421	if (pScrn->numEntities != 1) return FALSE;
422
423	pScrn->monitor = pScrn->confScreen->monitor;
424
425	WsfbGetRec(pScrn);
426	fPtr = WSFBPTR(pScrn);
427
428	fPtr->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
429
430#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
431	pScrn->racMemFlags = RAC_FB | RAC_COLORMAP | RAC_CURSOR | RAC_VIEWPORT;
432	pScrn->racIoFlags = pScrn->racMemFlags;
433#endif
434
435	dev = xf86FindOptionValue(fPtr->pEnt->device->options, "device");
436	fPtr->fd = wsfb_open(dev);
437	if (fPtr->fd == -1) {
438		return FALSE;
439	}
440
441	if (ioctl(fPtr->fd, WSDISPLAYIO_GET_FBINFO, &fPtr->fbi) != 0) {
442		struct wsdisplay_fbinfo info;
443		struct wsdisplayio_fbinfo *fbi = &fPtr->fbi;
444		int lb;
445
446		xf86Msg(X_WARNING, "ioctl(WSDISPLAYIO_GET_FBINFO) failed, " \
447			"falling back to old method\n");
448		if (ioctl(fPtr->fd, WSDISPLAYIO_GINFO, &info) == -1) {
449			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
450				   "ioctl WSDISPLAY_GINFO: %s\n",
451				   strerror(errno));
452			return FALSE;
453		}
454		if (ioctl(fPtr->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
455			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
456				   "ioctl WSDISPLAY_GTYPE: %s\n",
457				   strerror(errno));
458			return FALSE;
459		}
460		if (ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES, &lb) == -1) {
461			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
462				   "ioctl WSDISPLAYIO_LINEBYTES: %s\n",
463				   strerror(errno));
464			return FALSE;
465		}
466		/* ok, fake up a new style fbinfo */
467		fbi->fbi_width = info.width;
468		fbi->fbi_height = info.height;
469		fbi->fbi_stride = lb;
470		fbi->fbi_bitsperpixel = info.depth;
471		if (info.depth > 16) {
472			fbi->fbi_pixeltype = WSFB_RGB;
473			if (wstype == WSDISPLAY_TYPE_SUN24 ||
474			    wstype == WSDISPLAY_TYPE_SUNCG12 ||
475			    wstype == WSDISPLAY_TYPE_SUNCG14 ||
476			    wstype == WSDISPLAY_TYPE_SUNTCX ||
477			    wstype == WSDISPLAY_TYPE_SUNFFB ||
478			    wstype == WSDISPLAY_TYPE_XVR1000 ||
479			    wstype == WSDISPLAY_TYPE_VC4) {
480				fbi->fbi_subtype.fbi_rgbmasks.red_offset = 0;
481				fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
482				fbi->fbi_subtype.fbi_rgbmasks.green_offset = 8;
483				fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
484				fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 16;
485				fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
486			} else {
487				fbi->fbi_subtype.fbi_rgbmasks.red_offset = 16;
488				fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
489				fbi->fbi_subtype.fbi_rgbmasks.green_offset = 8;
490				fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
491				fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 0;
492				fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
493			}
494			fbi->fbi_subtype.fbi_rgbmasks.alpha_offset = 0;
495			fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 0;
496		} else if (info.depth <= 8) {
497			fbi->fbi_pixeltype = WSFB_CI;
498			fbi->fbi_subtype.fbi_cmapinfo.cmap_entries = info.cmsize;
499		}
500		fbi->fbi_flags = 0;
501		fbi->fbi_fbsize = info.width * lb;
502
503	}
504	/*
505	 * Allocate room for saving the colormap.
506	 */
507	if (fPtr->fbi.fbi_pixeltype == WSFB_CI) {
508		fPtr->saved_cmap.red =
509		    (unsigned char *)xalloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
510		if (fPtr->saved_cmap.red == NULL) {
511			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
512			    "Cannot malloc %d bytes\n",
513			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
514			return FALSE;
515		}
516		fPtr->saved_cmap.green =
517		    (unsigned char *)xalloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
518		if (fPtr->saved_cmap.green == NULL) {
519			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
520			    "Cannot malloc %d bytes\n",
521			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
522			xfree(fPtr->saved_cmap.red);
523			return FALSE;
524		}
525		fPtr->saved_cmap.blue =
526		    (unsigned char *)xalloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
527		if (fPtr->saved_cmap.blue == NULL) {
528			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
529			    "Cannot malloc %d bytes\n",
530			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
531			xfree(fPtr->saved_cmap.red);
532			xfree(fPtr->saved_cmap.green);
533			return FALSE;
534		}
535	}
536
537	/* Handle depth */
538	default_depth = fPtr->fbi.fbi_bitsperpixel <= 24 ? fPtr->fbi.fbi_bitsperpixel : 24;
539	if (!xf86SetDepthBpp(pScrn, default_depth, default_depth,
540		fPtr->fbi.fbi_bitsperpixel,
541		fPtr->fbi.fbi_bitsperpixel >= 24 ? Support24bppFb|Support32bppFb : 0))
542		return FALSE;
543
544	/* Check consistency. */
545	if (pScrn->bitsPerPixel != fPtr->fbi.fbi_bitsperpixel) {
546		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
547		    "specified depth (%d) or bpp (%d) doesn't match "
548		    "framebuffer depth (%d)\n", pScrn->depth,
549		    pScrn->bitsPerPixel, fPtr->fbi.fbi_bitsperpixel);
550		return FALSE;
551	}
552	xf86PrintDepthBpp(pScrn);
553
554	/* Get the depth24 pixmap format. */
555	if (pScrn->depth == 24 && pix24bpp == 0)
556		pix24bpp = xf86GetBppFromDepth(pScrn, 24);
557
558	/* Color weight */
559	if (fPtr->fbi.fbi_pixeltype == WSFB_RGB) {
560		rgb zeros = { 0, 0, 0 }, masks;
561
562		if (fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_size > 0) {
563			uint32_t msk;
564
565			msk = 0xffffffff;
566			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_size;
567			msk = ~msk;
568			masks.red = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_offset;
569
570			msk = 0xffffffff;
571			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_size;
572			msk = ~msk;
573			masks.green = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_offset;
574
575			msk = 0xffffffff;
576			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_size;
577			msk = ~msk;
578			masks.blue = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset;
579			xf86Msg(X_INFO, "masks generated: %08x %08x %08x\n",
580			    masks.red, masks.green, masks.blue);
581		} else {
582			masks.red = 0;
583			masks.green = 0;
584			masks.blue = 0;
585		}
586
587		if (!xf86SetWeight(pScrn, zeros, masks))
588			return FALSE;
589	}
590
591	/* Visual init */
592	if (!xf86SetDefaultVisual(pScrn, -1))
593		return FALSE;
594
595	/* We don't currently support DirectColor at > 8bpp . */
596	if (pScrn->depth > 8 && pScrn->defaultVisual != TrueColor) {
597		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Given default visual"
598			   " (%s) is not supported at depth %d\n",
599			   xf86GetVisualName(pScrn->defaultVisual),
600			   pScrn->depth);
601		return FALSE;
602	}
603
604	xf86SetGamma(pScrn,zeros);
605
606	pScrn->progClock = TRUE;
607	pScrn->rgbBits   = (pScrn->depth >= 8) ? 8 : pScrn->depth;
608	pScrn->chipset   = "wsfb";
609	pScrn->videoRam  = fPtr->fbi.fbi_fbsize;
610
611	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Vidmem: %dk\n",
612		   pScrn->videoRam/1024);
613
614	/* Handle options. */
615	xf86CollectOptions(pScrn, NULL);
616	if (!(fPtr->Options = xalloc(sizeof(WsfbOptions))))
617		return FALSE;
618	memcpy(fPtr->Options, WsfbOptions, sizeof(WsfbOptions));
619	xf86ProcessOptions(pScrn->scrnIndex, fPtr->pEnt->device->options,
620			   fPtr->Options);
621
622	/* Use shadow framebuffer by default, on depth >= 8 */
623	xf86Msg(X_INFO, "fbi_flags: %x\n", fPtr->fbi.fbi_flags);
624	if ((pScrn->depth >= 8) &&
625	   ((fPtr->fbi.fbi_flags & WSFB_VRAM_IS_RAM) == 0)) {
626		fPtr->shadowFB = xf86ReturnOptValBool(fPtr->Options,
627						      OPTION_SHADOW_FB, TRUE);
628	} else
629		if (xf86ReturnOptValBool(fPtr->Options,
630					 OPTION_SHADOW_FB, FALSE)) {
631			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
632				   "Shadow FB option ignored on depth < 8");
633		}
634
635	/* Rotation */
636	fPtr->rotate = WSFB_ROTATE_NONE;
637	if ((s = xf86GetOptValString(fPtr->Options, OPTION_ROTATE))) {
638		if (pScrn->depth >= 8) {
639			if (!xf86NameCmp(s, "CW")) {
640				fPtr->shadowFB = TRUE;
641				fPtr->rotate = WSFB_ROTATE_CW;
642				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
643				    "Rotating screen clockwise\n");
644			} else if (!xf86NameCmp(s, "CCW")) {
645				fPtr->shadowFB = TRUE;
646				fPtr->rotate = WSFB_ROTATE_CCW;
647				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
648				    "Rotating screen counter clockwise\n");
649			} else if (!xf86NameCmp(s, "UD")) {
650				fPtr->shadowFB = TRUE;
651				fPtr->rotate = WSFB_ROTATE_UD;
652				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
653				    "Rotating screen upside down\n");
654			} else {
655				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
656				    "\"%s\" is not a valid value for Option "
657				    "\"Rotate\"\n", s);
658				xf86DrvMsg(pScrn->scrnIndex, X_INFO,
659				    "Valid options are \"CW\", \"CCW\","
660				    " or \"UD\"\n");
661			}
662		} else {
663			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
664			    "Option \"Rotate\" ignored on depth < 8");
665		}
666	}
667
668	/* Fake video mode struct. */
669	mode = (DisplayModePtr)xalloc(sizeof(DisplayModeRec));
670	mode->prev = mode;
671	mode->next = mode;
672	mode->name = "wsfb current mode";
673	mode->status = MODE_OK;
674	mode->type = M_T_BUILTIN;
675	mode->Clock = 0;
676	mode->HDisplay = fPtr->fbi.fbi_width;
677	mode->HSyncStart = 0;
678	mode->HSyncEnd = 0;
679	mode->HTotal = 0;
680	mode->HSkew = 0;
681	mode->VDisplay = fPtr->fbi.fbi_height;
682	mode->VSyncStart = 0;
683	mode->VSyncEnd = 0;
684	mode->VTotal = 0;
685	mode->VScan = 0;
686	mode->Flags = 0;
687	if (pScrn->modes != NULL) {
688		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
689		   "Ignoring mode specification from screen section\n");
690	}
691	pScrn->currentMode = pScrn->modes = mode;
692	pScrn->virtualX = fPtr->fbi.fbi_width;
693	pScrn->virtualY = fPtr->fbi.fbi_height;
694	pScrn->displayWidth = pScrn->virtualX;
695
696	/* Set the display resolution. */
697	xf86SetDpi(pScrn, 0, 0);
698
699	from = X_DEFAULT;
700	fPtr->HWCursor = TRUE;
701	if (xf86GetOptValBool(fPtr->Options, OPTION_HW_CURSOR, &fPtr->HWCursor))
702		from = X_CONFIG;
703	if (xf86ReturnOptValBool(fPtr->Options, OPTION_SW_CURSOR, FALSE)) {
704		from = X_CONFIG;
705		fPtr->HWCursor = FALSE;
706	}
707	xf86DrvMsg(pScrn->scrnIndex, from, "Using %s cursor\n",
708		fPtr->HWCursor ? "HW" : "SW");
709
710	/* Load bpp-specific modules. */
711	switch(pScrn->bitsPerPixel) {
712	case 1:
713	case 4:
714	default:
715		mod = "fb";
716		break;
717	}
718
719
720	/* Load shadow if needed. */
721	if (fPtr->shadowFB) {
722		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
723			   "Using \"Shadow Framebuffer\"\n");
724		if (xf86LoadSubModule(pScrn, "shadow") == NULL) {
725			WsfbFreeRec(pScrn);
726			return FALSE;
727		}
728	}
729
730	if (mod && xf86LoadSubModule(pScrn, mod) == NULL) {
731		WsfbFreeRec(pScrn);
732		return FALSE;
733	}
734
735	if (xf86LoadSubModule(pScrn, "ramdac") == NULL) {
736		WsfbFreeRec(pScrn);
737		return FALSE;
738        }
739
740	if (mod) {
741		if (reqSym) {
742			xf86LoaderReqSymbols(reqSym, NULL);
743		} else {
744			xf86LoaderReqSymLists(fbSymbols, NULL);
745		}
746	}
747	TRACE_EXIT("PreInit");
748	return TRUE;
749}
750
751static Bool
752WsfbCreateScreenResources(ScreenPtr pScreen)
753{
754	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
755	WsfbPtr fPtr = WSFBPTR(pScrn);
756	PixmapPtr pPixmap;
757	Bool ret;
758
759	pScreen->CreateScreenResources = fPtr->CreateScreenResources;
760	ret = pScreen->CreateScreenResources(pScreen);
761	pScreen->CreateScreenResources = WsfbCreateScreenResources;
762
763	if (!ret)
764		return FALSE;
765
766	pPixmap = pScreen->GetScreenPixmap(pScreen);
767
768	if (!shadowAdd(pScreen, pPixmap, fPtr->rotate ?
769		shadowUpdateRotatePackedWeak() : shadowUpdatePackedWeak(),
770		WsfbWindowLinear, fPtr->rotate, NULL)) {
771		return FALSE;
772	}
773	return TRUE;
774}
775
776
777static Bool
778WsfbShadowInit(ScreenPtr pScreen)
779{
780	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
781	WsfbPtr fPtr = WSFBPTR(pScrn);
782
783	if (!shadowSetup(pScreen))
784		return FALSE;
785	fPtr->CreateScreenResources = pScreen->CreateScreenResources;
786	pScreen->CreateScreenResources = WsfbCreateScreenResources;
787
788	return TRUE;
789}
790
791static Bool
792WsfbScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
793{
794	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
795	WsfbPtr fPtr = WSFBPTR(pScrn);
796	VisualPtr visual;
797	int ret, flags, ncolors;
798	int wsmode = WSDISPLAYIO_MODE_DUMBFB;
799	int wstype;
800	size_t len;
801
802	TRACE_ENTER("WsfbScreenInit");
803#if DEBUG
804	ErrorF("\tbitsPerPixel=%d, depth=%d, defaultVisual=%s\n"
805	       "\tmask: %x,%x,%x, offset: %u,%u,%u\n",
806	       pScrn->bitsPerPixel,
807	       pScrn->depth,
808	       xf86GetVisualName(pScrn->defaultVisual),
809	       pScrn->mask.red,pScrn->mask.green,pScrn->mask.blue,
810	       pScrn->offset.red,pScrn->offset.green,pScrn->offset.blue);
811#endif
812	switch (fPtr->fbi.fbi_bitsperpixel) {
813	case 1:
814	case 4:
815	case 8:
816		len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
817		break;
818	case 16:
819		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
820			xf86Msg(X_ERROR, "Bogus stride == width in 16bit colour\n");
821			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * sizeof(short);
822		} else {
823			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
824		}
825		break;
826	case 24:
827		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
828			xf86Msg(X_ERROR, "Bogus stride == width in 24bit colour\n");
829			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * 3;
830		} else {
831			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
832		}
833		break;
834	case 32:
835		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
836			xf86Msg(X_ERROR, "Bogus stride == width in 32bit colour\n");
837			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * sizeof(int);
838		} else {
839			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
840		}
841		break;
842	default:
843		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
844			   "unsupported depth %d\n", fPtr->fbi.fbi_bitsperpixel);
845		return FALSE;
846	}
847	/* Switch to graphics mode - required before mmap. */
848	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &wsmode) == -1) {
849		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
850			   "ioctl WSDISPLAYIO_SMODE: %s\n",
851			   strerror(errno));
852		return FALSE;
853	}
854	/* Get wsdisplay type to handle quirks */
855	if (ioctl(fPtr->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
856		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
857			   "ioctl WSDISPLAY_GTYPE: %s\n",
858			   strerror(errno));
859		return FALSE;
860	}
861	fPtr->fbmem = wsfb_mmap(len, 0, fPtr->fd);
862
863	if (fPtr->fbmem == NULL) {
864		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
865			   "wsfb_mmap: %s\n", strerror(errno));
866		return FALSE;
867	}
868	fPtr->fbmem_len = len;
869
870	WsfbSave(pScrn);
871	pScrn->vtSema = TRUE;
872
873	/* MI layer */
874	miClearVisualTypes();
875	if (pScrn->bitsPerPixel > 8) {
876		if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
877				      pScrn->rgbBits, TrueColor))
878			return FALSE;
879	} else {
880		if (!miSetVisualTypes(pScrn->depth,
881				      miGetDefaultVisualMask(pScrn->depth),
882				      pScrn->rgbBits, pScrn->defaultVisual))
883			return FALSE;
884	}
885	if (!miSetPixmapDepths())
886		return FALSE;
887
888	if (fPtr->rotate == WSFB_ROTATE_CW
889	    || fPtr->rotate == WSFB_ROTATE_CCW) {
890		int tmp = pScrn->virtualX;
891		pScrn->virtualX = pScrn->displayWidth = pScrn->virtualY;
892		pScrn->virtualY = tmp;
893	}
894	if (fPtr->rotate && !fPtr->PointerMoved) {
895		fPtr->PointerMoved = pScrn->PointerMoved;
896		pScrn->PointerMoved = WsfbPointerMoved;
897	}
898
899	fPtr->fbstart = fPtr->fbmem;
900#ifdef	WSDISPLAY_TYPE_LUNA
901	if (wstype == WSDISPLAY_TYPE_LUNA) {
902		/*
903		 * XXX
904		 * LUNA's FB seems to have 64 dot (8 byte) offset.
905		 * This might be able to be changed in kernel lunafb driver,
906		 * but current setting was pulled from 4.4BSD-Lite2/luna68k.
907		 */
908		fPtr->fbstart += 8;
909	}
910#endif
911
912	if (fPtr->shadowFB) {
913		fPtr->shadow = xcalloc(1, pScrn->virtualX * pScrn->virtualY *
914		    pScrn->bitsPerPixel);
915
916		if (!fPtr->shadow) {
917			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
918			    "Failed to allocate shadow framebuffer\n");
919			return FALSE;
920		}
921	}
922
923	switch (pScrn->bitsPerPixel) {
924	case 1:
925		ret = fbScreenInit(pScreen,
926		    fPtr->fbstart,
927		    pScrn->virtualX, pScrn->virtualY,
928		    pScrn->xDpi, pScrn->yDpi,
929		    fPtr->fbi.fbi_stride * 8, pScrn->bitsPerPixel);
930		break;
931	case 4:
932	case 8:
933	case 16:
934	case 24:
935	case 32:
936		ret = fbScreenInit(pScreen,
937		    fPtr->shadowFB ? fPtr->shadow : fPtr->fbstart,
938		    pScrn->virtualX, pScrn->virtualY,
939		    pScrn->xDpi, pScrn->yDpi,
940		    pScrn->displayWidth, pScrn->bitsPerPixel);
941		break;
942	default:
943		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
944			   "Unsupported bpp: %d", pScrn->bitsPerPixel);
945		return FALSE;
946	} /* case */
947
948	if (!ret)
949		return FALSE;
950
951	if (pScrn->bitsPerPixel > 8) {
952		/* Fixup RGB ordering. */
953		visual = pScreen->visuals + pScreen->numVisuals;
954		while (--visual >= pScreen->visuals) {
955			if ((visual->class | DynamicClass) == DirectColor) {
956				visual->offsetRed   = pScrn->offset.red;
957				visual->offsetGreen = pScrn->offset.green;
958				visual->offsetBlue  = pScrn->offset.blue;
959				visual->redMask     = pScrn->mask.red;
960				visual->greenMask   = pScrn->mask.green;
961				visual->blueMask    = pScrn->mask.blue;
962			}
963		}
964	}
965
966	if (pScrn->bitsPerPixel >= 8) {
967		if (!fbPictureInit(pScreen, NULL, 0))
968			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
969				   "RENDER extension initialisation failed.");
970	}
971	if (fPtr->shadowFB && !WsfbShadowInit(pScreen)) {
972		xf86DrvMsg(scrnIndex, X_ERROR,
973		    "shadow framebuffer initialization failed\n");
974		return FALSE;
975	}
976
977#ifdef XFreeXDGA
978	if (!fPtr->rotate)
979		WsfbDGAInit(pScrn, pScreen);
980	else
981		xf86DrvMsg(scrnIndex, X_INFO, "Rotated display, "
982		    "disabling DGA\n");
983#endif
984	if (fPtr->rotate) {
985		xf86DrvMsg(scrnIndex, X_INFO, "Enabling Driver Rotation, "
986		    "disabling RandR\n");
987		xf86DisableRandR();
988		if (pScrn->bitsPerPixel == 24)
989			xf86DrvMsg(scrnIndex, X_WARNING,
990			    "Rotation might be broken in 24 bpp\n");
991	}
992
993	xf86SetBlackWhitePixels(pScreen);
994	miInitializeBackingStore(pScreen);
995	xf86SetBackingStore(pScreen);
996
997	/* Software cursor. */
998	miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
999
1000	/* check for hardware cursor support */
1001	if (fPtr->HWCursor)
1002		WsfbSetupCursor(pScreen);
1003
1004	/*
1005	 * Colormap
1006	 *
1007	 * Note that, even on less than 8 bit depth frame buffers, we
1008	 * expect the colormap to be programmable with 8 bit values.
1009	 * As of now, this is indeed the case on all OpenBSD supported
1010	 * graphics hardware.
1011	 */
1012	if (!miCreateDefColormap(pScreen))
1013		return FALSE;
1014	flags = CMAP_RELOAD_ON_MODE_SWITCH;
1015
1016	ncolors = 0;
1017	if (fPtr->fbi.fbi_pixeltype == WSFB_CI) {
1018		ncolors = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1019	}
1020
1021	/* On StaticGray visuals, fake a 256 entries colormap. */
1022	if (ncolors == 0)
1023		ncolors = 256;
1024
1025	if(!xf86HandleColormaps(pScreen, ncolors, 8, WsfbLoadPalette,
1026				NULL, flags))
1027		return FALSE;
1028
1029	pScreen->SaveScreen = WsfbSaveScreen;
1030
1031#ifdef XvExtension
1032	{
1033		XF86VideoAdaptorPtr *ptr;
1034
1035		int n = xf86XVListGenericAdaptors(pScrn,&ptr);
1036		if (n) {
1037			xf86XVScreenInit(pScreen,ptr,n);
1038		}
1039	}
1040#endif
1041
1042	/* Wrap the current CloseScreen function. */
1043	fPtr->CloseScreen = pScreen->CloseScreen;
1044	pScreen->CloseScreen = WsfbCloseScreen;
1045
1046	TRACE_EXIT("WsfbScreenInit");
1047	return TRUE;
1048}
1049
1050static Bool
1051WsfbCloseScreen(int scrnIndex, ScreenPtr pScreen)
1052{
1053	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1054	PixmapPtr pPixmap;
1055	WsfbPtr fPtr = WSFBPTR(pScrn);
1056
1057
1058	TRACE_ENTER("WsfbCloseScreen");
1059
1060	pPixmap = pScreen->GetScreenPixmap(pScreen);
1061	shadowRemove(pScreen, pPixmap);
1062
1063	if (pScrn->vtSema) {
1064		WsfbRestore(pScrn);
1065		if (munmap(fPtr->fbmem, fPtr->fbmem_len) == -1) {
1066			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1067				   "munmap: %s\n", strerror(errno));
1068		}
1069
1070		fPtr->fbmem = NULL;
1071	}
1072#ifdef XFreeXDGA
1073	if (fPtr->pDGAMode) {
1074		xfree(fPtr->pDGAMode);
1075		fPtr->pDGAMode = NULL;
1076		fPtr->nDGAMode = 0;
1077	}
1078#endif
1079	pScrn->vtSema = FALSE;
1080
1081	/* Unwrap CloseScreen. */
1082	pScreen->CloseScreen = fPtr->CloseScreen;
1083	TRACE_EXIT("WsfbCloseScreen");
1084	return (*pScreen->CloseScreen)(scrnIndex, pScreen);
1085}
1086
1087static void *
1088WsfbWindowLinear(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
1089		CARD32 *size, void *closure)
1090{
1091	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1092	WsfbPtr fPtr = WSFBPTR(pScrn);
1093
1094	/*
1095	 * XXX
1096	 * This should never happen. Is it really necessary?
1097	 */
1098	if (fPtr->fbi.fbi_stride)
1099		*size = fPtr->fbi.fbi_stride;
1100	else {
1101		if (ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES, size) == -1)
1102			return NULL;
1103		fPtr->fbi.fbi_stride = *size;
1104	}
1105	return ((CARD8 *)fPtr->fbmem + row * fPtr->fbi.fbi_stride + offset);
1106}
1107
1108static void
1109WsfbPointerMoved(int index, int x, int y)
1110{
1111    ScrnInfoPtr pScrn = xf86Screens[index];
1112    WsfbPtr fPtr = WSFBPTR(pScrn);
1113    int newX, newY;
1114
1115    switch (fPtr->rotate)
1116    {
1117    case WSFB_ROTATE_CW:
1118	/* 90 degrees CW rotation. */
1119	newX = pScrn->pScreen->height - y - 1;
1120	newY = x;
1121	break;
1122
1123    case WSFB_ROTATE_CCW:
1124	/* 90 degrees CCW rotation. */
1125	newX = y;
1126	newY = pScrn->pScreen->width - x - 1;
1127	break;
1128
1129    case WSFB_ROTATE_UD:
1130	/* 180 degrees UD rotation. */
1131	newX = pScrn->pScreen->width - x - 1;
1132	newY = pScrn->pScreen->height - y - 1;
1133	break;
1134
1135    default:
1136	/* No rotation. */
1137	newX = x;
1138	newY = y;
1139	break;
1140    }
1141
1142    /* Pass adjusted pointer coordinates to wrapped PointerMoved function. */
1143    (*fPtr->PointerMoved)(index, newX, newY);
1144}
1145
1146static Bool
1147WsfbEnterVT(int scrnIndex, int flags)
1148{
1149	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1150
1151	TRACE_ENTER("EnterVT");
1152	pScrn->vtSema = TRUE;
1153	TRACE_EXIT("EnterVT");
1154	return TRUE;
1155}
1156
1157static void
1158WsfbLeaveVT(int scrnIndex, int flags)
1159{
1160#if DEBUG
1161	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1162#endif
1163
1164	TRACE_ENTER("LeaveVT");
1165}
1166
1167static Bool
1168WsfbSwitchMode(int scrnIndex, DisplayModePtr mode, int flags)
1169{
1170#if DEBUG
1171	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1172#endif
1173
1174	TRACE_ENTER("SwitchMode");
1175	/* Nothing else to do. */
1176	return TRUE;
1177}
1178
1179static int
1180WsfbValidMode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
1181{
1182#if DEBUG
1183	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1184#endif
1185
1186	TRACE_ENTER("ValidMode");
1187	return MODE_OK;
1188}
1189
1190static void
1191WsfbLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
1192	       LOCO *colors, VisualPtr pVisual)
1193{
1194	WsfbPtr fPtr = WSFBPTR(pScrn);
1195	struct wsdisplay_cmap cmap;
1196	unsigned char red[256],green[256],blue[256];
1197	int i, indexMin=256, indexMax=0;
1198
1199	TRACE_ENTER("LoadPalette");
1200
1201	cmap.count   = 1;
1202	cmap.red   = red;
1203	cmap.green = green;
1204	cmap.blue  = blue;
1205
1206	if (numColors == 1) {
1207		/* Optimisation */
1208		cmap.index = indices[0];
1209		red[0]   = colors[indices[0]].red;
1210		green[0] = colors[indices[0]].green;
1211		blue[0]  = colors[indices[0]].blue;
1212		if (ioctl(fPtr->fd,WSDISPLAYIO_PUTCMAP, &cmap) == -1)
1213			ErrorF("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
1214	} else {
1215		/*
1216		 * Change all colors in 2 ioctls
1217		 * and limit the data to be transfered.
1218		 */
1219		for (i = 0; i < numColors; i++) {
1220			if (indices[i] < indexMin)
1221				indexMin = indices[i];
1222			if (indices[i] > indexMax)
1223				indexMax = indices[i];
1224		}
1225		cmap.index = indexMin;
1226		cmap.count = indexMax - indexMin + 1;
1227		cmap.red = &red[indexMin];
1228		cmap.green = &green[indexMin];
1229		cmap.blue = &blue[indexMin];
1230		/* Get current map. */
1231		if (ioctl(fPtr->fd, WSDISPLAYIO_GETCMAP, &cmap) == -1)
1232			ErrorF("ioctl FBIOGETCMAP: %s\n", strerror(errno));
1233		/* Change the colors that require updating. */
1234		for (i = 0; i < numColors; i++) {
1235			red[indices[i]]   = colors[indices[i]].red;
1236			green[indices[i]] = colors[indices[i]].green;
1237			blue[indices[i]]  = colors[indices[i]].blue;
1238		}
1239		/* Write the colormap back. */
1240		if (ioctl(fPtr->fd,WSDISPLAYIO_PUTCMAP, &cmap) == -1)
1241			ErrorF("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
1242	}
1243	TRACE_EXIT("LoadPalette");
1244}
1245
1246static Bool
1247WsfbSaveScreen(ScreenPtr pScreen, int mode)
1248{
1249	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1250	WsfbPtr fPtr = WSFBPTR(pScrn);
1251	int state;
1252
1253	TRACE_ENTER("SaveScreen");
1254
1255	if (!pScrn->vtSema)
1256		return TRUE;
1257
1258	if (mode != SCREEN_SAVER_FORCER) {
1259		state = xf86IsUnblank(mode)?WSDISPLAYIO_VIDEO_ON:
1260		                            WSDISPLAYIO_VIDEO_OFF;
1261		ioctl(fPtr->fd,
1262		      WSDISPLAYIO_SVIDEO, &state);
1263	}
1264	TRACE_EXIT("SaveScreen");
1265	return TRUE;
1266}
1267
1268
1269static void
1270WsfbSave(ScrnInfoPtr pScrn)
1271{
1272	WsfbPtr fPtr = WSFBPTR(pScrn);
1273
1274	TRACE_ENTER("WsfbSave");
1275
1276	/* nothing to save if we don't run in colour-indexed mode */
1277	if (fPtr->fbi.fbi_pixeltype != WSFB_CI)
1278		return;
1279
1280	fPtr->saved_cmap.index = 0;
1281	fPtr->saved_cmap.count = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1282	if (ioctl(fPtr->fd, WSDISPLAYIO_GETCMAP,
1283		  &(fPtr->saved_cmap)) == -1) {
1284		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1285			   "error saving colormap %s\n", strerror(errno));
1286	}
1287	TRACE_EXIT("WsfbSave");
1288
1289}
1290
1291static void
1292WsfbRestore(ScrnInfoPtr pScrn)
1293{
1294	WsfbPtr fPtr = WSFBPTR(pScrn);
1295	int mode;
1296
1297	TRACE_ENTER("WsfbRestore");
1298
1299	if (fPtr->fbi.fbi_pixeltype == WSFB_CI) {
1300		/* reset colormap for text mode */
1301		if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP,
1302			  &(fPtr->saved_cmap)) == -1) {
1303			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1304				   "error restoring colormap %s\n",
1305				   strerror(errno));
1306		}
1307	}
1308
1309	/* Clear the screen. */
1310	memset(fPtr->fbmem, 0, fPtr->fbmem_len);
1311
1312	/* Restore the text mode. */
1313	mode = WSDISPLAYIO_MODE_EMUL;
1314	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1315		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1316			   "error setting text mode %s\n", strerror(errno));
1317	}
1318	TRACE_EXIT("WsfbRestore");
1319}
1320
1321#ifdef XFreeXDGA
1322/***********************************************************************
1323 * DGA stuff
1324 ***********************************************************************/
1325
1326static Bool
1327WsfbDGAOpenFramebuffer(ScrnInfoPtr pScrn, char **DeviceName,
1328		       unsigned char **ApertureBase, int *ApertureSize,
1329		       int *ApertureOffset, int *flags)
1330{
1331	*DeviceName = NULL;		/* No special device */
1332	*ApertureBase = (unsigned char *)(pScrn->memPhysBase);
1333	*ApertureSize = pScrn->videoRam;
1334	*ApertureOffset = pScrn->fbOffset;
1335	*flags = 0;
1336
1337	return TRUE;
1338}
1339
1340static Bool
1341WsfbDGASetMode(ScrnInfoPtr pScrn, DGAModePtr pDGAMode)
1342{
1343	DisplayModePtr pMode;
1344	int scrnIdx = pScrn->pScreen->myNum;
1345	int frameX0, frameY0;
1346
1347	if (pDGAMode) {
1348		pMode = pDGAMode->mode;
1349		frameX0 = frameY0 = 0;
1350	} else {
1351		if (!(pMode = pScrn->currentMode))
1352			return TRUE;
1353
1354		frameX0 = pScrn->frameX0;
1355		frameY0 = pScrn->frameY0;
1356	}
1357
1358	if (!(*pScrn->SwitchMode)(scrnIdx, pMode, 0))
1359		return FALSE;
1360	(*pScrn->AdjustFrame)(scrnIdx, frameX0, frameY0, 0);
1361
1362	return TRUE;
1363}
1364
1365static void
1366WsfbDGASetViewport(ScrnInfoPtr pScrn, int x, int y, int flags)
1367{
1368	(*pScrn->AdjustFrame)(pScrn->pScreen->myNum, x, y, flags);
1369}
1370
1371static int
1372WsfbDGAGetViewport(ScrnInfoPtr pScrn)
1373{
1374	return (0);
1375}
1376
1377static DGAFunctionRec WsfbDGAFunctions =
1378{
1379	WsfbDGAOpenFramebuffer,
1380	NULL,       /* CloseFramebuffer */
1381	WsfbDGASetMode,
1382	WsfbDGASetViewport,
1383	WsfbDGAGetViewport,
1384	NULL,       /* Sync */
1385	NULL,       /* FillRect */
1386	NULL,       /* BlitRect */
1387	NULL,       /* BlitTransRect */
1388};
1389
1390static void
1391WsfbDGAAddModes(ScrnInfoPtr pScrn)
1392{
1393	WsfbPtr fPtr = WSFBPTR(pScrn);
1394	DisplayModePtr pMode = pScrn->modes;
1395	DGAModePtr pDGAMode;
1396
1397	do {
1398		pDGAMode = xrealloc(fPtr->pDGAMode,
1399				    (fPtr->nDGAMode + 1) * sizeof(DGAModeRec));
1400		if (!pDGAMode)
1401			break;
1402
1403		fPtr->pDGAMode = pDGAMode;
1404		pDGAMode += fPtr->nDGAMode;
1405		(void)memset(pDGAMode, 0, sizeof(DGAModeRec));
1406
1407		++fPtr->nDGAMode;
1408		pDGAMode->mode = pMode;
1409		pDGAMode->flags = DGA_CONCURRENT_ACCESS | DGA_PIXMAP_AVAILABLE;
1410		pDGAMode->byteOrder = pScrn->imageByteOrder;
1411		pDGAMode->depth = pScrn->depth;
1412		pDGAMode->bitsPerPixel = pScrn->bitsPerPixel;
1413		pDGAMode->red_mask = pScrn->mask.red;
1414		pDGAMode->green_mask = pScrn->mask.green;
1415		pDGAMode->blue_mask = pScrn->mask.blue;
1416		pDGAMode->visualClass = pScrn->bitsPerPixel > 8 ?
1417			TrueColor : PseudoColor;
1418		pDGAMode->xViewportStep = 1;
1419		pDGAMode->yViewportStep = 1;
1420		pDGAMode->viewportWidth = pMode->HDisplay;
1421		pDGAMode->viewportHeight = pMode->VDisplay;
1422
1423		if (fPtr->fbi.fbi_stride)
1424			pDGAMode->bytesPerScanline = fPtr->fbi.fbi_stride;
1425		else {
1426			ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES,
1427			      &fPtr->fbi.fbi_stride);
1428			pDGAMode->bytesPerScanline = fPtr->fbi.fbi_stride;
1429		}
1430
1431		pDGAMode->imageWidth = pMode->HDisplay;
1432		pDGAMode->imageHeight =  pMode->VDisplay;
1433		pDGAMode->pixmapWidth = pDGAMode->imageWidth;
1434		pDGAMode->pixmapHeight = pDGAMode->imageHeight;
1435		pDGAMode->maxViewportX = pScrn->virtualX -
1436			pDGAMode->viewportWidth;
1437		pDGAMode->maxViewportY = pScrn->virtualY -
1438			pDGAMode->viewportHeight;
1439
1440		pDGAMode->address = fPtr->fbstart;
1441
1442		pMode = pMode->next;
1443	} while (pMode != pScrn->modes);
1444}
1445
1446static Bool
1447WsfbDGAInit(ScrnInfoPtr pScrn, ScreenPtr pScreen)
1448{
1449	WsfbPtr fPtr = WSFBPTR(pScrn);
1450
1451	if (pScrn->depth < 8)
1452		return FALSE;
1453
1454	if (!fPtr->nDGAMode)
1455		WsfbDGAAddModes(pScrn);
1456
1457	return (DGAInit(pScreen, &WsfbDGAFunctions,
1458			fPtr->pDGAMode, fPtr->nDGAMode));
1459}
1460#endif
1461
1462static Bool
1463WsfbDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op,
1464    pointer ptr)
1465{
1466	xorgHWFlags *flag;
1467
1468	switch (op) {
1469	case GET_REQUIRED_HW_INTERFACES:
1470		flag = (CARD32*)ptr;
1471		(*flag) = 0;
1472		return TRUE;
1473	default:
1474		return FALSE;
1475	}
1476}
1477
1478