summaryrefslogtreecommitdiff
path: root/src/glide_driver.c
blob: a37589f38ffb73d116911aef065fb15c7f8cfef1 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
238
239
240
241
242
243
244
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
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
547
548
549
550
551
552
553
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
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
669
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
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
950
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
983
984
985
986
987
988
989
/*
 * XFree86 driver for Glide(tm). (Mainly for Voodoo 1 and 2 cards)
 *
 * Since Voodoo 1 and Voodoo 2 cards are very, very NOT made for running a
 * 2D windowing system, this driver is a little special. Basically, we have
 * a virtual framebuffer in RAM (the Shadow Framebuffer) and we copy selected
 * regions of this to the voodoo card at appropriate times. We get no hardware
 * acceleration help (there isn't any for 2D on these cards), but since the
 * framebuffer is in cached RAM, we get a useable display anyway. Also, we
 * don't have any interaction with any hardware since Glide is the layer
 * beneath the driver.
 *
 * Author:
 *   Henrik Harmsen (hch@cd.chalmers.se or Henrik.Harmsen@erv.ericsson.se)
 *
 * HISTORY
 * 1999-04-05
 * - First release for 3.9Pi
 *
 * 1999-04-17
 * - Soft link to libglide2x.so instead of addition to ModulePath
 * - Changed "EXTERN_MODULE" to EXTERN_MODULE
 * - Uses the "GlideDevice" option instead of the "BusID" line to select
 *   which voodoo board to use.
 * - Manpage updates
 *
 * 1999-06-25
 * - Modify glideSetup to not register the driver when libglide2x.so
 *   cannot be loaded, and to return appropriate error codes when it fails.
 * - Prevent GLIDEFreeScreen() from crashing if called early.
 *
 * 1999-08-22
 * - Minor fixes.
 *
 * 1999-11-22
 * - Minor change in GLIDE_FIND_FUNC by Loïc Grenié, grenie@math.jussieu.fr.
 *
 * TODO
 * - Support for adjusting gamma correction.
 * - Support for setting gamma individually for R,G,B when Glide 3 arrives
 *   for Linux.  This will allow me to get rid of that sick green tint my
 *   voodoo2 board produces...
 * - Support static loading.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "colormapst.h"
#include "xf86.h"
#include "xf86_OSproc.h"
#include "mipointer.h"
#include "micmap.h"
#include "globals.h"
#ifdef HAVE_XEXTPROTO_71
#include <X11/extensions/dpmsconst.h>
#else
#define DPMS_SERVER
#include <X11/extensions/dpms.h>
#endif

#include "fb.h"
#include "xf86cmap.h"
#include "shadowfb.h"

#include "compat-api.h"

#include <glide.h>

/* glide3x does not define this alias anymore, so let's do it ourselves. */
#ifndef GR_ZDEPTHVALUE_FARTHEST
#define GR_ZDEPTHVALUE_FARTHEST 0
#endif

#define TRUE 1
#define FALSE 0

typedef signed char s8;
typedef unsigned char u8;
typedef signed short int s16;
typedef unsigned short int u16;
typedef signed long int s32;
typedef unsigned long int u32;

/* Card-specific driver information */

#define GLIDEPTR(p) ((GLIDEPtr)((p)->driverPrivate))

typedef struct {
    u8 *ShadowPtr;
    u32 ShadowPitch;
    u32 SST_Index;
    CloseScreenProcPtr CloseScreen;
    Bool Blanked;
    u32 grRefreshRate;
    u32 grResolution;
    Bool OnAtExit;
    Bool GlideInitiated;
    EntityInfoPtr pEnt;
    OptionInfoPtr Options;
} GLIDERec, *GLIDEPtr;

