Home | History | Annotate | Line # | Download | only in dev
cons.c revision 1.7
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1988 University of Utah.
      3  1.1      cgd  * Copyright (c) 1991 The Regents of the University of California.
      4  1.1      cgd  * All rights reserved.
      5  1.1      cgd  *
      6  1.1      cgd  * This code is derived from software contributed to Berkeley by
      7  1.1      cgd  * the Systems Programming Group of the University of Utah Computer
      8  1.1      cgd  * Science Department.
      9  1.1      cgd  *
     10  1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1      cgd  * modification, are permitted provided that the following conditions
     12  1.1      cgd  * are met:
     13  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1      cgd  *    must display the following acknowledgement:
     20  1.1      cgd  *	This product includes software developed by the University of
     21  1.1      cgd  *	California, Berkeley and its contributors.
     22  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1      cgd  *    may be used to endorse or promote products derived from this software
     24  1.1      cgd  *    without specific prior written permission.
     25  1.1      cgd  *
     26  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1      cgd  * SUCH DAMAGE.
     37  1.1      cgd  *
     38  1.4      cgd  *	from: @(#)cons.c	7.2 (Berkeley) 5/9/91
     39  1.7   andrew  *	$Id: cons.c,v 1.7 1993/06/27 06:02:49 andrew Exp $
     40  1.1      cgd  */
     41  1.1      cgd 
     42  1.1      cgd 
     43  1.4      cgd #include "param.h"
     44  1.4      cgd #include "proc.h"
     45  1.4      cgd #include "user.h"
     46  1.4      cgd #include "systm.h"
     47  1.4      cgd #include "buf.h"
     48  1.4      cgd #include "ioctl.h"
     49  1.4      cgd #include "tty.h"
     50  1.4      cgd #include "file.h"
     51  1.4      cgd #include "conf.h"
     52  1.1      cgd 
     53  1.1      cgd #include "cons.h"
     54  1.1      cgd 
     55  1.1      cgd /* XXX - all this could be autoconfig()ed */
     56  1.6  deraadt #include "pc.h"
     57  1.6  deraadt #if NPC > 0
     58  1.1      cgd int pccnprobe(), pccninit(), pccngetc(), pccnputc();
     59  1.6  deraadt #endif
     60  1.1      cgd #include "com.h"
     61  1.1      cgd #if NCOM > 0
     62  1.1      cgd int comcnprobe(), comcninit(), comcngetc(), comcnputc();
     63  1.1      cgd #endif
     64  1.1      cgd 
     65  1.1      cgd struct	consdev constab[] = {
     66  1.6  deraadt #if NPC > 0
     67  1.1      cgd 	{ pccnprobe,	pccninit,	pccngetc,	pccnputc },
     68  1.6  deraadt #endif
     69  1.1      cgd #if NCOM > 0
     70  1.1      cgd 	{ comcnprobe,	comcninit,	comcngetc,	comcnputc },
     71  1.1      cgd #endif
     72  1.1      cgd 	{ 0 },
     73  1.1      cgd };
     74  1.1      cgd /* end XXX */
     75  1.1      cgd 
     76  1.1      cgd struct	tty *constty = 0;	/* virtual console output device */
     77  1.1      cgd struct	consdev *cn_tab;	/* physical console device info */
     78  1.1      cgd struct	tty *cn_tty;		/* XXX: console tty struct for tprintf */
     79  1.6  deraadt 
     80  1.7   andrew void
     81  1.6  deraadt consinit()
     82  1.6  deraadt {
     83  1.6  deraadt }
     84  1.1      cgd 
     85  1.7   andrew void
     86  1.1      cgd cninit()
     87  1.1      cgd {
     88  1.1      cgd 	register struct consdev *cp;
     89  1.1      cgd 
     90  1.1      cgd 	/*
     91  1.1      cgd 	 * Collect information about all possible consoles
     92  1.1      cgd 	 * and find the one with highest priority
     93  1.1      cgd 	 */
     94  1.1      cgd 	for (cp = constab; cp->cn_probe; cp++) {
     95  1.1      cgd 		(*cp->cn_probe)(cp);
     96  1.1      cgd 		if (cp->cn_pri > CN_DEAD &&
     97  1.1      cgd 		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
     98  1.1      cgd 			cn_tab = cp;
     99  1.1      cgd 	}
    100  1.1      cgd 	/*
    101  1.1      cgd 	 * No console, we can handle it
    102  1.1      cgd 	 */
    103  1.1      cgd 	if ((cp = cn_tab) == NULL)
    104  1.1      cgd 		return;
    105  1.1      cgd 	/*
    106  1.1      cgd 	 * Turn on console
    107  1.1      cgd 	 */
    108  1.1      cgd 	cn_tty = cp->cn_tp;
    109  1.1      cgd 	(*cp->cn_init)(cp);
    110  1.1      cgd }
    111  1.1      cgd 
    112  1.7   andrew int
    113  1.1      cgd cnopen(dev, flag, mode, p)
    114  1.1      cgd 	dev_t dev;
    115  1.1      cgd 	int flag, mode;
    116  1.1      cgd 	struct proc *p;
    117  1.1      cgd {
    118  1.1      cgd 	if (cn_tab == NULL)
    119  1.1      cgd 		return (0);
    120  1.1      cgd 	dev = cn_tab->cn_dev;
    121  1.1      cgd 	return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p));
    122  1.1      cgd }
    123  1.1      cgd 
    124  1.7   andrew int
    125  1.1      cgd cnclose(dev, flag, mode, p)
    126  1.1      cgd 	dev_t dev;
    127  1.1      cgd 	int flag, mode;
    128  1.1      cgd 	struct proc *p;
    129  1.1      cgd {
    130  1.1      cgd 	if (cn_tab == NULL)
    131  1.1      cgd 		return (0);
    132  1.1      cgd 	dev = cn_tab->cn_dev;
    133  1.1      cgd 	return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p));
    134  1.1      cgd }
    135  1.1      cgd 
    136  1.7   andrew int
    137  1.1      cgd cnread(dev, uio, flag)
    138  1.1      cgd 	dev_t dev;
    139  1.1      cgd 	struct uio *uio;
    140  1.1      cgd {
    141  1.1      cgd 	if (cn_tab == NULL)
    142  1.1      cgd 		return (0);
    143  1.1      cgd 	dev = cn_tab->cn_dev;
    144  1.1      cgd 	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
    145  1.1      cgd }
    146  1.1      cgd 
    147  1.7   andrew int
    148  1.1      cgd cnwrite(dev, uio, flag)
    149  1.1      cgd 	dev_t dev;
    150  1.1      cgd 	struct uio *uio;
    151  1.1      cgd {
    152  1.1      cgd 	if (cn_tab == NULL)
    153  1.1      cgd 		return (0);
    154  1.2      cgd 	if (constty)					/* 16 Aug 92*/
    155  1.2      cgd 		dev = constty->t_dev;
    156  1.2      cgd 	else
    157  1.2      cgd 		dev = cn_tab->cn_dev;
    158  1.1      cgd 	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
    159  1.1      cgd }
    160  1.1      cgd 
    161  1.7   andrew int
    162  1.1      cgd cnioctl(dev, cmd, data, flag, p)
    163  1.1      cgd 	dev_t dev;
    164  1.1      cgd 	caddr_t data;
    165  1.1      cgd 	struct proc *p;
    166  1.1      cgd {
    167  1.1      cgd 	int error;
    168  1.1      cgd 
    169  1.1      cgd 	if (cn_tab == NULL)
    170  1.1      cgd 		return (0);
    171  1.1      cgd 	/*
    172  1.1      cgd 	 * Superuser can always use this to wrest control of console
    173  1.1      cgd 	 * output from the "virtual" console.
    174  1.1      cgd 	 */
    175  1.1      cgd 	if (cmd == TIOCCONS && constty) {
    176  1.1      cgd 		error = suser(p->p_ucred, (u_short *) NULL);
    177  1.1      cgd 		if (error)
    178  1.1      cgd 			return (error);
    179  1.1      cgd 		constty = NULL;
    180  1.1      cgd 		return (0);
    181  1.1      cgd 	}
    182  1.1      cgd 	dev = cn_tab->cn_dev;
    183  1.1      cgd 	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
    184  1.1      cgd }
    185  1.1      cgd 
    186  1.1      cgd /*ARGSUSED*/
    187  1.7   andrew int
    188  1.1      cgd cnselect(dev, rw, p)
    189  1.1      cgd 	dev_t dev;
    190  1.1      cgd 	int rw;
    191  1.1      cgd 	struct proc *p;
    192  1.1      cgd {
    193  1.1      cgd 	if (cn_tab == NULL)
    194  1.1      cgd 		return (1);
    195  1.1      cgd 	return (ttselect(cn_tab->cn_dev, rw, p));
    196  1.1      cgd }
    197  1.1      cgd 
    198  1.7   andrew int
    199  1.1      cgd cngetc()
    200  1.1      cgd {
    201  1.1      cgd 	if (cn_tab == NULL)
    202  1.1      cgd 		return (0);
    203  1.1      cgd 	return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
    204  1.1      cgd }
    205  1.1      cgd 
    206  1.7   andrew int
    207  1.1      cgd cnputc(c)
    208  1.1      cgd 	register int c;
    209  1.1      cgd {
    210  1.1      cgd 	if (cn_tab == NULL)
    211  1.1      cgd 		return;
    212  1.1      cgd 	if (c) {
    213  1.1      cgd 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
    214  1.1      cgd 		if (c == '\n')
    215  1.1      cgd 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
    216  1.1      cgd 	}
    217  1.1      cgd }
    218  1.2      cgd 
    219  1.3      cgd int pg_wait = 0;
    220  1.2      cgd pg(p,q,r,s,t,u,v,w,x,y,z) char *p; {
    221  1.2      cgd 	printf(p,q,r,s,t,u,v,w,x,y,z);
    222  1.3      cgd 	if (pg_wait) {
    223  1.3      cgd 		printf("\n>");
    224  1.3      cgd 		return(cngetc());
    225  1.3      cgd 	} else
    226  1.3      cgd 		return 0;
    227  1.2      cgd }
    228