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