wsfb_driver.c revision 7b613f18
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	{ OPTION_HW_CURSOR, "HWCursor", OPTV_BOOLEAN, {1}, FALSE},
195	{ -1, NULL, OPTV_NONE, {0}, FALSE}
196};
197
198#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) <= 6
199/* Symbols needed from other modules */
200static const char *fbSymbols[] = {
201	"fbPictureInit",
202	"fbScreenInit",
203	NULL
204};
205static const char *shadowSymbols[] = {
206	"shadowAdd",
207	"shadowSetup",
208	"shadowUpdatePacked",
209	"shadowUpdatePackedWeak",
210	"shadowUpdateRotatePacked",
211	"shadowUpdateRotatePackedWeak",
212	NULL
213};
214
215static const char *ramdacSymbols[] = {
216	"xf86CreateCursorInfoRec",
217	"xf86DestroyCursorInfoRec",
218	"xf86InitCursor",
219	NULL
220};
221#endif
222
223static XF86ModuleVersionInfo WsfbVersRec = {
224	"wsfb",
225	MODULEVENDORSTRING,
226	MODINFOSTRING1,
227	MODINFOSTRING2,
228	XORG_VERSION_CURRENT,
229	PACKAGE_VERSION_MAJOR,
230	PACKAGE_VERSION_MINOR,
231	PACKAGE_VERSION_PATCHLEVEL,
232	ABI_CLASS_VIDEODRV,
233	ABI_VIDEODRV_VERSION,
234	NULL,
235	{0, 0, 0, 0}
236};
237
238_X_EXPORT XF86ModuleData wsfbModuleData = { &WsfbVersRec, WsfbSetup, NULL };
239
240static pointer
241WsfbSetup(pointer module, pointer opts, int *errmaj, int *errmin)
242{
243	static Bool setupDone = FALSE;
244	const char *osname;
245
246	/* Check that we're being loaded on a OpenBSD or NetBSD system. */
247	LoaderGetOS(&osname, NULL, NULL, NULL);
248	if (!osname || (strcmp(osname, "openbsd") != 0 &&
249	                strcmp(osname, "netbsd") != 0)) {
250		if (errmaj)
251			*errmaj = LDR_BADOS;
252		if (errmin)
253			*errmin = 0;
254		return NULL;
255	}
256	if (!setupDone) {
257		setupDone = TRUE;
258		xf86AddDriver(&WSFB, module, HaveDriverFuncs);
259		LoaderRefSymLists(fbSymbols, shadowSymbols, ramdacSymbols,
260		    NULL);
261		return (pointer)1;
262	} else {
263		if (errmaj != NULL)
264			*errmaj = LDR_ONCEONLY;
265		return NULL;
266	}
267}
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	free(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(const char *dev)
306{
307	int fd = -1;
308
309	/* Try argument from xorg.conf 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	const 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	free(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, bitsperpixel, wstype;
410	const char *dev;
411	char *mod = NULL, *s;
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 = lb * info.height;
502
503	}
504	/*
505	 * Allocate room for saving the colormap.
506	 */
507	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
508	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
509		fPtr->saved_cmap.red =
510		    (unsigned char *)malloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
511		if (fPtr->saved_cmap.red == NULL) {
512			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
513			    "Cannot malloc %d bytes\n",
514			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
515			return FALSE;
516		}
517		fPtr->saved_cmap.green =
518		    (unsigned char *)malloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
519		if (fPtr->saved_cmap.green == NULL) {
520			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
521			    "Cannot malloc %d bytes\n",
522			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
523			free(fPtr->saved_cmap.red);
524			return FALSE;
525		}
526		fPtr->saved_cmap.blue =
527		    (unsigned char *)malloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
528		if (fPtr->saved_cmap.blue == NULL) {
529			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
530			    "Cannot malloc %d bytes\n",
531			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
532			free(fPtr->saved_cmap.red);
533			free(fPtr->saved_cmap.green);
534			return FALSE;
535		}
536	}
537
538	/* Handle depth */
539	default_depth = fPtr->fbi.fbi_bitsperpixel <= 24 ? fPtr->fbi.fbi_bitsperpixel : 24;
540	bitsperpixel = fPtr->fbi.fbi_bitsperpixel;
541#if defined(__NetBSD__) && defined(WSDISPLAY_TYPE_LUNA)
542	if (wstype == WSDISPLAY_TYPE_LUNA) {
543		/*
544		 * XXX
545		 * LUNA's color framebuffers support 4bpp or 8bpp
546		 * but they have multiple 1bpp VRAM planes like ancient VGA.
547		 * For now, Xorg server supports only the first one plane
548		 * as 1bpp monochrome server.
549		 *
550		 * Note OpenBSD/luna88k workarounds this by switching depth
551		 * and palette settings by WSDISPLAYIO_SETGFXMODE ioctl.
552		 */
553		default_depth = 1;
554		bitsperpixel = 1;
555	}
556#endif
557	if (!xf86SetDepthBpp(pScrn, default_depth, default_depth,
558		bitsperpixel,
559		bitsperpixel >= 24 ? Support24bppFb|Support32bppFb : 0))
560		return FALSE;
561
562	/* Check consistency. */
563	if (pScrn->bitsPerPixel != bitsperpixel) {
564		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
565		    "specified depth (%d) or bpp (%d) doesn't match "
566		    "framebuffer depth (%d)\n", pScrn->depth,
567		    pScrn->bitsPerPixel, bitsperpixel);
568		return FALSE;
569	}
570	xf86PrintDepthBpp(pScrn);
571
572	/* Get the depth24 pixmap format. */
573	if (pScrn->depth == 24 && pix24bpp == 0)
574		pix24bpp = xf86GetBppFromDepth(pScrn, 24);
575
576	/* Color weight */
577	if (fPtr->fbi.fbi_pixeltype == WSFB_RGB) {
578		rgb zeros = { 0, 0, 0 }, masks;
579
580		if (fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_size > 0) {
581			uint32_t msk;
582
583			msk = 0xffffffff;
584			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_size;
585			msk = ~msk;
586			masks.red = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_offset;
587
588			msk = 0xffffffff;
589			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_size;
590			msk = ~msk;
591			masks.green = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_offset;
592
593			msk = 0xffffffff;
594			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_size;
595			msk = ~msk;
596			masks.blue = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset;
597			xf86Msg(X_INFO, "masks generated: %08lx %08lx %08lx\n",
598			    (unsigned long)masks.red,
599			    (unsigned long)masks.green,
600			    (unsigned long)masks.blue);
601		} else {
602			masks.red = 0;
603			masks.green = 0;
604			masks.blue = 0;
605		}
606
607		if (!xf86SetWeight(pScrn, zeros, masks))
608			return FALSE;
609	}
610
611	/* Visual init */
612	if (!xf86SetDefaultVisual(pScrn, -1))
613		return FALSE;
614
615	/* We don't currently support DirectColor at > 8bpp . */
616	if (pScrn->depth > 8 && pScrn->defaultVisual != TrueColor) {
617		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Given default visual"
618			   " (%s) is not supported at depth %d\n",
619			   xf86GetVisualName(pScrn->defaultVisual),
620			   pScrn->depth);
621		return FALSE;
622	}
623
624	xf86SetGamma(pScrn,zeros);
625
626	pScrn->progClock = TRUE;
627	pScrn->rgbBits   = (pScrn->depth >= 8) ? 8 : pScrn->depth;
628	pScrn->chipset   = "wsfb";
629	pScrn->videoRam  = fPtr->fbi.fbi_fbsize;
630
631	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Vidmem: %dk\n",
632		   pScrn->videoRam/1024);
633
634	/* Handle options. */
635	xf86CollectOptions(pScrn, NULL);
636	fPtr->Options = (OptionInfoRec *)malloc(sizeof(WsfbOptions));
637	if (fPtr->Options == NULL)
638		return FALSE;
639	memcpy(fPtr->Options, WsfbOptions, sizeof(WsfbOptions));
640	xf86ProcessOptions(pScrn->scrnIndex, fPtr->pEnt->device->options,
641			   fPtr->Options);
642
643	/* Use shadow framebuffer by default, on depth >= 8 */
644	xf86Msg(X_INFO, "fbi_flags: %x\n", fPtr->fbi.fbi_flags);
645	if ((pScrn->depth >= 8) &&
646	   ((fPtr->fbi.fbi_flags & WSFB_VRAM_IS_RAM) == 0)) {
647		fPtr->shadowFB = xf86ReturnOptValBool(fPtr->Options,
648						      OPTION_SHADOW_FB, TRUE);
649	} else
650		if (xf86ReturnOptValBool(fPtr->Options,
651					 OPTION_SHADOW_FB, FALSE)) {
652			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
653				   "Shadow FB option ignored on depth < 8");
654		}
655
656	/* Rotation */
657	fPtr->rotate = WSFB_ROTATE_NONE;
658	if ((s = xf86GetOptValString(fPtr->Options, OPTION_ROTATE))) {
659		if (pScrn->depth >= 8) {
660			if (!xf86NameCmp(s, "CW")) {
661				fPtr->shadowFB = TRUE;
662				fPtr->rotate = WSFB_ROTATE_CW;
663				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
664				    "Rotating screen clockwise\n");
665			} else if (!xf86NameCmp(s, "CCW")) {
666				fPtr->shadowFB = TRUE;
667				fPtr->rotate = WSFB_ROTATE_CCW;
668				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
669				    "Rotating screen counter clockwise\n");
670			} else if (!xf86NameCmp(s, "UD")) {
671				fPtr->shadowFB = TRUE;
672				fPtr->rotate = WSFB_ROTATE_UD;
673				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
674				    "Rotating screen upside down\n");
675			} else {
676				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
677				    "\"%s\" is not a valid value for Option "
678				    "\"Rotate\"\n", s);
679				xf86DrvMsg(pScrn->scrnIndex, X_INFO,
680				    "Valid options are \"CW\", \"CCW\","
681				    " or \"UD\"\n");
682			}
683		} else {
684			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
685			    "Option \"Rotate\" ignored on depth < 8");
686		}
687	}
688
689	/* Fake video mode struct. */
690	mode = (DisplayModePtr)malloc(sizeof(DisplayModeRec));
691	mode->prev = mode;
692	mode->next = mode;
693	mode->name = "wsfb current mode";
694	mode->status = MODE_OK;
695	mode->type = M_T_BUILTIN;
696	mode->Clock = 0;
697	mode->HDisplay = fPtr->fbi.fbi_width;
698	mode->HSyncStart = 0;
699	mode->HSyncEnd = 0;
700	mode->HTotal = 0;
701	mode->HSkew = 0;
702	mode->VDisplay = fPtr->fbi.fbi_height;
703	mode->VSyncStart = 0;
704	mode->VSyncEnd = 0;
705	mode->VTotal = 0;
706	mode->VScan = 0;
707	mode->Flags = 0;
708	if (pScrn->modes != NULL) {
709		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
710		   "Ignoring mode specification from screen section\n");
711	}
712	pScrn->currentMode = pScrn->modes = mode;
713	pScrn->virtualX = fPtr->fbi.fbi_width;
714	pScrn->virtualY = fPtr->fbi.fbi_height;
715	pScrn->displayWidth = pScrn->virtualX;
716
717	/* Set the display resolution. */
718	xf86SetDpi(pScrn, 0, 0);
719
720	from = X_DEFAULT;
721	fPtr->HWCursor = TRUE;
722	if (xf86GetOptValBool(fPtr->Options, OPTION_HW_CURSOR, &fPtr->HWCursor))
723		from = X_CONFIG;
724	if (xf86ReturnOptValBool(fPtr->Options, OPTION_SW_CURSOR, FALSE)) {
725		from = X_CONFIG;
726		fPtr->HWCursor = FALSE;
727	}
728	xf86DrvMsg(pScrn->scrnIndex, from, "Using %s cursor\n",
729		fPtr->HWCursor ? "HW" : "SW");
730
731	/* Load bpp-specific modules. */
732	switch(pScrn->bitsPerPixel) {
733	case 1:
734	case 4:
735	default:
736		mod = "fb";
737		break;
738	}
739
740
741	/* Load shadow if needed. */
742	if (fPtr->shadowFB) {
743		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
744			   "Using \"Shadow Framebuffer\"\n");
745		if (xf86LoadSubModule(pScrn, "shadow") == NULL) {
746			WsfbFreeRec(pScrn);
747			return FALSE;
748		}
749	}
750
751	if (mod && xf86LoadSubModule(pScrn, mod) == NULL) {
752		WsfbFreeRec(pScrn);
753		return FALSE;
754	}
755
756	if (xf86LoadSubModule(pScrn, "ramdac") == NULL) {
757		WsfbFreeRec(pScrn);
758		return FALSE;
759        }
760
761	if (mod) {
762		if (reqSym) {
763			xf86LoaderReqSymbols(reqSym, NULL);
764		} else {
765			xf86LoaderReqSymLists(fbSymbols, NULL);
766		}
767	}
768	TRACE_EXIT("PreInit");
769	return TRUE;
770}
771
772static Bool
773WsfbCreateScreenResources(ScreenPtr pScreen)
774{
775	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
776	WsfbPtr fPtr = WSFBPTR(pScrn);
777	PixmapPtr pPixmap;
778	Bool ret;
779
780	pScreen->CreateScreenResources = fPtr->CreateScreenResources;
781	ret = pScreen->CreateScreenResources(pScreen);
782	pScreen->CreateScreenResources = WsfbCreateScreenResources;
783
784	if (!ret)
785		return FALSE;
786
787	pPixmap = pScreen->GetScreenPixmap(pScreen);
788
789	if (!shadowAdd(pScreen, pPixmap, fPtr->rotate ?
790		shadowUpdateRotatePackedWeak() : shadowUpdatePackedWeak(),
791		WsfbWindowLinear, fPtr->rotate, NULL)) {
792		return FALSE;
793	}
794	return TRUE;
795}
796
797
798static Bool
799WsfbShadowInit(ScreenPtr pScreen)
800{
801	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
802	WsfbPtr fPtr = WSFBPTR(pScrn);
803
804	if (!shadowSetup(pScreen))
805		return FALSE;
806	fPtr->CreateScreenResources = pScreen->CreateScreenResources;
807	pScreen->CreateScreenResources = WsfbCreateScreenResources;
808
809	return TRUE;
810}
811
812static Bool
813WsfbScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
814{
815	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
816	WsfbPtr fPtr = WSFBPTR(pScrn);
817	VisualPtr visual;
818	int ret, flags, ncolors;
819	int wsmode = WSDISPLAYIO_MODE_DUMBFB;
820	int wstype;
821	size_t len;
822
823	TRACE_ENTER("WsfbScreenInit");
824#if DEBUG
825	ErrorF("\tbitsPerPixel=%d, depth=%d, defaultVisual=%s\n"
826	       "\tmask: %x,%x,%x, offset: %u,%u,%u\n",
827	       pScrn->bitsPerPixel,
828	       pScrn->depth,
829	       xf86GetVisualName(pScrn->defaultVisual),
830	       pScrn->mask.red,pScrn->mask.green,pScrn->mask.blue,
831	       pScrn->offset.red,pScrn->offset.green,pScrn->offset.blue);
832#endif
833	switch (fPtr->fbi.fbi_bitsperpixel) {
834	case 1:
835	case 4:
836	case 8:
837		len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
838		break;
839	case 16:
840		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
841			xf86Msg(X_ERROR, "Bogus stride == width in 16bit colour\n");
842			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * sizeof(short);
843		} else {
844			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
845		}
846		break;
847	case 24:
848		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
849			xf86Msg(X_ERROR, "Bogus stride == width in 24bit colour\n");
850			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * 3;
851		} else {
852			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
853		}
854		break;
855	case 32:
856		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
857			xf86Msg(X_ERROR, "Bogus stride == width in 32bit colour\n");
858			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * sizeof(int);
859		} else {
860			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
861		}
862		break;
863	default:
864		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
865			   "unsupported depth %d\n", fPtr->fbi.fbi_bitsperpixel);
866		return FALSE;
867	}
868	/* Switch to graphics mode - required before mmap. */
869	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &wsmode) == -1) {
870		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
871			   "ioctl WSDISPLAYIO_SMODE: %s\n",
872			   strerror(errno));
873		return FALSE;
874	}
875	/* Get wsdisplay type to handle quirks */
876	if (ioctl(fPtr->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
877		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
878			   "ioctl WSDISPLAY_GTYPE: %s\n",
879			   strerror(errno));
880		return FALSE;
881	}
882	fPtr->fbmem = wsfb_mmap(len, 0, fPtr->fd);
883
884	if (fPtr->fbmem == NULL) {
885		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
886			   "wsfb_mmap: %s\n", strerror(errno));
887		return FALSE;
888	}
889	fPtr->fbmem_len = len;
890
891	WsfbSave(pScrn);
892	pScrn->vtSema = TRUE;
893
894	/* MI layer */
895	miClearVisualTypes();
896	if (pScrn->bitsPerPixel > 8) {
897		if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
898				      pScrn->rgbBits, TrueColor))
899			return FALSE;
900	} else {
901		if (!miSetVisualTypes(pScrn->depth,
902				      miGetDefaultVisualMask(pScrn->depth),
903				      pScrn->rgbBits, pScrn->defaultVisual))
904			return FALSE;
905	}
906	if (!miSetPixmapDepths())
907		return FALSE;
908
909	if (fPtr->rotate == WSFB_ROTATE_CW
910	    || fPtr->rotate == WSFB_ROTATE_CCW) {
911		int tmp = pScrn->virtualX;
912		pScrn->virtualX = pScrn->displayWidth = pScrn->virtualY;
913		pScrn->virtualY = tmp;
914	}
915	if (fPtr->rotate && !fPtr->PointerMoved) {
916		fPtr->PointerMoved = pScrn->PointerMoved;
917		pScrn->PointerMoved = WsfbPointerMoved;
918	}
919
920	fPtr->fbstart = fPtr->fbmem;
921#ifdef	WSDISPLAY_TYPE_LUNA
922	if (wstype == WSDISPLAY_TYPE_LUNA) {
923		/*
924		 * XXX
925		 * LUNA's FB seems to have 64 dot (8 byte) offset.
926		 * This might be able to be changed in kernel lunafb driver,
927		 * but current setting was pulled from 4.4BSD-Lite2/luna68k.
928		 */
929		fPtr->fbstart += 8;
930	}
931#endif
932
933	if (fPtr->shadowFB) {
934		fPtr->shadow = calloc(1, pScrn->virtualX * pScrn->virtualY *
935		    pScrn->bitsPerPixel/8);
936
937		if (!fPtr->shadow) {
938			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
939			    "Failed to allocate shadow framebuffer\n");
940			return FALSE;
941		}
942	}
943
944	switch (pScrn->bitsPerPixel) {
945	case 1:
946		ret = fbScreenInit(pScreen,
947		    fPtr->fbstart,
948		    pScrn->virtualX, pScrn->virtualY,
949		    pScrn->xDpi, pScrn->yDpi,
950		    fPtr->fbi.fbi_stride * 8, pScrn->bitsPerPixel);
951		break;
952	case 4:
953	case 8:
954	case 16:
955	case 24:
956	case 32:
957		ret = fbScreenInit(pScreen,
958		    fPtr->shadowFB ? fPtr->shadow : fPtr->fbstart,
959		    pScrn->virtualX, pScrn->virtualY,
960		    pScrn->xDpi, pScrn->yDpi,
961		    pScrn->displayWidth, pScrn->bitsPerPixel);
962		break;
963	default:
964		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
965			   "Unsupported bpp: %d", pScrn->bitsPerPixel);
966		return FALSE;
967	} /* case */
968
969	if (!ret)
970		return FALSE;
971
972	if (pScrn->bitsPerPixel > 8) {
973		/* Fixup RGB ordering. */
974		visual = pScreen->visuals + pScreen->numVisuals;
975		while (--visual >= pScreen->visuals) {
976			if ((visual->class | DynamicClass) == DirectColor) {
977				visual->offsetRed   = pScrn->offset.red;
978				visual->offsetGreen = pScrn->offset.green;
979				visual->offsetBlue  = pScrn->offset.blue;
980				visual->redMask     = pScrn->mask.red;
981				visual->greenMask   = pScrn->mask.green;
982				visual->blueMask    = pScrn->mask.blue;
983			}
984		}
985	}
986
987	if (pScrn->bitsPerPixel >= 8) {
988		if (!fbPictureInit(pScreen, NULL, 0))
989			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
990				   "RENDER extension initialisation failed.");
991	}
992	if (fPtr->shadowFB && !WsfbShadowInit(pScreen)) {
993		xf86DrvMsg(scrnIndex, X_ERROR,
994		    "shadow framebuffer initialization failed\n");
995		return FALSE;
996	}
997
998#ifdef XFreeXDGA
999	if (!fPtr->rotate)
1000		WsfbDGAInit(pScrn, pScreen);
1001	else
1002		xf86DrvMsg(scrnIndex, X_INFO, "Rotated display, "
1003		    "disabling DGA\n");
1004#endif
1005	if (fPtr->rotate) {
1006		xf86DrvMsg(scrnIndex, X_INFO, "Enabling Driver Rotation, "
1007		    "disabling RandR\n");
1008		xf86DisableRandR();
1009		if (pScrn->bitsPerPixel == 24)
1010			xf86DrvMsg(scrnIndex, X_WARNING,
1011			    "Rotation might be broken in 24 bpp\n");
1012	}
1013
1014	xf86SetBlackWhitePixels(pScreen);
1015	miInitializeBackingStore(pScreen);
1016	xf86SetBackingStore(pScreen);
1017
1018	/* Software cursor. */
1019	miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
1020
1021	/* check for hardware cursor support */
1022	if (fPtr->HWCursor)
1023		WsfbSetupCursor(pScreen);
1024
1025	/*
1026	 * Colormap
1027	 *
1028	 * Note that, even on less than 8 bit depth frame buffers, we
1029	 * expect the colormap to be programmable with 8 bit values.
1030	 * As of now, this is indeed the case on all OpenBSD supported
1031	 * graphics hardware.
1032	 */
1033	if (!miCreateDefColormap(pScreen))
1034		return FALSE;
1035	flags = CMAP_RELOAD_ON_MODE_SWITCH;
1036
1037	ncolors = 0;
1038	if (fPtr->fbi.fbi_pixeltype == WSFB_CI) {
1039		ncolors = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1040	}
1041
1042	/* On StaticGray visuals, fake a 256 entries colormap. */
1043	if (ncolors == 0)
1044		ncolors = 256;
1045
1046	if(!xf86HandleColormaps(pScreen, ncolors, 8, WsfbLoadPalette,
1047				NULL, flags))
1048		return FALSE;
1049
1050#if defined(__NetBSD__) && defined(WSDISPLAY_TYPE_LUNA)
1051	if (wstype == WSDISPLAY_TYPE_LUNA) {
1052		ncolors = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1053		if (ncolors > 0) {
1054			/*
1055			 * Override palette to use 4bpp/8bpp framebuffers as
1056			 * monochrome server by using only the first plane.
1057			 * See also comment in WsfbPreInit().
1058			 */
1059			struct wsdisplay_cmap cmap;
1060			uint8_t r[256], g[256], b[256];
1061			int p;
1062
1063			for (p = 0; p < ncolors; p++)
1064				r[p] = g[p] = b[p] = (p & 1) ? 0xff : 0;
1065			cmap.index = 0;
1066			cmap.count = ncolors;
1067			cmap.red   = r;
1068			cmap.green = g;
1069			cmap.blue  = b;
1070			if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP, &cmap) == -1) {
1071				xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1072				   "ioctl WSDISPLAYIO_PUTCMAP: %s\n",
1073				   strerror(errno));
1074			}
1075		}
1076	}
1077#endif
1078
1079	pScreen->SaveScreen = WsfbSaveScreen;
1080
1081#ifdef XvExtension
1082	{
1083		XF86VideoAdaptorPtr *ptr;
1084
1085		int n = xf86XVListGenericAdaptors(pScrn,&ptr);
1086		if (n) {
1087			xf86XVScreenInit(pScreen,ptr,n);
1088		}
1089	}
1090#endif
1091
1092	/* Wrap the current CloseScreen function. */
1093	fPtr->CloseScreen = pScreen->CloseScreen;
1094	pScreen->CloseScreen = WsfbCloseScreen;
1095
1096	TRACE_EXIT("WsfbScreenInit");
1097	return TRUE;
1098}
1099
1100static Bool
1101WsfbCloseScreen(int scrnIndex, ScreenPtr pScreen)
1102{
1103	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1104	PixmapPtr pPixmap;
1105	WsfbPtr fPtr = WSFBPTR(pScrn);
1106
1107
1108	TRACE_ENTER("WsfbCloseScreen");
1109
1110	pPixmap = pScreen->GetScreenPixmap(pScreen);
1111	if (fPtr->shadowFB)
1112		shadowRemove(pScreen, pPixmap);
1113
1114	if (pScrn->vtSema) {
1115		WsfbRestore(pScrn);
1116		if (munmap(fPtr->fbmem, fPtr->fbmem_len) == -1) {
1117			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1118				   "munmap: %s\n", strerror(errno));
1119		}
1120
1121		fPtr->fbmem = NULL;
1122	}
1123#ifdef XFreeXDGA
1124	if (fPtr->pDGAMode) {
1125		free(fPtr->pDGAMode);
1126		fPtr->pDGAMode = NULL;
1127		fPtr->nDGAMode = 0;
1128	}
1129#endif
1130	pScrn->vtSema = FALSE;
1131
1132	/* Unwrap CloseScreen. */
1133	pScreen->CloseScreen = fPtr->CloseScreen;
1134	TRACE_EXIT("WsfbCloseScreen");
1135	return (*pScreen->CloseScreen)(scrnIndex, pScreen);
1136}
1137
1138static void *
1139WsfbWindowLinear(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
1140		CARD32 *size, void *closure)
1141{
1142	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1143	WsfbPtr fPtr = WSFBPTR(pScrn);
1144
1145	/*
1146	 * XXX
1147	 * This should never happen. Is it really necessary?
1148	 */
1149	if (fPtr->fbi.fbi_stride)
1150		*size = fPtr->fbi.fbi_stride;
1151	else {
1152		if (ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES, size) == -1)
1153			return NULL;
1154		fPtr->fbi.fbi_stride = *size;
1155	}
1156	return ((CARD8 *)fPtr->fbmem + row * fPtr->fbi.fbi_stride + offset);
1157}
1158
1159static void
1160WsfbPointerMoved(int index, int x, int y)
1161{
1162    ScrnInfoPtr pScrn = xf86Screens[index];
1163    WsfbPtr fPtr = WSFBPTR(pScrn);
1164    int newX, newY;
1165
1166    switch (fPtr->rotate)
1167    {
1168    case WSFB_ROTATE_CW:
1169	/* 90 degrees CW rotation. */
1170	newX = pScrn->pScreen->height - y - 1;
1171	newY = x;
1172	break;
1173
1174    case WSFB_ROTATE_CCW:
1175	/* 90 degrees CCW rotation. */
1176	newX = y;
1177	newY = pScrn->pScreen->width - x - 1;
1178	break;
1179
1180    case WSFB_ROTATE_UD:
1181	/* 180 degrees UD rotation. */
1182	newX = pScrn->pScreen->width - x - 1;
1183	newY = pScrn->pScreen->height - y - 1;
1184	break;
1185
1186    default:
1187	/* No rotation. */
1188	newX = x;
1189	newY = y;
1190	break;
1191    }
1192
1193    /* Pass adjusted pointer coordinates to wrapped PointerMoved function. */
1194    (*fPtr->PointerMoved)(index, newX, newY);
1195}
1196
1197static Bool
1198WsfbEnterVT(int scrnIndex, int flags)
1199{
1200	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1201	WsfbPtr fPtr = WSFBPTR(pScrn);
1202	int mode;
1203
1204	TRACE_ENTER("EnterVT");
1205	pScrn->vtSema = TRUE;
1206
1207	/* Restore the graphics mode. */
1208	mode = WSDISPLAYIO_MODE_DUMBFB;
1209	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1210		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1211			   "error setting graphics mode %s\n", strerror(errno));
1212	}
1213
1214	TRACE_EXIT("EnterVT");
1215	return TRUE;
1216}
1217
1218static void
1219WsfbLeaveVT(int scrnIndex, int flags)
1220{
1221	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1222	WsfbPtr fPtr = WSFBPTR(pScrn);
1223	int mode;
1224
1225	TRACE_ENTER("LeaveVT");
1226
1227	/*
1228	 * stuff to do:
1229	 * - turn off hw cursor
1230	 * - restore colour map if WSFB_CI
1231	 * - ioctl(WSDISPLAYIO_MODE_EMUL) to notify the kernel driver that
1232	 *   we're backing off
1233	 */
1234
1235	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
1236	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
1237		/* reset colormap for text mode */
1238		if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP,
1239			  &(fPtr->saved_cmap)) == -1) {
1240			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1241				   "error restoring colormap %s\n",
1242				   strerror(errno));
1243		}
1244	}
1245
1246	/* Restore the text mode. */
1247	mode = WSDISPLAYIO_MODE_EMUL;
1248	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1249		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1250			   "error setting text mode %s\n", strerror(errno));
1251	}
1252
1253	pScrn->vtSema = FALSE;
1254	TRACE_EXIT("LeaveVT");
1255}
1256
1257static Bool
1258WsfbSwitchMode(int scrnIndex, DisplayModePtr mode, int flags)
1259{
1260#if DEBUG
1261	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1262#endif
1263
1264	TRACE_ENTER("SwitchMode");
1265	/* Nothing else to do. */
1266	return TRUE;
1267}
1268
1269static int
1270WsfbValidMode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
1271{
1272#if DEBUG
1273	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1274#endif
1275
1276	TRACE_ENTER("ValidMode");
1277	return MODE_OK;
1278}
1279
1280static void
1281WsfbLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
1282	       LOCO *colors, VisualPtr pVisual)
1283{
1284	WsfbPtr fPtr = WSFBPTR(pScrn);
1285	struct wsdisplay_cmap cmap;
1286	unsigned char red[256],green[256],blue[256];
1287	int i, indexMin=256, indexMax=0;
1288
1289	TRACE_ENTER("LoadPalette");
1290
1291	/* nothing to do if there is no color palette support */
1292	if (fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries == 0)
1293		return;
1294
1295	cmap.count   = 1;
1296	cmap.red   = red;
1297	cmap.green = green;
1298	cmap.blue  = blue;
1299
1300	if (numColors == 1) {
1301		/* Optimisation */
1302		cmap.index = indices[0];
1303		red[0]   = colors[indices[0]].red;
1304		green[0] = colors[indices[0]].green;
1305		blue[0]  = colors[indices[0]].blue;
1306		if (ioctl(fPtr->fd,WSDISPLAYIO_PUTCMAP, &cmap) == -1)
1307			ErrorF("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
1308	} else {
1309		/*
1310		 * Change all colors in 2 ioctls
1311		 * and limit the data to be transfered.
1312		 */
1313		for (i = 0; i < numColors; i++) {
1314			if (indices[i] < indexMin)
1315				indexMin = indices[i];
1316			if (indices[i] > indexMax)
1317				indexMax = indices[i];
1318		}
1319		cmap.index = indexMin;
1320		cmap.count = indexMax - indexMin + 1;
1321		cmap.red = &red[indexMin];
1322		cmap.green = &green[indexMin];
1323		cmap.blue = &blue[indexMin];
1324		/* Get current map. */
1325		if (ioctl(fPtr->fd, WSDISPLAYIO_GETCMAP, &cmap) == -1)
1326			ErrorF("ioctl FBIOGETCMAP: %s\n", strerror(errno));
1327		/* Change the colors that require updating. */
1328		for (i = 0; i < numColors; i++) {
1329			red[indices[i]]   = colors[indices[i]].red;
1330			green[indices[i]] = colors[indices[i]].green;
1331			blue[indices[i]]  = colors[indices[i]].blue;
1332		}
1333		/* Write the colormap back. */
1334		if (ioctl(fPtr->fd,WSDISPLAYIO_PUTCMAP, &cmap) == -1)
1335			ErrorF("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
1336	}
1337	TRACE_EXIT("LoadPalette");
1338}
1339
1340static Bool
1341WsfbSaveScreen(ScreenPtr pScreen, int mode)
1342{
1343	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1344	WsfbPtr fPtr = WSFBPTR(pScrn);
1345	int state;
1346
1347	TRACE_ENTER("SaveScreen");
1348
1349	if (!pScrn->vtSema)
1350		return TRUE;
1351
1352	if (mode != SCREEN_SAVER_FORCER) {
1353		state = xf86IsUnblank(mode)?WSDISPLAYIO_VIDEO_ON:
1354		                            WSDISPLAYIO_VIDEO_OFF;
1355		ioctl(fPtr->fd,
1356		      WSDISPLAYIO_SVIDEO, &state);
1357	}
1358	TRACE_EXIT("SaveScreen");
1359	return TRUE;
1360}
1361
1362
1363static void
1364WsfbSave(ScrnInfoPtr pScrn)
1365{
1366	WsfbPtr fPtr = WSFBPTR(pScrn);
1367
1368	TRACE_ENTER("WsfbSave");
1369
1370	/* nothing to save if we don't run in colour-indexed mode */
1371	if (fPtr->fbi.fbi_pixeltype != WSFB_CI)
1372		return;
1373
1374	/* nothing to do if no color palette support */
1375	if (fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries == 0)
1376		return;
1377
1378	fPtr->saved_cmap.index = 0;
1379	fPtr->saved_cmap.count = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1380	if (ioctl(fPtr->fd, WSDISPLAYIO_GETCMAP,
1381		  &(fPtr->saved_cmap)) == -1) {
1382		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1383			   "error saving colormap %s\n", strerror(errno));
1384	}
1385	TRACE_EXIT("WsfbSave");
1386
1387}
1388
1389static void
1390WsfbRestore(ScrnInfoPtr pScrn)
1391{
1392	WsfbPtr fPtr = WSFBPTR(pScrn);
1393	int mode;
1394
1395	TRACE_ENTER("WsfbRestore");
1396
1397	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
1398	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
1399		/* reset colormap for text mode */
1400		if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP,
1401			  &(fPtr->saved_cmap)) == -1) {
1402			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1403				   "error restoring colormap %s\n",
1404				   strerror(errno));
1405		}
1406	}
1407
1408	/* Clear the screen. */
1409	memset(fPtr->fbmem, 0, fPtr->fbmem_len);
1410
1411	/* Restore the text mode. */
1412	mode = WSDISPLAYIO_MODE_EMUL;
1413	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1414		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1415			   "error setting text mode %s\n", strerror(errno));
1416	}
1417	TRACE_EXIT("WsfbRestore");
1418}
1419
1420#ifdef XFreeXDGA
1421/***********************************************************************
1422 * DGA stuff
1423 ***********************************************************************/
1424
1425static Bool
1426WsfbDGAOpenFramebuffer(ScrnInfoPtr pScrn, char **DeviceName,
1427		       unsigned char **ApertureBase, int *ApertureSize,
1428		       int *ApertureOffset, int *flags)
1429{
1430	*DeviceName = NULL;		/* No special device */
1431	*ApertureBase = (unsigned char *)(pScrn->memPhysBase);
1432	*ApertureSize = pScrn->videoRam;
1433	*ApertureOffset = pScrn->fbOffset;
1434	*flags = 0;
1435
1436	return TRUE;
1437}
1438
1439static Bool
1440WsfbDGASetMode(ScrnInfoPtr pScrn, DGAModePtr pDGAMode)
1441{
1442	DisplayModePtr pMode;
1443	int scrnIdx = pScrn->pScreen->myNum;
1444	int frameX0, frameY0;
1445
1446	if (pDGAMode) {
1447		pMode = pDGAMode->mode;
1448		frameX0 = frameY0 = 0;
1449	} else {
1450		if (!(pMode = pScrn->currentMode))
1451			return TRUE;
1452
1453		frameX0 = pScrn->frameX0;
1454		frameY0 = pScrn->frameY0;
1455	}
1456
1457	if (!(*pScrn->SwitchMode)(scrnIdx, pMode, 0))
1458		return FALSE;
1459	(*pScrn->AdjustFrame)(scrnIdx, frameX0, frameY0, 0);
1460
1461	return TRUE;
1462}
1463
1464static void
1465WsfbDGASetViewport(ScrnInfoPtr pScrn, int x, int y, int flags)
1466{
1467	(*pScrn->AdjustFrame)(pScrn->pScreen->myNum, x, y, flags);
1468}
1469
1470static int
1471WsfbDGAGetViewport(ScrnInfoPtr pScrn)
1472{
1473	return (0);
1474}
1475
1476static DGAFunctionRec WsfbDGAFunctions =
1477{
1478	WsfbDGAOpenFramebuffer,
1479	NULL,       /* CloseFramebuffer */
1480	WsfbDGASetMode,
1481	WsfbDGASetViewport,
1482	WsfbDGAGetViewport,
1483	NULL,       /* Sync */
1484	NULL,       /* FillRect */
1485	NULL,       /* BlitRect */
1486	NULL,       /* BlitTransRect */
1487};
1488
1489static void
1490WsfbDGAAddModes(ScrnInfoPtr pScrn)
1491{
1492	WsfbPtr fPtr = WSFBPTR(pScrn);
1493	DisplayModePtr pMode = pScrn->modes;
1494	DGAModePtr pDGAMode;
1495
1496	do {
1497		pDGAMode = realloc(fPtr->pDGAMode,
1498				    (fPtr->nDGAMode + 1) * sizeof(DGAModeRec));
1499		if (!pDGAMode)
1500			break;
1501
1502		fPtr->pDGAMode = pDGAMode;
1503		pDGAMode += fPtr->nDGAMode;
1504		(void)memset(pDGAMode, 0, sizeof(DGAModeRec));
1505
1506		++fPtr->nDGAMode;
1507		pDGAMode->mode = pMode;
1508		pDGAMode->flags = DGA_CONCURRENT_ACCESS | DGA_PIXMAP_AVAILABLE;
1509		pDGAMode->byteOrder = pScrn->imageByteOrder;
1510		pDGAMode->depth = pScrn->depth;
1511		pDGAMode->bitsPerPixel = pScrn->bitsPerPixel;
1512		pDGAMode->red_mask = pScrn->mask.red;
1513		pDGAMode->green_mask = pScrn->mask.green;
1514		pDGAMode->blue_mask = pScrn->mask.blue;
1515		pDGAMode->visualClass = pScrn->bitsPerPixel > 8 ?
1516			TrueColor : PseudoColor;
1517		pDGAMode->xViewportStep = 1;
1518		pDGAMode->yViewportStep = 1;
1519		pDGAMode->viewportWidth = pMode->HDisplay;
1520		pDGAMode->viewportHeight = pMode->VDisplay;
1521
1522		if (fPtr->fbi.fbi_stride)
1523			pDGAMode->bytesPerScanline = fPtr->fbi.fbi_stride;
1524		else {
1525			ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES,
1526			      &fPtr->fbi.fbi_stride);
1527			pDGAMode->bytesPerScanline = fPtr->fbi.fbi_stride;
1528		}
1529
1530		pDGAMode->imageWidth = pMode->HDisplay;
1531		pDGAMode->imageHeight =  pMode->VDisplay;
1532		pDGAMode->pixmapWidth = pDGAMode->imageWidth;
1533		pDGAMode->pixmapHeight = pDGAMode->imageHeight;
1534		pDGAMode->maxViewportX = pScrn->virtualX -
1535			pDGAMode->viewportWidth;
1536		pDGAMode->maxViewportY = pScrn->virtualY -
1537			pDGAMode->viewportHeight;
1538
1539		pDGAMode->address = fPtr->fbstart;
1540
1541		pMode = pMode->next;
1542	} while (pMode != pScrn->modes);
1543}
1544
1545static Bool
1546WsfbDGAInit(ScrnInfoPtr pScrn, ScreenPtr pScreen)
1547{
1548	WsfbPtr fPtr = WSFBPTR(pScrn);
1549
1550	if (pScrn->depth < 8)
1551		return FALSE;
1552
1553	if (!fPtr->nDGAMode)
1554		WsfbDGAAddModes(pScrn);
1555
1556	return (DGAInit(pScreen, &WsfbDGAFunctions,
1557			fPtr->pDGAMode, fPtr->nDGAMode));
1558}
1559#endif
1560
1561static Bool
1562WsfbDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op,
1563    pointer ptr)
1564{
1565	xorgHWFlags *flag;
1566
1567	switch (op) {
1568	case GET_REQUIRED_HW_INTERFACES:
1569		flag = (CARD32*)ptr;
1570		(*flag) = 0;
1571		return TRUE;
1572	default:
1573		return FALSE;
1574	}
1575}
1576
1577