Home | History | Annotate | Line # | Download | only in dev
cons.c revision 1.1
      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  *	@(#)cons.c	7.2 (Berkeley) 5/9/91
     39  */
     40 
     41 
     42 #include "sys/param.h"
     43 #include "sys/proc.h"
     44 #include "sys/user.h"
     45 #include "sys/systm.h"
     46 #include "sys/buf.h"
     47 #include "sys/ioctl.h"
     48 #include "sys/tty.h"
     49 #include "sys/file.h"
     50 #include "sys/conf.h"
     51 
     52 #include "cons.h"
     53 
     54 /* XXX - all this could be autoconfig()ed */
     55 int pccnprobe(), pccninit(), pccngetc(), pccnputc();
     56 #include "com.h"
     57 #if NCOM > 0
     58 int comcnprobe(), comcninit(), comcngetc(), comcnputc();
     59 #endif
     60 
     61 struct	consdev constab[] = {
     62 	{ pccnprobe,	pccninit,	pccngetc,	pccnputc },
     63 #if NCOM > 0
     64 	{ comcnprobe,	comcninit,	comcngetc,	comcnputc },
     65 #endif
     66 	{ 0 },
     67 };
     68 /* end XXX */
     69 
     70 struct	tty *constty = 0;	/* virtual console output device */
     71 struct	consdev *cn_tab;	/* physical console device info */
     72 struct	tty *cn_tty;		/* XXX: console tty struct for tprintf */
     73 
     74 cninit()
     75 {
     76 	register struct consdev *cp;
     77 
     78 	/*
     79 	 * Collect information about all possible consoles
     80 	 * and find the one with highest priority
     81 	 */
     82 	for (cp = constab; cp->cn_probe; cp++) {
     83 		(*cp->cn_probe)(cp);
     84 		if (cp->cn_pri > CN_DEAD &&
     85 		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
     86 			cn_tab = cp;
     87 	}
     88 	/*
     89 	 * No console, we can handle it
     90 	 */
     91 	if ((cp = cn_tab) == NULL)
     92 		return;
     93 	/*
     94 	 * Turn on console
     95 	 */
     96 	cn_tty = cp->cn_tp;
     97 	(*cp->cn_init)(cp);
     98 }
     99 
    100 cnopen(dev, flag, mode, p)
    101 	dev_t dev;
    102 	int flag, mode;
    103 	struct proc *p;
    104 {
    105 	if (cn_tab == NULL)
    106 		return (0);
    107 	dev = cn_tab->cn_dev;
    108 	return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p));
    109 }
    110 
    111 cnclose(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_close)(dev, flag, mode, p));
    120 }
    121 
    122 cnread(dev, uio, flag)
    123 	dev_t dev;
    124 	struct uio *uio;
    125 {
    126 	if (cn_tab == NULL)
    127 		return (0);
    128 	dev = cn_tab->cn_dev;
    129 	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
    130 }
    131 
    132 cnwrite(dev, uio, flag)
    133 	dev_t dev;
    134 	struct uio *uio;
    135 {
    136 	if (cn_tab == NULL)
    137 		return (0);
    138 	dev = cn_tab->cn_dev;
    139 	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
    140 }
    141 
    142 cnioctl(dev, cmd, data, flag, p)
    143 	dev_t dev;
    144 	caddr_t data;
    145 	struct proc *p;
    146 {
    147 	int error;
    148 
    149 	if (cn_tab == NULL)
    150 		return (0);
    151 	/*
    152 	 * Superuser can always use this to wrest control of console
    153 	 * output from the "virtual" console.
    154 	 */
    155 	if (cmd == TIOCCONS && constty) {
    156 		error = suser(p->p_ucred, (u_short *) NULL);
    157 		if (error)
    158 			return (error);
    159 		constty = NULL;
    160 		return (0);
    161 	}
    162 	dev = cn_tab->cn_dev;
    163 	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
    164 }
    165 
    166 /*ARGSUSED*/
    167 cnselect(dev, rw, p)
    168 	dev_t dev;
    169 	int rw;
    170 	struct proc *p;
    171 {
    172 	if (cn_tab == NULL)
    173 		return (1);
    174 	return (ttselect(cn_tab->cn_dev, rw, p));
    175 }
    176 
    177 cngetc()
    178 {
    179 	if (cn_tab == NULL)
    180 		return (0);
    181 	return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
    182 }
    183 
    184 cnputc(c)
    185 	register int c;
    186 {
    187 	if (cn_tab == NULL)
    188 		return;
    189 	if (c) {
    190 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
    191 		if (c == '\n')
    192 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
    193 	}
    194 }
    195