static const OptionInfoRec *GLIDEAvailableOptions(int chipid, int busid);
static void GLIDEIdentify(int flags);
static Bool GLIDEProbe(DriverPtr drv, int flags);
static Bool GLIDEPreInit(ScrnInfoPtr pScrn, int flags);
static Bool GLIDEScreenInit(SCREEN_INIT_ARGS_DECL);
static Bool GLIDEEnterVT(VT_FUNC_ARGS_DECL);
static void GLIDELeaveVT(VT_FUNC_ARGS_DECL);
static Bool GLIDECloseScreen(CLOSE_SCREEN_ARGS_DECL);
static Bool GLIDESaveScreen(ScreenPtr pScreen, int mode);
static void GLIDEFreeScreen(FREE_SCREEN_ARGS_DECL);
static void GLIDERefreshArea(ScrnInfoPtr pScrn, int num, BoxPtr pbox);
static Bool GLIDEModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode);
static void GLIDERestore(ScrnInfoPtr pScrn, Bool Closing);
static void GLIDERefreshAll(ScrnInfoPtr pScrn);

static void GLIDEDisplayPowerManagementSet(ScrnInfoPtr pScrn,
                                           int PowerManagementMode,
                                           int flags);

#define GLIDE_VERSION 4000
#define GLIDE_NAME "GLIDE"
#define GLIDE_DRIVER_NAME "glide"
#define GLIDE_MAJOR_VERSION PACKAGE_VERSION_MAJOR
#define GLIDE_MINOR_VERSION PACKAGE_VERSION_MINOR
#define GLIDE_PATCHLEVEL PACKAGE_VERSION_PATCHLEVEL

/*
 * This contains the functions needed by the server after loading the
 * driver module.  It must be supplied, and gets added the driver list by
 * the Module Setup funtion in the dynamic case.  In the static case a
 * reference to this is compiled in, and this requires that the name of
 * this DriverRec be an upper-case version of the driver name.
 */
_X_EXPORT DriverRec GLIDE = {
    GLIDE_VERSION,
    GLIDE_DRIVER_NAME,
    GLIDEIdentify,
    GLIDEProbe,
    GLIDEAvailableOptions,
    NULL,
    0
};

typedef enum {
    OPTION_ON_AT_EXIT,
    OPTION_GLIDEDEVICE
} GLIDEOpts;

static const OptionInfoRec GLIDEOptions[] = {
    { OPTION_ON_AT_EXIT, "OnAtExit", OPTV_BOOLEAN, { 0 }, FALSE },
    { OPTION_GLIDEDEVICE, "GlideDevice", OPTV_INTEGER, { 0 }, FALSE },
    { -1, NULL, OPTV_NONE, { 0 }, FALSE }
};

/* Supported chipsets */
static SymTabRec GLIDEChipsets[] = {
    { 0, "Voodoo" },
    { -1, NULL }
};

#ifdef XFree86LOADER

static MODULESETUPPROTO(glideSetup);

static XF86ModuleVersionInfo glideVersRec = {
    "glide",
    MODULEVENDORSTRING,
    MODINFOSTRING1,
    MODINFOSTRING2,
    XORG_VERSION_CURRENT,
    GLIDE_MAJOR_VERSION, GLIDE_MINOR_VERSION, GLIDE_PATCHLEVEL,
    ABI_CLASS_VIDEODRV,         /* This is a video driver */
    ABI_VIDEODRV_VERSION,
    MOD_CLASS_VIDEODRV,
    { 0, 0, 0, 0 }
};

_X_EXPORT XF86ModuleData glideModuleData = {
    &glideVersRec,
    glideSetup,
    NULL
};

static pointer
glideSetup(pointer module, pointer opts, int *errmaj, int *errmin)
{
    static Bool setupDone = FALSE;
    int errmaj2 = 0, errmin2 = 0;

    if (!setupDone) {
        setupDone = TRUE;
        /* This module should be loaded only once */
        *errmaj = LDR_ONCEONLY;
        xf86AddDriver(&GLIDE, module, 0);

        /*
         * The return value must be non-NULL on success even though there
         * is no TearDownProc.
         */
        return (pointer)1;
    }
    else {
        if (errmaj)
            *errmaj = LDR_ONCEONLY;
        return NULL;
    }
}
#endif                          /* XFree86LOADER */

