wsfb_driver.c revision 0ef55ed1
1/*
2 * Copyright © 2001-2012 Matthieu Herrb
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 *    - Redistributions of source code must retain the above copyright
10 *      notice, this list of conditions and the following disclaimer.
11 *    - Redistributions in binary form must reproduce the above
12 *      copyright notice, this list of conditions and the following
13 *      disclaimer in the documentation and/or other materials provided
14 *      with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31/*
32 * Based on fbdev.c written by:
33 *
34 * Authors:  Alan Hourihane, <alanh@fairlite.demon.co.uk>
35 *	     Michel Dänzer, <michdaen@iiic.ethz.ch>
36 */
37
38#ifdef HAVE_CONFIG_H
39#include "config.h"
40#endif
41
42#include <errno.h>
43#include <fcntl.h>
44#include <unistd.h>
45#include <sys/ioctl.h>
46#include <sys/types.h>
47#include <sys/mman.h>
48#include <sys/time.h>
49#include <errno.h>
50#include <dev/wscons/wsconsio.h>
51
52/* All drivers need this. */
53#include "xf86.h"
54#include "xf86_OSproc.h"
55#include "xf86_OSlib.h"
56
57#include "mipointer.h"
58#include "mibstore.h"
59#include "micmap.h"
60#include "colormapst.h"
61#include "xf86cmap.h"
62#include "shadow.h"
63#include "dgaproc.h"
64
65/* For visuals */
66#include "fb.h"
67
68#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
69#include "xf86Resources.h"
70#include "xf86RAC.h"
71#endif
72
73#ifdef XvExtension
74#include "xf86xv.h"
75#endif
76
77#include "wsfb.h"
78
79/* #include "wsconsio.h" */
80
81#include <sys/mman.h>
82
83#ifdef X_PRIVSEP
84extern int priv_open_device(const char *);
85#else
86#define priv_open_device(n)    open(n,O_RDWR|O_NONBLOCK|O_EXCL)
87#endif
88
89#if defined(__NetBSD__)
90#define WSFB_DEFAULT_DEV "/dev/ttyE0"
91#else
92#define WSFB_DEFAULT_DEV "/dev/ttyC0"
93#endif
94
95#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) > 6
96#define xf86LoaderReqSymLists(...) do {} while (0)
97#define LoaderRefSymLists(...) do {} while (0)
98#define xf86LoaderReqSymbols(...) do {} while (0)
99#endif
100
101#define DEBUG 0
102
103#if DEBUG
104# define TRACE_ENTER(str)       ErrorF("wsfb: " str " %d\n",pScrn->scrnIndex)
105# define TRACE_EXIT(str)        ErrorF("wsfb: " str " done\n")
106# define TRACE(str)             ErrorF("wsfb trace: " str "\n")
107#else
108# define TRACE_ENTER(str)
109# define TRACE_EXIT(str)
110# define TRACE(str)
111#endif
112
113/* Prototypes */
114static pointer WsfbSetup(pointer, pointer, int *, int *);
115static Bool WsfbGetRec(ScrnInfoPtr);
116static void WsfbFreeRec(ScrnInfoPtr);
117static const OptionInfoRec * WsfbAvailableOptions(int, int);
118static void WsfbIdentify(int);
119static Bool WsfbProbe(DriverPtr, int);
120static Bool WsfbPreInit(ScrnInfoPtr, int);
121static Bool WsfbScreenInit(int, ScreenPtr, int, char **);
122static Bool WsfbCloseScreen(int, ScreenPtr);
123static void *WsfbWindowLinear(ScreenPtr, CARD32, CARD32, int, CARD32 *,
124			      void *);
125static void WsfbPointerMoved(int, int, int);
126static Bool WsfbEnterVT(int, int);
127static void WsfbLeaveVT(int, int);
128static Bool WsfbSwitchMode(int, DisplayModePtr, int);
129static int WsfbValidMode(int, DisplayModePtr, Bool, int);
130static void WsfbLoadPalette(ScrnInfoPtr, int, int *, LOCO *, VisualPtr);
131static Bool WsfbSaveScreen(ScreenPtr, int);
132static void WsfbSave(ScrnInfoPtr);
133static void WsfbRestore(ScrnInfoPtr);
134
135/* DGA stuff */
136#ifdef XFreeXDGA
137static Bool WsfbDGAOpenFramebuffer(ScrnInfoPtr, char **, unsigned char **,
138				   int *, int *, int *);
139static Bool WsfbDGASetMode(ScrnInfoPtr, DGAModePtr);
140static void WsfbDGASetViewport(ScrnInfoPtr, int, int, int);
141static Bool WsfbDGAInit(ScrnInfoPtr, ScreenPtr);
142#endif
143
144static void WsfbShadowUpdateSwap32(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;
413	char *mod = NULL, *s;
414	const char *reqSym = NULL;
415	Gamma zeros = {0.0, 0.0, 0.0};
416	DisplayModePtr mode;
417	MessageType from;
418
419	if (flags & PROBE_DETECT) return FALSE;
420
421	TRACE_ENTER("PreInit");
422
423	if (pScrn->numEntities != 1) return FALSE;
424
425	pScrn->monitor = pScrn->confScreen->monitor;
426
427	WsfbGetRec(pScrn);
428	fPtr = WSFBPTR(pScrn);
429
430	fPtr->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
431
432#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
433	pScrn->racMemFlags = RAC_FB | RAC_COLORMAP | RAC_CURSOR | RAC_VIEWPORT;
434	pScrn->racIoFlags = pScrn->racMemFlags;
435#endif
436
437	dev = xf86FindOptionValue(fPtr->pEnt->device->options, "device");
438	fPtr->fd = wsfb_open(dev);
439	if (fPtr->fd == -1) {
440		return FALSE;
441	}
442
443	if (ioctl(fPtr->fd, WSDISPLAYIO_GET_FBINFO, &fPtr->fbi) != 0) {
444		struct wsdisplay_fbinfo info;
445		struct wsdisplayio_fbinfo *fbi = &fPtr->fbi;
446		int lb;
447
448		xf86Msg(X_WARNING, "ioctl(WSDISPLAYIO_GET_FBINFO) failed, " \
449			"falling back to old method\n");
450		if (ioctl(fPtr->fd, WSDISPLAYIO_GINFO, &info) == -1) {
451			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
452				   "ioctl WSDISPLAY_GINFO: %s\n",
453				   strerror(errno));
454			return FALSE;
455		}
456		if (ioctl(fPtr->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
457			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
458				   "ioctl WSDISPLAY_GTYPE: %s\n",
459				   strerror(errno));
460			return FALSE;
461		}
462		if (ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES, &lb) == -1) {
463			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
464				   "ioctl WSDISPLAYIO_LINEBYTES: %s\n",
465				   strerror(errno));
466			return FALSE;
467		}
468		/* ok, fake up a new style fbinfo */
469		fbi->fbi_width = info.width;
470		fbi->fbi_height = info.height;
471		fbi->fbi_stride = lb;
472		fbi->fbi_bitsperpixel = info.depth;
473		if (info.depth > 16) {
474			fbi->fbi_pixeltype = WSFB_RGB;
475			if (wstype == WSDISPLAY_TYPE_SUN24 ||
476			    wstype == WSDISPLAY_TYPE_SUNCG12 ||
477			    wstype == WSDISPLAY_TYPE_SUNCG14 ||
478			    wstype == WSDISPLAY_TYPE_SUNTCX ||
479			    wstype == WSDISPLAY_TYPE_SUNFFB ||
480			    wstype == WSDISPLAY_TYPE_XVR1000 ||
481			    wstype == WSDISPLAY_TYPE_VC4) {
482				fbi->fbi_subtype.fbi_rgbmasks.red_offset = 0;
483				fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
484				fbi->fbi_subtype.fbi_rgbmasks.green_offset = 8;
485				fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
486				fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 16;
487				fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
488			} else {
489				fbi->fbi_subtype.fbi_rgbmasks.red_offset = 16;
490				fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
491				fbi->fbi_subtype.fbi_rgbmasks.green_offset = 8;
492				fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
493				fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 0;
494				fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
495			}
496			fbi->fbi_subtype.fbi_rgbmasks.alpha_offset = 0;
497			fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 0;
498		} else if (info.depth <= 8) {
499			fbi->fbi_pixeltype = WSFB_CI;
500			fbi->fbi_subtype.fbi_cmapinfo.cmap_entries = info.cmsize;
501		}
502		fbi->fbi_flags = 0;
503		fbi->fbi_fbsize = lb * info.height;
504
505	}
506	/*
507	 * Allocate room for saving the colormap.
508	 */
509	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
510	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
511		fPtr->saved_cmap.red =
512		    (unsigned char *)malloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
513		if (fPtr->saved_cmap.red == NULL) {
514			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
515			    "Cannot malloc %d bytes\n",
516			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
517			return FALSE;
518		}
519		fPtr->saved_cmap.green =
520		    (unsigned char *)malloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
521		if (fPtr->saved_cmap.green == NULL) {
522			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
523			    "Cannot malloc %d bytes\n",
524			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
525			free(fPtr->saved_cmap.red);
526			return FALSE;
527		}
528		fPtr->saved_cmap.blue =
529		    (unsigned char *)malloc(fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
530		if (fPtr->saved_cmap.blue == NULL) {
531			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
532			    "Cannot malloc %d bytes\n",
533			    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries);
534			free(fPtr->saved_cmap.red);
535			free(fPtr->saved_cmap.green);
536			return FALSE;
537		}
538	}
539
540	/* Handle depth */
541	default_depth = fPtr->fbi.fbi_bitsperpixel <= 24 ? fPtr->fbi.fbi_bitsperpixel : 24;
542	bitsperpixel = fPtr->fbi.fbi_bitsperpixel;
543#if defined(__NetBSD__) && defined(WSDISPLAY_TYPE_LUNA)
544	if (wstype == WSDISPLAY_TYPE_LUNA) {
545		/*
546		 * XXX
547		 * LUNA's color framebuffers support 4bpp or 8bpp
548		 * but they have multiple 1bpp VRAM planes like ancient VGA.
549		 * For now, Xorg server supports only the first one plane
550		 * as 1bpp monochrome server.
551		 *
552		 * Note OpenBSD/luna88k workarounds this by switching depth
553		 * and palette settings by WSDISPLAYIO_SETGFXMODE ioctl.
554		 */
555		default_depth = 1;
556		bitsperpixel = 1;
557	}
558#endif
559	if (!xf86SetDepthBpp(pScrn, default_depth, default_depth,
560		bitsperpixel,
561		bitsperpixel >= 24 ? Support24bppFb|Support32bppFb : 0))
562		return FALSE;
563
564	/* Check consistency. */
565	if (pScrn->bitsPerPixel != bitsperpixel) {
566		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
567		    "specified depth (%d) or bpp (%d) doesn't match "
568		    "framebuffer depth (%d)\n", pScrn->depth,
569		    pScrn->bitsPerPixel, bitsperpixel);
570		return FALSE;
571	}
572	xf86PrintDepthBpp(pScrn);
573
574	/* Get the depth24 pixmap format. */
575	if (pScrn->depth == 24 && pix24bpp == 0)
576		pix24bpp = xf86GetBppFromDepth(pScrn, 24);
577
578	/* Handle options. */
579	xf86CollectOptions(pScrn, NULL);
580	fPtr->Options = (OptionInfoRec *)malloc(sizeof(WsfbOptions));
581	if (fPtr->Options == NULL)
582		return FALSE;
583	memcpy(fPtr->Options, WsfbOptions, sizeof(WsfbOptions));
584	xf86ProcessOptions(pScrn->scrnIndex, fPtr->pEnt->device->options,
585			   fPtr->Options);
586
587	/* Use shadow framebuffer by default, on depth >= 8 */
588	xf86Msg(X_INFO, "fbi_flags: %x\n", fPtr->fbi.fbi_flags);
589	if ((pScrn->depth >= 8) &&
590	   ((fPtr->fbi.fbi_flags & WSFB_VRAM_IS_RAM) == 0)) {
591		fPtr->shadowFB = xf86ReturnOptValBool(fPtr->Options,
592						      OPTION_SHADOW_FB, TRUE);
593	} else
594		if (xf86ReturnOptValBool(fPtr->Options,
595					 OPTION_SHADOW_FB, FALSE)) {
596			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
597				   "Shadow FB option ignored on depth < 8");
598		}
599
600	/* Rotation */
601	fPtr->rotate = WSFB_ROTATE_NONE;
602	if ((s = xf86GetOptValString(fPtr->Options, OPTION_ROTATE))) {
603		if (pScrn->depth >= 8) {
604			if (!xf86NameCmp(s, "CW")) {
605				fPtr->shadowFB = TRUE;
606				fPtr->rotate = WSFB_ROTATE_CW;
607				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
608				    "Rotating screen clockwise\n");
609			} else if (!xf86NameCmp(s, "CCW")) {
610				fPtr->shadowFB = TRUE;
611				fPtr->rotate = WSFB_ROTATE_CCW;
612				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
613				    "Rotating screen counter clockwise\n");
614			} else if (!xf86NameCmp(s, "UD")) {
615				fPtr->shadowFB = TRUE;
616				fPtr->rotate = WSFB_ROTATE_UD;
617				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
618				    "Rotating screen upside down\n");
619			} else {
620				xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
621				    "\"%s\" is not a valid value for Option "
622				    "\"Rotate\"\n", s);
623				xf86DrvMsg(pScrn->scrnIndex, X_INFO,
624				    "Valid options are \"CW\", \"CCW\","
625				    " or \"UD\"\n");
626			}
627		} else {
628			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
629			    "Option \"Rotate\" ignored on depth < 8");
630		}
631	}
632
633
634	fPtr->useSwap32 = FALSE;
635	/* Color weight */
636	if (fPtr->fbi.fbi_pixeltype == WSFB_RGB) {
637		rgb zeros = { 0, 0, 0 }, masks;
638
639		if (fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_size > 0) {
640			uint32_t msk;
641
642			/*
643			 * see if we need to byte-swap pixels
644			 * XXX this requires a shadow FB and is incompatible
645			 * (for now ) with rotation
646			 */
647			if ((fPtr->fbi.fbi_bitsperpixel == 32) &&
648			    (fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset == 24) &&
649			    (fPtr->rotate == WSFB_ROTATE_NONE) &&
650			    (fPtr->shadowFB == TRUE)) {
651			    	/*
652			    	 * looks like BGRA - set the swap flag and flip
653			    	 * the offsets
654			    	 */
655			    	xf86Msg(X_INFO, "endian-flipped RGB framebuffer "
656			    			"detected, using WsfbShadowUpdateSwap32()\n");
657			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset = 0;
658			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_offset = 8;
659			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_offset = 16;
660			    	fPtr->fbi.fbi_subtype.fbi_rgbmasks.alpha_offset = 24;
661				fPtr->useSwap32 = TRUE;
662			}
663
664			msk = 0xffffffff;
665			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_size;
666			msk = ~msk;
667			masks.red = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.red_offset;
668
669			msk = 0xffffffff;
670			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_size;
671			msk = ~msk;
672			masks.green = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.green_offset;
673
674			msk = 0xffffffff;
675			msk = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_size;
676			msk = ~msk;
677			masks.blue = msk << fPtr->fbi.fbi_subtype.fbi_rgbmasks.blue_offset;
678			xf86Msg(X_INFO, "masks generated: %08lx %08lx %08lx\n",
679			    (unsigned long)masks.red,
680			    (unsigned long)masks.green,
681			    (unsigned long)masks.blue);
682		} else {
683			masks.red = 0;
684			masks.green = 0;
685			masks.blue = 0;
686		}
687
688		if (!xf86SetWeight(pScrn, zeros, masks))
689			return FALSE;
690	}
691
692	/* Visual init */
693	if (!xf86SetDefaultVisual(pScrn, -1))
694		return FALSE;
695
696	/* We don't currently support DirectColor at > 8bpp . */
697	if (pScrn->depth > 8 && pScrn->defaultVisual != TrueColor) {
698		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Given default visual"
699			   " (%s) is not supported at depth %d\n",
700			   xf86GetVisualName(pScrn->defaultVisual),
701			   pScrn->depth);
702		return FALSE;
703	}
704
705	xf86SetGamma(pScrn,zeros);
706
707	pScrn->progClock = TRUE;
708	pScrn->rgbBits   = (pScrn->depth >= 8) ? 8 : pScrn->depth;
709	pScrn->chipset   = "wsfb";
710	pScrn->videoRam  = fPtr->fbi.fbi_fbsize;
711
712	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Vidmem: %dk\n",
713		   pScrn->videoRam/1024);
714
715	/* Fake video mode struct. */
716	mode = (DisplayModePtr)malloc(sizeof(DisplayModeRec));
717	mode->prev = mode;
718	mode->next = mode;
719	mode->name = "wsfb current mode";
720	mode->status = MODE_OK;
721	mode->type = M_T_BUILTIN;
722	mode->Clock = 0;
723	mode->HDisplay = fPtr->fbi.fbi_width;
724	mode->HSyncStart = 0;
725	mode->HSyncEnd = 0;
726	mode->HTotal = 0;
727	mode->HSkew = 0;
728	mode->VDisplay = fPtr->fbi.fbi_height;
729	mode->VSyncStart = 0;
730	mode->VSyncEnd = 0;
731	mode->VTotal = 0;
732	mode->VScan = 0;
733	mode->Flags = 0;
734	if (pScrn->modes != NULL) {
735		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
736		   "Ignoring mode specification from screen section\n");
737	}
738	pScrn->currentMode = pScrn->modes = mode;
739	pScrn->virtualX = fPtr->fbi.fbi_width;
740	pScrn->virtualY = fPtr->fbi.fbi_height;
741	pScrn->displayWidth = pScrn->virtualX;
742
743	/* Set the display resolution. */
744	xf86SetDpi(pScrn, 0, 0);
745
746	from = X_DEFAULT;
747	fPtr->HWCursor = TRUE;
748	if (xf86GetOptValBool(fPtr->Options, OPTION_HW_CURSOR, &fPtr->HWCursor))
749		from = X_CONFIG;
750	if (xf86ReturnOptValBool(fPtr->Options, OPTION_SW_CURSOR, FALSE)) {
751		from = X_CONFIG;
752		fPtr->HWCursor = FALSE;
753	}
754	xf86DrvMsg(pScrn->scrnIndex, from, "Using %s cursor\n",
755		fPtr->HWCursor ? "HW" : "SW");
756
757	/* Load bpp-specific modules. */
758	switch(pScrn->bitsPerPixel) {
759	case 1:
760	case 4:
761	default:
762		mod = "fb";
763		break;
764	}
765
766
767	/* Load shadow if needed. */
768	if (fPtr->shadowFB) {
769		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
770			   "Using \"Shadow Framebuffer\"\n");
771		if (xf86LoadSubModule(pScrn, "shadow") == NULL) {
772			WsfbFreeRec(pScrn);
773			return FALSE;
774		}
775	}
776
777	if (mod && xf86LoadSubModule(pScrn, mod) == NULL) {
778		WsfbFreeRec(pScrn);
779		return FALSE;
780	}
781
782	if (xf86LoadSubModule(pScrn, "ramdac") == NULL) {
783		WsfbFreeRec(pScrn);
784		return FALSE;
785        }
786
787	if (mod) {
788		if (reqSym) {
789			xf86LoaderReqSymbols(reqSym, NULL);
790		} else {
791			xf86LoaderReqSymLists(fbSymbols, NULL);
792		}
793	}
794	TRACE_EXIT("PreInit");
795	return TRUE;
796}
797
798static Bool
799WsfbCreateScreenResources(ScreenPtr pScreen)
800{
801	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
802	WsfbPtr fPtr = WSFBPTR(pScrn);
803	PixmapPtr pPixmap;
804	Bool ret;
805
806	pScreen->CreateScreenResources = fPtr->CreateScreenResources;
807	ret = pScreen->CreateScreenResources(pScreen);
808	pScreen->CreateScreenResources = WsfbCreateScreenResources;
809
810	if (!ret)
811		return FALSE;
812
813	pPixmap = pScreen->GetScreenPixmap(pScreen);
814
815	if (!shadowAdd(pScreen, pPixmap, fPtr->rotate ?
816		shadowUpdateRotatePackedWeak() : (fPtr->useSwap32 ?
817		    WsfbShadowUpdateSwap32 : shadowUpdatePackedWeak()),
818		WsfbWindowLinear, fPtr->rotate, NULL)) {
819		return FALSE;
820	}
821	return TRUE;
822}
823
824
825static Bool
826WsfbShadowInit(ScreenPtr pScreen)
827{
828	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
829	WsfbPtr fPtr = WSFBPTR(pScrn);
830
831	if (!shadowSetup(pScreen))
832		return FALSE;
833	fPtr->CreateScreenResources = pScreen->CreateScreenResources;
834	pScreen->CreateScreenResources = WsfbCreateScreenResources;
835
836	return TRUE;
837}
838
839static Bool
840WsfbScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
841{
842	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
843	WsfbPtr fPtr = WSFBPTR(pScrn);
844	VisualPtr visual;
845	int ret, flags, ncolors;
846	int wsmode = WSDISPLAYIO_MODE_DUMBFB;
847	int wstype;
848	size_t len;
849
850	TRACE_ENTER("WsfbScreenInit");
851#if DEBUG
852	ErrorF("\tbitsPerPixel=%d, depth=%d, defaultVisual=%s\n"
853	       "\tmask: %x,%x,%x, offset: %u,%u,%u\n",
854	       pScrn->bitsPerPixel,
855	       pScrn->depth,
856	       xf86GetVisualName(pScrn->defaultVisual),
857	       pScrn->mask.red,pScrn->mask.green,pScrn->mask.blue,
858	       pScrn->offset.red,pScrn->offset.green,pScrn->offset.blue);
859#endif
860	switch (fPtr->fbi.fbi_bitsperpixel) {
861	case 1:
862	case 4:
863	case 8:
864		len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
865		break;
866	case 16:
867		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
868			xf86Msg(X_ERROR, "Bogus stride == width in 16bit colour\n");
869			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * sizeof(short);
870		} else {
871			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
872		}
873		break;
874	case 24:
875		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
876			xf86Msg(X_ERROR, "Bogus stride == width in 24bit colour\n");
877			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * 3;
878		} else {
879			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
880		}
881		break;
882	case 32:
883		if (fPtr->fbi.fbi_stride == fPtr->fbi.fbi_width) {
884			xf86Msg(X_ERROR, "Bogus stride == width in 32bit colour\n");
885			len = fPtr->fbi.fbi_width * fPtr->fbi.fbi_height * sizeof(int);
886		} else {
887			len = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
888		}
889		break;
890	default:
891		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
892			   "unsupported depth %d\n", fPtr->fbi.fbi_bitsperpixel);
893		return FALSE;
894	}
895	/* Switch to graphics mode - required before mmap. */
896	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &wsmode) == -1) {
897		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
898			   "ioctl WSDISPLAYIO_SMODE: %s\n",
899			   strerror(errno));
900		return FALSE;
901	}
902	/* Get wsdisplay type to handle quirks */
903	if (ioctl(fPtr->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
904		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
905			   "ioctl WSDISPLAY_GTYPE: %s\n",
906			   strerror(errno));
907		return FALSE;
908	}
909	fPtr->fbmem = wsfb_mmap(len, 0, fPtr->fd);
910
911	if (fPtr->fbmem == NULL) {
912		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
913			   "wsfb_mmap: %s\n", strerror(errno));
914		return FALSE;
915	}
916	fPtr->fbmem_len = len;
917
918	WsfbSave(pScrn);
919	pScrn->vtSema = TRUE;
920
921	/* MI layer */
922	miClearVisualTypes();
923	if (pScrn->bitsPerPixel > 8) {
924		if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
925				      pScrn->rgbBits, TrueColor))
926			return FALSE;
927	} else {
928		if (!miSetVisualTypes(pScrn->depth,
929				      miGetDefaultVisualMask(pScrn->depth),
930				      pScrn->rgbBits, pScrn->defaultVisual))
931			return FALSE;
932	}
933	if (!miSetPixmapDepths())
934		return FALSE;
935
936	if (fPtr->rotate == WSFB_ROTATE_CW
937	    || fPtr->rotate == WSFB_ROTATE_CCW) {
938		int tmp = pScrn->virtualX;
939		pScrn->virtualX = pScrn->displayWidth = pScrn->virtualY;
940		pScrn->virtualY = tmp;
941	}
942	if (fPtr->rotate && !fPtr->PointerMoved) {
943		fPtr->PointerMoved = pScrn->PointerMoved;
944		pScrn->PointerMoved = WsfbPointerMoved;
945	}
946
947	fPtr->fbstart = fPtr->fbmem;
948#ifdef	WSDISPLAY_TYPE_LUNA
949	if (wstype == WSDISPLAY_TYPE_LUNA) {
950		/*
951		 * XXX
952		 * LUNA's FB seems to have 64 dot (8 byte) offset.
953		 * This might be able to be changed in kernel lunafb driver,
954		 * but current setting was pulled from 4.4BSD-Lite2/luna68k.
955		 */
956		fPtr->fbstart += 8;
957	}
958#endif
959
960	if (fPtr->shadowFB) {
961		fPtr->shadow = calloc(1, pScrn->virtualX * pScrn->virtualY *
962		    pScrn->bitsPerPixel/8);
963
964		if (!fPtr->shadow) {
965			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
966			    "Failed to allocate shadow framebuffer\n");
967			return FALSE;
968		}
969	}
970
971	switch (pScrn->bitsPerPixel) {
972	case 1:
973		ret = fbScreenInit(pScreen,
974		    fPtr->fbstart,
975		    pScrn->virtualX, pScrn->virtualY,
976		    pScrn->xDpi, pScrn->yDpi,
977		    fPtr->fbi.fbi_stride * 8, pScrn->bitsPerPixel);
978		break;
979	case 4:
980	case 8:
981	case 16:
982	case 24:
983	case 32:
984		ret = fbScreenInit(pScreen,
985		    fPtr->shadowFB ? fPtr->shadow : fPtr->fbstart,
986		    pScrn->virtualX, pScrn->virtualY,
987		    pScrn->xDpi, pScrn->yDpi,
988		    pScrn->displayWidth, pScrn->bitsPerPixel);
989		break;
990	default:
991		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
992			   "Unsupported bpp: %d", pScrn->bitsPerPixel);
993		return FALSE;
994	} /* case */
995
996	if (!ret)
997		return FALSE;
998
999	if (pScrn->bitsPerPixel > 8) {
1000		/* Fixup RGB ordering. */
1001		visual = pScreen->visuals + pScreen->numVisuals;
1002		while (--visual >= pScreen->visuals) {
1003			if ((visual->class | DynamicClass) == DirectColor) {
1004				visual->offsetRed   = pScrn->offset.red;
1005				visual->offsetGreen = pScrn->offset.green;
1006				visual->offsetBlue  = pScrn->offset.blue;
1007				visual->redMask     = pScrn->mask.red;
1008				visual->greenMask   = pScrn->mask.green;
1009				visual->blueMask    = pScrn->mask.blue;
1010			}
1011		}
1012	}
1013
1014	if (pScrn->bitsPerPixel >= 8) {
1015		if (!fbPictureInit(pScreen, NULL, 0))
1016			xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
1017				   "RENDER extension initialisation failed.");
1018	}
1019	if (fPtr->shadowFB && !WsfbShadowInit(pScreen)) {
1020		xf86DrvMsg(scrnIndex, X_ERROR,
1021		    "shadow framebuffer initialization failed\n");
1022		return FALSE;
1023	}
1024
1025#ifdef XFreeXDGA
1026	if (!fPtr->rotate)
1027		WsfbDGAInit(pScrn, pScreen);
1028	else
1029		xf86DrvMsg(scrnIndex, X_INFO, "Rotated display, "
1030		    "disabling DGA\n");
1031#endif
1032	if (fPtr->rotate) {
1033		xf86DrvMsg(scrnIndex, X_INFO, "Enabling Driver Rotation, "
1034		    "disabling RandR\n");
1035		xf86DisableRandR();
1036		if (pScrn->bitsPerPixel == 24)
1037			xf86DrvMsg(scrnIndex, X_WARNING,
1038			    "Rotation might be broken in 24 bpp\n");
1039	}
1040
1041	xf86SetBlackWhitePixels(pScreen);
1042	miInitializeBackingStore(pScreen);
1043	xf86SetBackingStore(pScreen);
1044
1045	/* Software cursor. */
1046	miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
1047
1048	/* check for hardware cursor support */
1049	if (fPtr->HWCursor)
1050		WsfbSetupCursor(pScreen);
1051
1052	/*
1053	 * Colormap
1054	 *
1055	 * Note that, even on less than 8 bit depth frame buffers, we
1056	 * expect the colormap to be programmable with 8 bit values.
1057	 * As of now, this is indeed the case on all OpenBSD supported
1058	 * graphics hardware.
1059	 */
1060	if (!miCreateDefColormap(pScreen))
1061		return FALSE;
1062	flags = CMAP_RELOAD_ON_MODE_SWITCH;
1063
1064	ncolors = 0;
1065	if (fPtr->fbi.fbi_pixeltype == WSFB_CI) {
1066		ncolors = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1067	}
1068
1069	/* On StaticGray visuals, fake a 256 entries colormap. */
1070	if (ncolors == 0)
1071		ncolors = 256;
1072
1073	if(!xf86HandleColormaps(pScreen, ncolors, 8, WsfbLoadPalette,
1074				NULL, flags))
1075		return FALSE;
1076
1077#if defined(__NetBSD__) && defined(WSDISPLAY_TYPE_LUNA)
1078	if (wstype == WSDISPLAY_TYPE_LUNA) {
1079		ncolors = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1080		if (ncolors > 0) {
1081			/*
1082			 * Override palette to use 4bpp/8bpp framebuffers as
1083			 * monochrome server by using only the first plane.
1084			 * See also comment in WsfbPreInit().
1085			 */
1086			struct wsdisplay_cmap cmap;
1087			uint8_t r[256], g[256], b[256];
1088			int p;
1089
1090			for (p = 0; p < ncolors; p++)
1091				r[p] = g[p] = b[p] = (p & 1) ? 0xff : 0;
1092			cmap.index = 0;
1093			cmap.count = ncolors;
1094			cmap.red   = r;
1095			cmap.green = g;
1096			cmap.blue  = b;
1097			if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP, &cmap) == -1) {
1098				xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1099				   "ioctl WSDISPLAYIO_PUTCMAP: %s\n",
1100				   strerror(errno));
1101			}
1102		}
1103	}
1104#endif
1105
1106	pScreen->SaveScreen = WsfbSaveScreen;
1107
1108#ifdef XvExtension
1109	{
1110		XF86VideoAdaptorPtr *ptr;
1111
1112		int n = xf86XVListGenericAdaptors(pScrn,&ptr);
1113		if (n) {
1114			xf86XVScreenInit(pScreen,ptr,n);
1115		}
1116	}
1117#endif
1118
1119	/* Wrap the current CloseScreen function. */
1120	fPtr->CloseScreen = pScreen->CloseScreen;
1121	pScreen->CloseScreen = WsfbCloseScreen;
1122
1123	TRACE_EXIT("WsfbScreenInit");
1124	return TRUE;
1125}
1126
1127static Bool
1128WsfbCloseScreen(int scrnIndex, ScreenPtr pScreen)
1129{
1130	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1131	PixmapPtr pPixmap;
1132	WsfbPtr fPtr = WSFBPTR(pScrn);
1133
1134
1135	TRACE_ENTER("WsfbCloseScreen");
1136
1137	pPixmap = pScreen->GetScreenPixmap(pScreen);
1138	if (fPtr->shadowFB)
1139		shadowRemove(pScreen, pPixmap);
1140
1141	if (pScrn->vtSema) {
1142		WsfbRestore(pScrn);
1143		if (munmap(fPtr->fbmem, fPtr->fbmem_len) == -1) {
1144			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1145				   "munmap: %s\n", strerror(errno));
1146		}
1147
1148		fPtr->fbmem = NULL;
1149	}
1150#ifdef XFreeXDGA
1151	if (fPtr->pDGAMode) {
1152		free(fPtr->pDGAMode);
1153		fPtr->pDGAMode = NULL;
1154		fPtr->nDGAMode = 0;
1155	}
1156#endif
1157	pScrn->vtSema = FALSE;
1158
1159	/* Unwrap CloseScreen. */
1160	pScreen->CloseScreen = fPtr->CloseScreen;
1161	TRACE_EXIT("WsfbCloseScreen");
1162	return (*pScreen->CloseScreen)(scrnIndex, pScreen);
1163}
1164
1165static void *
1166WsfbWindowLinear(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
1167		CARD32 *size, void *closure)
1168{
1169	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1170	WsfbPtr fPtr = WSFBPTR(pScrn);
1171
1172	/*
1173	 * XXX
1174	 * This should never happen. Is it really necessary?
1175	 */
1176	if (fPtr->fbi.fbi_stride)
1177		*size = fPtr->fbi.fbi_stride;
1178	else {
1179		if (ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES, size) == -1)
1180			return NULL;
1181		fPtr->fbi.fbi_stride = *size;
1182	}
1183	return ((CARD8 *)fPtr->fbmem + row * fPtr->fbi.fbi_stride + offset);
1184}
1185
1186static void
1187WsfbPointerMoved(int index, int x, int y)
1188{
1189    ScrnInfoPtr pScrn = xf86Screens[index];
1190    WsfbPtr fPtr = WSFBPTR(pScrn);
1191    int newX, newY;
1192
1193    switch (fPtr->rotate)
1194    {
1195    case WSFB_ROTATE_CW:
1196	/* 90 degrees CW rotation. */
1197	newX = pScrn->pScreen->height - y - 1;
1198	newY = x;
1199	break;
1200
1201    case WSFB_ROTATE_CCW:
1202	/* 90 degrees CCW rotation. */
1203	newX = y;
1204	newY = pScrn->pScreen->width - x - 1;
1205	break;
1206
1207    case WSFB_ROTATE_UD:
1208	/* 180 degrees UD rotation. */
1209	newX = pScrn->pScreen->width - x - 1;
1210	newY = pScrn->pScreen->height - y - 1;
1211	break;
1212
1213    default:
1214	/* No rotation. */
1215	newX = x;
1216	newY = y;
1217	break;
1218    }
1219
1220    /* Pass adjusted pointer coordinates to wrapped PointerMoved function. */
1221    (*fPtr->PointerMoved)(index, newX, newY);
1222}
1223
1224static Bool
1225WsfbEnterVT(int scrnIndex, int flags)
1226{
1227	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1228	WsfbPtr fPtr = WSFBPTR(pScrn);
1229	int mode;
1230
1231	TRACE_ENTER("EnterVT");
1232	pScrn->vtSema = TRUE;
1233
1234	/* Restore the graphics mode. */
1235	mode = WSDISPLAYIO_MODE_DUMBFB;
1236	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1237		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1238			   "error setting graphics mode %s\n", strerror(errno));
1239	}
1240
1241	TRACE_EXIT("EnterVT");
1242	return TRUE;
1243}
1244
1245static void
1246WsfbLeaveVT(int scrnIndex, int flags)
1247{
1248	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1249	WsfbPtr fPtr = WSFBPTR(pScrn);
1250	int mode;
1251
1252	TRACE_ENTER("LeaveVT");
1253
1254	/*
1255	 * stuff to do:
1256	 * - turn off hw cursor
1257	 * - restore colour map if WSFB_CI
1258	 * - ioctl(WSDISPLAYIO_MODE_EMUL) to notify the kernel driver that
1259	 *   we're backing off
1260	 */
1261
1262	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
1263	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
1264		/* reset colormap for text mode */
1265		if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP,
1266			  &(fPtr->saved_cmap)) == -1) {
1267			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1268				   "error restoring colormap %s\n",
1269				   strerror(errno));
1270		}
1271	}
1272
1273	/* Restore the text mode. */
1274	mode = WSDISPLAYIO_MODE_EMUL;
1275	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1276		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1277			   "error setting text mode %s\n", strerror(errno));
1278	}
1279
1280	pScrn->vtSema = FALSE;
1281	TRACE_EXIT("LeaveVT");
1282}
1283
1284static Bool
1285WsfbSwitchMode(int scrnIndex, DisplayModePtr mode, int flags)
1286{
1287#if DEBUG
1288	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1289#endif
1290
1291	TRACE_ENTER("SwitchMode");
1292	/* Nothing else to do. */
1293	return TRUE;
1294}
1295
1296static int
1297WsfbValidMode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
1298{
1299#if DEBUG
1300	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1301#endif
1302
1303	TRACE_ENTER("ValidMode");
1304	return MODE_OK;
1305}
1306
1307static void
1308WsfbLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
1309	       LOCO *colors, VisualPtr pVisual)
1310{
1311	WsfbPtr fPtr = WSFBPTR(pScrn);
1312	struct wsdisplay_cmap cmap;
1313	unsigned char red[256],green[256],blue[256];
1314	int i, indexMin=256, indexMax=0;
1315
1316	TRACE_ENTER("LoadPalette");
1317
1318	/* nothing to do if there is no color palette support */
1319	if (fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries == 0)
1320		return;
1321
1322	cmap.count   = 1;
1323	cmap.red   = red;
1324	cmap.green = green;
1325	cmap.blue  = blue;
1326
1327	if (numColors == 1) {
1328		/* Optimisation */
1329		cmap.index = indices[0];
1330		red[0]   = colors[indices[0]].red;
1331		green[0] = colors[indices[0]].green;
1332		blue[0]  = colors[indices[0]].blue;
1333		if (ioctl(fPtr->fd,WSDISPLAYIO_PUTCMAP, &cmap) == -1)
1334			ErrorF("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
1335	} else {
1336		/*
1337		 * Change all colors in 2 ioctls
1338		 * and limit the data to be transfered.
1339		 */
1340		for (i = 0; i < numColors; i++) {
1341			if (indices[i] < indexMin)
1342				indexMin = indices[i];
1343			if (indices[i] > indexMax)
1344				indexMax = indices[i];
1345		}
1346		cmap.index = indexMin;
1347		cmap.count = indexMax - indexMin + 1;
1348		cmap.red = &red[indexMin];
1349		cmap.green = &green[indexMin];
1350		cmap.blue = &blue[indexMin];
1351		/* Get current map. */
1352		if (ioctl(fPtr->fd, WSDISPLAYIO_GETCMAP, &cmap) == -1)
1353			ErrorF("ioctl FBIOGETCMAP: %s\n", strerror(errno));
1354		/* Change the colors that require updating. */
1355		for (i = 0; i < numColors; i++) {
1356			red[indices[i]]   = colors[indices[i]].red;
1357			green[indices[i]] = colors[indices[i]].green;
1358			blue[indices[i]]  = colors[indices[i]].blue;
1359		}
1360		/* Write the colormap back. */
1361		if (ioctl(fPtr->fd,WSDISPLAYIO_PUTCMAP, &cmap) == -1)
1362			ErrorF("ioctl FBIOPUTCMAP: %s\n", strerror(errno));
1363	}
1364	TRACE_EXIT("LoadPalette");
1365}
1366
1367static Bool
1368WsfbSaveScreen(ScreenPtr pScreen, int mode)
1369{
1370	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1371	WsfbPtr fPtr = WSFBPTR(pScrn);
1372	int state;
1373
1374	TRACE_ENTER("SaveScreen");
1375
1376	if (!pScrn->vtSema)
1377		return TRUE;
1378
1379	if (mode != SCREEN_SAVER_FORCER) {
1380		state = xf86IsUnblank(mode)?WSDISPLAYIO_VIDEO_ON:
1381		                            WSDISPLAYIO_VIDEO_OFF;
1382		ioctl(fPtr->fd,
1383		      WSDISPLAYIO_SVIDEO, &state);
1384	}
1385	TRACE_EXIT("SaveScreen");
1386	return TRUE;
1387}
1388
1389
1390static void
1391WsfbSave(ScrnInfoPtr pScrn)
1392{
1393	WsfbPtr fPtr = WSFBPTR(pScrn);
1394
1395	TRACE_ENTER("WsfbSave");
1396
1397	/* nothing to save if we don't run in colour-indexed mode */
1398	if (fPtr->fbi.fbi_pixeltype != WSFB_CI)
1399		return;
1400
1401	/* nothing to do if no color palette support */
1402	if (fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries == 0)
1403		return;
1404
1405	fPtr->saved_cmap.index = 0;
1406	fPtr->saved_cmap.count = fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries;
1407	if (ioctl(fPtr->fd, WSDISPLAYIO_GETCMAP,
1408		  &(fPtr->saved_cmap)) == -1) {
1409		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1410			   "error saving colormap %s\n", strerror(errno));
1411	}
1412	TRACE_EXIT("WsfbSave");
1413
1414}
1415
1416static void
1417WsfbRestore(ScrnInfoPtr pScrn)
1418{
1419	WsfbPtr fPtr = WSFBPTR(pScrn);
1420	int mode;
1421
1422	TRACE_ENTER("WsfbRestore");
1423
1424	if (fPtr->fbi.fbi_pixeltype == WSFB_CI &&
1425	    fPtr->fbi.fbi_subtype.fbi_cmapinfo.cmap_entries > 0) {
1426		/* reset colormap for text mode */
1427		if (ioctl(fPtr->fd, WSDISPLAYIO_PUTCMAP,
1428			  &(fPtr->saved_cmap)) == -1) {
1429			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1430				   "error restoring colormap %s\n",
1431				   strerror(errno));
1432		}
1433	}
1434
1435	/* Clear the screen. */
1436	memset(fPtr->fbmem, 0, fPtr->fbmem_len);
1437
1438	/* Restore the text mode. */
1439	mode = WSDISPLAYIO_MODE_EMUL;
1440	if (ioctl(fPtr->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
1441		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1442			   "error setting text mode %s\n", strerror(errno));
1443	}
1444	TRACE_EXIT("WsfbRestore");
1445}
1446
1447#ifdef XFreeXDGA
1448/***********************************************************************
1449 * DGA stuff
1450 ***********************************************************************/
1451
1452static Bool
1453WsfbDGAOpenFramebuffer(ScrnInfoPtr pScrn, char **DeviceName,
1454		       unsigned char **ApertureBase, int *ApertureSize,
1455		       int *ApertureOffset, int *flags)
1456{
1457	*DeviceName = NULL;		/* No special device */
1458	*ApertureBase = (unsigned char *)(pScrn->memPhysBase);
1459	*ApertureSize = pScrn->videoRam;
1460	*ApertureOffset = pScrn->fbOffset;
1461	*flags = 0;
1462
1463	return TRUE;
1464}
1465
1466static Bool
1467WsfbDGASetMode(ScrnInfoPtr pScrn, DGAModePtr pDGAMode)
1468{
1469	DisplayModePtr pMode;
1470	int scrnIdx = pScrn->pScreen->myNum;
1471	int frameX0, frameY0;
1472
1473	if (pDGAMode) {
1474		pMode = pDGAMode->mode;
1475		frameX0 = frameY0 = 0;
1476	} else {
1477		if (!(pMode = pScrn->currentMode))
1478			return TRUE;
1479
1480		frameX0 = pScrn->frameX0;
1481		frameY0 = pScrn->frameY0;
1482	}
1483
1484	if (!(*pScrn->SwitchMode)(scrnIdx, pMode, 0))
1485		return FALSE;
1486	(*pScrn->AdjustFrame)(scrnIdx, frameX0, frameY0, 0);
1487
1488	return TRUE;
1489}
1490
1491static void
1492WsfbDGASetViewport(ScrnInfoPtr pScrn, int x, int y, int flags)
1493{
1494	(*pScrn->AdjustFrame)(pScrn->pScreen->myNum, x, y, flags);
1495}
1496
1497static int
1498WsfbDGAGetViewport(ScrnInfoPtr pScrn)
1499{
1500	return (0);
1501}
1502
1503static DGAFunctionRec WsfbDGAFunctions =
1504{
1505	WsfbDGAOpenFramebuffer,
1506	NULL,       /* CloseFramebuffer */
1507	WsfbDGASetMode,
1508	WsfbDGASetViewport,
1509	WsfbDGAGetViewport,
1510	NULL,       /* Sync */
1511	NULL,       /* FillRect */
1512	NULL,       /* BlitRect */
1513	NULL,       /* BlitTransRect */
1514};
1515
1516static void
1517WsfbDGAAddModes(ScrnInfoPtr pScrn)
1518{
1519	WsfbPtr fPtr = WSFBPTR(pScrn);
1520	DisplayModePtr pMode = pScrn->modes;
1521	DGAModePtr pDGAMode;
1522
1523	do {
1524		pDGAMode = realloc(fPtr->pDGAMode,
1525				    (fPtr->nDGAMode + 1) * sizeof(DGAModeRec));
1526		if (!pDGAMode)
1527			break;
1528
1529		fPtr->pDGAMode = pDGAMode;
1530		pDGAMode += fPtr->nDGAMode;
1531		(void)memset(pDGAMode, 0, sizeof(DGAModeRec));
1532
1533		++fPtr->nDGAMode;
1534		pDGAMode->mode = pMode;
1535		pDGAMode->flags = DGA_CONCURRENT_ACCESS | DGA_PIXMAP_AVAILABLE;
1536		pDGAMode->byteOrder = pScrn->imageByteOrder;
1537		pDGAMode->depth = pScrn->depth;
1538		pDGAMode->bitsPerPixel = pScrn->bitsPerPixel;
1539		pDGAMode->red_mask = pScrn->mask.red;
1540		pDGAMode->green_mask = pScrn->mask.green;
1541		pDGAMode->blue_mask = pScrn->mask.blue;
1542		pDGAMode->visualClass = pScrn->bitsPerPixel > 8 ?
1543			TrueColor : PseudoColor;
1544		pDGAMode->xViewportStep = 1;
1545		pDGAMode->yViewportStep = 1;
1546		pDGAMode->viewportWidth = pMode->HDisplay;
1547		pDGAMode->viewportHeight = pMode->VDisplay;
1548
1549		if (fPtr->fbi.fbi_stride)
1550			pDGAMode->bytesPerScanline = fPtr->fbi.fbi_stride;
1551		else {
1552			ioctl(fPtr->fd, WSDISPLAYIO_LINEBYTES,
1553			      &fPtr->fbi.fbi_stride);
1554			pDGAMode->bytesPerScanline = fPtr->fbi.fbi_stride;
1555		}
1556
1557		pDGAMode->imageWidth = pMode->HDisplay;
1558		pDGAMode->imageHeight =  pMode->VDisplay;
1559		pDGAMode->pixmapWidth = pDGAMode->imageWidth;
1560		pDGAMode->pixmapHeight = pDGAMode->imageHeight;
1561		pDGAMode->maxViewportX = pScrn->virtualX -
1562			pDGAMode->viewportWidth;
1563		pDGAMode->maxViewportY = pScrn->virtualY -
1564			pDGAMode->viewportHeight;
1565
1566		pDGAMode->address = fPtr->fbstart;
1567
1568		pMode = pMode->next;
1569	} while (pMode != pScrn->modes);
1570}
1571
1572static Bool
1573WsfbDGAInit(ScrnInfoPtr pScrn, ScreenPtr pScreen)
1574{
1575	WsfbPtr fPtr = WSFBPTR(pScrn);
1576
1577	if (pScrn->depth < 8)
1578		return FALSE;
1579
1580	if (!fPtr->nDGAMode)
1581		WsfbDGAAddModes(pScrn);
1582
1583	return (DGAInit(pScreen, &WsfbDGAFunctions,
1584			fPtr->pDGAMode, fPtr->nDGAMode));
1585}
1586#endif
1587
1588static Bool
1589WsfbDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op,
1590    pointer ptr)
1591{
1592	xorgHWFlags *flag;
1593
1594	switch (op) {
1595	case GET_REQUIRED_HW_INTERFACES:
1596		flag = (CARD32*)ptr;
1597		(*flag) = 0;
1598		return TRUE;
1599	default:
1600		return FALSE;
1601	}
1602}
1603
1604static inline void
1605memcpy32sw(void *dest, void *src, int len)
1606{
1607	uint32_t *d = dest, *s = src;
1608
1609#if DEBUG
1610	if ((((long)dest & 3) + ((long)src & 3) + (len & 3)) != 0) {
1611		xf86Msg(X_ERROR, "unaligned %s\n", __func__);
1612		return;
1613	}
1614#endif
1615	while (len > 0) {
1616		*d = bswap32(*s);
1617		d++;
1618		s++;
1619		len -= 4;
1620	}
1621}
1622
1623/* adapted from miext/shadow/shpacked.c::shadowUpdatePacked() */
1624void
1625WsfbShadowUpdateSwap32(ScreenPtr pScreen, shadowBufPtr pBuf)
1626{
1627    RegionPtr	damage = shadowDamage (pBuf);
1628    PixmapPtr	pShadow = pBuf->pPixmap;
1629    int		nbox = RegionNumRects (damage);
1630    BoxPtr	pbox = RegionRects (damage);
1631    FbBits	*shaBase, *shaLine, *sha;
1632    FbStride	shaStride;
1633    int		scrBase, scrLine, scr;
1634    int		shaBpp;
1635    int		shaXoff, shaYoff; /* XXX assumed to be zero */
1636    int		x, y, w, h, width;
1637    int         i;
1638    FbBits	*winBase = NULL, *win;
1639    CARD32      winSize;
1640
1641    fbGetDrawable (&pShadow->drawable, shaBase, shaStride, shaBpp, shaXoff, shaYoff);
1642    while (nbox--)
1643    {
1644	x = pbox->x1 * shaBpp;
1645	y = pbox->y1;
1646	w = (pbox->x2 - pbox->x1) * shaBpp;
1647	h = pbox->y2 - pbox->y1;
1648
1649	scrLine = (x >> FB_SHIFT);
1650	shaLine = shaBase + y * shaStride + (x >> FB_SHIFT);
1651
1652	x &= FB_MASK;
1653	w = (w + x + FB_MASK) >> FB_SHIFT;
1654
1655	while (h--)
1656	{
1657	    winSize = 0;
1658	    scrBase = 0;
1659	    width = w;
1660	    scr = scrLine;
1661	    sha = shaLine;
1662	    while (width) {
1663		/* how much remains in this window */
1664		i = scrBase + winSize - scr;
1665		if (i <= 0 || scr < scrBase)
1666		{
1667		    winBase = (FbBits *) (*pBuf->window) (pScreen,
1668							  y,
1669							  scr * sizeof (FbBits),
1670							  SHADOW_WINDOW_WRITE,
1671							  &winSize,
1672							  pBuf->closure);
1673		    if(!winBase)
1674			return;
1675		    scrBase = scr;
1676		    winSize /= sizeof (FbBits);
1677		    i = winSize;
1678		}
1679		win = winBase + (scr - scrBase);
1680		if (i > width)
1681		    i = width;
1682		width -= i;
1683		scr += i;
1684		memcpy32sw(win, sha, i * sizeof(FbBits));
1685		sha += i;
1686	    }
1687	    shaLine += shaStride;
1688	    y++;
1689	}
1690	pbox++;
1691    }
1692}
1693
1694