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