static Bool
GLIDEGetRec(ScrnInfoPtr pScrn)
{
    /*
     * Allocate an GLIDERec, and hook it into pScrn->driverPrivate.
     * pScrn->driverPrivate is initialised to NULL, so we can check if
     * the allocation has already been done.
     */
    if (pScrn->driverPrivate != NULL)
        return TRUE;

    pScrn->driverPrivate = xnfcalloc(sizeof(GLIDERec), 1);

    /* Initialize it */
    /* No init here yet */

    return TRUE;
}

static void
GLIDEFreeRec(ScrnInfoPtr pScrn)
{
    if (pScrn->driverPrivate == NULL)
        return;
    free(pScrn->driverPrivate);
    pScrn->driverPrivate = NULL;
}

static const OptionInfoRec *
GLIDEAvailableOptions(int chipid, int busid)
{
    return GLIDEOptions;
}

/* Mandatory */
static void
GLIDEIdentify(int flags)
{
    xf86PrintChipsets(GLIDE_NAME, "driver for Glide devices (Voodoo cards)",
                      GLIDEChipsets);
}

#if defined(GLIDE3)
static int
glide_get_num_boards(void)
{
    FxI32 num_sst;
    int ret;

    ret = grGet(GR_NUM_BOARDS, sizeof(num_sst), &num_sst);
    if (!ret) {
        xf86Msg(X_ERROR,
                "GLIDEProbe(): Error calling grGet(GR_NUM_BOARDS)!\n");
        return -1;
    }

    return num_sst;
}
#else
static int
glide_get_num_boards(void)
{
    GrHwConfiguration hw;
    int ret;

    ret = grSstQueryBoards(&hw);
    if (!ret) {
        xf86Msg(X_ERROR, "GLIDEProbe(): Error calling grSstQueryBoards!\n");
        return -1;
    }

    return hw.num_sst;
}
#endif

/* Mandatory */
static Bool
GLIDEProbe(DriverPtr drv, int flags)
{
    int i, num_sst, sst;
    GDevPtr *devList;
    GDevPtr dev = NULL;
    int numdevList;
    Bool foundScreen = FALSE;
    ScrnInfoPtr pScrn;
    int GlideDevice;

    numdevList = xf86MatchDevice(GLIDE_DRIVER_NAME, &devList);
    if (numdevList <= 0)
        return FALSE;

    num_sst = glide_get_num_boards();
    if (num_sst < 0)
        goto cleanup;

    /* num_sst: number of Glide boards available */
    if (num_sst > 0 && (flags & PROBE_DETECT)) {
        /* XXX Need to call xf886AddDeviceToConfigure() here */
        return TRUE;
    }

    for (sst = 0; sst < num_sst; sst++) {
        for (i = 0; i < numdevList; i++) {
            dev = devList[i];
            GlideDevice = xf86SetIntOption(dev->options, "GlideDevice", 0);
            if (GlideDevice == sst) {
                int entityIndex;

                /* Match */
                entityIndex = xf86ClaimNoSlot(drv, 0, dev, TRUE);
                pScrn = NULL;

                /* Allocate a ScrnInfoRec and claim the slot */
                pScrn = xf86AllocateScreen(drv, 0);
                if (pScrn) {
                    GLIDEPtr pGlide;

                    xf86AddEntityToScreen(pScrn, entityIndex);

                    /* I'm not going to "claim" the glide device since no
                     * other driver than this can drive it */
                    /* (A glide device is not a PCI device) */
                    /* XXX Need to see how this fits in with the new RAC */

                    /* Fill in what we can of the ScrnInfoRec */
                    pScrn->driverVersion = GLIDE_VERSION;
                    pScrn->driverName = GLIDE_DRIVER_NAME;
                    pScrn->name = GLIDE_NAME;
                    pScrn->Probe = GLIDEProbe;
                    pScrn->PreInit = GLIDEPreInit;
                    pScrn->ScreenInit = GLIDEScreenInit;
                    pScrn->EnterVT = GLIDEEnterVT;
                    pScrn->LeaveVT = GLIDELeaveVT;
                    pScrn->FreeScreen = GLIDEFreeScreen;

                    /* Allocate the GLIDERec driverPrivate */
                    if (!GLIDEGetRec(pScrn))
                        break;

                    pGlide = GLIDEPTR(pScrn);
                    pGlide->SST_Index = sst;

                    /*
                     * XXX This is a hack because don't have the PCI info.
                     * Set it as an ISA entity with no resources.
                     */
                    foundScreen = TRUE;
                }
                break;
            }
        }
    }

cleanup:
    free(devList);
    return foundScreen;
}

