fbdev.c 31.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * Copyright (C) 1994-2003 The XFree86 Project, Inc.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is fur-
 * nished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
 * NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
 * NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of the XFree86 Project shall not
 * be used in advertising or otherwise to promote the sale, use or other deal-
 * ings in this Software without prior written authorization from the XFree86
 * Project.
 */

Kaleb Keithley's avatar
Kaleb Keithley committed
27
28
/*
 * Authors:  Alan Hourihane, <alanh@fairlite.demon.co.uk>
Michel Dänzer's avatar
Michel Dänzer committed
29
 *	     Michel Dänzer, <michel@tungstengraphics.com>
Kaleb Keithley's avatar
Kaleb Keithley committed
30
31
 */

32
33
34
35
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

36
37
#include <string.h>

Kaleb Keithley's avatar
Kaleb Keithley committed
38
39
40
41
42
43
44
45
46
47
48
/* all driver need this */
#include "xf86.h"
#include "xf86_OSproc.h"

#include "mipointer.h"
#include "micmap.h"
#include "colormapst.h"
#include "xf86cmap.h"
#include "shadow.h"
#include "dgaproc.h"

49
50
#include "sunxi_disp.h"

Kaleb Keithley's avatar
Kaleb Keithley committed
51
52
53
/* for visuals */
#include "fb.h"

54
#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
Kaleb Keithley's avatar
Kaleb Keithley committed
55
56
#include "xf86Resources.h"
#include "xf86RAC.h"
57
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
58
59
60
61
62

#include "fbdevhw.h"

#include "xf86xv.h"

Dave Airlie's avatar
Dave Airlie committed
63
64
#include "compat-api.h"

65
#ifdef XSERVER_LIBPCIACCESS
66
67
68
#include <pciaccess.h>
#endif

69
static Bool debug = 0;
Kaleb Keithley's avatar
Kaleb Keithley committed
70

71
#define TRACE_ENTER(str) \
72
    do { if (debug) ErrorF("sunxifb: " str " %d\n",pScrn->scrnIndex); } while (0)
73
#define TRACE_EXIT(str) \
74
    do { if (debug) ErrorF("sunxifb: " str " done\n"); } while (0)
75
#define TRACE(str) \
76
    do { if (debug) ErrorF("sunxifb trace: " str "\n"); } while (0)
Kaleb Keithley's avatar
Kaleb Keithley committed
77
78
79
80
81
82
83

/* -------------------------------------------------------------------- */
/* prototypes                                                           */

static const OptionInfoRec * FBDevAvailableOptions(int chipid, int busid);
static void	FBDevIdentify(int flags);
static Bool	FBDevProbe(DriverPtr drv, int flags);
84
#ifdef XSERVER_LIBPCIACCESS
85
86
87
static Bool	FBDevPciProbe(DriverPtr drv, int entity_num,
     struct pci_device *dev, intptr_t match_data);
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
88
static Bool	FBDevPreInit(ScrnInfoPtr pScrn, int flags);
Dave Airlie's avatar
Dave Airlie committed
89
90
static Bool	FBDevScreenInit(SCREEN_INIT_ARGS_DECL);
static Bool	FBDevCloseScreen(CLOSE_SCREEN_ARGS_DECL);
Kaleb Keithley's avatar
Kaleb Keithley committed
91
92
static void *	FBDevWindowLinear(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
				  CARD32 *size, void *closure);
Dave Airlie's avatar
Dave Airlie committed
93
static void	FBDevPointerMoved(SCRN_ARG_TYPE arg, int x, int y);
Kaleb Keithley's avatar
Kaleb Keithley committed
94
static Bool	FBDevDGAInit(ScrnInfoPtr pScrn, ScreenPtr pScreen);
95
96
static Bool	FBDevDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op,
				pointer ptr);
Kaleb Keithley's avatar
Kaleb Keithley committed
97
98
99
100
101
102
103
104
105
106
107
108
109


enum { FBDEV_ROTATE_NONE=0, FBDEV_ROTATE_CW=270, FBDEV_ROTATE_UD=180, FBDEV_ROTATE_CCW=90 };


/* -------------------------------------------------------------------- */

/*
 * This is intentionally screen-independent.  It indicates the binding
 * choice made in the first PreInit.
 */
static int pix24bpp = 0;

110
#define FBDEV_VERSION		4000
111
112
#define FBDEV_NAME		"SUNXIFB"
#define FBDEV_DRIVER_NAME	"sunxifb"
Kaleb Keithley's avatar
Kaleb Keithley committed
113

114
#ifdef XSERVER_LIBPCIACCESS
115
116
117
118
119
120
121
122
123
124
static const struct pci_id_match fbdev_device_match[] = {
    {
	PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY,
	0x00030000, 0x00ffffff, 0
    },

    { 0, 0, 0 },
};
#endif

125
_X_EXPORT DriverRec FBDEV = {
126
	FBDEV_VERSION,
Kaleb Keithley's avatar
Kaleb Keithley committed
127
128
129
130
131
132
133
134
	FBDEV_DRIVER_NAME,
#if 0
	"driver for linux framebuffer devices",
#endif
	FBDevIdentify,
	FBDevProbe,
	FBDevAvailableOptions,
	NULL,
135
	0,
136
137
	FBDevDriverFunc,

138
#ifdef XSERVER_LIBPCIACCESS
139
140
141
    fbdev_device_match,
    FBDevPciProbe
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
142
143
144
145
};

/* Supported "chipsets" */
static SymTabRec FBDevChipsets[] = {
146
    { 0, "sunxifb" },
Kaleb Keithley's avatar
Kaleb Keithley committed
147
148
149
150
151
152
153
    {-1, NULL }
};

/* Supported options */
typedef enum {
	OPTION_SHADOW_FB,
	OPTION_ROTATE,
154
155
	OPTION_FBDEV,
	OPTION_DEBUG
Kaleb Keithley's avatar
Kaleb Keithley committed
156
157
158
159
160
161
} FBDevOpts;

static const OptionInfoRec FBDevOptions[] = {
	{ OPTION_SHADOW_FB,	"ShadowFB",	OPTV_BOOLEAN,	{0},	FALSE },
	{ OPTION_ROTATE,	"Rotate",	OPTV_STRING,	{0},	FALSE },
	{ OPTION_FBDEV,		"fbdev",	OPTV_STRING,	{0},	FALSE },
162
	{ OPTION_DEBUG,		"debug",	OPTV_BOOLEAN,	{0},	FALSE },
Kaleb Keithley's avatar
Kaleb Keithley committed
163
164
165
166
167
168
169
170
171
172
173
	{ -1,			NULL,		OPTV_NONE,	{0},	FALSE }
};

