fbdevhw.c 25.4 KB
Newer Older
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1
/* all driver need this */
2
3
4
5
#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

Adam Jackson's avatar
Adam Jackson committed
6
7
#include <string.h>

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
8
9
10
11
12
13
14
15
16
17
18
19
#include "xf86.h"
#include "xf86_OSproc.h"

/* pci stuff */
#include "xf86PciInfo.h"
#include "xf86Pci.h"

#include "xf86cmap.h"

#include "fbdevhw.h"
#include "fbpriv.h"

20
21
22
23
24
25
#if 0
/* kernel header doesn't work with -ansi */
# include "asm/page.h"	/* #define for PAGE_* */
#else
# define PAGE_MASK               (~(getpagesize() - 1))
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
26
27
28

#include "globals.h"
#define DPMS_SERVER
29
#include <X11/extensions/dpms.h>
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
30
31
32

#define DEBUG 0

33
34
#define PAGE_MASK               (~(getpagesize() - 1))

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#if DEBUG
# define TRACE_ENTER(str)	ErrorF("fbdevHW: " str " %d\n",pScrn->scrnIndex)
#else
# define TRACE_ENTER(str)
#endif

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

static MODULESETUPPROTO(fbdevhwSetup);

static XF86ModuleVersionInfo fbdevHWVersRec =
{
	"fbdevhw",
	MODULEVENDORSTRING,
	MODINFOSTRING1,
	MODINFOSTRING2,
Egbert Eich's avatar
Egbert Eich committed
51
	XORG_VERSION_CURRENT,
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
52
53
54
55
56
57
58
	0, 0, 2,
	ABI_CLASS_VIDEODRV,
	ABI_VIDEODRV_VERSION,
	MOD_CLASS_NONE,
	{0,0,0,0}
};

59
60
61
62
63
_X_EXPORT XF86ModuleData fbdevhwModuleData = {
    &fbdevHWVersRec,
    fbdevhwSetup,
    NULL
};
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

static pointer
fbdevhwSetup(pointer module, pointer opts, int *errmaj, int *errmin)
{
	const char *osname;

	/* Check that we're being loaded on a Linux system */
	LoaderGetOS(&osname, NULL, NULL, NULL);
	if (!osname || strcmp(osname, "linux") != 0) {
		if (errmaj)
			*errmaj = LDR_BADOS;
		if (errmin)
			*errmin = 0;
		return NULL;
	} else {
		/* OK */
		return (pointer)1;
	}
}

#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
87
88
89
90
91
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

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

#define FBDEVHWPTRLVAL(p) (p)->privates[fbdevHWPrivateIndex].ptr
#define FBDEVHWPTR(p) ((fbdevHWPtr)(FBDEVHWPTRLVAL(p)))

static int fbdevHWPrivateIndex = -1;

typedef struct {
	/* framebuffer device: filename (/dev/fb*), handle, more */
	char*				device;
	int				fd;
	void*				fbmem;
	unsigned int			fbmem_len;
	unsigned int			fboff;
	char*				mmio;
	unsigned int			mmio_len;

	/* current hardware state */
	struct fb_fix_screeninfo	fix;
	struct fb_var_screeninfo	var;

	/* saved video mode */
	struct fb_var_screeninfo	saved_var;

	/* FIXME: unused??? [geert] */
	struct fb_cmap			saved_cmap;
	unsigned short			*saved_red;
	unsigned short			*saved_green;
	unsigned short			*saved_blue;

	/* buildin video mode */
	DisplayModeRec			buildin;

} fbdevHWRec, *fbdevHWPtr;

Bool
fbdevHWGetRec(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr;
	
	if (fbdevHWPrivateIndex < 0)
		fbdevHWPrivateIndex = xf86AllocateScrnInfoPrivateIndex();

	if (FBDEVHWPTR(pScrn) != NULL)
		return TRUE;
	
	fPtr = FBDEVHWPTRLVAL(pScrn) = xnfcalloc(sizeof(fbdevHWRec), 1);
	return TRUE;
}

void
fbdevHWFreeRec(ScrnInfoPtr pScrn)
{
	if (fbdevHWPrivateIndex < 0)
		return;
	if (FBDEVHWPTR(pScrn) == NULL)
		return;
	xfree(FBDEVHWPTR(pScrn));
	FBDEVHWPTRLVAL(pScrn) = NULL;
}

