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