/* -------------------------------------------------------------------- */

#ifdef XFree86LOADER

MODULESETUPPROTO(FBDevSetup);

static XF86ModuleVersionInfo FBDevVersRec =
{
174
	"sunxifb",
Kaleb Keithley's avatar
Kaleb Keithley committed
175
176
177
	MODULEVENDORSTRING,
	MODINFOSTRING1,
	MODINFOSTRING2,
178
	XORG_VERSION_CURRENT,
179
	PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
Kaleb Keithley's avatar
Kaleb Keithley committed
180
181
	ABI_CLASS_VIDEODRV,
	ABI_VIDEODRV_VERSION,
182
	MOD_CLASS_VIDEODRV,
Kaleb Keithley's avatar
Kaleb Keithley committed
183
184
185
	{0,0,0,0}
};

186
_X_EXPORT XF86ModuleData sunxifbModuleData = { &FBDevVersRec, FBDevSetup, NULL };
Kaleb Keithley's avatar
Kaleb Keithley committed
187
188
189
190
191
192
193
194

pointer
FBDevSetup(pointer module, pointer opts, int *errmaj, int *errmin)
{
	static Bool setupDone = FALSE;

	if (!setupDone) {
		setupDone = TRUE;
195
		xf86AddDriver(&FBDEV, module, HaveDriverFuncs);
Kaleb Keithley's avatar
Kaleb Keithley committed
196
197
198
199
200
201
202
203
204
205
206
207
		return (pointer)1;
	} else {
		if (errmaj) *errmaj = LDR_ONCEONLY;
		return NULL;
	}
}

#endif /* XFree86LOADER */

/* -------------------------------------------------------------------- */
/* our private data, and two functions to allocate/free this            */

208
#include "fbdev_priv.h"
Kaleb Keithley's avatar
Kaleb Keithley committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

static Bool
FBDevGetRec(ScrnInfoPtr pScrn)
{
	if (pScrn->driverPrivate != NULL)
		return TRUE;
	
	pScrn->driverPrivate = xnfcalloc(sizeof(FBDevRec), 1);
	return TRUE;
}

static void
FBDevFreeRec(ScrnInfoPtr pScrn)
{
	if (pScrn->driverPrivate == NULL)
		return;
225
	free(pScrn->driverPrivate);
Kaleb Keithley's avatar
Kaleb Keithley committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
	pScrn->driverPrivate = NULL;
}

/* -------------------------------------------------------------------- */

static const OptionInfoRec *
FBDevAvailableOptions(int chipid, int busid)
{
	return FBDevOptions;
}

static void
FBDevIdentify(int flags)
{
	xf86PrintChipsets(FBDEV_NAME, "driver for framebuffer", FBDevChipsets);
}

243

244
#ifdef XSERVER_LIBPCIACCESS
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
static Bool FBDevPciProbe(DriverPtr drv, int entity_num,
			  struct pci_device *dev, intptr_t match_data)
{
    ScrnInfoPtr pScrn = NULL;

    if (!xf86LoadDrvSubModule(drv, "fbdevhw"))
	return FALSE;
	    
    pScrn = xf86ConfigPciEntity(NULL, 0, entity_num, NULL, NULL,
				NULL, NULL, NULL, NULL);
    if (pScrn) {
	char *device;
	GDevPtr devSection = xf86GetDevFromEntity(pScrn->entityList[0],
						  pScrn->entityInstanceList[0]);

	device = xf86FindOptionValue(devSection->options, "fbdev");
	if (fbdevHWProbe(NULL, device, NULL)) {
	    pScrn->driverVersion = FBDEV_VERSION;
	    pScrn->driverName    = FBDEV_DRIVER_NAME;
	    pScrn->name          = FBDEV_NAME;
	    pScrn->Probe         = FBDevProbe;
	    pScrn->PreInit       = FBDevPreInit;
	    pScrn->ScreenInit    = FBDevScreenInit;
	    pScrn->SwitchMode    = fbdevHWSwitchModeWeak();
	    pScrn->AdjustFrame   = fbdevHWAdjustFrameWeak();
	    pScrn->EnterVT       = fbdevHWEnterVTWeak();
	    pScrn->LeaveVT       = fbdevHWLeaveVTWeak();
	    pScrn->ValidMode     = fbdevHWValidModeWeak();

	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
		       "claimed PCI slot %d@%d:%d:%d\n", 
		       dev->bus, dev->domain, dev->dev, dev->func);
	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		       "using %s\n", device ? device : "default device");
	}
	else {
	    pScrn = NULL;
	}
    }

    return (pScrn != NULL);
}
#endif