/* -------------------------------------------------------------------- */
/* some helpers for printing debug informations                         */

#if DEBUG
static void
print_fbdev_mode(char *txt, struct fb_var_screeninfo *var)
{
	ErrorF( "fbdev %s mode:\t%d   %d %d %d %d   %d %d %d %d   %d %d:%d:%d\n",
		txt,var->pixclock,
		var->xres, var->right_margin, var->hsync_len, var->left_margin,
		var->yres, var->lower_margin, var->vsync_len, var->upper_margin,
		var->bits_per_pixel,
		var->red.length, var->green.length, var->blue.length);
}

static void
print_xfree_mode(char *txt, DisplayModePtr mode)
{
	ErrorF( "xfree %s mode:\t%d   %d %d %d %d   %d %d %d %d\n",
		txt,mode->Clock,
		mode->HDisplay, mode->HSyncStart, mode->HSyncEnd, mode->HTotal,
		mode->VDisplay, mode->VSyncStart, mode->VSyncEnd, mode->VTotal);
}
#endif

/* -------------------------------------------------------------------- */
/* Convert timings between the XFree and the Frame Buffer Device        */

static void
xfree2fbdev_fblayout(ScrnInfoPtr pScrn, struct fb_var_screeninfo *var)
{
186
187
	var->xres_virtual   = pScrn->displayWidth ? pScrn->displayWidth :
			      pScrn->virtualX;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
188
189
	var->yres_virtual   = pScrn->virtualY;
	var->bits_per_pixel = pScrn->bitsPerPixel;
190
191
192
193
194
195
196
197
198
199
	if (pScrn->defaultVisual == TrueColor ||
	    pScrn->defaultVisual == DirectColor) {
	    var->red.length     = pScrn->weight.red;
	    var->green.length   = pScrn->weight.green;
	    var->blue.length    = pScrn->weight.blue;
	} else {
	    var->red.length     = 8;
	    var->green.length   = 8;
	    var->blue.length    = 8;
	}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
}

static void
xfree2fbdev_timing(DisplayModePtr mode, struct fb_var_screeninfo *var)
{
	var->xres = mode->HDisplay;
	var->yres = mode->VDisplay;
	if (var->xres_virtual < var->xres)
		var->xres_virtual = var->xres;
	if (var->yres_virtual < var->yres)
		var->yres_virtual = var->yres;
	var->xoffset = var->yoffset = 0;
	var->pixclock = mode->Clock ? 1000000000/mode->Clock : 0;
	var->right_margin = mode->HSyncStart-mode->HDisplay;
	var->hsync_len = mode->HSyncEnd-mode->HSyncStart;
	var->left_margin = mode->HTotal-mode->HSyncEnd;
	var->lower_margin = mode->VSyncStart-mode->VDisplay;
	var->vsync_len = mode->VSyncEnd-mode->VSyncStart;
	var->upper_margin = mode->VTotal-mode->VSyncEnd;
	var->sync = 0;
	if (mode->Flags & V_PHSYNC)
		var->sync |= FB_SYNC_HOR_HIGH_ACT;
	if (mode->Flags & V_PVSYNC)
		var->sync |= FB_SYNC_VERT_HIGH_ACT;
	if (mode->Flags & V_PCSYNC)
		var->sync |= FB_SYNC_COMP_HIGH_ACT;
#if 1 /* Badly needed for PAL/NTSC on Amiga (amifb)!! [geert] */
	if (mode->Flags & V_BCAST)
		var->sync |= FB_SYNC_BROADCAST;
#endif
	if (mode->Flags & V_INTERLACE)
		var->vmode = FB_VMODE_INTERLACED;
	else if (mode->Flags & V_DBLSCAN)
		var->vmode = FB_VMODE_DOUBLE;
	else
		var->vmode = FB_VMODE_NONINTERLACED;
}

