~ubuntu-branches/ubuntu/intrepid/xserver-xgl/intrepid

« back to all changes in this revision

Viewing changes to hw/kdrive/linux/linux.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Garrett
  • Date: 2006-02-13 14:21:43 UTC
  • Revision ID: james.westby@ubuntu.com-20060213142143-mad6z9xzem7hzxz9
Tags: upstream-7.0.0
ImportĀ upstreamĀ versionĀ 7.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $RCSId: xc/programs/Xserver/hw/kdrive/linux/linux.c,v 1.6 2001/07/24 21:26:17 keithp Exp $
 
3
 *
 
4
 * Copyright ļæ½ 1999 Keith Packard
 
5
 *
 
6
 * Permission to use, copy, modify, distribute, and sell this software and its
 
7
 * documentation for any purpose is hereby granted without fee, provided that
 
8
 * the above copyright notice appear in all copies and that both that
 
9
 * copyright notice and this permission notice appear in supporting
 
10
 * documentation, and that the name of Keith Packard not be used in
 
11
 * advertising or publicity pertaining to distribution of the software without
 
12
 * specific, written prior permission.  Keith Packard makes no
 
13
 * representations about the suitability of this software for any purpose.  It
 
14
 * is provided "as is" without express or implied warranty.
 
15
 *
 
16
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 
17
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 
18
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 
19
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 
20
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 
21
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 
22
 * PERFORMANCE OF THIS SOFTWARE.
 
23
 */
 
24
 
 
25
#ifdef HAVE_CONFIG_H
 
26
#include <kdrive-config.h>
 
27
#endif
 
28
#include "kdrive.h"
 
29
#include "klinux.h"
 
30
#include <errno.h>
 
31
#include <signal.h>
 
32
#include <linux/vt.h>
 
33
#include <linux/kd.h>
 
34
#include <sys/stat.h>
 
35
#include <sys/ioctl.h>
 
36
#include <X11/keysym.h>
 
37
#include <linux/apm_bios.h>
 
38
 
 
39
static int  vtno;
 
40
int  LinuxConsoleFd;
 
41
int  LinuxApmFd = -1;
 
42
static int  activeVT;
 
43
static Bool enabled;
 
44
 
 
45
static void
 
46
LinuxVTRequest (int sig)
 
47
{
 
48
    kdSwitchPending = TRUE;
 
49
}
 
50
 
 
51
/* Check before chowning -- this avoids touching the file system */
 
52
static void
 
53
LinuxCheckChown (char *file)
 
54
{
 
55
    struct stat     st;
 
56
    __uid_t         u;
 
57
    __gid_t         g;
 
58
 
 
59
    if (stat (file, &st) < 0)
 
60
        return;
 
61
    u = getuid ();
 
62
    g = getgid ();
 
63
    if (st.st_uid != u || st.st_gid != g)
 
64
        chown (file, u, g);
 
65
}
 
66
 
 
67
static int
 
68
LinuxInit (void)
 