Kaleb Keithley's avatar
Kaleb Keithley committed
290
291
292
293
294
295
296
static Bool
FBDevProbe(DriverPtr drv, int flags)
{
	int i;
	ScrnInfoPtr pScrn;
       	GDevPtr *devSections;
	int numDevSections;
297
#ifndef XSERVER_LIBPCIACCESS
Kaleb Keithley's avatar
Kaleb Keithley committed
298
	int bus,device,func;
299
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
	char *dev;
	Bool foundScreen = FALSE;

	TRACE("probe start");

	/* For now, just bail out for PROBE_DETECT. */
	if (flags & PROBE_DETECT)
		return FALSE;

	if ((numDevSections = xf86MatchDevice(FBDEV_DRIVER_NAME, &devSections)) <= 0) 
	    return FALSE;
	
	if (!xf86LoadDrvSubModule(drv, "fbdevhw"))
	    return FALSE;
	    
	for (i = 0; i < numDevSections; i++) {
	    Bool isIsa = FALSE;
	    Bool isPci = FALSE;

	    dev = xf86FindOptionValue(devSections[i]->options,"fbdev");
	    if (devSections[i]->busID) {
321
#ifndef XSERVER_LIBPCIACCESS
Kaleb Keithley's avatar
Kaleb Keithley committed
322
323
324
325
326
	        if (xf86ParsePciBusString(devSections[i]->busID,&bus,&device,
					  &func)) {
		    if (!xf86CheckPciSlot(bus,device,func))
		        continue;
		    isPci = TRUE;
327
328
		} else
#endif
Adam Jackson's avatar
Adam Jackson committed
329
#ifdef HAVE_ISA
330
		if (xf86ParseIsaBusString(devSections[i]->busID))
Kaleb Keithley's avatar
Kaleb Keithley committed
331
		    isIsa = TRUE;
Adam Jackson's avatar
Adam Jackson committed
332
333
334
		else
#endif
		    0;
Kaleb Keithley's avatar
Kaleb Keithley committed
335
336
337
338
339
		  
	    }
	    if (fbdevHWProbe(NULL,dev,NULL)) {
		pScrn = NULL;
		if (isPci) {
Adam Jackson's avatar
Adam Jackson committed
340
#ifndef XSERVER_LIBPCIACCESS
Kaleb Keithley's avatar
Kaleb Keithley committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
		    /* XXX what about when there's no busID set? */
		    int entity;
		    
		    entity = xf86ClaimPciSlot(bus,device,func,drv,
					      0,devSections[i],
					      TRUE);
		    pScrn = xf86ConfigPciEntity(pScrn,0,entity,
						      NULL,RES_SHARED_VGA,
						      NULL,NULL,NULL,NULL);
		    /* xf86DrvMsg() can't be called without setting these */
		    pScrn->driverName    = FBDEV_DRIVER_NAME;
		    pScrn->name          = FBDEV_NAME;
		    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
			       "claimed PCI slot %d:%d:%d\n",bus,device,func);

356
#endif
Adam Jackson's avatar
Adam Jackson committed
357
358
		} else if (isIsa) {
#ifdef HAVE_ISA
Kaleb Keithley's avatar
Kaleb Keithley committed
359
360
361
362
363
364
365
		    int entity;
		    
		    entity = xf86ClaimIsaSlot(drv, 0,
					      devSections[i], TRUE);
		    pScrn = xf86ConfigIsaEntity(pScrn,0,entity,
						      NULL,RES_SHARED_VGA,
						      NULL,NULL,NULL,NULL);
Adam Jackson's avatar
Adam Jackson committed
366
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
367
368
369
370
371
372
373
374
375
376
377
378
		} else {
		   int entity;

		    entity = xf86ClaimFbSlot(drv, 0,
					      devSections[i], TRUE);
		    pScrn = xf86ConfigFbEntity(pScrn,0,entity,
					       NULL,NULL,NULL,NULL);
		   
		}
		if (pScrn) {
		    foundScreen = TRUE;
		    
379
		    pScrn->driverVersion = FBDEV_VERSION;
Kaleb Keithley's avatar
Kaleb Keithley committed
380
381
382
383
384
		    pScrn->driverName    = FBDEV_DRIVER_NAME;
		    pScrn->name          = FBDEV_NAME;
		    pScrn->Probe         = FBDevProbe;
		    pScrn->PreInit       = FBDevPreInit;
		    pScrn->ScreenInit    = FBDevScreenInit;
385
386
387
388
389
		    pScrn->SwitchMode    = fbdevHWSwitchModeWeak();
		    pScrn->AdjustFrame   = fbdevHWAdjustFrameWeak();
		    pScrn->EnterVT       = fbdevHWEnterVTWeak();
		    pScrn->LeaveVT       = fbdevHWLeaveVTWeak();
		    pScrn->ValidMode     = fbdevHWValidModeWeak();
Kaleb Keithley's avatar
Kaleb Keithley committed
390
391
392
393
394
395
		    
		    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
			       "using %s\n", dev ? dev : "default device");
		}
	    }
	}
396
	free(devSections);
Kaleb Keithley's avatar
Kaleb Keithley committed
397
398
399
400
401
402
403
404
405
	TRACE("probe done");
	return foundScreen;
}

static Bool
FBDevPreInit(ScrnInfoPtr pScrn, int flags)
{
	FBDevPtr fPtr;
	int default_depth, fbbpp;
Adam Jackson's avatar
Adam Jackson committed
406
	const char *s;
407
	int type;
Kaleb Keithley's avatar
Kaleb Keithley committed
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423

	if (flags & PROBE_DETECT) return FALSE;

	TRACE_ENTER("PreInit");

	/* Check the number of entities, and fail if it isn't one. */
	if (pScrn->numEntities != 1)
		return FALSE;

	pScrn->monitor = pScrn->confScreen->monitor;

	FBDevGetRec(pScrn);
	fPtr = FBDEVPTR(pScrn);

	fPtr->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);

424
#ifndef XSERVER_LIBPCIACCESS
Kaleb Keithley's avatar
Kaleb Keithley committed
425
426
427
428
429
430
431
432
433
434
	pScrn->racMemFlags = RAC_FB | RAC_COLORMAP | RAC_CURSOR | RAC_VIEWPORT;
	/* XXX Is this right?  Can probably remove RAC_FB */
	pScrn->racIoFlags = RAC_FB | RAC_COLORMAP | RAC_CURSOR | RAC_VIEWPORT;

	if (fPtr->pEnt->location.type == BUS_PCI &&
	    xf86RegisterResources(fPtr->pEnt->index,NULL,ResExclusive)) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "xf86RegisterResources() found resource conflicts\n");
		return FALSE;
	}
435
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
436
437
438
439
	/* open device */
	if (!fbdevHWInit(pScrn,NULL,xf86FindOptionValue(fPtr->pEnt->device->options,"fbdev")))
		return FALSE;
	default_depth = fbdevHWGetDepth(pScrn,&fbbpp);
440
	if (!xf86SetDepthBpp(pScrn, default_depth, default_depth, fbbpp,
441
			     Support24bppFb | Support32bppFb | SupportConvert32to24 | SupportConvert24to32))