238
static Bool
239
fbdev_modes_equal(struct fb_var_screeninfo *set, struct fb_var_screeninfo *req)
240
{
241
	return (set->xres_virtual >= req->xres_virtual &&
242
		set->yres_virtual >= req->yres_virtual &&
243
244
245
246
247
248
249
250
251
252
253
254
255
		set->bits_per_pixel == req->bits_per_pixel &&
		set->red.length == req->red.length &&
		set->green.length == req->green.length &&
		set->blue.length == req->blue.length &&
		set->xres == req->xres && set->yres == req->yres &&
		set->pixclock == req->pixclock &&
		set->right_margin == req->right_margin &&
		set->hsync_len == req->hsync_len &&
		set->left_margin == req->left_margin &&
		set->lower_margin == req->lower_margin &&
		set->vsync_len == req->vsync_len &&
		set->upper_margin == req->upper_margin &&
		set->sync == req->sync && set->vmode == req->vmode);
256
257
}

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
258
259
260
static void
fbdev2xfree_timing(struct fb_var_screeninfo *var, DisplayModePtr mode)
{
Adam Jackson's avatar
Adam Jackson committed
261
	mode->Clock = var->pixclock ? 1000000000/var->pixclock : 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
290
291
292
293
294
295
296
297
298
	mode->HDisplay = var->xres;
	mode->HSyncStart = mode->HDisplay+var->right_margin;
	mode->HSyncEnd = mode->HSyncStart+var->hsync_len;
	mode->HTotal = mode->HSyncEnd+var->left_margin;
	mode->VDisplay = var->yres;
	mode->VSyncStart = mode->VDisplay+var->lower_margin;
	mode->VSyncEnd = mode->VSyncStart+var->vsync_len;
	mode->VTotal = mode->VSyncEnd+var->upper_margin;
	mode->Flags = 0;
	mode->Flags |= var->sync & FB_SYNC_HOR_HIGH_ACT ? V_PHSYNC : V_NHSYNC;
	mode->Flags |= var->sync & FB_SYNC_VERT_HIGH_ACT ? V_PVSYNC : V_NVSYNC;
	mode->Flags |= var->sync & FB_SYNC_COMP_HIGH_ACT ? V_PCSYNC : V_NCSYNC;
#if 1 /* Badly needed for PAL/NTSC on Amiga (amifb)!! [geert] */
	if (var->sync & FB_SYNC_BROADCAST)
		mode->Flags |= V_BCAST;
#endif
	if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
		mode->Flags |= V_INTERLACE;
	else if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE)
		mode->Flags |= V_DBLSCAN;
	mode->SynthClock = mode->Clock;
	mode->CrtcHDisplay = mode->HDisplay;
	mode->CrtcHSyncStart = mode->HSyncStart;
	mode->CrtcHSyncEnd = mode->HSyncEnd;
	mode->CrtcHTotal = mode->HTotal;
	mode->CrtcVDisplay = mode->VDisplay;
	mode->CrtcVSyncStart = mode->VSyncStart;
	mode->CrtcVSyncEnd = mode->VSyncEnd;
	mode->CrtcVTotal = mode->VTotal;
	mode->CrtcHAdjusted = FALSE;
	mode->CrtcVAdjusted = FALSE;
}


/* -------------------------------------------------------------------- */
/* open correct framebuffer device                                      */

299
300
301
/**
 * Try to find the framebuffer device for a given PCI device 
 */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
