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