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