302
static int
303
fbdev_open_pci(struct pci_device * pPci, char **namep)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
304
{
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    struct	fb_fix_screeninfo fix;
    char	filename[256];
    int	fd,i,j;


    /* There are two ways to that we can determine which fb device is
     * associated with this PCI device.  The more modern way is to look in
     * the sysfs directory for the PCI device for a file named
     * "graphics:fb*"
     */

    for (i = 0; i < 8; i++) {
	sprintf(filename, 
		"/sys/bus/pci/devices/%04x:%02x:%02x.%d/graphics:fb%d",
		pPci->domain, pPci->bus, pPci->dev, pPci->func, i);

	fd = open(filename, O_RDONLY, 0);
	if (fd != -1) {
	    close(fd);
	    sprintf(filename, "/dev/fb%d", i);

	    fd = open(filename, O_RDWR, 0);
	    if (fd != -1) {
		if (ioctl(fd, FBIOGET_FSCREENINFO, (void*) & fix) != -1) {
		    if (namep) {
			*namep = xnfalloc(16);
			strncpy(*namep,fix.id,16);
		    }

		    return fd;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
335
		}
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
	    }
	}

	close(fd);
    }


    /* The other way is to examine the resources associated with each fb
     * device and see if there is a match with the PCI device.  This technique
     * has some problems on certain mixed 64-bit / 32-bit architectures.
     * There is a flaw in the fb_fix_screeninfo structure in that it only
     * returns the low 32-bits of the address of the resources associated with
     * a device.  However, on a mixed architecture the base addresses of PCI
     * devices, even for 32-bit applications, may be higher than 0x0f0000000.
     */

    for (i = 0; i < 8; i++) {
	sprintf(filename,"/dev/fb%d",i);
	if (-1 == (fd = open(filename,O_RDWR,0))) {
	    xf86DrvMsg(-1, X_WARNING,
		       "open %s: %s\n", filename, strerror(errno));
	    continue;
	}
	if (-1 == ioctl(fd,FBIOGET_FSCREENINFO,(void*)&fix)) {
	    close(fd);
	    continue;
	}
	for (j = 0; j < 6; j++) {
	    const pciaddr_t res_start = pPci->regions[j].base_addr;
	    const pciaddr_t res_end = res_start + pPci->regions[j].size;

	    if ((0 != fix.smem_len &&
		 (pciaddr_t) fix.smem_start >= res_start &&
		 (pciaddr_t) fix.smem_start < res_end) ||
		(0 != fix.mmio_len &&
		 (pciaddr_t) fix.mmio_start >= res_start &&
		 (pciaddr_t) fix.mmio_start < res_end))
	      break;
	}
	if (j == 6) {
	    close(fd);
	    continue;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
378
	}
379
380
381
382
383
384
385
386
387
	if (namep) {
	    *namep = xnfalloc(16);
	    strncpy(*namep,fix.id,16);
	}
	return fd;
    }

    if (namep)
      *namep = NULL;
388

389
390
391
    xf86DrvMsg(-1, X_ERROR,
	       "Unable to find a valid framebuffer device\n");
    return -1;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
}

static int
fbdev_open(int scrnIndex, char *dev, char** namep)
{
	struct	fb_fix_screeninfo fix;
	int    fd;

	/* try argument (from XF86Config) first */
	if (dev) {
	    fd = open(dev,O_RDWR,0);
	} else {
	    /* second: environment variable */
	    dev = getenv("FRAMEBUFFER");
	    if ((NULL == dev) || ((fd = open(dev,O_RDWR,0)) == -1)) {
		/* last try: default device */
		dev = "/dev/fb0";
		fd = open(dev,O_RDWR,0);
	    }
	}

	if (fd == -1) {
	    xf86DrvMsg(scrnIndex, X_ERROR,
		       "open %s: %s\n", dev, strerror(errno));
	    return -1;
	}

	if (namep) {
	    if (-1 == ioctl(fd,FBIOGET_FSCREENINFO,(void*)(&fix))) {
		*namep = NULL;
		xf86DrvMsg(scrnIndex, X_ERROR,
			   "FBIOGET_FSCREENINFO: %s\n", strerror(errno));
		return -1;
	    } else {
		*namep = xnfalloc(16);
		strncpy(*namep,fix.id,16);
	    }
	}
	return fd;
}

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

Bool
436
fbdevHWProbe(struct pci_device * pPci, char *device,char **namep)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
{
	int fd;

	if (pPci)
		fd = fbdev_open_pci(pPci,namep);
	else
		fd = fbdev_open(-1,device,namep);

	if (-1 == fd)
		return FALSE;
	close(fd);
	return TRUE;
}

Bool
452
fbdevHWInit(ScrnInfoPtr pScrn, struct pci_device * pPci, char *device)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
{
	fbdevHWPtr fPtr;

	TRACE_ENTER("Init");

	fbdevHWGetRec(pScrn);
	fPtr = FBDEVHWPTR(pScrn);

	/* open device */
	if (pPci)
		fPtr->fd = fbdev_open_pci(pPci,NULL);
	else
		fPtr->fd = fbdev_open(pScrn->scrnIndex,device,NULL);
	if (-1 == fPtr->fd) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "Failed to open framebuffer device, consult warnings"
			   " and/or errors above for possible reasons\n"
			   "\t(you may have to look at the server log to see"
			   " warnings)\n");
		return FALSE;
	}

	/* get current fb device settings */
	if (-1 == ioctl(fPtr->fd,FBIOGET_FSCREENINFO,(void*)(&fPtr->fix))) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "ioctl FBIOGET_FSCREENINFO: %s\n",
			   strerror(errno));
		return FALSE;
	}
	if (-1 == ioctl(fPtr->fd,FBIOGET_VSCREENINFO,(void*)(&fPtr->var))) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "ioctl FBIOGET_VSCREENINFO: %s\n",
			   strerror(errno));
		return FALSE;
	}

	/* we can use the current settings as "buildin mode" */
	fbdev2xfree_timing(&fPtr->var, &fPtr->buildin);
	fPtr->buildin.name  = "current";
	fPtr->buildin.next  = &fPtr->buildin;
	fPtr->buildin.prev  = &fPtr->buildin;
	fPtr->buildin.type |= M_T_BUILTIN;
	
	return TRUE;
}

