2 * Copyright 1992 by Rich Murphey <Rich@Rice.edu>
3 * Copyright 1993 by David Wexelblat <dwex@goblin.org>
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the names of Rich Murphey and David Wexelblat
10 * not be used in advertising or publicity pertaining to distribution of
11 * the software without specific, written prior permission. Rich Murphey and
12 * David Wexelblat make no representations about the suitability of this
13 * software for any purpose. It is provided "as is" without express or
16 * RICH MURPHEY AND DAVID WEXELBLAT DISCLAIM ALL WARRANTIES WITH REGARD TO
17 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL RICH MURPHEY OR DAVID WEXELBLAT BE LIABLE FOR
19 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 #ifdef HAVE_XORG_CONFIG_H
27 #include <xorg-config.h>
36 #include "xf86_OSlib.h"
38 #include <sys/utsname.h>
39 #include <sys/ioctl.h>
43 static Bool KeepTty
= FALSE
;
44 static int devConsoleFd
= -1;
45 #if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
46 static int VTnum
= -1;
47 static int initialVT
= -1;
51 /* Stock 0.1 386bsd pccons console driver interface */
53 # define PCCONS_CONSOLE_DEV1 "/dev/ttyv0"
55 # define PCCONS_CONSOLE_DEV1 "/dev/ttyC0"
57 #define PCCONS_CONSOLE_DEV2 "/dev/vga"
58 #define PCCONS_CONSOLE_MODE O_RDWR|O_NDELAY
61 #ifdef SYSCONS_SUPPORT
62 /* The FreeBSD 1.1 version syscons driver uses /dev/ttyv0 */
63 #define SYSCONS_CONSOLE_DEV1 "/dev/ttyv0"
64 #define SYSCONS_CONSOLE_DEV2 "/dev/vga"
65 #define SYSCONS_CONSOLE_MODE O_RDWR|O_NDELAY
69 /* Hellmuth Michaelis' pcvt driver */
71 # define PCVT_CONSOLE_DEV "/dev/ttyv0"
73 # define PCVT_CONSOLE_DEV "/dev/ttyC0"
75 #define PCVT_CONSOLE_MODE O_RDWR|O_NDELAY
78 #if defined(WSCONS_SUPPORT) && defined(__NetBSD__)
79 /* NetBSD's new console driver */
80 #define WSCONS_PCVT_COMPAT_CONSOLE_DEV "/dev/ttyE0"
84 #define setpgrp setpgid
87 #define CHECK_DRIVER_MSG \
88 "Check your kernel's console driver configuration and /dev entries"
90 static char *supported_drivers
[] = {
92 "pccons (with X support)",
94 #ifdef SYSCONS_SUPPORT
100 #ifdef WSCONS_SUPPORT
107 * Functions to probe for the existance of a supported console driver.
108 * Any function returns either a valid file descriptor (driver probed
109 * succesfully), -1 (driver not found), or uses FatalError() if the
110 * driver was found but proved to not support the required mode to run
114 typedef int (*xf86ConsOpen_t
)(void);
116 #ifdef PCCONS_SUPPORT
117 static int xf86OpenPccons(void);
118 #endif /* PCCONS_SUPPORT */
120 #ifdef SYSCONS_SUPPORT
121 static int xf86OpenSyscons(void);
122 #endif /* SYSCONS_SUPPORT */
125 static int xf86OpenPcvt(void);
126 #endif /* PCVT_SUPPORT */
128 #ifdef WSCONS_SUPPORT
129 static int xf86OpenWScons(void);
133 * The sequence of the driver probes is important; start with the
134 * driver that is best distinguishable, and end with the most generic
135 * driver. (Otherwise, pcvt would also probe as syscons, and either
136 * pcvt or syscons might succesfully probe as pccons.)
138 static xf86ConsOpen_t xf86ConsTab
[] = {
142 #ifdef SYSCONS_SUPPORT
145 #ifdef PCCONS_SUPPORT
148 #ifdef WSCONS_SUPPORT
159 xf86ConsOpen_t
*driver
;
160 #if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
166 if (serverGeneration
== 1)
168 /* check if we are run with euid==0 */
171 FatalError("xf86OpenConsole: Server must be suid root");
177 * detaching the controlling tty solves problems of kbd character
178 * loss. This is not interesting for CO driver, because it is
181 setpgrp(0, getpid());
182 if ((i
= open("/dev/tty",O_RDWR
)) >= 0)
184 ioctl(i
,TIOCNOTTY
,(char *)0);
189 /* detect which driver we are running on */
190 for (driver
= xf86ConsTab
; *driver
; driver
++)
192 if ((fd
= (*driver
)()) >= 0)
196 /* Check that a supported console driver was found */
199 char cons_drivers
[80] = {0, };
200 for (i
= 0; i
< sizeof(supported_drivers
) / sizeof(char *); i
++)
204 strcat(cons_drivers
, ", ");
206 strcat(cons_drivers
, supported_drivers
[i
]);
209 "%s: No console driver found\n\tSupported drivers: %s\n\t%s",
210 "xf86OpenConsole", cons_drivers
, CHECK_DRIVER_MSG
);
212 #if 0 /* stdin is already closed in OsInit() */
215 xf86Info
.consoleFd
= fd
;
216 xf86Info
.screenFd
= fd
;
218 switch (xf86Info
.consType
)
220 #ifdef PCCONS_SUPPORT
222 if (ioctl (xf86Info
.consoleFd
, CONSOLE_X_MODE_ON
, 0) < 0)
224 FatalError("%s: CONSOLE_X_MODE_ON failed (%s)\n%s",
225 "xf86OpenConsole", strerror(errno
),
229 * Hack to prevent keyboard hanging when syslogd closes
232 if ((devConsoleFd
= open("/dev/console", O_WRONLY
,0)) < 0)
235 "xf86OpenConsole: couldn't open /dev/console (%s)\n",
240 #if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
242 /* as of FreeBSD 2.2.8, syscons driver does not need the #1 vt
243 * switching anymore. Here we check for FreeBSD 3.1 and up.
244 * Add cases for other *BSD that behave the same.
246 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
248 i
= atof(uts
.release
) * 100;
249 if (i
>= 310) goto acquire_vt
;
251 /* otherwise fall through */
254 * First activate the #1 VT. This is a hack to allow a server
255 * to be started while another one is active. There should be
258 if (initialVT
!= 1) {
260 if (ioctl(xf86Info
.consoleFd
, VT_ACTIVATE
, 1) != 0)
263 "xf86OpenConsole: VT_ACTIVATE failed\n");
273 ioctl(xf86Info
.consoleFd
, VT_ACTIVATE
, xf86Info
.vtno
));
276 xf86Msg(X_WARNING
, "xf86OpenConsole: VT_ACTIVATE failed\n");
279 ioctl(xf86Info
.consoleFd
, VT_WAITACTIVE
, xf86Info
.vtno
));
282 xf86Msg(X_WARNING
, "xf86OpenConsole: VT_WAITACTIVE failed\n");
285 signal(SIGUSR1
, xf86VTRequest
);
287 vtmode
.mode
= VT_PROCESS
;
288 vtmode
.relsig
= SIGUSR1
;
289 vtmode
.acqsig
= SIGUSR1
;
290 vtmode
.frsig
= SIGUSR1
;
291 if (ioctl(xf86Info
.consoleFd
, VT_SETMODE
, &vtmode
) < 0)
293 FatalError("xf86OpenConsole: VT_SETMODE VT_PROCESS failed");
295 #if !defined(USE_DEV_IO) && !defined(USE_I386_IOPL)
296 if (ioctl(xf86Info
.consoleFd
, KDENABIO
, 0) < 0)
298 FatalError("xf86OpenConsole: KDENABIO failed (%s)",
302 if (ioctl(xf86Info
.consoleFd
, KDSETMODE
, KD_GRAPHICS
) < 0)
304 FatalError("xf86OpenConsole: KDSETMODE KD_GRAPHICS failed");
307 #endif /* SYSCONS_SUPPORT || PCVT_SUPPORT */
308 #ifdef WSCONS_SUPPORT
310 fprintf(stderr
, "xf86OpenConsole\n");
311 /* xf86Info.consoleFd = open("/dev/wskbd0", 0); */
318 /* serverGeneration != 1 */
319 #if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
320 if (xf86Info
.consType
== SYSCONS
|| xf86Info
.consType
== PCVT
)
322 if (ioctl(xf86Info
.consoleFd
, VT_ACTIVATE
, xf86Info
.vtno
) != 0)
324 xf86Msg(X_WARNING
, "xf86OpenConsole: VT_ACTIVATE failed\n");
327 #endif /* SYSCONS_SUPPORT || PCVT_SUPPORT */
333 #ifdef PCCONS_SUPPORT
340 if ((fd
= open(PCCONS_CONSOLE_DEV1
, PCCONS_CONSOLE_MODE
, 0))
342 (fd
= open(PCCONS_CONSOLE_DEV2
, PCCONS_CONSOLE_MODE
, 0))
345 if (ioctl(fd
, CONSOLE_X_MODE_OFF
, 0) < 0)
348 "%s: CONSOLE_X_MODE_OFF failed (%s)\n%s\n%s",
351 "Was expecting pccons driver with X support",
354 xf86Info
.consType
= PCCONS
;
355 xf86Msg(X_PROBED
, "Using pccons driver with X support\n");
360 #endif /* PCCONS_SUPPORT */
362 #ifdef SYSCONS_SUPPORT
371 long syscons_version
;
374 /* Check for syscons */
375 if ((fd
= open(SYSCONS_CONSOLE_DEV1
, SYSCONS_CONSOLE_MODE
, 0)) >= 0
376 || (fd
= open(SYSCONS_CONSOLE_DEV2
, SYSCONS_CONSOLE_MODE
, 0)) >= 0)
378 if (ioctl(fd
, VT_GETMODE
, &vtmode
) >= 0)
380 /* Get syscons version */
381 if (ioctl(fd
, CONS_GETVERS
, &syscons_version
) < 0)
386 xf86Info
.vtno
= VTnum
;
390 if (ioctl(fd
, VT_GETACTIVE
, &initialVT
) < 0)
393 if (xf86Info
.vtno
== -1)
396 * For old syscons versions (<0x100), VT_OPENQRY returns
397 * the current VT rather than the next free VT. In this
398 * case, the server gets started on the current VT instead
399 * of the next free VT.
403 /* check for the fixed VT_OPENQRY */
404 if (syscons_version
>= 0x100)
407 if (ioctl(fd
, VT_OPENQRY
, &xf86Info
.vtno
) < 0)
416 if (xf86Info
.vtno
== -1)
419 * All VTs are in use. If initialVT was found, use it.
420 * Otherwise, if stdin is a VT, use that one.
421 * XXX stdin is already closed, so this won't work.
425 xf86Info
.vtno
= initialVT
;
427 else if ((fstat(0, &status
) >= 0)
428 && S_ISCHR(status
.st_mode
)
429 && (ioctl(0, VT_GETMODE
, &vtmode
) >= 0))
432 xf86Info
.vtno
= minor(status
.st_rdev
) + 1;
436 if (syscons_version
>= 0x100)
438 FatalError("%s: Cannot find a free VT",
441 /* Should no longer reach here */
442 FatalError("%s: %s %s\n\t%s %s",
444 "syscons versions prior to 1.0 require",
446 "server's stdin be a VT",
447 "or the use of the vtxx server option");
455 sprintf(vtname
, "/dev/ttyv%01x", xf86Info
.vtno
- 1);
457 sprintf(vtname
, "/dev/ttyC%01x", xf86Info
.vtno
- 1);
459 if ((fd
= open(vtname
, SYSCONS_CONSOLE_MODE
, 0)) < 0)
461 FatalError("xf86OpenSyscons: Cannot open %s (%s)",
462 vtname
, strerror(errno
));
464 if (ioctl(fd
, VT_GETMODE
, &vtmode
) < 0)
466 FatalError("xf86OpenSyscons: VT_GETMODE failed");
468 xf86Info
.consType
= SYSCONS
;
469 xf86Msg(X_PROBED
, "Using syscons driver with X support");
470 if (syscons_version
>= 0x100)
472 xf86ErrorF(" (version %ld.%ld)\n", syscons_version
>> 8,
473 syscons_version
& 0xFF);
477 xf86ErrorF(" (version 0.x)\n");
479 xf86Msg(from
, "using VT number %d\n\n", xf86Info
.vtno
);
483 /* VT_GETMODE failed, probably not syscons */
491 #endif /* SYSCONS_SUPPORT */
499 /* This looks much like syscons, since pcvt is API compatible */
502 char vtname
[12], *vtprefix
;
504 struct pcvtid pcvt_version
;
507 vtprefix
= "/dev/ttyv";
509 vtprefix
= "/dev/ttyC";
512 fd
= open(PCVT_CONSOLE_DEV
, PCVT_CONSOLE_MODE
, 0);
513 #ifdef WSCONS_PCVT_COMPAT_CONSOLE_DEV
516 fd
= open(WSCONS_PCVT_COMPAT_CONSOLE_DEV
, PCVT_CONSOLE_MODE
, 0);
517 vtprefix
= "/dev/ttyE";
522 if (ioctl(fd
, VGAPCVTID
, &pcvt_version
) >= 0)
524 if(ioctl(fd
, VT_GETMODE
, &vtmode
) < 0)
526 FatalError("%s: VT_GETMODE failed\n%s%s\n%s",
528 "Found pcvt driver but X11 seems to be",
529 " not supported.", CHECK_DRIVER_MSG
);
532 xf86Info
.vtno
= VTnum
;
534 if (ioctl(fd
, VT_GETACTIVE
, &initialVT
) < 0)
537 if (xf86Info
.vtno
== -1)
539 if (ioctl(fd
, VT_OPENQRY
, &xf86Info
.vtno
) < 0)
545 if (xf86Info
.vtno
== -1)
548 * All VTs are in use. If initialVT was found, use it.
549 * Otherwise, if stdin is a VT, use that one.
550 * XXX stdin is already closed, so this won't work.
554 xf86Info
.vtno
= initialVT
;
556 else if ((fstat(0, &status
) >= 0)
557 && S_ISCHR(status
.st_mode
)
558 && (ioctl(0, VT_GETMODE
, &vtmode
) >= 0))
561 xf86Info
.vtno
= minor(status
.st_rdev
) + 1;
565 FatalError("%s: Cannot find a free VT",
572 sprintf(vtname
, "%s%01x", vtprefix
, xf86Info
.vtno
- 1);
573 if ((fd
= open(vtname
, PCVT_CONSOLE_MODE
, 0)) < 0)
575 FatalError("xf86OpenPcvt: Cannot open %s (%s)",
576 vtname
, strerror(errno
));
578 if (ioctl(fd
, VT_GETMODE
, &vtmode
) < 0)
580 FatalError("xf86OpenPcvt: VT_GETMODE failed");
582 xf86Info
.consType
= PCVT
;
583 #ifdef WSCONS_SUPPORT
585 "Using wscons driver in pcvt compatibility mode "
587 pcvt_version
.rmajor
, pcvt_version
.rminor
);
589 xf86Msg(X_PROBED
, "Using pcvt driver (version %d.%d)\n",
590 pcvt_version
.rmajor
, pcvt_version
.rminor
);
603 #endif /* PCVT_SUPPORT */
605 #ifdef WSCONS_SUPPORT
611 int mode
= WSDISPLAYIO_MODE_MAPPED
;
615 /* XXX Is this ok? */
616 for (i
= 0; i
< 8; i
++) {
617 #if defined(__NetBSD__)
618 sprintf(ttyname
, "/dev/ttyE%d", i
);
619 #elif defined(__OpenBSD__)
620 sprintf(ttyname
, "/dev/ttyC%d", i
);
622 if ((fd
= open(ttyname
, 2)) != -1)
626 if (ioctl(fd
, WSDISPLAYIO_SMODE
, &mode
) < 0) {
627 FatalError("%s: WSDISPLAYIO_MODE_MAPPED failed (%s)\n%s",
628 "xf86OpenConsole", strerror(errno
),
631 xf86Info
.consType
= WSCONS
;
632 xf86Msg(X_PROBED
, "Using wscons driver\n");
637 #endif /* WSCONS_SUPPORT */
642 #if defined(SYSCONS_SUPPORT) || defined(PCVT_SUPPORT)
646 switch (xf86Info
.consType
)
648 #ifdef PCCONS_SUPPORT
650 ioctl (xf86Info
.consoleFd
, CONSOLE_X_MODE_OFF
, 0);
652 #endif /* PCCONS_SUPPORT */
653 #if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
656 ioctl(xf86Info
.consoleFd
, KDSETMODE
, KD_TEXT
); /* Back to text mode */
657 if (ioctl(xf86Info
.consoleFd
, VT_GETMODE
, &VT
) != -1)
660 ioctl(xf86Info
.consoleFd
, VT_SETMODE
, &VT
); /* dflt vt handling */
662 #if !defined(USE_DEV_IO) && !defined(USE_I386_IOPL)
663 if (ioctl(xf86Info
.consoleFd
, KDDISABIO
, 0) < 0)
665 xf86FatalError("xf86CloseConsole: KDDISABIO failed (%s)",
670 ioctl(xf86Info
.consoleFd
, VT_ACTIVATE
, initialVT
);
672 #endif /* SYSCONS_SUPPORT || PCVT_SUPPORT */
673 #ifdef WSCONS_SUPPORT
676 int mode
= WSDISPLAYIO_MODE_EMUL
;
677 ioctl(xf86Info
.screenFd
, WSDISPLAYIO_SMODE
, &mode
);
683 if (xf86Info
.screenFd
!= xf86Info
.consoleFd
)
685 close(xf86Info
.screenFd
);
686 close(xf86Info
.consoleFd
);
687 if ((xf86Info
.consoleFd
= open("/dev/console",O_RDONLY
,0)) <0)
689 xf86FatalError("xf86CloseConsole: Cannot open /dev/console (%s)",
693 close(xf86Info
.consoleFd
);
694 if (devConsoleFd
>= 0)
700 xf86ProcessArgument(int argc
, char *argv
[], int i
)
703 * Keep server from detaching from controlling tty. This is useful
704 * when debugging (so the server can receive keyboard signals.
706 if (!strcmp(argv
[i
], "-keeptty"))
711 #if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
712 if ((argv
[i
][0] == 'v') && (argv
[i
][1] == 't'))
714 if (sscanf(argv
[i
], "vt%2d", &VTnum
) == 0 ||
715 VTnum
< 1 || VTnum
> 12)
723 #endif /* SYSCONS_SUPPORT || PCVT_SUPPORT */
730 #if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
731 ErrorF("vtXX use the specified VT number (1-12)\n");
732 #endif /* SYSCONS_SUPPORT || PCVT_SUPPORT */
734 ErrorF("don't detach controlling tty (for debugging only)\n");