/* Mandatory */
static Bool
GLIDEPreInit(ScrnInfoPtr pScrn, int flags)
{
    GLIDEPtr pGlide;
    MessageType from;
    int ret;
    ClockRangePtr clockRanges;

    if (flags & PROBE_DETECT)
        return FALSE;

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

    /* Set pScrn->monitor */
    pScrn->monitor = pScrn->confScreen->monitor;

    if (!xf86SetDepthBpp(pScrn, 16, 0, 0, Support32bppFb))
        return FALSE;

    /* Check that the returned depth is one we support */
    switch (pScrn->depth) {
    case 16:
    case 24:
        /* OK */
        break;
    default:
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
                   "Given depth (%d) is not supported by this driver\n",
                   pScrn->depth);
        return FALSE;
    }
    xf86PrintDepthBpp(pScrn);

    /*
     * This must happen after pScrn->display has been set because
     * xf86SetWeight references it.
     */
    if (pScrn->depth > 8) {
        /* The defaults are OK for us */
        rgb zeros = { 0, 0, 0 };

        if (!xf86SetWeight(pScrn, zeros, zeros))
            return FALSE;

        /* XXX check that weight returned is supported */
    }

    /* Set the default visual. */
    if (!xf86SetDefaultVisual(pScrn, -1))
        return FALSE;

    /* We don't support DirectColor at > 8bpp */
    if (pScrn->depth > 8 && pScrn->defaultVisual != TrueColor) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Given default visual"
                   " (%s) is not supported at depth %d\n",
                   xf86GetVisualName(pScrn->defaultVisual), pScrn->depth);
        return FALSE;
    }

    /* Set default gamma */
    {
        Gamma zeros = { 0.0, 0.0, 0.0 };

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

    /* We use a programmable clock */
    pScrn->progClock = TRUE;

    pGlide = GLIDEPTR(pScrn);

    /* Get the entity */
    pGlide->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);

    /* Collect all of the relevant option flags (fill in pScrn->options) */
    xf86CollectOptions(pScrn, NULL);

    /* Process the options */
    pGlide->Options = malloc(sizeof(GLIDEOptions));
    if (pGlide->Options == NULL)
        return FALSE;
    memcpy(pGlide->Options, GLIDEOptions, sizeof(GLIDEOptions));
    xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, pGlide->Options);

    pGlide->OnAtExit = FALSE;
    from = X_DEFAULT;
    if (xf86GetOptValBool(pGlide->Options, OPTION_ON_AT_EXIT,
                          &(pGlide->OnAtExit)))
        from = X_CONFIG;

    xf86DrvMsg(pScrn->scrnIndex, from,
               "Voodoo card will be %s when exiting server.\n",
               pGlide->OnAtExit ? "ON" : "OFF");

    /*
     * If the user has specified the amount of memory in the XF86Config
     * file, we respect that setting.
     */
    if (pGlide->pEnt->device->videoRam != 0) {
        pScrn->videoRam = pGlide->pEnt->device->videoRam;
        from = X_CONFIG;
    }
    else {
        /* It's just virtual framebuffer anyway so let's say we have an
         * 8MB sized framebuffer. */
        pScrn->videoRam = 8192;
        from = X_PROBED;
    }
#if 0
    xf86DrvMsg(pScrn->scrnIndex, from, "Virtual video RAM: %d kB\n",
               pScrn->videoRam);