69
{
 
70
    int fd = -1;
 
71
    char vtname[11];
 
72
    struct vt_stat vts;
 
73
 
 
74
    LinuxConsoleFd = -1;
 
75
    /* check if we're run with euid==0 */
 
76
    if (geteuid() != 0)
 
77
    {
 
78
        FatalError("LinuxInit: Server must be suid root\n");
 
79
    }
 
80
 
 
81
    if (kdVirtualTerminal >= 0)
 
82
        vtno = kdVirtualTerminal;
 
83
    else
 
84
    {
 
85
        if ((fd = open("/dev/tty0",O_WRONLY,0)) < 0) 
 
86
        {
 
87
            FatalError(
 
88
                       "LinuxInit: Cannot open /dev/tty0 (%s)\n",
 
89
                       strerror(errno));
 
90
        }
 
91
        if ((ioctl(fd, VT_OPENQRY, &vtno) < 0) ||
 
92
            (vtno == -1))
 
93
        {
 
94
            FatalError("xf86OpenConsole: Cannot find a free VT\n");
 
95
        }
 
96
    }
 
97
    close(fd);
 
98
 
 
99
    sprintf(vtname,"/dev/tty%d",vtno); /* /dev/tty1-64 */
 
100
 
 
101
    if ((LinuxConsoleFd = open(vtname, O_RDWR|O_NDELAY, 0)) < 0)
 
102
    {
 
103
        FatalError("LinuxInit: Cannot open %s (%s)\n",
 
104
                   vtname, strerror(errno));
 
105
    }
 
106
 
 
107
    /* change ownership of the vt */
 
108
    LinuxCheckChown (vtname);
 
109
 
 
110
    /*
 
111
     * the current VT device we're running on is not "console", we want
 
112
     * to grab all consoles too
 
113
     *
 
114
     * Why is this needed?
 
115
     */
 
116
    LinuxCheckChown ("/dev/tty0");
 
117
    /*
 
118
     * Linux doesn't switch to an active vt after the last close of a vt,
 
119
     * so we do this ourselves by remembering which is active now.
 
120
     */
 
121
    memset (&vts, '\0', sizeof (vts));  /* valgrind */
 
122
    if (ioctl(LinuxConsoleFd, VT_GETSTATE, &vts) == 0)
 
123
    {
 
124
        activeVT = vts.v_active;
 
125
    }
 
126
 
 
127
    return 1;
 
128
}
 
129
 
 
130
Bool
 
131
LinuxFindPci (CARD16 vendor, CARD16 device, CARD32 count, KdCardAttr *attr)
 
132
{
 
133
    FILE    *f;
 
134
    char    line[2048], *l, *end;
 
135
    CARD32  bus, id, addr;
 
136
    int     n;
 
137
    CARD32  ven_dev;
 
138
    Bool    ret = FALSE;
 
139
    int     i;
 
140
 
 
141
    attr->vendorID = vendor;
 
142
    attr->deviceID = device;
 
143
    ven_dev = (((CARD32) vendor) << 16) | ((CARD32) device);
 
144
    f = fopen ("/proc/bus/pci/devices", "r");
 
145
    if (!f)
 
146
        return FALSE;
 
147
    attr->io = 0;
 
148
    while (fgets (line, sizeof (line)-1, f))
 
149
    {
 
150
        line[sizeof(line)-1] = '\0';
 
151
        l = line;
 
152
        bus = strtoul (l, &end, 16);
 
153
        if (end == l)
 
154
            continue;
 
155
        l = end;
 
156
        id = strtoul (l, &end, 16);
 
157
        if (end == l)
 
158
            continue;
 
159
        l = end;
 
160
        if (id != ven_dev)
 
161
            continue;
 
162
        if (count--)
 
163
            continue;
 
164
        (void) strtoul (l, &end, 16);   /* IRQ */
 
165
        if (end == l)
 
166
            continue;
 
167
        l = end;
 
168
        n = 0;
 
169
        for (i = 0; i < 6; i++)
 
170
        {
 
171
            addr = strtoul (l, &end, 16);
 
172
            if (end == l)
 
173
                break;
 
174
            if (addr & 1)
 
175
                attr->io = addr & ~0xf;
 
176
            else
 
177
            {
 
178
                if (n == KD_MAX_CARD_ADDRESS)
 
179
                    break;
 
180
                attr->address[n++] = addr & ~0xf;
 
181
            }
 
182
            l = end;
 
183
        }
 
184
        while (n > 0)
 
185
        {
 
186
            if (attr->address[n-1] != 0)
 
187
                break;
 
188
            n--;
 
189
        }
 
190
        attr->naddr = n;
 
191
        attr->domain = 0; /* XXX */
 
192
        attr->bus = (bus >> 8) & 0xff;
 
193
        attr->slot = (bus >> 3) & 0x1f;
 
194
        attr->func = bus & 0x07;
 
195
        ret = TRUE;
 
196
        break;
 
197
    }
 
198
    fclose (f);
 
199
    return ret;
 
200
}
 