Kaleb Keithley's avatar
Kaleb Keithley committed
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
		return FALSE;
	xf86PrintDepthBpp(pScrn);

	/* Get the depth24 pixmap format */
	if (pScrn->depth == 24 && pix24bpp == 0)
		pix24bpp = xf86GetBppFromDepth(pScrn, 24);

	/* color weight */
	if (pScrn->depth > 8) {
		rgb zeros = { 0, 0, 0 };
		if (!xf86SetWeight(pScrn, zeros, zeros))
			return FALSE;
	}

	/* visual init */
	if (!xf86SetDefaultVisual(pScrn, -1))
		return FALSE;

	/* We don't currently support DirectColor at > 8bpp */
	if (pScrn->depth > 8 && pScrn->defaultVisual != TrueColor) {
462
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "requested default visual"
Kaleb Keithley's avatar
Kaleb Keithley committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
			   " (%s) is not supported at depth %d\n",
			   xf86GetVisualName(pScrn->defaultVisual), pScrn->depth);
		return FALSE;
	}

	{
		Gamma zeros = {0.0, 0.0, 0.0};

		if (!xf86SetGamma(pScrn,zeros)) {
			return FALSE;
		}
	}

	pScrn->progClock = TRUE;
	pScrn->rgbBits   = 8;
478
	pScrn->chipset   = "sunxifb";
Kaleb Keithley's avatar
Kaleb Keithley committed
479
480
	pScrn->videoRam  = fbdevHWGetVidmem(pScrn);

481
482
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "hardware: %s (video memory:"
		   " %dkB)\n", fbdevHWGetName(pScrn), pScrn->videoRam/1024);
Kaleb Keithley's avatar
Kaleb Keithley committed
483
484
485

	/* handle options */
	xf86CollectOptions(pScrn, NULL);
486
	if (!(fPtr->Options = malloc(sizeof(FBDevOptions))))
Kaleb Keithley's avatar
Kaleb Keithley committed
487
488
489
490
491
492
493
		return FALSE;
	memcpy(fPtr->Options, FBDevOptions, sizeof(FBDevOptions));
	xf86ProcessOptions(pScrn->scrnIndex, fPtr->pEnt->device->options, fPtr->Options);

	/* use shadow framebuffer by default */
	fPtr->shadowFB = xf86ReturnOptValBool(fPtr->Options, OPTION_SHADOW_FB, TRUE);

494
495
	debug = xf86ReturnOptValBool(fPtr->Options, OPTION_DEBUG, FALSE);

Kaleb Keithley's avatar
Kaleb Keithley committed
496
497
498
499
500
501
502
503
504
	/* rotation */
	fPtr->rotate = FBDEV_ROTATE_NONE;
	if ((s = xf86GetOptValString(fPtr->Options, OPTION_ROTATE)))
	{
	  if(!xf86NameCmp(s, "CW"))
	  {
	    fPtr->shadowFB = TRUE;
	    fPtr->rotate = FBDEV_ROTATE_CW;
	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
505
		       "rotating screen clockwise\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
506
507
508
509
510
511
	  }
	  else if(!xf86NameCmp(s, "CCW"))
	  {
	    fPtr->shadowFB = TRUE;
	    fPtr->rotate = FBDEV_ROTATE_CCW;
	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
512
		       "rotating screen counter-clockwise\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
513
514
515
516
517
518
	  }
	  else if(!xf86NameCmp(s, "UD"))
	  {
	    fPtr->shadowFB = TRUE;
	    fPtr->rotate = FBDEV_ROTATE_UD;
	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
519
		       "rotating screen upside-down\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
520
521
522
523
524
525
	  }
	  else
	  {
	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
		       "\"%s\" is not a valid value for Option \"Rotate\"\n", s);
	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
526
		       "valid options are \"CW\", \"CCW\" and \"UD\"\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
527
528
529
530
531
	  }
	}

	/* select video modes */

532
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "checking modes against framebuffer device...\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
533
534
	fbdevHWSetVideoModes(pScrn);

535
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "checking modes against monitor...\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
	{
		DisplayModePtr mode, first = mode = pScrn->modes;
		
		if (mode != NULL) do {
			mode->status = xf86CheckModeForMonitor(mode, pScrn->monitor);
			mode = mode->next;
		} while (mode != NULL && mode != first);

		xf86PruneDriverModes(pScrn);
	}

	if (NULL == pScrn->modes)
		fbdevHWUseBuildinMode(pScrn);
	pScrn->currentMode = pScrn->modes;

551
552
	/* First approximation, may be refined in ScreenInit */
	pScrn->displayWidth = pScrn->virtualX;
Kaleb Keithley's avatar
Kaleb Keithley committed
553
554
555
556
557
558
559

	xf86PrintModes(pScrn);

	/* Set display resolution */
	xf86SetDpi(pScrn, 0, 0);

	/* Load bpp-specific modules */
560
	switch ((type = fbdevHWGetType(pScrn)))
Kaleb Keithley's avatar
Kaleb Keithley committed
561
562
563
564
565
566
567
568
569
570
571
	{
	case FBDEVHW_PACKED_PIXELS:
		switch (pScrn->bitsPerPixel)
		{
		case 8:
		case 16:
		case 24:
		case 32:
			break;
		default:
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
572
573
			"unsupported number of bits per pixel: %d",
			pScrn->bitsPerPixel);
Kaleb Keithley's avatar
Kaleb Keithley committed
574
575
576
577
578
579
			return FALSE;
		}
		break;
	case FBDEVHW_INTERLEAVED_PLANES:
               /* Not supported yet, don't know what to do with this */
               xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
580
581
                          "interleaved planes are not yet supported by the "
			  "fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
582
583
584
585
586
		return FALSE;
	case FBDEVHW_TEXT:
               /* This should never happen ...
                * we should check for this much much earlier ... */
               xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
587
                          "text mode is not supported by the fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
588
589
590
591
		return FALSE;
       case FBDEVHW_VGA_PLANES:
               /* Not supported yet */
               xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
592
593
                          "EGA/VGA planes are not yet supported by the fbdev "
			  "driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
594
595
596
               return FALSE;
       default:
               xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
597
                          "unrecognised fbdev hardware type (%d)\n", type);
Kaleb Keithley's avatar
Kaleb Keithley committed
598
599
               return FALSE;
	}
Adam Jackson's avatar
Adam Jackson committed
600
	if (xf86LoadSubModule(pScrn, "fb") == NULL) {
Kaleb Keithley's avatar
Kaleb Keithley committed
601
602
603
604
605
606
		FBDevFreeRec(pScrn);
		return FALSE;
	}

	/* Load shadow if needed */
	if (fPtr->shadowFB) {
607
608
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "using shadow"
			   " framebuffer\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
609
610
611
612
613
614
615
616
617
618
		if (!xf86LoadSubModule(pScrn, "shadow")) {
			FBDevFreeRec(pScrn);
			return FALSE;
		}
	}

	TRACE_EXIT("PreInit");
	return TRUE;
}

