xf86sbusBus.c 22.6 KB
Newer Older
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * SBUS bus-specific code.
 *
 * Copyright (C) 2000 Jakub Jelinek (jakub@redhat.com)
 *
 * 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
 * furnished 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,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * JAKUB JELINEK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

24
25
26
27
#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
28
29
30
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>
31
#include <X11/X.h>
32
#include "os.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
33
34
35
36
37
38
39
40
41
42
#include "xf86.h"
#include "xf86Priv.h"
#include "xf86_OSlib.h"
#include "xf86cmap.h"

#include "xf86Bus.h"

#include "xf86sbusBus.h"
#include "xf86Sbus.h"

43
Bool sbusSlotClaimed = FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
44
45
46
47
48
49
50
51
52
53
54
55

static int xf86nSbusInfo;

static void
CheckSbusDevice(const char *device, int fbNum)
{
    int fd, i;
    struct fbgattr fbattr;
    sbusDevicePtr psdp;

    fd = open(device, O_RDONLY, 0);
    if (fd < 0)
56
        return;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
57
58
    memset(&fbattr, 0, sizeof(fbattr));
    if (ioctl(fd, FBIOGATTR, &fbattr) < 0) {
59
60
61
62
        if (ioctl(fd, FBIOGTYPE, &fbattr.fbtype) < 0) {
            close(fd);
            return;
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
63
64
65
    }
    close(fd);
    for (i = 0; sbusDeviceTable[i].devId; i++)
66
67
68
69
70
71
        if (sbusDeviceTable[i].fbType == fbattr.fbtype.fb_type)
            break;
    if (!sbusDeviceTable[i].devId)
        return;
    xf86SbusInfo =
        xnfrealloc(xf86SbusInfo, sizeof(psdp) * (++xf86nSbusInfo + 1));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
72
    xf86SbusInfo[xf86nSbusInfo] = NULL;
73
    xf86SbusInfo[xf86nSbusInfo - 1] = psdp = xnfcalloc(sizeof(sbusDevice), 1);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
74
75
76
77
78
79
80
81
    psdp->devId = sbusDeviceTable[i].devId;
    psdp->fbNum = fbNum;
    psdp->device = xnfstrdup(device);
    psdp->width = fbattr.fbtype.fb_width;
    psdp->height = fbattr.fbtype.fb_height;
    psdp->fd = -1;
}

82
void
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
83
84
85
86
87
88
xf86SbusProbe(void)
{
    int i, useProm = 0;
    char fbDevName[32];
    sbusDevicePtr psdp, *psdpp;

89
    xf86SbusInfo = malloc(sizeof(psdp));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
90
91
    *xf86SbusInfo = NULL;
    for (i = 0; i < 32; i++) {
92
93
        snprintf(fbDevName, sizeof(fbDevName), "/dev/fb%d", i);
        CheckSbusDevice(fbDevName, i);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
94
95
    }
    if (sparcPromInit() >= 0) {
96
97
        useProm = 1;
        sparcPromAssignNodes();
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
98
99
    }
    for (psdpp = xf86SbusInfo; (psdp = *psdpp); psdpp++) {
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
        for (i = 0; sbusDeviceTable[i].devId; i++)
            if (sbusDeviceTable[i].devId == psdp->devId)
                psdp->descr = sbusDeviceTable[i].descr;
        /*
         * If we can use PROM information and found the PROM node for this
         * device, we can tell more about the card.
         */
        if (useProm && psdp->node.node) {
            char *prop, *promPath;
            int len, chiprev, vmsize;

            switch (psdp->devId) {
            case SBUS_DEVICE_MGX:
                prop = sparcPromGetProperty(&psdp->node, "fb_size", &len);
                if (prop && len == 4 && *(int *) prop == 0x400000)
                    psdp->descr = "Quantum 3D MGXplus with 4M VRAM";
                break;
            case SBUS_DEVICE_CG6:
                chiprev = 0;
                vmsize = 0;
                prop = sparcPromGetProperty(&psdp->node, "chiprev", &len);
                if (prop && len == 4)
                    chiprev = *(int *) prop;
                prop = sparcPromGetProperty(&psdp->node, "vmsize", &len);
                if (prop && len == 4)
                    vmsize = *(int *) prop;
                switch (chiprev) {
                case 1:
                case 2:
                case 3:
                case 4:
                    psdp->descr = "Sun Double width GX";
                    break;
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    psdp->descr = "Sun Single width GX";
                    break;
                case 11:
                    switch (vmsize) {
                    case 2:
                        psdp->descr = "Sun Turbo GX with 1M VSIMM";
                        break;
                    case 4:
                        psdp->descr = "Sun Turbo GX Plus";
                        break;
                    default:
                        psdp->descr = "Sun Turbo GX";
                        break;
                    }
                }
                break;
            case SBUS_DEVICE_CG14:
                prop = sparcPromGetProperty(&psdp->node, "reg", &len);
                vmsize = 0;
                if (prop && !(len % 12) && len > 0)
                    vmsize = *(int *) (prop + len - 4);
                switch (vmsize) {
                case 0x400000:
                    psdp->descr = "Sun SX with 4M VSIMM";
                    break;
                case 0x800000:
                    psdp->descr = "Sun SX with 8M VSIMM";
                    break;
                }
                break;
            case SBUS_DEVICE_LEO:
                prop = sparcPromGetProperty(&psdp->node, "model", &len);
                if (prop && len > 0 && !strstr(prop, "501-2503"))
                    psdp->descr = "Sun Turbo ZX";
                break;
            case SBUS_DEVICE_TCX:
                if (sparcPromGetBool(&psdp->node, "tcx-8-bit"))
                    psdp->descr = "Sun TCX (8bit)";
                else
                    psdp->descr = "Sun TCX (S24)";
                break;
            case SBUS_DEVICE_FFB:
                prop = sparcPromGetProperty(&psdp->node, "name", &len);
                chiprev = 0;
                prop = sparcPromGetProperty(&psdp->node, "board_type", &len);
                if (prop && len == 4)
                    chiprev = *(int *) prop;
                if (strstr(prop, "afb")) {
                    if (chiprev == 3)
                        psdp->descr = "Sun|Elite3D-M6 Horizontal";
                }
                else {
                    switch (chiprev) {
                    case 0x08:
                        psdp->descr = "Sun FFB 67MHz Creator";
                        break;
                    case 0x0b:
                        psdp->descr = "Sun FFB 67MHz Creator 3D";
                        break;
                    case 0x1b:
                        psdp->descr = "Sun FFB 75MHz Creator 3D";
                        break;
                    case 0x20:
                    case 0x28:
                        psdp->descr = "Sun FFB2 Vertical Creator";
                        break;
                    case 0x23:
                    case 0x2b:
                        psdp->descr = "Sun FFB2 Vertical Creator 3D";
                        break;
                    case 0x30:
                        psdp->descr = "Sun FFB2+ Vertical Creator";
                        break;
                    case 0x33:
                        psdp->descr = "Sun FFB2+ Vertical Creator 3D";
                        break;
                    case 0x40:
                    case 0x48:
                        psdp->descr = "Sun FFB2 Horizontal Creator";
                        break;
                    case 0x43:
                    case 0x4b:
                        psdp->descr = "Sun FFB2 Horizontal Creator 3D";
                        break;
                    }
                }
                break;
            }

            xf86Msg(X_PROBED, "SBUS:(0x%08x) %s", psdp->node.node, psdp->descr);
            promPath = sparcPromNode2Pathname(&psdp->node);
            if (promPath) {
                xf86ErrorF(" at %s", promPath);
                free(promPath);
            }
        }
        else
            xf86Msg(X_PROBED, "SBUS: %s", psdp->descr);
        xf86ErrorF("\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
237
238
    }
    if (useProm)
239
        sparcPromClose();
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
}

/*
 * Parse a BUS ID string, and return the SBUS bus parameters if it was
 * in the correct format for a SBUS bus id.
 */

Bool
xf86ParseSbusBusString(const char *busID, int *fbNum)
{
    /*
     * The format is assumed to be one of:
     * "fbN", e.g. "fb1", which means the device corresponding to /dev/fbN
     * "nameN", e.g. "cgsix0", which means Nth instance of card NAME
     * "/prompath", e.g. "/sbus@0,10001000/cgsix@3,0" which is PROM pathname
     * to the device.
     */

    const char *id;
    int i, len;

    if (StringToBusType(busID, &id) != BUS_SBUS)
262
        return FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
263
264

    if (*id != '/') {
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
        if (!strncmp(id, "fb", 2)) {
            if (!isdigit(id[2]))
                return FALSE;
            *fbNum = atoi(id + 2);
            return TRUE;
        }
        else {
            sbusDevicePtr *psdpp;
            int devId;

            for (i = 0, len = 0; sbusDeviceTable[i].devId; i++) {
                len = strlen(sbusDeviceTable[i].promName);
                if (!strncmp(sbusDeviceTable[i].promName, id, len)
                    && isdigit(id[len]))
                    break;
            }
            devId = sbusDeviceTable[i].devId;
            if (!devId)
                return FALSE;
            i = atoi(id + len);
            for (psdpp = xf86SbusInfo; *psdpp; ++psdpp) {
                if ((*psdpp)->devId != devId)
                    continue;
                if (!i) {
                    *fbNum = (*psdpp)->fbNum;
                    return TRUE;
                }
                i--;
            }
        }
        return FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
296
297
298
    }

    if (sparcPromInit() >= 0) {
299
300
301
302
303
304
305
306
307
308
309
310
        i = sparcPromPathname2Node(id);
        sparcPromClose();
        if (i) {
            sbusDevicePtr *psdpp;

            for (psdpp = xf86SbusInfo; *psdpp; ++psdpp) {
                if ((*psdpp)->node.node == i) {
                    *fbNum = (*psdpp)->fbNum;
                    return TRUE;
                }
            }
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    }
    return FALSE;
}

/*
 * Compare a BUS ID string with a SBUS bus id.  Return TRUE if they match.
 */

Bool
xf86CompareSbusBusString(const char *busID, int fbNum)
{
    int iFbNum;

    if (xf86ParseSbusBusString(busID, &iFbNum)) {
325
326
327
328
        return fbNum == iFbNum;
    }
    else {
        return FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    }
}

/*
 * Check if the slot requested is free.  If it is already in use, return FALSE.
 */

Bool
xf86CheckSbusSlot(int fbNum)
{
    int i;
    EntityPtr p;

    for (i = 0; i < xf86NumEntities; i++) {
343
344
345
346
        p = xf86Entities[i];
        /* Check if this SBUS slot is taken */
        if (p->bus.type == BUS_SBUS && p->bus.id.sbus.fbNum == fbNum)
            return FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
347
348
349
350
351
352
353
354
355
356
357
    }

    return TRUE;
}

/*
 * If the slot requested is already in use, return -1.
 * Otherwise, claim the slot for the screen requesting it.
 */

int
358
xf86ClaimSbusSlot(sbusDevicePtr psdp, DriverPtr drvp, GDevPtr dev, Bool active)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
359
360
361
362
363
364
365
366
367
368
{
    EntityPtr p = NULL;

    int num;

    if (xf86CheckSbusSlot(psdp->fbNum)) {
        num = xf86AllocateEntity();
        p = xf86Entities[num];
        p->driver = drvp;
        p->chipset = -1;
369
        p->bus.type = BUS_SBUS;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
370
        xf86AddDevToEntity(num, dev);
371
        p->bus.id.sbus.fbNum = psdp->fbNum;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
372
373
        p->active = active;
        p->inUse = FALSE;
374
375
376
377
378
        sbusSlotClaimed = TRUE;
        return num;
    }
    else
        return -1;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
379
380
}

381
int
382
383
384
xf86MatchSbusInstances(const char *driverName, int sbusDevId,
                       GDevPtr * devList, int numDevs, DriverPtr drvp,
                       int **foundEntities)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
385
{
386
    int i, j;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
387
388
389
390
391
392
393
394
395
396
    sbusDevicePtr psdp, *psdpp;
    int numClaimedInstances = 0;
    int allocatedInstances = 0;
    int numFound = 0;
    GDevPtr devBus = NULL;
    GDevPtr dev = NULL;
    int *retEntities = NULL;
    int useProm = 0;

    struct Inst {
397
398
399
        sbusDevicePtr sbus;
        GDevPtr dev;
        Bool claimed;           /* BusID matches with a device section */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
400
401
402
403
    } *instances = NULL;

    *foundEntities = NULL;
    for (psdpp = xf86SbusInfo, psdp = *psdpp; psdp; psdp = *++psdpp) {
404
405
406
407
408
409
410
411
412
413
414
        if (psdp->devId != sbusDevId)
            continue;
        if (psdp->fd == -2)
            continue;
        ++allocatedInstances;
        instances = xnfrealloc(instances,
                               allocatedInstances * sizeof(struct Inst));
        instances[allocatedInstances - 1].sbus = psdp;
        instances[allocatedInstances - 1].dev = NULL;
        instances[allocatedInstances - 1].claimed = FALSE;
        numFound++;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
415
416
417
418
419
420
421
422
    }

    /*
     * This may be debatable, but if no SBUS devices with a matching vendor
     * type is found, return zero now.  It is probably not desirable to
     * allow the config file to override this.
     */
    if (allocatedInstances <= 0) {
423
424
        free(instances);
        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
425
426
427
    }

    if (sparcPromInit() >= 0)
428
        useProm = 1;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
429
430

    if (xf86DoConfigure && xf86DoConfigurePass1) {
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
        GDevPtr pGDev;
        int actualcards = 0;

        for (i = 0; i < allocatedInstances; i++) {
            actualcards++;
            pGDev = xf86AddBusDeviceToConfigure(drvp->driverName, BUS_SBUS,
                                                instances[i].sbus, -1);
            if (pGDev) {
                /*
                 * XF86Match???Instances() treat chipID and chipRev as
                 * overrides, so clobber them here.
                 */
                pGDev->chipID = pGDev->chipRev = -1;
            }
        }
        free(instances);
        if (useProm)
            sparcPromClose();
        return actualcards;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
450
451
    }

452
    DebugF("%s instances found: %d\n", driverName, allocatedInstances);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
453
454

    for (i = 0; i < allocatedInstances; i++) {
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
        char *promPath = NULL;

        psdp = instances[i].sbus;
        devBus = NULL;
        dev = NULL;
        if (useProm && psdp->node.node)
            promPath = sparcPromNode2Pathname(&psdp->node);

        for (j = 0; j < numDevs; j++) {
            if (devList[j]->busID && *devList[j]->busID) {
                if (xf86CompareSbusBusString(devList[j]->busID, psdp->fbNum)) {
                    if (devBus)
                        xf86MsgVerb(X_WARNING, 0,
                                    "%s: More than one matching Device section for "
                                    "instance (BusID: %s) found: %s\n",
                                    driverName, devList[j]->identifier,
                                    devList[j]->busID);
                    else
                        devBus = devList[j];
                }
            }
            else {
                if (!dev && !devBus) {
                    if (promPath)
                        xf86Msg(X_PROBED,
                                "Assigning device section with no busID to SBUS:%s\n",
                                promPath);
                    else
                        xf86Msg(X_PROBED,
                                "Assigning device section with no busID to SBUS:fb%d\n",
                                psdp->fbNum);
                    dev = devList[j];
                }
                else
                    xf86MsgVerb(X_WARNING, 0,
                                "%s: More than one matching Device section "
                                "found: %s\n", driverName,
                                devList[j]->identifier);
            }
        }
        if (devBus)
            dev = devBus;       /* busID preferred */
        if (!dev && psdp->fd != -2) {
            if (promPath) {
                xf86MsgVerb(X_WARNING, 0, "%s: No matching Device section "
                            "for instance (BusID SBUS:%s) found\n",
                            driverName, promPath);
            }
            else
                xf86MsgVerb(X_WARNING, 0, "%s: No matching Device section "
                            "for instance (BusID SBUS:fb%d) found\n",
                            driverName, psdp->fbNum);
        }
        else if (dev) {
            numClaimedInstances++;
            instances[i].claimed = TRUE;
            instances[i].dev = dev;
        }
        free(promPath);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
514
515
    }

516
    DebugF("%s instances found: %d\n", driverName, numClaimedInstances);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
517
518
519
520
521
522
523

    /*
     * Of the claimed instances, check that another driver hasn't already
     * claimed its slot.
     */
    numFound = 0;
    for (i = 0; i < allocatedInstances && numClaimedInstances > 0; i++) {
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
        if (!instances[i].claimed)
            continue;
        psdp = instances[i].sbus;
        if (!xf86CheckSbusSlot(psdp->fbNum))
            continue;

        DebugF("%s: card at fb%d %08x is claimed by a Device section\n",
               driverName, psdp->fbNum, psdp->node.node);

        /* Allocate an entry in the lists to be returned */
        numFound++;
        retEntities = xnfrealloc(retEntities, numFound * sizeof(int));
        retEntities[numFound - 1]
            = xf86ClaimSbusSlot(psdp, drvp, instances[i].dev,
                                instances[i].dev->active ? TRUE : FALSE);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
539
    }
540
    free(instances);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
541
    if (numFound > 0) {
542
        *foundEntities = retEntities;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
543
544
545
    }

    if (useProm)
546
        sparcPromClose();
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
547
548
549
550
551
552
553

    return numFound;
}

/*
 * xf86GetSbusInfoForEntity() -- Get the sbusDevicePtr of entity.
 */
554
sbusDevicePtr
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
555
556
557
558
559
xf86GetSbusInfoForEntity(int entityIndex)
{
    sbusDevicePtr *psdpp;
    EntityPtr p = xf86Entities[entityIndex];

560
561
    if (entityIndex >= xf86NumEntities || p->bus.type != BUS_SBUS)
        return NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
562
563

    for (psdpp = xf86SbusInfo; *psdpp != NULL; psdpp++) {
564
565
        if (p->bus.id.sbus.fbNum == (*psdpp)->fbNum)
            return *psdpp;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
566
567
568
569
    }
    return NULL;
}

570
int
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
571
572
573
574
575
xf86GetEntityForSbusInfo(sbusDevicePtr psdp)
{
    int i;

    for (i = 0; i < xf86NumEntities; i++) {
576
        EntityPtr p = xf86Entities[i];
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
577

578
579
580
581
582
        if (p->bus.type != BUS_SBUS)
            continue;

        if (p->bus.id.sbus.fbNum == psdp->fbNum)
            return i;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
583
584
585
586
    }
    return -1;
}

587
void
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
xf86SbusUseBuiltinMode(ScrnInfoPtr pScrn, sbusDevicePtr psdp)
{
    DisplayModePtr mode;

    mode = xnfcalloc(sizeof(DisplayModeRec), 1);
    mode->name = "current";
    mode->next = mode;
    mode->prev = mode;
    mode->type = M_T_BUILTIN;
    mode->Clock = 100000000;
    mode->HDisplay = psdp->width;
    mode->HSyncStart = psdp->width;
    mode->HSyncEnd = psdp->width;
    mode->HTotal = psdp->width;
    mode->VDisplay = psdp->height;
    mode->VSyncStart = psdp->height;
    mode->VSyncEnd = psdp->height;
    mode->VTotal = psdp->height;
    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;
    pScrn->modes = mode;
    pScrn->virtualX = psdp->width;
    pScrn->virtualY = psdp->height;
}

622
623
624
static DevPrivateKeyRec sbusPaletteKeyRec;
#define sbusPaletteKey (&sbusPaletteKeyRec)

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
625
626
627
628
629
630
631
632
633
typedef struct _sbusCmap {
    sbusDevicePtr psdp;
    CloseScreenProcPtr CloseScreen;
    Bool origCmapValid;
    unsigned char origRed[16];
    unsigned char origGreen[16];
    unsigned char origBlue[16];
} sbusCmapRec, *sbusCmapPtr;

634
635
#define SBUSCMAPPTR(pScreen) ((sbusCmapPtr) \
    dixLookupPrivate(&(pScreen)->devPrivates, sbusPaletteKey))
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
636
637
638

static void
xf86SbusCmapLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
639
                        LOCO * colors, VisualPtr pVisual)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
640
641
642
643
{
    int i, index;
    sbusCmapPtr cmap;
    struct fbcmap fbcmap;
644
645
    unsigned char *data = malloc(numColors * 3);

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
646
    cmap = SBUSCMAPPTR(pScrn->pScreen);
647
648
    if (!cmap)
        return;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
649
650
651
652
653
654
    fbcmap.count = 0;
    fbcmap.index = indices[0];
    fbcmap.red = data;
    fbcmap.green = data + numColors;
    fbcmap.blue = fbcmap.green + numColors;
    for (i = 0; i < numColors; i++) {
655
656
657
658
659
660
661
662
663
        index = indices[i];
        if (fbcmap.count && index != fbcmap.index + fbcmap.count) {
            ioctl(cmap->psdp->fd, FBIOPUTCMAP, &fbcmap);
            fbcmap.count = 0;
            fbcmap.index = index;
        }
        fbcmap.red[fbcmap.count] = colors[index].red;
        fbcmap.green[fbcmap.count] = colors[index].green;
        fbcmap.blue[fbcmap.count++] = colors[index].blue;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
664
    }
665
    ioctl(cmap->psdp->fd, FBIOPUTCMAP, &fbcmap);
666
    free(data);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
667
668
669
670
671
672
673
}

static Bool
xf86SbusCmapCloseScreen(int i, ScreenPtr pScreen)
{
    sbusCmapPtr cmap;
    struct fbcmap fbcmap;
674

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
675
676
    cmap = SBUSCMAPPTR(pScreen);
    if (cmap->origCmapValid) {
677
678
679
680
681
682
        fbcmap.index = 0;
        fbcmap.count = 16;
        fbcmap.red = cmap->origRed;
        fbcmap.green = cmap->origGreen;
        fbcmap.blue = cmap->origBlue;
        ioctl(cmap->psdp->fd, FBIOPUTCMAP, &fbcmap);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
683
684
    }
    pScreen->CloseScreen = cmap->CloseScreen;
685
    free(cmap);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
686
    return (*pScreen->CloseScreen) (i, pScreen);
687
}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
688

689
Bool
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
690
691
692
693
694
695
xf86SbusHandleColormaps(ScreenPtr pScreen, sbusDevicePtr psdp)
{
    sbusCmapPtr cmap;
    struct fbcmap fbcmap;
    unsigned char data[2];

696
697
698
    if (!dixRegisterPrivateKey(sbusPaletteKey, PRIVATE_SCREEN, 0))
        FatalError("Cannot register sbus private key");

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
699
    cmap = xnfcalloc(1, sizeof(sbusCmapRec));
700
    dixSetPrivate(&pScreen->devPrivates, sbusPaletteKey, cmap);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
701
702
703
704
705
706
    cmap->psdp = psdp;
    fbcmap.index = 0;
    fbcmap.count = 16;
    fbcmap.red = cmap->origRed;
    fbcmap.green = cmap->origGreen;
    fbcmap.blue = cmap->origBlue;
707
708
    if (ioctl(psdp->fd, FBIOGETCMAP, &fbcmap) >= 0)
        cmap->origCmapValid = TRUE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
709
710
711
712
713
714
    fbcmap.index = 0;
    fbcmap.count = 2;
    fbcmap.red = data;
    fbcmap.green = data;
    fbcmap.blue = data;
    if (pScreen->whitePixel == 0) {
715
716
717
718
719
720
        data[0] = 255;
        data[1] = 0;
    }
    else {
        data[0] = 0;
        data[1] = 255;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
721
    }
722
    ioctl(psdp->fd, FBIOPUTCMAP, &fbcmap);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
723
724
725
    cmap->CloseScreen = pScreen->CloseScreen;
    pScreen->CloseScreen = xf86SbusCmapCloseScreen;
    return xf86HandleColormaps(pScreen, 256, 8,
726
                               xf86SbusCmapLoadPalette, NULL, 0);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
727
}
728
729
730
731
732
733
734
735
736
737

Bool
xf86SbusConfigure(void *busData, sbusDevicePtr sBus)
{
    if (sBus && sBus->fbNum == ((sbusDevicePtr) busData)->fbNum)
        return 0;
    return 1;
}

void
738
xf86SbusConfigureNewDev(void *busData, sbusDevicePtr sBus, GDevRec * GDev)
739
740
741
742
743
744
745
746
747
748
{
    char *promPath = NULL;

    sBus = (sbusDevicePtr) busData;
    GDev->identifier = sBus->descr;
    if (sparcPromInit() >= 0) {
        promPath = sparcPromNode2Pathname(&sBus->node);
        sparcPromClose();
    }
    if (promPath) {
749
        XNFasprintf(&GDev->busID, "SBUS:%s", promPath);
750
        free(promPath);
751
752
    }
    else {
753
        XNFasprintf(&GDev->busID, "SBUS:fb%d", sBus->fbNum);
754
755
    }
}