201
 
 
202
unsigned char *
 
203
LinuxGetPciCfg(KdCardAttr *attr) 
 
204
{
 
205
    char filename[256];
 
206
    FILE *f;
 
207
    unsigned char *cfg;
 
208
    int r;
 
209
 
 
210
    snprintf(filename, 255, "/proc/bus/pci/%02x/%02x.%x",
 
211
             attr->bus >> 8, (attr->bus & 0xff) >> 3, attr->bus & 7);
 
212
/*     fprintf(stderr,"Find card on path %s\n",filename); */
 
213
 
 
214
    if (!(f=fopen(filename,"r"))) 
 
215
        return NULL;
 
216
 
 
217
    if (!(cfg=xalloc(256))) 
 
218
    {
 
219
        fclose(f);
 
220
        return NULL;
 
221
    }
 
222
 
 
223
    if (256 != (r=fread(cfg, 1, 256, f)))
 
224
    {
 
225
        fprintf(stderr,"LinuxGetPciCfg: read %d, expected 256\n",r);
 
226
        free(cfg);
 
227
        cfg=NULL;
 
228
    }
 
229
    fclose(f);
 
230
/*     fprintf(stderr,"LinuxGetPciCfg: success, returning %p\n",cfg); */
 
231
    return cfg;
 
232
}
 
233
 
 
234
static void
 
235
LinuxSetSwitchMode (int mode)
 
236
{
 
237
    struct sigaction    act;
 
238
    struct vt_mode      VT;
 
239
    
 
240
    if (ioctl(LinuxConsoleFd, VT_GETMODE, &VT) < 0) 
 
241
    {
 
242
        FatalError ("LinuxInit: VT_GETMODE failed\n");
 
243
    }
 
244
 
 
245
    if (mode == VT_PROCESS)
 
246
    {
 
247
        act.sa_handler = LinuxVTRequest;
 
248
        sigemptyset (&act.sa_mask);
 
249
        act.sa_flags = 0;
 
250
        sigaction (SIGUSR1, &act, 0);
 
251
    
 
252
        VT.mode = mode;
 
253
        VT.relsig = SIGUSR1;
 
254
        VT.acqsig = SIGUSR1;
 
255
    }
 
256
    else
 
257
    {
 
258
        act.sa_handler = SIG_IGN;
 
259
        sigemptyset (&act.sa_mask);
 
260
        act.sa_flags = 0;
 
261
        sigaction (SIGUSR1, &act, 0);
 
262
    
 
263
        VT.mode = mode;
 
264
        VT.relsig = 0;
 
265
        VT.acqsig = 0;
 
266
    }
 
267
    if (ioctl(LinuxConsoleFd, VT_SETMODE, &VT) < 0) 
 
268
    {
 
269
        FatalError("LinuxInit: VT_SETMODE failed\n");
 
270
    }
 
271
}
 
272
 
 
273
static void
 
274
LinuxApmBlock (pointer blockData, OSTimePtr pTimeout, pointer pReadmask)
 
275
{
 
276
}
 
277
 
 
278
static Bool LinuxApmRunning;
 
279
 
 
280
static void
 
281
LinuxApmWakeup (pointer blockData, int result, pointer pReadmask)
 