#endif

    /* Set up clock ranges so that the xf86ValidateModes() function will not
     * fail a mode because of the clock requirement (because we don't use the
     * clock value anyway) */
    clockRanges = xnfcalloc(sizeof(ClockRange), 1);
    clockRanges->next = NULL;
    clockRanges->minClock = 10000;
    clockRanges->maxClock = 300000;
    clockRanges->clockIndex = -1;       /* programmable */
    clockRanges->interlaceAllowed = TRUE;
    clockRanges->doubleScanAllowed = TRUE;

    /* Select valid modes from those available */
    ret = xf86ValidateModes(pScrn, pScrn->monitor->Modes,
                            pScrn->display->modes, clockRanges,
                            NULL, 256, 2048,
                            pScrn->bitsPerPixel, 128, 2048,
                            pScrn->display->virtualX,
                            pScrn->display->virtualY,
                            pScrn->videoRam * 1024, LOOKUP_BEST_REFRESH);
    if (ret == -1) {
        GLIDEFreeRec(pScrn);
        return FALSE;
    }

    /* Prune the modes marked as invalid */
    xf86PruneDriverModes(pScrn);

    /* If no valid modes, return */
    if (ret == 0 || pScrn->modes == NULL) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No valid modes found\n");
        GLIDEFreeRec(pScrn);
        return FALSE;
    }

    /* Set the current mode to the first in the list */
    pScrn->currentMode = pScrn->modes;

    /* Do some checking, we will not support a virtual framebuffer larger than
     * the visible screen. */
    if (pScrn->currentMode->HDisplay != pScrn->virtualX ||
        pScrn->currentMode->VDisplay != pScrn->virtualY ||
        pScrn->displayWidth != pScrn->virtualX) {
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
                   "Virtual size doesn't equal display size. Forcing virtual "
                   "size to equal display size.\n");
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
                   "(Virtual size: %dx%d, Display size: %dx%d)\n",
                   pScrn->virtualX, pScrn->virtualY,
                   pScrn->currentMode->HDisplay,
                   pScrn->currentMode->VDisplay);
        /* I'm not entirely sure this is "legal" but I hope so. */
        pScrn->virtualX = pScrn->currentMode->HDisplay;
        pScrn->virtualY = pScrn->currentMode->VDisplay;
        pScrn->displayWidth = pScrn->virtualX;
    }

    /* TODO: Note: If I return FALSE right here, the server will not restore
     * the console correctly, forcing a reboot. Must find that. (valid for
     * 3.9Pi) */

    /* Print the list of modes being used */
    xf86PrintModes(pScrn);

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

    /* Load fb */
    if (xf86LoadSubModule(pScrn, "fb") == NULL) {
        GLIDEFreeRec(pScrn);
        return FALSE;
    }

    /* Load the shadow framebuffer */
    if (!xf86LoadSubModule(pScrn, "shadowfb")) {
        GLIDEFreeRec(pScrn);
        return FALSE;
    }

    return TRUE;
}

/*
 * This gets called at the start of each server generation
 */
