Home | History | Annotate | Line # | Download | only in dev
cons.c revision 1.7
      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.7 1993/06/27 06:02:49 andrew 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 struct	tty *cn_tty;		/* XXX: console tty struct for tprintf */
     79 
     80 void
     81 consinit()
     82 {
     83 }
     84 
     85 void
     86 cninit()
     87 {
     88 	register struct consdev *cp;
     89 
     90 	/*
     91 	 * Collect information about all possible consoles
     92 	 * and find the one with highest priority
     93 	 */
     94 	for (cp = constab; cp->cn_probe; cp++) {
     95 		(*cp->cn_probe)(cp);
     96 		if (cp->cn_pri > CN_DEAD &&
     97 		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
     98 			cn_tab = cp;
     99 	}
    100 	/*
    101 	 * No console, we can handle it
    102 	 */
    103 	if ((cp = cn_tab) == NULL)
    104 		return;
    105 	/*
    106 	 * Turn on console
    107 	 */
    108 	cn_tty = cp->cn_tp;
    109 	(*cp->cn_init)(cp);
    110 }
    111 
    112 int
    113 cnopen(dev, flag, mode, p)
    114 	dev_t dev;
    115 	int flag, mode;
    116 	struct proc *p;
    117 {
    118 	if (cn_tab == NULL)
    119 		return (0);
    120 	dev = cn_tab->cn_dev;
    121 	return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p));
    122 }
    123 
    124 int
    125 cnclose(dev, flag, mode, p)
    126 	dev_t dev;
    127 	int flag, mode;
    128 	struct proc *p;
    129 {
    130 	if (cn_tab == NULL)
    131 		return (0);
    132 	dev = cn_tab->cn_dev;
    133 	return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p));
    134 }
    135 
    136 int
    137 cnread(dev, uio, flag)
    138 	dev_t dev;
    139 	struct uio *uio;
    140 {
    141 	if (cn_tab == NULL)
    142 		return (0);
    143 	dev = cn_tab->cn_dev;
    144 	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
    145 }
    146 
    147 int
    148 cnwrite(dev, uio, flag)
    149 	dev_t dev;
    150 	struct uio *uio;
    151 {
    152 	if (cn_tab == NULL)
    153 		return (0);
    154 	if (constty)					/* 16 Aug 92*/
    155 		dev = constty->t_dev;
    156 	else
    157 		dev = cn_tab->cn_dev;
    158 	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
    159 }
    160 
    161 int
    162 cnioctl(dev, cmd, data, flag, p)
    163 	dev_t dev;
    164 	caddr_t data;
    165 	struct proc *p;
    166 {
    167 	int error;
    168 
    169 	if (cn_tab == NULL)
    170 		return (0);
    171 	/*
    172 	 * Superuser can always use this to wrest control of console
    173 	 * output from the "virtual" console.
    174 	 */
    175 	if (cmd == TIOCCONS && constty) {
    176 		error = suser(p->p_ucred, (u_short *) NULL);
    177 		if (error)
    178 			return (error);
    179 		constty = NULL;
    180 		return (0);
    181 	}
    182 	dev = cn_tab->cn_dev;
    183 	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
    184 }
    185 
    186 /*ARGSUSED*/
    187 int
    188 cnselect(dev, rw, p)
    189 	dev_t dev;
    190 	int rw;
    191 	struct proc *p;
    192 {
    193 	if (cn_tab == NULL)
    194 		return (1);
    195 	return (ttselect(cn_tab->cn_dev, rw, p));
    196 }
    197 
    198 int
    199 cngetc()
    200 {
    201 	if (cn_tab == NULL)
    202 		return (0);
    203 	return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
    204 }
    205 
    206 int
    207 cnputc(c)
    208 	register int c;
    209 {
    210 	if (cn_tab == NULL)
    211 		return;
    212 	if (c) {
    213 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
    214 		if (c == '\n')
    215 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
    216 	}
    217 }
    218 
    219 int pg_wait = 0;
    220 pg(p,q,r,s,t,u,v,w,x,y,z) char *p; {
    221 	printf(p,q,r,s,t,u,v,w,x,y,z);
    222 	if (pg_wait) {
    223 		printf("\n>");
    224 		return(cngetc());
    225 	} else
    226 		return 0;
    227 }
    228