char*
fbdevHWGetName(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);
	return fPtr->fix.id;
}

int
fbdevHWGetDepth(ScrnInfoPtr pScrn, int *fbbpp)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);

	if (fbbpp)
	    *fbbpp = fPtr->var.bits_per_pixel;

	if (fPtr->fix.visual == FB_VISUAL_TRUECOLOR ||
	    fPtr->fix.visual == FB_VISUAL_DIRECTCOLOR)
		return fPtr->var.red.length+fPtr->var.green.length+
			fPtr->var.blue.length;
	else
		return fPtr->var.bits_per_pixel;
}

int
fbdevHWGetLineLength(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);

	if (fPtr->fix.line_length)
		return fPtr->fix.line_length;
	else
		return fPtr->var.xres_virtual*fPtr->var.bits_per_pixel/8;
}

int
fbdevHWGetType(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);
	return fPtr->fix.type;
}

int
fbdevHWGetVidmem(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);
	return fPtr->fix.smem_len;
}

547
548
549
550
551
552
static Bool
fbdevHWSetMode(ScrnInfoPtr pScrn, DisplayModePtr mode, Bool check)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);
	struct fb_var_screeninfo req_var = fPtr->var, set_var;
	
553
	TRACE_ENTER("SetMode");
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584

	xfree2fbdev_fblayout(pScrn, &req_var);
	xfree2fbdev_timing(mode, &req_var);

#if DEBUG
	print_xfree_mode("init", mode);
	print_fbdev_mode("init", &req_var);
#endif

	set_var = req_var;

	if (check)
		set_var.activate = FB_ACTIVATE_TEST;

	if (0 != ioctl(fPtr->fd, FBIOPUT_VSCREENINFO, (void*)(&set_var))) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "FBIOPUT_VSCREENINFO: %s\n", strerror(errno));
		return FALSE;
	}

	if (!fbdev_modes_equal(&set_var, &req_var)) {
		if (!check)
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "FBIOPUT_VSCREENINFO succeeded but modified "
				   "mode\n");
#if DEBUG
		print_fbdev_mode("returned", &set_var);
#endif
		return FALSE;
	}

585
586
	if (!check)
		fPtr->var = set_var;
587
588
589
590

	return TRUE;
}

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
591
592
593
594
void
fbdevHWSetVideoModes(ScrnInfoPtr pScrn)
{
	char **modename;
595
	DisplayModePtr mode,this,last = pScrn->modes;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
596
597
598
599
600

	TRACE_ENTER("VerifyModes");
	if (NULL == pScrn->display->modes)
		return;

601
602
603
	pScrn->virtualX = pScrn->display->virtualX;
	pScrn->virtualY = pScrn->display->virtualY;

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
604
605
606
607
608
609
610
611
612
	for (modename = pScrn->display->modes; *modename != NULL; modename++) {
		for (mode = pScrn->monitor->Modes; mode != NULL; mode = mode->next)
			if (0 == strcmp(mode->name,*modename))
				break;
		if (NULL == mode) {
			xf86DrvMsg(pScrn->scrnIndex, X_INFO,
				   "\tmode \"%s\" not found\n", *modename);
			continue;
		}
613
614

		if (!fbdevHWSetMode(pScrn, mode, TRUE)) {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
615
616
617
618
619
620
			xf86DrvMsg(pScrn->scrnIndex, X_INFO,
				   "\tmode \"%s\" test failed\n", *modename);
			continue;
		}
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
			   "\tmode \"%s\" ok\n", *modename);
621
622
623
624
625
626

		if (pScrn->virtualX < mode->HDisplay)
			pScrn->virtualX = mode->HDisplay;
		if (pScrn->virtualY < mode->VDisplay)
			pScrn->virtualY = mode->VDisplay;

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
		if (NULL == pScrn->modes) {
			pScrn->modes = xnfalloc(sizeof(DisplayModeRec));
			this = pScrn->modes;
			memcpy(this,mode,sizeof(DisplayModeRec));
			this->next = this;
			this->prev = this;
		} else {
			this = xnfalloc(sizeof(DisplayModeRec));
			memcpy(this,mode,sizeof(DisplayModeRec));
			this->next = pScrn->modes;
			this->prev = last;
			last->next = this;
			pScrn->modes->prev = this;
		}
		last = this;
	}
}

