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.8 1993/07/07 11:00:23 deraadt Exp $ 40 */ 41 42 43 #include "param.h" 44 #include "proc.h" 45 #include "user.h" 46 #include "systm.h" 47 #include "buf.h" 48 #include "ioctl.h" 49 #include "tty.h" 50 #include "file.h" 51 #include "conf.h" 52 53 #include "cons.h" 54 55 /* XXX - all this could be autoconfig()ed */ 56 #include "pc.h" 57 #if NPC > 0 58 int pccnprobe(), pccninit(), pccngetc(), pccnputc(); 59 #endif 60 #include "com.h" 61 #if NCOM > 0 62 int comcnprobe(), comcninit(), comcngetc(), comcnputc(); 63 #endif 64 65 struct consdev constab[] = { 66 #if NPC > 0 67 { pccnprobe, pccninit, pccngetc, pccnputc }, 68 #endif 69 #if NCOM > 0 70 { comcnprobe, comcninit, comcngetc, comcnputc }, 71 #endif 72 { 0 }, 73 }; 74 /* end XXX */ 75 76 struct tty *constty = 0; /* virtual console output device */ 77 struct consdev *cn_tab; /* physical console device info */ 78 79 void 80 consinit() 81 { 82 } 83 84 void 85 cninit() 86 { 87 register struct consdev *cp; 88 89 /* 90 * Collect information about all possible consoles 91 * and find the one with highest priority 92 */ 93 for (cp = constab; cp->cn_probe; cp++) { 94 (*cp->cn_probe)(cp); 95 if (cp->cn_pri > CN_DEAD && 96 (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri)) 97 cn_tab = cp; 98 } 99 /* 100 * No console, we can handle it 101 */ 102 if ((cp = cn_tab) == NULL) 103 return; 104 /* 105 * Turn on console 106 */ 107 (*cp->cn_init)(cp); 108 } 109 110 int 111 cnopen(dev, flag, mode, p) 112 dev_t dev; 113 int flag, mode; 114 struct proc *p; 115 { 116 if (cn_tab == NULL) 117 return (0); 118 dev = cn_tab->cn_dev; 119 return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p)); 120 } 121 122 int 123 cnclose(dev, flag, mode, p) 124 dev_t dev; 125 int flag, mode; 126 struct proc *p; 127 { 128 if (cn_tab == NULL) 129 return (0); 130 dev = cn_tab->cn_dev; 131 return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p)); 132 } 133 134 int 135 cnread(dev, uio, flag) 136 dev_t dev; 137 struct uio *uio; 138 { 139 if (cn_tab == NULL) 140 return (0); 141 dev = cn_tab->cn_dev; 142 return ((*cdevsw[major(dev)].d_read)(dev, uio, flag)); 143 } 144 145 int 146 cnwrite(dev, uio, flag) 147 dev_t dev; 148 struct uio *uio; 149 { 150 if (cn_tab == NULL) 151 return (0); 152 if (constty) /* 16 Aug 92*/ 153 dev = constty->t_dev; 154 else 155 dev = cn_tab->cn_dev; 156 return ((*cdevsw[major(dev)].d_write)(dev, uio, flag)); 157 } 158 159 int 160 cnioctl(dev, cmd, data, flag, p) 161 dev_t dev; 162 caddr_t data; 163 struct proc *p; 164 { 165 int error; 166 167 if (cn_tab == NULL) 168 return (0); 169 /* 170 * Superuser can always use this to wrest control of console 171 * output from the "virtual" console. 172 */ 173 if (cmd == TIOCCONS && constty) { 174 error = suser(p->p_ucred, (u_short *) NULL); 175 if (error) 176 return (error); 177 constty = NULL; 178 return (0); 179 } 180 dev = cn_tab->cn_dev; 181 return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p)); 182 } 183 184 /*ARGSUSED*/ 185 int 186 cnselect(dev, rw, p) 187 dev_t dev; 188 int rw; 189 struct proc *p; 190 { 191 if (cn_tab == NULL) 192 return (1); 193 return (ttselect(cn_tab->cn_dev, rw, p)); 194 } 195 196 int 197 cngetc() 198 { 199 if (cn_tab == NULL) 200 return (0); 201 return ((*cn_tab->cn_getc)(cn_tab->cn_dev)); 202 } 203 204 int 205 cnputc(c) 206 register int c; 207 { 208 if (cn_tab == NULL) 209 return; 210 if (c) { 211 (*cn_tab->cn_putc)(cn_tab->cn_dev, c); 212 if (c == '\n') 213 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r'); 214 } 215 } 216 217 int pg_wait = 0; 218 pg(p,q,r,s,t,u,v,w,x,y,z) char *p; { 219 printf(p,q,r,s,t,u,v,w,x,y,z); 220 if (pg_wait) { 221 printf("\n>"); 222 return(cngetc()); 223 } else 224 return 0; 225 } 226