619

620
static Bool
621
FBDevCreateScreenResources(ScreenPtr pScreen)
622
623
{
    PixmapPtr pPixmap;
624
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
625
626
627
628
629
630
631
632
    FBDevPtr fPtr = FBDEVPTR(pScrn);
    Bool ret;

    pScreen->CreateScreenResources = fPtr->CreateScreenResources;
    ret = pScreen->CreateScreenResources(pScreen);
    pScreen->CreateScreenResources = FBDevCreateScreenResources;

    if (!ret)
633
	return FALSE;
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648

    pPixmap = pScreen->GetScreenPixmap(pScreen);

    if (!shadowAdd(pScreen, pPixmap, fPtr->rotate ?
		   shadowUpdateRotatePackedWeak() : shadowUpdatePackedWeak(),
		   FBDevWindowLinear, fPtr->rotate, NULL)) {
	return FALSE;
    }

    return TRUE;
}

static Bool
FBDevShadowInit(ScreenPtr pScreen)
{
649
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
650
    FBDevPtr fPtr = FBDEVPTR(pScrn);
651
652
653
654
655
    
    if (!shadowSetup(pScreen)) {
	return FALSE;
    }

656
657
    fPtr->CreateScreenResources = pScreen->CreateScreenResources;
    pScreen->CreateScreenResources = FBDevCreateScreenResources;
658

659
    return TRUE;
660
661
662
}


Kaleb Keithley's avatar
Kaleb Keithley committed
663
static Bool
Dave Airlie's avatar
Dave Airlie committed
664
FBDevScreenInit(SCREEN_INIT_ARGS_DECL)
Kaleb Keithley's avatar
Kaleb Keithley committed
665
{
666
	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Kaleb Keithley's avatar
Kaleb Keithley committed
667
668
669
	FBDevPtr fPtr = FBDEVPTR(pScrn);
	VisualPtr visual;
	int init_picture = 0;
670
	int ret, flags;
671
	int type;
Kaleb Keithley's avatar
Kaleb Keithley committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685

	TRACE_ENTER("FBDevScreenInit");

#if DEBUG
	ErrorF("\tbitsPerPixel=%d, depth=%d, defaultVisual=%s\n"
	       "\tmask: %x,%x,%x, offset: %d,%d,%d\n",
	       pScrn->bitsPerPixel,
	       pScrn->depth,
	       xf86GetVisualName(pScrn->defaultVisual),
	       pScrn->mask.red,pScrn->mask.green,pScrn->mask.blue,
	       pScrn->offset.red,pScrn->offset.green,pScrn->offset.blue);
#endif

	if (NULL == (fPtr->fbmem = fbdevHWMapVidmem(pScrn))) {
Dave Airlie's avatar
Dave Airlie committed
686
	        xf86DrvMsg(pScrn->scrnIndex,X_ERROR,"mapping of video memory"
687
			   " failed\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
688
689
690
691
692
693
694
		return FALSE;
	}
	fPtr->fboff = fbdevHWLinearOffset(pScrn);

	fbdevHWSave(pScrn);

	if (!fbdevHWModeInit(pScrn, pScrn->currentMode)) {
Dave Airlie's avatar
Dave Airlie committed
695
		xf86DrvMsg(pScrn->scrnIndex,X_ERROR,"mode initialization failed\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
696
697
698
		return FALSE;
	}
	fbdevHWSaveScreen(pScreen, SCREEN_SAVER_ON);
Dave Airlie's avatar
Dave Airlie committed
699
	fbdevHWAdjustFrame(ADJUST_FRAME_ARGS(pScrn, 0, 0));
Kaleb Keithley's avatar
Kaleb Keithley committed
700
701
702
703
704

	/* mi layer */
	miClearVisualTypes();
	if (pScrn->bitsPerPixel > 8) {
		if (!miSetVisualTypes(pScrn->depth, TrueColorMask, pScrn->rgbBits, TrueColor)) {
Dave Airlie's avatar
Dave Airlie committed
705
			xf86DrvMsg(pScrn->scrnIndex,X_ERROR,"visual type setup failed"
706
707
				   " for %d bits per pixel [1]\n",
				   pScrn->bitsPerPixel);
Kaleb Keithley's avatar
Kaleb Keithley committed
708
709
710
711
712
713
			return FALSE;
		}
	} else {
		if (!miSetVisualTypes(pScrn->depth,
				      miGetDefaultVisualMask(pScrn->depth),
				      pScrn->rgbBits, pScrn->defaultVisual)) {
Dave Airlie's avatar
Dave Airlie committed
714
			xf86DrvMsg(pScrn->scrnIndex,X_ERROR,"visual type setup failed"
715
716
				   " for %d bits per pixel [2]\n",
				   pScrn->bitsPerPixel);
Kaleb Keithley's avatar
Kaleb Keithley committed
717
718
719
720
			return FALSE;
		}
	}
	if (!miSetPixmapDepths()) {
Dave Airlie's avatar
Dave Airlie committed
721
	  xf86DrvMsg(pScrn->scrnIndex,X_ERROR,"pixmap depth setup failed\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
722
723
724
725
726
	  return FALSE;
	}

	if(fPtr->rotate==FBDEV_ROTATE_CW || fPtr->rotate==FBDEV_ROTATE_CCW)
	{
727
728
729
	  int tmp = pScrn->virtualX;
	  pScrn->virtualX = pScrn->displayWidth = pScrn->virtualY;
	  pScrn->virtualY = tmp;
730
731
732
733
734
735
736
	} else if (!fPtr->shadowFB) {
		/* FIXME: this doesn't work for all cases, e.g. when each scanline
			has a padding which is independent from the depth (controlfb) */
		pScrn->displayWidth = fbdevHWGetLineLength(pScrn) /
				      (pScrn->bitsPerPixel / 8);

		if (pScrn->displayWidth != pScrn->virtualX) {
Dave Airlie's avatar
Dave Airlie committed
737
			xf86DrvMsg(pScrn->scrnIndex, X_INFO,
738
739
740
				   "Pitch updated to %d after ModeInit\n",
				   pScrn->displayWidth);
		}
Kaleb Keithley's avatar
Kaleb Keithley committed
741
742
743
744
745
746
747
	}

	if(fPtr->rotate && !fPtr->PointerMoved) {
		fPtr->PointerMoved = pScrn->PointerMoved;
		pScrn->PointerMoved = FBDevPointerMoved;
	}

748
	fPtr->fbstart = fPtr->fbmem + fPtr->fboff;
Kaleb Keithley's avatar
Kaleb Keithley committed
749

750
	if (fPtr->shadowFB) {
751
752
	    fPtr->shadow = calloc(1, pScrn->virtualX * pScrn->virtualY *
				  pScrn->bitsPerPixel);
753
754
755
756
757
758
759
760

	    if (!fPtr->shadow) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "Failed to allocate shadow framebuffer\n");
		return FALSE;
	    }
	}

761
	switch ((type = fbdevHWGetType(pScrn)))
Kaleb Keithley's avatar
Kaleb Keithley committed
762
763
764
765
766
767
768
	{
	case FBDEVHW_PACKED_PIXELS:
		switch (pScrn->bitsPerPixel) {
		case 8:
		case 16:
		case 24:
		case 32:
769
770
771
772
773
			ret = fbScreenInit(pScreen, fPtr->shadowFB ? fPtr->shadow
					   : fPtr->fbstart, pScrn->virtualX,
					   pScrn->virtualY, pScrn->xDpi,
					   pScrn->yDpi, pScrn->displayWidth,
					   pScrn->bitsPerPixel);
Kaleb Keithley's avatar
Kaleb Keithley committed
774
775
776
			init_picture = 1;
			break;
	 	default:
Dave Airlie's avatar
Dave Airlie committed
777
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
778
779
780
				   "internal error: invalid number of bits per"
				   " pixel (%d) encountered in"
				   " FBDevScreenInit()\n", pScrn->bitsPerPixel);
Kaleb Keithley's avatar
Kaleb Keithley committed
781
782
783
784
785
786
787
			ret = FALSE;
			break;
		}
		break;
	case FBDEVHW_INTERLEAVED_PLANES:
		/* This should never happen ...
		* we should check for this much much earlier ... */
Dave Airlie's avatar
Dave Airlie committed
788
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
789
790
		           "internal error: interleaved planes are not yet "
			   "supported by the fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
791
792
793
794
795
		ret = FALSE;
		break;
	case FBDEVHW_TEXT:
		/* This should never happen ...
		* we should check for this much much earlier ... */
Dave Airlie's avatar
Dave Airlie committed
796
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
797
798
		           "internal error: text mode is not supported by the "
			   "fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
799
800
801
802
		ret = FALSE;
		break;
	case FBDEVHW_VGA_PLANES:
		/* Not supported yet */
Dave Airlie's avatar
Dave Airlie committed
803
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
804
805
		           "internal error: EGA/VGA Planes are not yet "
			   "supported by the fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
806
807
808
		ret = FALSE;
		break;
	default:
Dave Airlie's avatar
Dave Airlie committed
809
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
810
		           "internal error: unrecognised hardware type (%d) "
811
			   "encountered in FBDevScreenInit()\n", type);
Kaleb Keithley's avatar
Kaleb Keithley committed
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
		ret = FALSE;
		break;
	}
	if (!ret)
		return FALSE;

	if (pScrn->bitsPerPixel > 8) {
		/* Fixup RGB ordering */
		visual = pScreen->visuals + pScreen->numVisuals;
		while (--visual >= pScreen->visuals) {
			if ((visual->class | DynamicClass) == DirectColor) {
				visual->offsetRed   = pScrn->offset.red;
				visual->offsetGreen = pScrn->offset.green;
				visual->offsetBlue  = pScrn->offset.blue;
				visual->redMask     = pScrn->mask.red;
				visual->greenMask   = pScrn->mask.green;
				visual->blueMask    = pScrn->mask.blue;
			}
		}
	}

	/* must be after RGB ordering fixed */
	if (init_picture && !fbPictureInit(pScreen, NULL, 0))
		xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
