2
* $RCSId: xc/programs/Xserver/hw/kdrive/linux/linux.c,v 1.6 2001/07/24 21:26:17 keithp Exp $
4
* Copyright ļæ½ 1999 Keith Packard
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.
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.
26
#include <kdrive-config.h>
35
#include <sys/ioctl.h>
36
#include <X11/keysym.h>
37
#include <linux/apm_bios.h>
46
LinuxVTRequest (int sig)
48
kdSwitchPending = TRUE;
51
/* Check before chowning -- this avoids touching the file system */
53
LinuxCheckChown (char *file)
59
if (stat (file, &st) < 0)
63
if (st.st_uid != u || st.st_gid != g)
75
/* check if we're run with euid==0 */
78
FatalError("LinuxInit: Server must be suid root\n");
81
if (kdVirtualTerminal >= 0)
82
vtno = kdVirtualTerminal;
85
if ((fd = open("/dev/tty0",O_WRONLY,0)) < 0)
88
"LinuxInit: Cannot open /dev/tty0 (%s)\n",
91
if ((ioctl(fd, VT_OPENQRY, &vtno) < 0) ||
94
FatalError("xf86OpenConsole: Cannot find a free VT\n");
99
sprintf(vtname,"/dev/tty%d",vtno); /* /dev/tty1-64 */
101
if ((LinuxConsoleFd = open(vtname, O_RDWR|O_NDELAY, 0)) < 0)
103
FatalError("LinuxInit: Cannot open %s (%s)\n",
104
vtname, strerror(errno));
107
/* change ownership of the vt */
108
LinuxCheckChown (vtname);
111
* the current VT device we're running on is not "console", we want
112
* to grab all consoles too
114
* Why is this needed?
116
LinuxCheckChown ("/dev/tty0");
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.
121
memset (&vts, '\0', sizeof (vts)); /* valgrind */
122
if (ioctl(LinuxConsoleFd, VT_GETSTATE, &vts) == 0)
124
activeVT = vts.v_active;
131
LinuxFindPci (CARD16 vendor, CARD16 device, CARD32 count, KdCardAttr *attr)
134
char line[2048], *l, *end;
135
CARD32 bus, id, addr;
141
attr->vendorID = vendor;
142
attr->deviceID = device;
143
ven_dev = (((CARD32) vendor) << 16) | ((CARD32) device);
144
f = fopen ("/proc/bus/pci/devices", "r");
148
while (fgets (line, sizeof (line)-1, f))
150
line[sizeof(line)-1] = '\0';
152
bus = strtoul (l, &end, 16);
156
id = strtoul (l, &end, 16);
164
(void) strtoul (l, &end, 16); /* IRQ */
169
for (i = 0; i < 6; i++)
171
addr = strtoul (l, &end, 16);
175
attr->io = addr & ~0xf;
178
if (n == KD_MAX_CARD_ADDRESS)
180
attr->address[n++] = addr & ~0xf;
186
if (attr->address[n-1] != 0)
191
attr->domain = 0; /* XXX */
192
attr->bus = (bus >> 8) & 0xff;
193
attr->slot = (bus >> 3) & 0x1f;
194
attr->func = bus & 0x07;
203
LinuxGetPciCfg(KdCardAttr *attr)
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); */
214
if (!(f=fopen(filename,"r")))
217
if (!(cfg=xalloc(256)))
223
if (256 != (r=fread(cfg, 1, 256, f)))
225
fprintf(stderr,"LinuxGetPciCfg: read %d, expected 256\n",r);
230
/* fprintf(stderr,"LinuxGetPciCfg: success, returning %p\n",cfg); */
235
LinuxSetSwitchMode (int mode)
237
struct sigaction act;
240
if (ioctl(LinuxConsoleFd, VT_GETMODE, &VT) < 0)
242
FatalError ("LinuxInit: VT_GETMODE failed\n");
245
if (mode == VT_PROCESS)
247
act.sa_handler = LinuxVTRequest;
248
sigemptyset (&act.sa_mask);
250
sigaction (SIGUSR1, &act, 0);
258
act.sa_handler = SIG_IGN;
259
sigemptyset (&act.sa_mask);
261
sigaction (SIGUSR1, &act, 0);
267
if (ioctl(LinuxConsoleFd, VT_SETMODE, &VT) < 0)
269
FatalError("LinuxInit: VT_SETMODE failed\n");
274
LinuxApmBlock (pointer blockData, OSTimePtr pTimeout, pointer pReadmask)
278
static Bool LinuxApmRunning;
281
LinuxApmWakeup (pointer blockData, int result, pointer pReadmask)
283
fd_set *readmask = (fd_set *) pReadmask;
285
if (result > 0 && LinuxApmFd >= 0 && FD_ISSET (LinuxApmFd, readmask))
288
Bool running = LinuxApmRunning;
289
int cmd = APM_IOC_SUSPEND;
291
while (read (LinuxApmFd, &event, sizeof (event)) == sizeof (event))
294
case APM_SYS_STANDBY:
295
case APM_USER_STANDBY:
297
cmd = APM_IOC_STANDBY;
299
case APM_SYS_SUSPEND:
300
case APM_USER_SUSPEND:
301
case APM_CRITICAL_SUSPEND:
303
cmd = APM_IOC_SUSPEND;
305
case APM_NORMAL_RESUME:
306
case APM_CRITICAL_RESUME:
307
case APM_STANDBY_RESUME:
312
if (running && !LinuxApmRunning)
315
LinuxApmRunning = TRUE;
317
else if (!running && LinuxApmRunning)
320
LinuxApmRunning = FALSE;
321
ioctl (LinuxApmFd, cmd, 0);
327
#define NOBLOCK FNONBLOCK
329
#define NOBLOCK FNDELAY
339
kdSwitchPending = FALSE;
340
ioctl (LinuxConsoleFd, VT_RELDISP, VT_ACKACQ);
343
* Open the APM driver
345
LinuxApmFd = open ("/dev/apm_bios", 2);
346
if (LinuxApmFd < 0 && errno == ENOENT)
347
LinuxApmFd = open ("/dev/misc/apm_bios", 2);
350
LinuxApmRunning = TRUE;
351
fcntl (LinuxApmFd, F_SETFL, fcntl (LinuxApmFd, F_GETFL) | NOBLOCK);
352
RegisterBlockAndWakeupHandlers (LinuxApmBlock, LinuxApmWakeup, 0);
353
AddEnabledDevice (LinuxApmFd);
359
LinuxSetSwitchMode (VT_AUTO);
360
if (ioctl(LinuxConsoleFd, VT_ACTIVATE, vtno) != 0)
362
FatalError("LinuxInit: VT_ACTIVATE failed\n");
364
if (ioctl(LinuxConsoleFd, VT_WAITACTIVE, vtno) != 0)
366
FatalError("LinuxInit: VT_WAITACTIVE failed\n");
368
LinuxSetSwitchMode (VT_PROCESS);
369
if (ioctl(LinuxConsoleFd, KDSETMODE, KD_GRAPHICS) < 0)
371
FatalError("LinuxInit: KDSETMODE KD_GRAPHICS failed\n");
377
LinuxSpecialKey (KeySym sym)
382
if (XK_F1 <= sym && sym <= XK_F12)
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)))
389
ioctl (LinuxConsoleFd, VT_ACTIVATE, con);
399
ioctl(LinuxConsoleFd, KDSETMODE, KD_TEXT); /* Back to text mode ... */
402
kdSwitchPending = FALSE;
403
ioctl (LinuxConsoleFd, VT_RELDISP, 1);
408
RemoveBlockAndWakeupHandlers (LinuxApmBlock, LinuxApmWakeup, 0);
409
RemoveEnabledDevice (LinuxApmFd);
422
if (LinuxConsoleFd < 0)
425
if (ioctl(LinuxConsoleFd, VT_GETMODE, &VT) != -1)
428
ioctl(LinuxConsoleFd, VT_SETMODE, &VT); /* set dflt vt handling */
430
memset (&vts, '\0', sizeof (vts)); /* valgrind */
431
ioctl (LinuxConsoleFd, VT_GETSTATE, &vts);
432
if (vtno == vts.v_active)
435
* Find a legal VT to switch to, either the one we started from
436
* or the lowest active one that isn't ours
439
activeVT == vts.v_active ||
440
!(vts.v_state & (1 << activeVT)))
442
for (activeVT = 1; activeVT < 16; activeVT++)
443
if (activeVT != vtno && (vts.v_state & (1 << activeVT)))
449
* Perform a switch back to the active VT when we were started
453
ioctl (LinuxConsoleFd, VT_ACTIVATE, activeVT);
454
ioctl (LinuxConsoleFd, VT_WAITACTIVE, activeVT);
458
close(LinuxConsoleFd); /* make the vt-manager happy */
459
fd = open ("/dev/tty0", O_RDWR|O_NDELAY, 0);
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);
471
KdOsFuncs LinuxFuncs = {
483
KdOsInit (&LinuxFuncs);