DisplayModePtr
fbdevHWGetBuildinMode(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);
	return &fPtr->buildin;
}

void
fbdevHWUseBuildinMode(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);

	TRACE_ENTER("UseBuildinMode");
	pScrn->modes    = &fPtr->buildin;
	pScrn->virtualX = pScrn->display->virtualX;
	pScrn->virtualY = pScrn->display->virtualY;
	if (pScrn->virtualX < fPtr->buildin.HDisplay)
		pScrn->virtualX = fPtr->buildin.HDisplay;
	if (pScrn->virtualY < fPtr->buildin.VDisplay)
		pScrn->virtualY = fPtr->buildin.VDisplay;
}

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

669
static void
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
calculateFbmem_len(fbdevHWPtr fPtr)
{
	fPtr->fboff = (unsigned long) fPtr->fix.smem_start & ~PAGE_MASK;
	fPtr->fbmem_len = (fPtr->fboff+fPtr->fix.smem_len+~PAGE_MASK) &
			  PAGE_MASK;
}


void*
fbdevHWMapVidmem(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);

	TRACE_ENTER("MapVidmem");
	if (NULL == fPtr->fbmem) {
		calculateFbmem_len(fPtr);
		fPtr->fbmem = mmap(NULL, fPtr->fbmem_len, PROT_READ | PROT_WRITE,
				   MAP_SHARED, fPtr->fd, 0);
		if (-1 == (long)fPtr->fbmem) {
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "mmap fbmem: %s\n", strerror(errno));
			fPtr->fbmem = NULL;
		} else {
		    /* Perhaps we'd better add fboff to fbmem and return 0 in
		       fbdevHWLinearOffset()? Of course we then need to mask
		       fPtr->fbmem with PAGE_MASK in fbdevHWUnmapVidmem() as
		       well. [geert] */
		}
	}
	pScrn->memPhysBase = (unsigned long)fPtr->fix.smem_start & (unsigned long)(PAGE_MASK);
	pScrn->fbOffset = (unsigned long)fPtr->fix.smem_start & (unsigned long)(~PAGE_MASK);
	return fPtr->fbmem;
}

int
fbdevHWLinearOffset(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);

	TRACE_ENTER("LinearOffset");
	return fPtr->fboff;
}

Bool
fbdevHWUnmapVidmem(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);

	TRACE_ENTER("UnmapVidmem");
	if (NULL != fPtr->fbmem) {
		if (-1 == munmap(fPtr->fbmem, fPtr->fbmem_len))
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "munmap fbmem: %s\n", strerror(errno));
		fPtr->fbmem = NULL;
	}
	return TRUE;
}

void*
fbdevHWMapMMIO(ScrnInfoPtr pScrn)
{
	unsigned int mmio_off;

	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);

	TRACE_ENTER("MapMMIO");
	if (NULL == fPtr->mmio) {
		/* tell the kernel not to use accels to speed up console scrolling */
		fPtr->var.accel_flags = 0;
		if (0 != ioctl(fPtr->fd,FBIOPUT_VSCREENINFO,(void*)(&fPtr->var))) {
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "FBIOPUT_VSCREENINFO: %s\n", strerror(errno));
			return FALSE;
		}
		mmio_off = (unsigned long) fPtr->fix.mmio_start & ~PAGE_MASK;
		fPtr->mmio_len = (mmio_off+fPtr->fix.mmio_len+~PAGE_MASK) &
				  PAGE_MASK;
		if (NULL == fPtr->fbmem)
			calculateFbmem_len(fPtr);
		fPtr->mmio = mmap(NULL, fPtr->mmio_len, PROT_READ | PROT_WRITE,
				  MAP_SHARED, fPtr->fd, fPtr->fbmem_len);
		if (-1 == (long)fPtr->mmio) {
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "mmap mmio: %s\n", strerror(errno));
			fPtr->mmio = NULL;
		} else
			fPtr->mmio += mmio_off;
	}
	return fPtr->mmio;
}