/* Mandatory */
static Bool
GLIDEScreenInit(SCREEN_INIT_ARGS_DECL)
{
    ScrnInfoPtr pScrn;
    GLIDEPtr pGlide;
    int ret;
    VisualPtr visual;

    /*
     * First get the ScrnInfoRec
     */
    pScrn = xf86ScreenToScrn(pScreen);

    pGlide = GLIDEPTR(pScrn);

    if (!GLIDEModeInit(pScrn, pScrn->currentMode))
        return FALSE;

    /*
     * The next step is to setup the screen's visuals, and initialise the
     * framebuffer code.  In cases where the framebuffer's default
     * choices for things like visual layouts and bits per RGB are OK,
     * this may be as simple as calling the framebuffer's ScreenInit()
     * function.  If not, the visuals will need to be setup before calling
     * a fb ScreenInit() function and fixed up after.
     *
     * For most PC hardware at depths >= 8, the defaults that fb uses
     * are not appropriate.  In this driver, we fixup the visuals after.
     */

    /*
     * Reset the visual list.
     */
    miClearVisualTypes();

    /* Setup the visuals we support. Only TrueColor. */
    ret = miSetVisualTypes(pScrn->depth,
                           miGetDefaultVisualMask(pScrn->depth),
                           pScrn->rgbBits, pScrn->defaultVisual);
    if (!ret)
        return FALSE;

    miSetPixmapDepths();

    pGlide->ShadowPitch =
            ((pScrn->virtualX * pScrn->bitsPerPixel >> 3) + 3) & ~3L;
    pGlide->ShadowPtr = xnfalloc(pGlide->ShadowPitch * pScrn->virtualY);

    /*
     * Call the framebuffer layer's ScreenInit function, and fill in other
     * pScreen fields.
     */
    ret = fbScreenInit(pScreen, pGlide->ShadowPtr,
                       pScrn->virtualX, pScrn->virtualY,
                       pScrn->xDpi, pScrn->yDpi,
                       pScrn->displayWidth,
                       pScrn->bitsPerPixel);
    if (!ret)
        return FALSE;

    /* 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 */
    fbPictureInit(pScreen, 0, 0);

    xf86SetBlackWhitePixels(pScreen);
    xf86SetBackingStore(pScreen);

    /* Initialize software cursor. Must precede creation of the default
     * colormap */
    miDCInitialize(pScreen, xf86GetPointerScreenFuncs());

    /* Initialise default colourmap */
    if (!miCreateDefColormap(pScreen))
        return FALSE;

    ShadowFBInit(pScreen, GLIDERefreshArea);

    xf86DPMSInit(pScreen, GLIDEDisplayPowerManagementSet, 0);

    pScreen->SaveScreen = GLIDESaveScreen;

    /* Wrap the current CloseScreen function */
    pGlide->CloseScreen = pScreen->CloseScreen;
    pScreen->CloseScreen = GLIDECloseScreen;

    /* Report any unused options (only for the first generation) */
    if (serverGeneration == 1)
        xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);

    /* Done */
    return TRUE;
}

/*
 * This is called when VT switching back to the X server.  Its job is
 * to reinitialise the video mode.
 *
 * We may wish to unmap video/MMIO memory too.
 */
/* Mandatory */
static Bool
GLIDEEnterVT(VT_FUNC_ARGS_DECL)
{
    SCRN_INFO_PTR(arg);

    return GLIDEModeInit(pScrn, pScrn->currentMode);
}

/*
 * This is called when VT switching away from the X server.  Its job is
 * to restore the previous (text) mode.
 *
 * We may wish to remap video/MMIO memory too.
 */
/* Mandatory */
static void
GLIDELeaveVT(VT_FUNC_ARGS_DECL)
{
    SCRN_INFO_PTR(arg);

    GLIDERestore(pScrn, FALSE);
}

/*
 * This is called at the end of each server generation.  It restores the
 * original (text) mode.  It should also unmap the video memory, and free
 * any per-generation data allocated by the driver.  It should finish
 * by unwrapping and calling the saved CloseScreen function.
 */
/* Mandatory */
static Bool
GLIDECloseScreen(CLOSE_SCREEN_ARGS_DECL)
{
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
    GLIDEPtr pGlide = GLIDEPTR(pScrn);

    if (pScrn->vtSema)
        GLIDERestore(pScrn, TRUE);
    free(pGlide->ShadowPtr);

    pScrn->vtSema = FALSE;

    pScreen->CloseScreen = pGlide->CloseScreen;

    return (*pScreen->CloseScreen)(CLOSE_SCREEN_ARGS);
}

/*
 * Free up any persistent data structures
 */
/* Optional */
static void
GLIDEFreeScreen(FREE_SCREEN_ARGS_DECL)
{
    SCRN_INFO_PTR(arg);
    GLIDEPtr pGlide = GLIDEPTR(pScrn);

    /*
     * This only gets called when a screen is being deleted.  It does not
     * get called routinely at the end of a server generation.
     */
    if (pGlide && pGlide->ShadowPtr)
        free(pGlide->ShadowPtr);
    GLIDEFreeRec(pScrn);
}

/*
 * Do screen blanking
 */
