kern_cons.c revision 7680
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.24 1995/04/02 16:14:51 joerg 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 - all this could be autoconfig()ed */ 52#include "sc.h" 53#include "vt.h" 54#if NSC > 0 || NVT > 0 55int pccnprobe(), pccninit(), pccngetc(), pccncheckc(), pccnputc(); 56#endif 57 58#include "sio.h" 59#if NSIO > 0 60int siocnprobe(), siocninit(), siocngetc(), siocncheckc(), siocnputc(); 61#endif 62 63static struct consdev constab[] = { 64#if NSC > 0 || NVT > 0 65 { pccnprobe, pccninit, pccngetc, pccncheckc, pccnputc }, 66#endif 67#if NSIO > 0 68 { siocnprobe, siocninit, siocngetc, siocncheckc, siocnputc }, 69#endif 70 { 0 }, 71}; 72/* end XXX */ 73 74struct tty *constty = 0; /* virtual console output device */ 75struct tty *cn_tty; /* XXX: console tty struct for tprintf */ 76int cons_unavail = 0; /* XXX: 77 * physical console not available for 78 * input (i.e., it is in graphics mode) 79 */ 80 81static u_char cn_is_open; /* nonzero if logical console is open */ 82static u_char cn_phys_is_open; /* nonzero if physical console is open */ 83static d_close_t *cn_phys_close; /* physical device close function */ 84static d_open_t *cn_phys_open; /* physical device open function */ 85static struct consdev *cn_tab; /* physical console device info */ 86static struct tty *cn_tp; /* physical console tty struct */ 87 88void 89cninit() 90{ 91 register struct consdev *cp; 92 struct cdevsw *cdp; 93 94 /* 95 * Collect information about all possible consoles 96 * and find the one with highest priority 97 */ 98 for (cp = constab; cp->cn_probe; cp++) { 99 (*cp->cn_probe)(cp); 100 if (cp->cn_pri > CN_DEAD && 101 (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri)) 102 cn_tab = cp; 103 } 104 /* 105 * No console, give up. 106 */ 107 if ((cp = cn_tab) == NULL) 108 return; 109 /* 110 * Hook the open and close functions. 111 */ 112 cdp = &cdevsw[major(cn_tab->cn_dev)]; 113 cn_phys_close = cdp->d_close; 114 cdp->d_close = cnclose; 115 cn_phys_open = cdp->d_open; 116 cdp->d_open = cnopen; 117 cn_tp = (*cdp->d_devtotty)(cn_tab->cn_dev); 118 /* 119 * Turn on console 120 */ 121 cn_tty = cp->cn_tp; 122 (*cp->cn_init)(cp); 123} 124 125int 126cnopen(dev, flag, mode, p) 127 dev_t dev; 128 int flag, mode; 129 struct proc *p; 130{ 131 dev_t cndev, physdev; 132 int retval; 133 134 if (cn_tab == NULL) 135 return (0); 136 cndev = cn_tab->cn_dev; 137 physdev = (major(dev) == major(cndev) ? dev : cndev); 138 retval = (*cn_phys_open)(physdev, flag, mode, p); 139 if (retval == 0) { 140 if (dev == cndev) 141 cn_phys_is_open = 1; 142 else if (physdev == cndev) 143 cn_is_open = 1; 144 } 145 return (retval); 146} 147 148int 149cnclose(dev, flag, mode, p) 150 dev_t dev; 151 int flag, mode; 152 struct proc *p; 153{ 154 dev_t cndev; 155 156 if (cn_tab == NULL) 157 return (0); 158 cndev = cn_tab->cn_dev; 159 if (dev == cndev) { 160 /* the physical device is about to be closed */ 161 cn_phys_is_open = 0; 162 if (cn_is_open) { 163 if (cn_tp) { 164 /* perform a ttyhalfclose() */ 165 /* reset session and proc group */ 166 cn_tp->t_pgrp = NULL; 167 cn_tp->t_session = NULL; 168 } 169 return (0); 170 } 171 } else if (major(dev) != major(cndev)) { 172 /* the logical console is about to be closed */ 173 cn_is_open = 0; 174 if (cn_phys_is_open) 175 return (0); 176 dev = cndev; 177 } 178 return ((*cn_phys_close)(dev, flag, mode, p)); 179} 180 181int 182cnread(dev, uio, flag) 183 dev_t dev; 184 struct uio *uio; 185 int flag; 186{ 187 if (cn_tab == NULL) 188 return (0); 189 dev = cn_tab->cn_dev; 190 return ((*cdevsw[major(dev)].d_read)(dev, uio, flag)); 191} 192 193int 194cnwrite(dev, uio, flag) 195 dev_t dev; 196 struct uio *uio; 197 int flag; 198{ 199 if (cn_tab == NULL) 200 return (0); 201 if (constty) 202 dev = constty->t_dev; 203 else 204 dev = cn_tab->cn_dev; 205 return ((*cdevsw[major(dev)].d_write)(dev, uio, flag)); 206} 207 208int 209cnioctl(dev, cmd, data, flag, p) 210 dev_t dev; 211 int cmd; 212 caddr_t data; 213 int flag; 214 struct proc *p; 215{ 216 int error; 217 218 if (cn_tab == NULL) 219 return (0); 220 /* 221 * Superuser can always use this to wrest control of console 222 * output from the "virtual" console. 223 */ 224 if (cmd == TIOCCONS && constty) { 225 error = suser(p->p_ucred, (u_short *) NULL); 226 if (error) 227 return (error); 228 constty = NULL; 229 return (0); 230 } 231 dev = cn_tab->cn_dev; 232 return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p)); 233} 234 235/*ARGSUSED*/ 236int 237cnselect(dev, rw, p) 238 dev_t dev; 239 int rw; 240 struct proc *p; 241{ 242 if (cn_tab == NULL) 243 return (1); 244 245 dev = cn_tab->cn_dev; 246 247 return ((*cdevsw[major(dev)].d_select)(dev, rw, p)); 248} 249 250int 251cngetc() 252{ 253 int c; 254 if (cn_tab == NULL) 255 return (0); 256 c = (*cn_tab->cn_getc)(cn_tab->cn_dev); 257 if (c == '\r') c = '\n'; /* console input is always ICRNL */ 258 return (c); 259} 260 261int 262cncheckc() 263{ 264 if (cn_tab == NULL) 265 return (0); 266 return ((*cn_tab->cn_checkc)(cn_tab->cn_dev)); 267} 268 269void 270cnputc(c) 271 register int c; 272{ 273 if (cn_tab == NULL) 274 return; 275 if (c) { 276 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 277 if (c == '\n') 278 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 279 } 280} 281 282int 283pg(const char *p, ...) { 284 va_list args; 285 va_start(args, p); 286 printf("%r\n>", p, args); 287 return(cngetc()); 288} 289 290 291