xref: /original-bsd/sys/hp/dev/cons.c (revision c8089215)
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department.
9  *
10  * %sccs.include.redist.c%
11  *
12  * from: Utah $Hdr: cons.c 1.1 90/07/09$
13  *
14  *	@(#)cons.c	7.5 (Berkeley) 12/16/90
15  */
16 
17 #include "sys/param.h"
18 #include "sys/user.h"
19 #include "sys/systm.h"
20 #include "sys/buf.h"
21 #include "sys/ioctl.h"
22 #include "sys/tty.h"
23 #include "sys/file.h"
24 #include "sys/conf.h"
25 
26 #include "cons.h"
27 
28 /* XXX - all this could be autoconfig()ed */
29 #include "ite.h"
30 #if NITE > 0
31 int itecnprobe(), itecninit(), itecngetc(), itecnputc();
32 #endif
33 #include "dca.h"
34 #if NDCA > 0
35 int dcacnprobe(), dcacninit(), dcacngetc(), dcacnputc();
36 #endif
37 #include "dcm.h"
38 #if NDCM > 0
39 int dcmcnprobe(), dcmcninit(), dcmcngetc(), dcmcnputc();
40 #endif
41 
42 struct	consdev constab[] = {
43 #if NITE > 0
44 	{ itecnprobe,	itecninit,	itecngetc,	itecnputc },
45 #endif
46 #if NDCA > 0
47 	{ dcacnprobe,	dcacninit,	dcacngetc,	dcacnputc },
48 #endif
49 #if NDCM > 0
50 	{ dcmcnprobe,	dcmcninit,	dcmcngetc,	dcmcnputc },
51 #endif
52 	{ 0 },
53 };
54 /* end XXX */
55 
56 struct	tty *constty = 0;	/* virtual console output device */
57 struct	consdev *cn_tab;	/* physical console device info */
58 struct	tty *cn_tty;		/* XXX: console tty struct for tprintf */
59 
60 cninit()
61 {
62 	register struct consdev *cp;
63 
64 	/*
65 	 * Collect information about all possible consoles
66 	 * and find the one with highest priority
67 	 */
68 	for (cp = constab; cp->cn_probe; cp++) {
69 		(*cp->cn_probe)(cp);
70 		if (cp->cn_pri > CN_DEAD &&
71 		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
72 			cn_tab = cp;
73 	}
74 	/*
75 	 * No console, we can handle it
76 	 */
77 	if ((cp = cn_tab) == NULL)
78 		return;
79 	/*
80 	 * Turn on console
81 	 */
82 	cn_tty = cp->cn_tp;
83 	(*cp->cn_init)(cp);
84 }
85 
86 cnopen(dev, flag)
87 	dev_t dev;
88 {
89 	if (cn_tab == NULL)
90 		return(0);
91 	dev = cn_tab->cn_dev;
92 	return ((*cdevsw[major(dev)].d_open)(dev, flag));
93 }
94 
95 cnclose(dev, flag)
96 	dev_t dev;
97 {
98 	if (cn_tab == NULL)
99 		return(0);
100 	dev = cn_tab->cn_dev;
101 	return ((*cdevsw[major(dev)].d_close)(dev, flag));
102 }
103 
104 cnread(dev, uio, flag)
105 	dev_t dev;
106 	struct uio *uio;
107 {
108 	if (cn_tab == NULL)
109 		return(0);
110 	dev = cn_tab->cn_dev;
111 	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
112 }
113 
114 cnwrite(dev, uio, flag)
115 	dev_t dev;
116 	struct uio *uio;
117 {
118 	if (cn_tab == NULL)
119 		return(0);
120 	dev = cn_tab->cn_dev;
121 	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
122 }
123 
124 cnioctl(dev, cmd, data, flag)
125 	dev_t dev;
126 	caddr_t data;
127 {
128 	int error;
129 
130 	if (cn_tab == NULL)
131 		return(0);
132 	/*
133 	 * Superuser can always use this to wrest control of console
134 	 * output from the "virtual" console.
135 	 */
136 	if (cmd == TIOCCONS && constty) {
137 		error = suser(u.u_cred, &u.u_acflag);
138 		if (error)
139 			return (error);
140 		constty = NULL;
141 		return (0);
142 	}
143 	dev = cn_tab->cn_dev;
144 	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag));
145 }
146 
147 /*ARGSUSED*/
148 cnselect(dev, rw)
149 	dev_t dev;
150 	int rw;
151 {
152 	if (cn_tab == NULL)
153 		return(1);
154 	return(ttselect(cn_tab->cn_dev, rw));
155 }
156 
157 cngetc()
158 {
159 	if (cn_tab == NULL)
160 		return(0);
161 	return((*cn_tab->cn_getc)(cn_tab->cn_dev));
162 }
163 
164 cnputc(c)
165 	register int c;
166 {
167 	if (cn_tab == NULL)
168 		return;
169 	if (c) {
170 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
171 		if (c == '\n')
172 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
173 	}
174 }
175