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