282
{
 
283
    fd_set  *readmask = (fd_set *) pReadmask;
 
284
 
 
285
    if (result > 0 && LinuxApmFd >= 0 && FD_ISSET (LinuxApmFd, readmask))
 
286
    {
 
287
        apm_event_t event;
 
288
        Bool        running = LinuxApmRunning;
 
289
        int         cmd = APM_IOC_SUSPEND;
 
290
 
 
291
        while (read (LinuxApmFd, &event, sizeof (event)) == sizeof (event))
 
292
        {
 
293
            switch (event) {
 
294
            case APM_SYS_STANDBY:
 
295
            case APM_USER_STANDBY:
 
296
                running = FALSE;
 
297
                cmd = APM_IOC_STANDBY;
 
298
                break;
 
299
            case APM_SYS_SUSPEND:
 
300
            case APM_USER_SUSPEND:
 
301
            case APM_CRITICAL_SUSPEND:
 
302
                running = FALSE;
 
303
                cmd = APM_IOC_SUSPEND;
 
304
                break;
 
305
            case APM_NORMAL_RESUME:
 
306
            case APM_CRITICAL_RESUME:
 
307
            case APM_STANDBY_RESUME:
 
308
                running = TRUE;
 
309
                break;
 
310
            }
 
311
        }
 
312
        if (running && !LinuxApmRunning)
 
313
        {
 
314
            KdResume ();
 
315
            LinuxApmRunning = TRUE;
 
316
        }
 
317
        else if (!running && LinuxApmRunning)
 
318
        {
 
319
            KdSuspend ();
 
320
            LinuxApmRunning = FALSE;
 
321
            ioctl (LinuxApmFd, cmd, 0);
 
322
        }
 
323
    }
 
324
}
 
325
 
 
326
#ifdef FNONBLOCK
 
327
#define NOBLOCK FNONBLOCK
 
328
#else
 
329
#define NOBLOCK FNDELAY
 
330
#endif
 
331
 
 
332
static void
 
333
LinuxEnable (void)
 
334
{
 
335
    if (enabled)
 
336
        return;
 
337
    if (kdSwitchPending)
 
338
    {
 
339
        kdSwitchPending = FALSE;
 
340
        ioctl (LinuxConsoleFd, VT_RELDISP, VT_ACKACQ);
 
341
    }
 
342
    /*
 
343
     * Open the APM driver
 
344
     */
 
345
    LinuxApmFd = open ("/dev/apm_bios", 2);
 
346
    if (LinuxApmFd < 0 && errno == ENOENT)
 
347
        LinuxApmFd = open ("/dev/misc/apm_bios", 2); 
 
348
    if (LinuxApmFd >= 0)
 
349
    {
 
350
        LinuxApmRunning = TRUE;
 
351
        fcntl (LinuxApmFd, F_SETFL, fcntl (LinuxApmFd, F_GETFL) | NOBLOCK);
 
352
        RegisterBlockAndWakeupHandlers (LinuxApmBlock, LinuxApmWakeup, 0);
 
353
        AddEnabledDevice (LinuxApmFd);
 
354
    }
 
355
        
 
356
    /*
 
357
     * now get the VT
 
358
     */
 
359
    LinuxSetSwitchMode (VT_AUTO);
 
360
    if (ioctl(LinuxConsoleFd, VT_ACTIVATE, vtno) != 0)
 
361
    {
 
362
        FatalError("LinuxInit: VT_ACTIVATE failed\n");
 
363
    }
 
364
    if (ioctl(LinuxConsoleFd, VT_WAITACTIVE, vtno) != 0)
 
365
    {
 
366
        FatalError("LinuxInit: VT_WAITACTIVE failed\n");
 
367
    }
 
368
    LinuxSetSwitchMode (VT_PROCESS);
 
369
    if (ioctl(LinuxConsoleFd, KDSETMODE, KD_GRAPHICS) < 0)
 
370
    {
 
371
        FatalError("LinuxInit: KDSETMODE KD_GRAPHICS failed\n");
 
372
    }
 
373
    enabled = TRUE;
 
374
}
 
375
 
 
376
static Bool
 
377
LinuxSpecialKey (KeySym sym)
 
378
{
 
379
    struct vt_stat  vts;
 
380
    int             con;
 
381
    
 
382
    if (XK_F1 <= sym && sym <= XK_F12)
 
383
    {
 
384
        con = sym - XK_F1 + 1;
 
385
        memset (&vts, '\0', sizeof (vts));      /* valgrind */
 
386
        ioctl (LinuxConsoleFd, VT_GETSTATE, &vts);
 
387
        if (con != vts.v_active && (vts.v_state & (1 << con)))
 
388
        {
 
389
            ioctl (LinuxConsoleFd, VT_ACTIVATE, con);
 
390
            return TRUE;
 
391
        }
 
392
    }
 
393
    return FALSE;
 
394
}
 