836
			   "Render extension initialisation failed\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
837

838
	if (fPtr->shadowFB && !FBDevShadowInit(pScreen)) {
Dave Airlie's avatar
Dave Airlie committed
839
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
840
		       "shadow framebuffer initialization failed\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
841
842
843
844
845
846
	    return FALSE;
	}

	if (!fPtr->rotate)
	  FBDevDGAInit(pScrn, pScreen);
	else {
Dave Airlie's avatar
Dave Airlie committed
847
848
	  xf86DrvMsg(pScrn->scrnIndex, X_INFO, "display rotated; disabling DGA\n");
	  xf86DrvMsg(pScrn->scrnIndex, X_INFO, "using driver rotation; disabling "
849
			                "XRandR\n");
850
	  xf86DisableRandR();
Kaleb Keithley's avatar
Kaleb Keithley committed
851
	  if (pScrn->bitsPerPixel == 24)
Dave Airlie's avatar
Dave Airlie committed
852
	    xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "rotation might be broken at 24 "
853
                                             "bits per pixel\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
854
855
856
857
858
859
860
861
862
	}

	xf86SetBlackWhitePixels(pScreen);
	xf86SetBackingStore(pScreen);

	/* software cursor */
	miDCInitialize(pScreen, xf86GetPointerScreenFuncs());

	/* colormap */
863
	switch ((type = fbdevHWGetType(pScrn)))
Kaleb Keithley's avatar
Kaleb Keithley committed
864
865
866
	{
	/* XXX It would be simpler to use miCreateDefColormap() in all cases. */
	case FBDEVHW_PACKED_PIXELS:
867
		if (!miCreateDefColormap(pScreen)) {
Dave Airlie's avatar
Dave Airlie committed
868
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
869
870
                                   "internal error: miCreateDefColormap failed "
				   "in FBDevScreenInit()\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
871
			return FALSE;
872
		}
Kaleb Keithley's avatar
Kaleb Keithley committed
873
874
		break;
	case FBDEVHW_INTERLEAVED_PLANES:
Dave Airlie's avatar
Dave Airlie committed
875
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
876
877
		           "internal error: interleaved planes are not yet "
			   "supported by the fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
878
879
		return FALSE;
	case FBDEVHW_TEXT:
Dave Airlie's avatar
Dave Airlie committed
880
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
881
882
		           "internal error: text mode is not supported by "
			   "the fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
883
884
		return FALSE;
	case FBDEVHW_VGA_PLANES:
Dave Airlie's avatar
Dave Airlie committed
885
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
886
887
		           "internal error: EGA/VGA planes are not yet "
			   "supported by the fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
888
889
		return FALSE;
	default:
Dave Airlie's avatar
Dave Airlie committed
890
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
891
892
		           "internal error: unrecognised fbdev hardware type "
			   "(%d) encountered in FBDevScreenInit()\n", type);