/* Mandatory */
static Bool
GLIDESaveScreen(ScreenPtr pScreen, int mode)
{
    ScrnInfoPtr pScrn;
    GLIDEPtr pGlide;
    Bool unblank;

    unblank = xf86IsUnblank(mode);
    pScrn = xf86ScreenToScrn(pScreen);
    pGlide = GLIDEPTR(pScrn);
    pGlide->Blanked = !unblank;
    if (unblank)
        GLIDERefreshAll(pScrn);
    else
        grBufferClear(0, 0, GR_ZDEPTHVALUE_FARTHEST);

    return TRUE;
}

static Bool
GLIDEModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
    GLIDEPtr pGlide;
    int ret;
    int width, height;
    double refresh;
    Bool match = FALSE;

    pGlide = GLIDEPTR(pScrn);

    if (mode->Flags & V_INTERLACE) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
                   "Interlaced modes not supported\n");
        return FALSE;
    }

    width = mode->HDisplay;
    height = mode->VDisplay;

#if 0
    ErrorF("mode->HDisplay = %d, pScrn->displayWidth = %d\n",
           mode->HDisplay, pScrn->displayWidth);
    ErrorF("mode->VDisplay = %d, mode->HTotal = %d, mode->VTotal = %d\n",
           mode->VDisplay, mode->HTotal, mode->VTotal);
    ErrorF("mode->Clock = %d\n", mode->Clock);
#endif

    if (width == 640 && height == 480) {
        match = TRUE;
        pGlide->grResolution = GR_RESOLUTION_640x480;
    }
    if (width == 800 && height == 600) {
        match = TRUE;
        pGlide->grResolution = GR_RESOLUTION_800x600;
    }
    if (width == 960 && height == 720) {
        match = TRUE;
        pGlide->grResolution = GR_RESOLUTION_960x720;
    }
    if (width == 1024 && height == 768) {
        match = TRUE;
        pGlide->grResolution = GR_RESOLUTION_1024x768;
    }
    if (width == 1280 && height == 1024) {
        match = TRUE;
        pGlide->grResolution = GR_RESOLUTION_1280x1024;
    }
    if (width == 1600 && height == 1200) {
        match = TRUE;
        pGlide->grResolution = GR_RESOLUTION_1600x1200;
    }

    if (!match) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
                   "Selected width = %d and height = %d is not supported by "
                   "glide\n", width, height);
        return FALSE;
    }

    refresh = (mode->Clock * 1.0e3) / ((double)(mode->HTotal) *
                                       (double)(mode->VTotal));
#if 0
    ErrorF("Calculated refresh rate for mode is %.2fHz\n", refresh);
#endif

    /* The Glide header files indicate there are a rather large number of
     * refresh rates available. In practice, though, only 60, 75 and 85Hz seem
     * to be available. If we try using another refresh rate, glide will
     * default to 60Hz. */
    pGlide->grRefreshRate = GR_REFRESH_60Hz;
    if (refresh > 74.0)
        pGlide->grRefreshRate = GR_REFRESH_75Hz;
    if (refresh > 84.0)
        pGlide->grRefreshRate = GR_REFRESH_85Hz;

    /* Initialize the video card */
    grGlideInit();
    grSstSelect(pGlide->SST_Index);

    ret = grSstWinOpen(0, pGlide->grResolution, pGlide->grRefreshRate,
                       GR_COLORFORMAT_ARGB, GR_ORIGIN_UPPER_LEFT, 2, 0);
    if (!ret) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "grSstWinOpen returned %d. "
                   "You are probably trying to use a resolution that is not "
                   "supported by your hardware.", ret);
        return FALSE;
    }

    grRenderBuffer(GR_BUFFER_FRONTBUFFER);
    grClipWindow(0, 0, 1024, 768);
    grBufferClear(0, 0, GR_ZDEPTHVALUE_FARTHEST);

    pGlide->Blanked = FALSE;
    pGlide->GlideInitiated = TRUE;

    return TRUE;
}

static void
GLIDERestore(ScrnInfoPtr pScrn, Bool Closing)
{
    GLIDEPtr pGlide;

    pGlide = GLIDEPTR(pScrn);

    if (!(pGlide->GlideInitiated))
        return;

    pGlide->GlideInitiated = FALSE;
    pGlide->Blanked = TRUE;
    grBufferClear(0, 0, GR_ZDEPTHVALUE_FARTHEST);
    if (!Closing || !(pGlide->OnAtExit))
        grGlideShutdown();
}

