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