Kaleb Keithley's avatar
Kaleb Keithley committed
893
894
895
		return FALSE;
	}
	flags = CMAP_PALETTED_TRUECOLOR;
896
	if(!xf86HandleColormaps(pScreen, 256, 8, fbdevHWLoadPaletteWeak(), 
897
				NULL, flags))
Kaleb Keithley's avatar
Kaleb Keithley committed
898
899
		return FALSE;

900
	xf86DPMSInit(pScreen, fbdevHWDPMSSetWeak(), 0);
Kaleb Keithley's avatar
Kaleb Keithley committed
901

902
	pScreen->SaveScreen = fbdevHWSaveScreenWeak();
Kaleb Keithley's avatar
Kaleb Keithley committed
903
904
905
906
907

	/* Wrap the current CloseScreen function */
	fPtr->CloseScreen = pScreen->CloseScreen;
	pScreen->CloseScreen = FBDevCloseScreen;

908
#if XV
Kaleb Keithley's avatar
Kaleb Keithley committed
909
910
911
912
913
914
915
916
	{
	    XF86VideoAdaptorPtr *ptr;

	    int n = xf86XVListGenericAdaptors(pScrn,&ptr);
	    if (n) {
		xf86XVScreenInit(pScreen,ptr,n);
	    }
	}
917
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
918

919
920
921
	fPtr->sunxi_disp_private = sunxi_disp_init(xf86FindOptionValue(
	                                fPtr->pEnt->device->options,"fbdev"));

Kaleb Keithley's avatar
Kaleb Keithley committed
922
923
924
925
926
927
	TRACE_EXIT("FBDevScreenInit");

	return TRUE;
}

static Bool
Dave Airlie's avatar
Dave Airlie committed
928
FBDevCloseScreen(CLOSE_SCREEN_ARGS_DECL)
Kaleb Keithley's avatar
Kaleb Keithley committed
929
{
Dave Airlie's avatar
Dave Airlie committed
930
	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Kaleb Keithley's avatar
Kaleb Keithley committed
931
	FBDevPtr fPtr = FBDEVPTR(pScrn);
932
933
934
935
936
937
938

	if (fPtr->sunxi_disp_private) {
	    sunxi_disp_close(fPtr->sunxi_disp_private);
	    free(fPtr->sunxi_disp_private);
	    fPtr->sunxi_disp_private = NULL;
	}

Kaleb Keithley's avatar
Kaleb Keithley committed
939
940
	fbdevHWRestore(pScrn);
	fbdevHWUnmapVidmem(pScrn);
941
	if (fPtr->shadow) {
942
	    shadowRemove(pScreen, pScreen->GetScreenPixmap(pScreen));
943
	    free(fPtr->shadow);
944
945
	    fPtr->shadow = NULL;
	}
Kaleb Keithley's avatar
Kaleb Keithley committed
946
	if (fPtr->pDGAMode) {
947
	  free(fPtr->pDGAMode);
Kaleb Keithley's avatar
Kaleb Keithley committed
948
949
950
951
952
	  fPtr->pDGAMode = NULL;
	  fPtr->nDGAMode = 0;
	}
	pScrn->vtSema = FALSE;

953
	pScreen->CreateScreenResources = fPtr->CreateScreenResources;
Kaleb Keithley's avatar
Kaleb Keithley committed
954
	pScreen->CloseScreen = fPtr->CloseScreen;
Dave Airlie's avatar
Dave Airlie committed
955
	return (*pScreen->CloseScreen)(CLOSE_SCREEN_ARGS);
Kaleb Keithley's avatar
Kaleb Keithley committed
956
957
958
959
960
961
962
963
964
965
966
967
}



/***********************************************************************
 * Shadow stuff
 ***********************************************************************/

static void *
FBDevWindowLinear(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
		 CARD32 *size, void *closure)
{
968
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Kaleb Keithley's avatar
Kaleb Keithley committed
969
970
971
972
973
974
975
976
977
978
    FBDevPtr fPtr = FBDEVPTR(pScrn);

    if (!pScrn->vtSema)
      return NULL;

    if (fPtr->lineLength)
      *size = fPtr->lineLength;
    else
      *size = fPtr->lineLength = fbdevHWGetLineLength(pScrn);

979
    return ((CARD8 *)fPtr->fbstart + row * fPtr->lineLength + offset);
Kaleb Keithley's avatar
Kaleb Keithley committed
980
981
982
}

static void
Dave Airlie's avatar
Dave Airlie committed
983
FBDevPointerMoved(SCRN_ARG_TYPE arg, int x, int y)
Kaleb Keithley's avatar
Kaleb Keithley committed
984
{
Dave Airlie's avatar
Dave Airlie committed
985
    SCRN_INFO_PTR(arg);
Kaleb Keithley's avatar
Kaleb Keithley committed
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
    FBDevPtr fPtr = FBDEVPTR(pScrn);
    int newX, newY;

    switch (fPtr->rotate)
    {
    case FBDEV_ROTATE_CW:
	/* 90 degrees CW rotation. */
	newX = pScrn->pScreen->height - y - 1;
	newY = x;
	break;

    case FBDEV_ROTATE_CCW:
	/* 90 degrees CCW rotation. */
	newX = y;
	newY = pScrn->pScreen->width - x - 1;
	break;

    case FBDEV_ROTATE_UD:
	/* 180 degrees UD rotation. */
	newX = pScrn->pScreen->width - x - 1;
	newY = pScrn->pScreen->height - y - 1;
	break;

    default:
	/* No rotation. */
	newX = x;
	newY = y;
	break;
    }

    /* Pass adjusted pointer coordinates to wrapped PointerMoved function. */
Dave Airlie's avatar
Dave Airlie committed
1017
    (*fPtr->PointerMoved)(arg, newX, newY);
Kaleb Keithley's avatar
Kaleb Keithley committed
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
}


/***********************************************************************
 * DGA stuff
 ***********************************************************************/
