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