Bool
fbdevHWUnmapMMIO(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);

	TRACE_ENTER("UnmapMMIO");
	if (NULL != fPtr->mmio) {
		if (-1 == munmap((void *)((unsigned long)fPtr->mmio & PAGE_MASK), fPtr->mmio_len))
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "munmap mmio: %s\n", strerror(errno));
		fPtr->mmio = NULL;
		/* FIXME: restore var.accel_flags [geert] */
	}
	return TRUE;
}

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

Bool
fbdevHWModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode)
{	
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);
	
	pScrn->vtSema = TRUE;

	/* set */
787
	if (!fbdevHWSetMode(pScrn, mode, FALSE))
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
788
		return FALSE;
789

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
790
791
792
793
794
795
796
797
798
799
800
	/* read back */
	if (0 != ioctl(fPtr->fd,FBIOGET_FSCREENINFO,(void*)(&fPtr->fix))) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "FBIOGET_FSCREENINFO: %s\n", strerror(errno));
		return FALSE;
	}
	if (0 != ioctl(fPtr->fd,FBIOGET_VSCREENINFO,(void*)(&fPtr->var))) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "FBIOGET_VSCREENINFO: %s\n", strerror(errno));
		return FALSE;
	}
801

802
803
804
805
806
807
808
809
810
811
812
813
	if (pScrn->defaultVisual == TrueColor ||
	    pScrn->defaultVisual == DirectColor) {
	    /* XXX: This is a hack, but it should be a NOP for all the setups that
	     * worked before and actually seems to fix some others...
	     */
	    pScrn->offset.red   = fPtr->var.red.offset;
	    pScrn->offset.green = fPtr->var.green.offset;
	    pScrn->offset.blue  = fPtr->var.blue.offset;
	    pScrn->mask.red     = ((1 << fPtr->var.red.length) - 1) << fPtr->var.red.offset;
	    pScrn->mask.green   = ((1 << fPtr->var.green.length) - 1) << fPtr->var.green.offset;
	    pScrn->mask.blue    = ((1 << fPtr->var.blue.length) - 1) << fPtr->var.blue.offset;
	}
814

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
	return TRUE;
}

/* -------------------------------------------------------------------- */
/* video mode save/restore                                              */

/* TODO: colormap */
void
fbdevHWSave(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);

	TRACE_ENTER("Save");
	if (0 != ioctl(fPtr->fd,FBIOGET_VSCREENINFO,(void*)(&fPtr->saved_var)))
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "FBIOGET_VSCREENINFO: %s\n", strerror(errno));
}

void
fbdevHWRestore(ScrnInfoPtr pScrn)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);

	TRACE_ENTER("Restore");
	if (0 != ioctl(fPtr->fd,FBIOPUT_VSCREENINFO,(void*)(&fPtr->saved_var)))
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "FBIOPUT_VSCREENINFO: %s\n", strerror(errno));
}

/* -------------------------------------------------------------------- */
/* callback for xf86HandleColormaps                                     */

void
fbdevHWLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
		 LOCO *colors, VisualPtr pVisual)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);
	struct fb_cmap cmap;
	unsigned short red,green,blue;
	int i;

856
	TRACE_ENTER("LoadPalette");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
	cmap.len   = 1;
	cmap.red   = &red;
	cmap.green = &green;
	cmap.blue  = &blue;
	cmap.transp = NULL;
	for (i = 0; i < numColors; i++) {
		cmap.start = indices[i];
		red   = (colors[indices[i]].red   << 8) |
			colors[indices[i]].red;
		green = (colors[indices[i]].green << 8) |
			colors[indices[i]].green;
		blue  = (colors[indices[i]].blue  << 8) |
			colors[indices[i]].blue;
		if (-1 == ioctl(fPtr->fd,FBIOPUTCMAP,(void*)&cmap))
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "FBIOPUTCMAP: %s\n", strerror(errno));
	}
}

/* -------------------------------------------------------------------- */
/* these can be hooked directly into ScrnInfoRec                        */

