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