static void
GLIDERefreshArea(ScrnInfoPtr pScrn, int num, BoxPtr pbox)
{
    GLIDEPtr pGlide = GLIDEPTR(pScrn);
    int Bpp;
    unsigned char *src;
    s32 x1, x2;

    if (pGlide->Blanked)
        return;

    Bpp = pScrn->bitsPerPixel >> 3;
    if (pScrn->bitsPerPixel == 16) {
        while (num--) {
            /* We align to an even number of pixels so we won't have to copy
             * half-words over the PCI bus */
            x1 = (pbox->x1) & ~1;
            x2 = (pbox->x2 + 1) & ~1;
            src = pGlide->ShadowPtr + (pbox->y1 * pGlide->ShadowPitch) +
                    (x1 * Bpp);
#if defined(GLIDE3) && defined(GLIDE3_ALPHA)
            grLfbWriteRegion(GR_BUFFER_FRONTBUFFER, x1, pbox->y1,
                             GR_LFB_SRC_FMT_565, x2 - x1, pbox->y2 - pbox->y1,
                             FALSE, pGlide->ShadowPitch, src);
#else
            grLfbWriteRegion(GR_BUFFER_FRONTBUFFER, x1, pbox->y1,
                             GR_LFB_SRC_FMT_565, x2 - x1, pbox->y2 - pbox->y1,
                             pGlide->ShadowPitch, src);
#endif
            pbox++;
        }
    }
    else {
        while (num--) {
            x1 = pbox->x1;
            x2 = pbox->x2;
            src = pGlide->ShadowPtr + (pbox->y1 * pGlide->ShadowPitch) +
                    (pbox->x1 * Bpp);
#if defined(GLIDE3) && defined(GLIDE3_ALPHA)
            grLfbWriteRegion(GR_BUFFER_FRONTBUFFER, x1, pbox->y1,
                             GR_LFB_SRC_FMT_888, x2 - x1, pbox->y2 - pbox->y1,
                             FALSE, pGlide->ShadowPitch, src);
#else
            grLfbWriteRegion(GR_BUFFER_FRONTBUFFER, x1, pbox->y1,
                             GR_LFB_SRC_FMT_888, x2 - x1, pbox->y2 - pbox->y1,
                             pGlide->ShadowPitch, src);
#endif
            pbox++;
        }
    }
}

/*
 * GLIDEDisplayPowerManagementSet --
 *
 * Sets VESA Display Power Management Signaling (DPMS) Mode.
 */
static void
GLIDEDisplayPowerManagementSet(ScrnInfoPtr pScrn, int PowerManagementMode,
                               int flags)
{
    GLIDEPtr pGlide = GLIDEPTR(pScrn);
    static int oldmode = -1;

#if 0
    ErrorF("GLIDEDisplayPowerManagementSet: %d\n", PowerManagementMode);
#endif

    if (oldmode == DPMSModeOff && PowerManagementMode != DPMSModeOff)
        GLIDEModeInit(pScrn, pScrn->currentMode);

    switch (PowerManagementMode) {
    case DPMSModeOn:
        /* Screen: On; HSync: On, VSync: On */
        pGlide->Blanked = FALSE;
        GLIDERefreshAll(pScrn);
        break;
    case DPMSModeStandby:
    case DPMSModeSuspend:
        pGlide->Blanked = TRUE;
        grBufferClear(0, 0, GR_ZDEPTHVALUE_FARTHEST);
        break;
    case DPMSModeOff:
        GLIDERestore(pScrn, FALSE);
        break;
    }
    oldmode = PowerManagementMode;
}

static void
GLIDERefreshAll(ScrnInfoPtr pScrn)
{
    BoxRec box;

    box.x1 = 0;
    box.x2 = pScrn->virtualX;
    box.y1 = 0;
    box.y2 = pScrn->virtualY;
    GLIDERefreshArea(pScrn, 1, &box);
}