static Bool FBDevDGAOpenFramebuffer(ScrnInfoPtr pScrn, char **DeviceName,
				   unsigned char **ApertureBase,
				   int *ApertureSize, int *ApertureOffset,
				   int *flags);
static Bool FBDevDGASetMode(ScrnInfoPtr pScrn, DGAModePtr pDGAMode);
static void FBDevDGASetViewport(ScrnInfoPtr pScrn, int x, int y, int flags);

static Bool
FBDevDGAOpenFramebuffer(ScrnInfoPtr pScrn, char **DeviceName,
		       unsigned char **ApertureBase, int *ApertureSize,
		       int *ApertureOffset, int *flags)
{
    *DeviceName = NULL;		/* No special device */
    *ApertureBase = (unsigned char *)(pScrn->memPhysBase);
    *ApertureSize = pScrn->videoRam;
    *ApertureOffset = pScrn->fbOffset;
    *flags = 0;

    return TRUE;
}

static Bool
FBDevDGASetMode(ScrnInfoPtr pScrn, DGAModePtr pDGAMode)
{
    DisplayModePtr pMode;
    int scrnIdx = pScrn->pScreen->myNum;
    int frameX0, frameY0;

    if (pDGAMode) {
	pMode = pDGAMode->mode;
	frameX0 = frameY0 = 0;
    }
    else {
	if (!(pMode = pScrn->currentMode))
	    return TRUE;

	frameX0 = pScrn->frameX0;
	frameY0 = pScrn->frameY0;
    }

Dave Airlie's avatar
Dave Airlie committed
1064
    if (!(*pScrn->SwitchMode)(SWITCH_MODE_ARGS(pScrn, pMode)))
Kaleb Keithley's avatar
Kaleb Keithley committed
1065
	return FALSE;
Dave Airlie's avatar
Dave Airlie committed
1066
    (*pScrn->AdjustFrame)(ADJUST_FRAME_ARGS(pScrn, frameX0, frameY0));
Kaleb Keithley's avatar
Kaleb Keithley committed
1067
1068
1069
1070
1071
1072
1073

    return TRUE;
}

static void
FBDevDGASetViewport(ScrnInfoPtr pScrn, int x, int y, int flags)
{
Dave Airlie's avatar
Dave Airlie committed
1074
    (*pScrn->AdjustFrame)(ADJUST_FRAME_ARGS(pScrn, x, y));
Kaleb Keithley's avatar
Kaleb Keithley committed
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
}

static int
FBDevDGAGetViewport(ScrnInfoPtr pScrn)
{
    return (0);
}

static DGAFunctionRec FBDevDGAFunctions =
{
    FBDevDGAOpenFramebuffer,
    NULL,       /* CloseFramebuffer */
    FBDevDGASetMode,
    FBDevDGASetViewport,
    FBDevDGAGetViewport,
    NULL,       /* Sync */
    NULL,       /* FillRect */
    NULL,       /* BlitRect */
    NULL,       /* BlitTransRect */
};

static void
FBDevDGAAddModes(ScrnInfoPtr pScrn)
{
    FBDevPtr fPtr = FBDEVPTR(pScrn);
    DisplayModePtr pMode = pScrn->modes;
    DGAModePtr pDGAMode;

    do {
1104
1105
	pDGAMode = realloc(fPtr->pDGAMode,
		           (fPtr->nDGAMode + 1) * sizeof(DGAModeRec));
Kaleb Keithley's avatar
Kaleb Keithley committed
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
	if (!pDGAMode)
	    break;

	fPtr->pDGAMode = pDGAMode;
	pDGAMode += fPtr->nDGAMode;
	(void)memset(pDGAMode, 0, sizeof(DGAModeRec));

	++fPtr->nDGAMode;
	pDGAMode->mode = pMode;
	pDGAMode->flags = DGA_CONCURRENT_ACCESS | DGA_PIXMAP_AVAILABLE;
	pDGAMode->byteOrder = pScrn->imageByteOrder;
	pDGAMode->depth = pScrn->depth;
	pDGAMode->bitsPerPixel = pScrn->bitsPerPixel;
	pDGAMode->red_mask = pScrn->mask.red;
	pDGAMode->green_mask = pScrn->mask.green;
	pDGAMode->blue_mask = pScrn->mask.blue;
	pDGAMode->visualClass = pScrn->bitsPerPixel > 8 ?
	    TrueColor : PseudoColor;
	pDGAMode->xViewportStep = 1;
	pDGAMode->yViewportStep = 1;
	pDGAMode->viewportWidth = pMode->HDisplay;
	pDGAMode->viewportHeight = pMode->VDisplay;

	if (fPtr->lineLength)
	  pDGAMode->bytesPerScanline = fPtr->lineLength;
	else
	  pDGAMode->bytesPerScanline = fPtr->lineLength = fbdevHWGetLineLength(pScrn);

	pDGAMode->imageWidth = pMode->HDisplay;
	pDGAMode->imageHeight =  pMode->VDisplay;
	pDGAMode->pixmapWidth = pDGAMode->imageWidth;
	pDGAMode->pixmapHeight = pDGAMode->imageHeight;
	pDGAMode->maxViewportX = pScrn->virtualX -
				    pDGAMode->viewportWidth;
	pDGAMode->maxViewportY = pScrn->virtualY -
				    pDGAMode->viewportHeight;

	pDGAMode->address = fPtr->fbstart;

	pMode = pMode->next;
    } while (pMode != pScrn->modes);
}

static Bool
FBDevDGAInit(ScrnInfoPtr pScrn, ScreenPtr pScreen)
{
1152
#ifdef XFreeXDGA
Kaleb Keithley's avatar
Kaleb Keithley committed
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
    FBDevPtr fPtr = FBDEVPTR(pScrn);

    if (pScrn->depth < 8)
	return FALSE;

    if (!fPtr->nDGAMode)
	FBDevDGAAddModes(pScrn);

    return (DGAInit(pScreen, &FBDevDGAFunctions,
	    fPtr->pDGAMode, fPtr->nDGAMode));
1163
1164
1165
#else
    return TRUE;
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
1166
}
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181

static Bool
FBDevDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op, pointer ptr)
{
    xorgHWFlags *flag;
    
    switch (op) {
	case GET_REQUIRED_HW_INTERFACES:
	    flag = (CARD32*)ptr;
	    (*flag) = 0;
	    return TRUE;
	default:
	    return FALSE;
    }
}