395
 
 
396
static void
 
397
LinuxDisable (void)
 
398
{
 
399
    ioctl(LinuxConsoleFd, KDSETMODE, KD_TEXT);  /* Back to text mode ... */
 
400
    if (kdSwitchPending)
 
401
    {
 
402
        kdSwitchPending = FALSE;
 
403
        ioctl (LinuxConsoleFd, VT_RELDISP, 1);
 
404
    }
 
405
    enabled = FALSE;
 
406
    if (LinuxApmFd >= 0)
 
407
    {
 
408
        RemoveBlockAndWakeupHandlers (LinuxApmBlock, LinuxApmWakeup, 0);
 
409
        RemoveEnabledDevice (LinuxApmFd);
 
410
        close (LinuxApmFd);
 
411
        LinuxApmFd = -1;
 
412
    }
 
413
}
 
414
 
 
415
static void
 
416
LinuxFini (void)
 
417
{
 
418
    struct vt_mode   VT;
 
419
    struct vt_stat  vts;
 
420
    int             fd;
 
421
 
 
422
    if (LinuxConsoleFd < 0)
 
423
        return;
 
424
 
 
425
    if (ioctl(LinuxConsoleFd, VT_GETMODE, &VT) != -1)
 
426
    {
 
427
        VT.mode = VT_AUTO;
 
428
        ioctl(LinuxConsoleFd, VT_SETMODE, &VT); /* set dflt vt handling */
 
429
    }
 
430
    memset (&vts, '\0', sizeof (vts));  /* valgrind */
 
431
    ioctl (LinuxConsoleFd, VT_GETSTATE, &vts);
 
432
    if (vtno == vts.v_active)
 
433
    {
 
434
        /*
 
435
         * Find a legal VT to switch to, either the one we started from
 
436
         * or the lowest active one that isn't ours
 
437
         */
 
438
        if (activeVT < 0 || 
 
439
            activeVT == vts.v_active || 
 
440
            !(vts.v_state & (1 << activeVT)))
 
441
        {
 
442
            for (activeVT = 1; activeVT < 16; activeVT++)
 
443
                if (activeVT != vtno && (vts.v_state & (1 << activeVT)))
 
444
                    break;
 
445
            if (activeVT == 16)
 
446
                activeVT = -1;
 
447
        }
 
448
        /*
 
449
         * Perform a switch back to the active VT when we were started
 
450
         */
 
451
        if (activeVT >= -1)
 
452
        {
 
453
            ioctl (LinuxConsoleFd, VT_ACTIVATE, activeVT);
 
454
            ioctl (LinuxConsoleFd, VT_WAITACTIVE, activeVT);
 
455
            activeVT = -1;
 
456
        }
 
457
    }
 
458
    close(LinuxConsoleFd);                /* make the vt-manager happy */
 
459
    fd = open ("/dev/tty0", O_RDWR|O_NDELAY, 0);
 
460
    if (fd >= 0)
 
461
    {
 
462
        memset (&vts, '\0', sizeof (vts));      /* valgrind */
 
463
        ioctl (fd, VT_GETSTATE, &vts);
 
464
        if (ioctl (fd, VT_DISALLOCATE, vtno) < 0)
 
465
            fprintf (stderr, "Can't deallocate console %d errno %d\n", vtno, errno);
 
466
        close (fd);
 
467
    }
 
468
    return;
 
469
}
 
470
 
 
471
KdOsFuncs   LinuxFuncs = {
 
472
    LinuxInit,
 
473
    LinuxEnable,
 
474
    LinuxSpecialKey,
 
475
    LinuxDisable,
 
476
    LinuxFini,
 
477
    0
 
478
};
 
479
 
 
480
void
 
481
OsVendorInit (void)
 
482
{
 
483
    KdOsInit (&LinuxFuncs);
 
484
}