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