kern_cons.c revision 9326
1/* 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1991 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 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * from: @(#)cons.c 7.2 (Berkeley) 5/9/91 39 * $Id: cons.c,v 1.29 1995/06/14 04:52:39 bde Exp $ 40 */ 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/conf.h> 45#include <sys/proc.h> 46#include <sys/tty.h> 47 48#include <machine/cons.h> 49#include <machine/stdarg.h> 50 51/* XXX this should be config(8)ed. */ 52#include "sc.h" 53#include "vt.h" 54#include "sio.h" 55static struct consdev constab[] = { 56#if NSC > 0 || NVT > 0 57 { pccnprobe, pccninit, pccngetc, pccncheckc, pccnputc }, 58#endif 59#if NSIO > 0 60 { siocnprobe, siocninit, siocngetc, siocncheckc, siocnputc }, 61#endif 62 { 0 }, 63}; 64 65struct tty *constty = 0; /* virtual console output device */ 66struct tty *cn_tty; /* XXX: console tty struct for tprintf */ 67int cons_unavail = 0; /* XXX: 68 * physical console not available for 69 * input (i.e., it is in graphics mode) 70 */ 71 72static u_char cn_is_open; /* nonzero if logical console is open */ 73static u_char cn_phys_is_open; /* nonzero if physical console is open */ 74static d_close_t *cn_phys_close; /* physical device close function */ 75static d_open_t *cn_phys_open; /* physical device open function */ 76static struct consdev *cn_tab; /* physical console device info */ 77static struct tty *cn_tp; /* physical console tty struct */ 78 79void 80cninit() 81{ 82 register struct consdev *cp; 83 struct cdevsw *cdp; 84 85 /* 86 * Collect information about all possible consoles 87 * and find the one with highest priority 88 */ 89 for (cp = constab; cp->cn_probe; cp++) { 90 (*cp->cn_probe)(cp); 91 if (cp->cn_pri > CN_DEAD && 92 (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri)) 93 cn_tab = cp; 94 } 95 /* 96 * No console, give up. 97 */ 98 if ((cp = cn_tab) == NULL) 99 return; 100 /* 101 * Hook the open and close functions. 102 */ 103 cdp = &cdevsw[major(cn_tab->cn_dev)]; 104 cn_phys_close = cdp->d_close; 105 cdp->d_close = cnclose; 106 cn_phys_open = cdp->d_open; 107 cdp->d_open = cnopen; 108 cn_tp = (*cdp->d_devtotty)(cn_tab->cn_dev); 109 /* 110 * XXX there are too many tty pointers. cn_tty is only used for 111 * sysctl(CPU_CONSDEV) (not for tprintf like the above comment 112 * says). cn_tp in struct consdev hasn't been initialized 113 * (except statically to NULL) or used (except to initialize 114 * cn_tty to the wrong value) for a year or two. 115 */ 116 cn_tty = cn_tp; 117 /* 118 * Turn on console 119 */ 120 (*cp->cn_init)(cp); 121} 122 123int 124cnopen(dev, flag, mode, p) 125 dev_t dev; 126 int flag, mode; 127 struct proc *p; 128{ 129 dev_t cndev, physdev; 130 int retval; 131 132 if (cn_tab == NULL) 133 return (0); 134 cndev = cn_tab->cn_dev; 135 physdev = (major(dev) == major(cndev) ? dev : cndev); 136 retval = (*cn_phys_open)(physdev, flag, mode, p); 137 if (retval == 0) { 138 if (dev == cndev) 139 cn_phys_is_open = 1; 140 else if (physdev == cndev) 141 cn_is_open = 1; 142 } 143 return (retval); 144} 145 146int 147cnclose(dev, flag, mode, p) 148 dev_t dev; 149 int flag, mode; 150 struct proc *p; 151{ 152 dev_t cndev; 153 154 if (cn_tab == NULL) 155 return (0); 156 cndev = cn_tab->cn_dev; 157 if (dev == cndev) { 158 /* the physical device is about to be closed */ 159 cn_phys_is_open = 0; 160 if (cn_is_open) { 161 if (cn_tp) { 162 /* perform a ttyhalfclose() */ 163 /* reset session and proc group */ 164 cn_tp->t_pgrp = NULL; 165 cn_tp->t_session = NULL; 166 } 167 return (0); 168 } 169 } else if (major(dev) != major(cndev)) { 170 /* the logical console is about to be closed */ 171 cn_is_open = 0; 172 if (cn_phys_is_open) 173 return (0); 174 dev = cndev; 175 } 176 return ((*cn_phys_close)(dev, flag, mode, p)); 177} 178 179int 180cnread(dev, uio, flag) 181 dev_t dev; 182 struct uio *uio; 183 int flag; 184{ 185 if (cn_tab == NULL) 186 return (0); 187 dev = cn_tab->cn_dev; 188 return ((*cdevsw[major(dev)].d_read)(dev, uio, flag)); 189} 190 191int 192cnwrite(dev, uio, flag) 193 dev_t dev; 194 struct uio *uio; 195 int flag; 196{ 197 if (cn_tab == NULL) 198 return (0); 199 if (constty) 200 dev = constty->t_dev; 201 else 202 dev = cn_tab->cn_dev; 203 return ((*cdevsw[major(dev)].d_write)(dev, uio, flag)); 204} 205 206int 207cnioctl(dev, cmd, data, flag, p) 208 dev_t dev; 209 int cmd; 210 caddr_t data; 211 int flag; 212 struct proc *p; 213{ 214 int error; 215 216 if (cn_tab == NULL) 217 return (0); 218 /* 219 * Superuser can always use this to wrest control of console 220 * output from the "virtual" console. 221 */ 222 if (cmd == TIOCCONS && constty) { 223 error = suser(p->p_ucred, (u_short *) NULL); 224 if (error) 225 return (error); 226 constty = NULL; 227 return (0); 228 } 229 dev = cn_tab->cn_dev; 230 return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p)); 231} 232 233/*ARGSUSED*/ 234int 235cnselect(dev, rw, p) 236 dev_t dev; 237 int rw; 238 struct proc *p; 239{ 240 if (cn_tab == NULL) 241 return (1); 242 243 dev = cn_tab->cn_dev; 244 245 return ((*cdevsw[major(dev)].d_select)(dev, rw, p)); 246} 247 248int 249cngetc() 250{ 251 int c; 252 if (cn_tab == NULL) 253 return (0); 254 c = (*cn_tab->cn_getc)(cn_tab->cn_dev); 255 if (c == '\r') c = '\n'; /* console input is always ICRNL */ 256 return (c); 257} 258 259int 260cncheckc() 261{ 262 if (cn_tab == NULL) 263 return (0); 264 return ((*cn_tab->cn_checkc)(cn_tab->cn_dev)); 265} 266 267void 268cnputc(c) 269 register int c; 270{ 271 if (cn_tab == NULL) 272 return; 273 if (c) { 274 if (c == '\n') 275 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 276 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 277 } 278} 279 280int 281pg(const char *p, ...) { 282 va_list args; 283 va_start(args, p); 284 printf("%r\n>", p, args); 285 return(cngetc()); 286} 287 288 289