879
ModeStatus
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
880
881
882
883
884
fbdevHWValidMode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
{
	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];

	TRACE_ENTER("ValidMode");
885
886

	if (!fbdevHWSetMode(pScrn, mode, TRUE))
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
887
		return MODE_BAD;
888

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
889
890
891
892
893
894
895
896
897
	return MODE_OK;
}

Bool
fbdevHWSwitchMode(int scrnIndex, DisplayModePtr mode, int flags)
{
	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];

	TRACE_ENTER("SwitchMode");
898
899

	if (!fbdevHWSetMode(pScrn, mode, FALSE))
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
900
		return FALSE;
901

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
902
903
904
905
906
907
908
909
910
911
	return TRUE;
}

void
fbdevHWAdjustFrame(int scrnIndex, int x, int y, int flags)
{
	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);

	TRACE_ENTER("AdjustFrame");
912
913
914
915
	if ( x < 0 || x + fPtr->var.xres > fPtr->var.xres_virtual || 
	     y < 0 || y + fPtr->var.yres > fPtr->var.yres_virtual )
		return;

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
916
917
918
	fPtr->var.xoffset = x;
	fPtr->var.yoffset = y;
	if (-1 == ioctl(fPtr->fd,FBIOPAN_DISPLAY,(void*)&fPtr->var))
Lars Knoll's avatar
compile    
Lars Knoll committed
919
		xf86DrvMsgVerb(scrnIndex, X_WARNING, 5, 
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
			   "FBIOPAN_DISPLAY: %s\n", strerror(errno));
}

Bool
fbdevHWEnterVT(int scrnIndex, int flags)
{
	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
	
	TRACE_ENTER("EnterVT");
	if (!fbdevHWModeInit(pScrn, pScrn->currentMode))
		return FALSE;
	fbdevHWAdjustFrame(scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
	return TRUE;
}

void
fbdevHWLeaveVT(int scrnIndex, int flags)
{
	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];

	TRACE_ENTER("LeaveVT");
	fbdevHWRestore(pScrn);
}

void
fbdevHWDPMSSet(ScrnInfoPtr pScrn, int mode, int flags)
{
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);
	unsigned long fbmode;

950
	TRACE_ENTER("DPMSSet");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
	if (!pScrn->vtSema)
		return;

	switch (mode) {
		case DPMSModeOn:
			fbmode = 0;
			break;
		case DPMSModeStandby:
			fbmode = 2;
			break;
		case DPMSModeSuspend:
			fbmode = 3;
			break;
		case DPMSModeOff:
			fbmode = 4;
			break;
		default:
			return;
	}

	if (-1 == ioctl(fPtr->fd, FBIOBLANK, (void *)fbmode))
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "FBIOBLANK: %s\n", strerror(errno));
}

Bool
fbdevHWSaveScreen(ScreenPtr pScreen, int mode)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	fbdevHWPtr fPtr = FBDEVHWPTR(pScrn);
	unsigned long unblank;

983
	TRACE_ENTER("HWSaveScreen");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
984
985
986
987
988
989
990
991
992
993
994
995
996
	if (!pScrn->vtSema)
		return TRUE;

	unblank = xf86IsUnblank(mode);

	if (-1 == ioctl(fPtr->fd, FBIOBLANK, (void *)(1-unblank))) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "FBIOBLANK: %s\n", strerror(errno));
		return FALSE;
	}

	return TRUE;
}
997

998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
xf86SwitchModeProc *
fbdevHWSwitchModeWeak(void) { return fbdevHWSwitchMode; }

xf86AdjustFrameProc *
fbdevHWAdjustFrameWeak(void) { return fbdevHWAdjustFrame; }

xf86EnterVTProc *
fbdevHWEnterVTWeak(void) { return fbdevHWEnterVT; }

xf86LeaveVTProc *
fbdevHWLeaveVTWeak(void) { return fbdevHWLeaveVT; }

xf86ValidModeProc *
fbdevHWValidModeWeak(void) { return fbdevHWValidMode; }

xf86DPMSSetProc *
fbdevHWDPMSSetWeak(void) { return fbdevHWDPMSSet; }

xf86LoadPaletteProc *
fbdevHWLoadPaletteWeak(void) { return fbdevHWLoadPalette; }

SaveScreenProcPtr
fbdevHWSaveScreenWeak(void) { return fbdevHWSaveScreen; }