Home | History | Annotate | Line # | Download | only in dev
cons.c revision 1.40.4.5
      1  1.40.4.5      fvdl /*	$NetBSD: cons.c,v 1.40.4.5 2001/10/13 17:42:45 fvdl Exp $	*/
      2      1.18       cgd 
      3       1.1       cgd /*
      4       1.1       cgd  * Copyright (c) 1988 University of Utah.
      5      1.17       cgd  * Copyright (c) 1990, 1993
      6      1.17       cgd  *	The Regents of the University of California.  All rights reserved.
      7       1.1       cgd  *
      8       1.1       cgd  * This code is derived from software contributed to Berkeley by
      9       1.1       cgd  * the Systems Programming Group of the University of Utah Computer
     10       1.1       cgd  * Science Department.
     11       1.1       cgd  *
     12       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     13       1.1       cgd  * modification, are permitted provided that the following conditions
     14       1.1       cgd  * are met:
     15       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     17       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     19       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     20       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     21       1.1       cgd  *    must display the following acknowledgement:
     22       1.1       cgd  *	This product includes software developed by the University of
     23       1.1       cgd  *	California, Berkeley and its contributors.
     24       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     25       1.1       cgd  *    may be used to endorse or promote products derived from this software
     26       1.1       cgd  *    without specific prior written permission.
     27       1.1       cgd  *
     28       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38       1.1       cgd  * SUCH DAMAGE.
     39       1.1       cgd  *
     40      1.17       cgd  * from: Utah $Hdr: cons.c 1.7 92/01/21$
     41      1.17       cgd  *
     42      1.17       cgd  *	@(#)cons.c	8.2 (Berkeley) 1/12/94
     43       1.1       cgd  */
     44       1.1       cgd 
     45      1.10       cgd #include <sys/param.h>
     46      1.10       cgd #include <sys/proc.h>
     47      1.10       cgd #include <sys/user.h>
     48      1.10       cgd #include <sys/systm.h>
     49      1.10       cgd #include <sys/buf.h>
     50      1.10       cgd #include <sys/ioctl.h>
     51      1.10       cgd #include <sys/tty.h>
     52      1.10       cgd #include <sys/file.h>
     53      1.10       cgd #include <sys/conf.h>
     54      1.10       cgd #include <sys/vnode.h>
     55       1.1       cgd 
     56  1.40.4.1   thorpej #include <miscfs/specfs/specdev.h>
     57  1.40.4.1   thorpej 
     58      1.11       cgd #include <dev/cons.h>
     59       1.1       cgd 
     60      1.10       cgd struct	tty *constty = NULL;	/* virtual console output device */
     61  1.40.4.5      fvdl struct vnode *consvp = NULL;	/* virtual console output vnode */
     62       1.1       cgd struct	consdev *cn_tab;	/* physical console device info */
     63       1.1       cgd 
     64       1.7    andrew int
     65  1.40.4.2      fvdl cnopen(devvp, mode, flag, p)
     66  1.40.4.1   thorpej 	struct vnode *devvp;
     67       1.1       cgd 	int flag, mode;
     68       1.1       cgd 	struct proc *p;
     69       1.1       cgd {
     70  1.40.4.1   thorpej 	int error;
     71  1.40.4.4      fvdl 	dev_t cndev, rdev;
     72  1.40.4.2      fvdl 	struct vnode *vp;
     73      1.21   mycroft 
     74       1.1       cgd 	if (cn_tab == NULL)
     75       1.1       cgd 		return (0);
     76      1.10       cgd 
     77  1.40.4.1   thorpej 
     78      1.10       cgd 	/*
     79      1.10       cgd 	 * always open the 'real' console device, so we don't get nailed
     80      1.10       cgd 	 * later.  This follows normal device semantics; they always get
     81      1.10       cgd 	 * open() calls.
     82      1.10       cgd 	 */
     83      1.37       mrg 	cndev = cn_tab->cn_dev;
     84      1.37       mrg 	if (cndev == NODEV) {
     85      1.33       leo 		/*
     86      1.33       leo 		 * This is most likely an error in the console attach
     87      1.33       leo 		 * code. Panicing looks better than jumping into nowhere
     88      1.33       leo 		 * through cdevsw below....
     89      1.33       leo 		 */
     90      1.33       leo 		panic("cnopen: cn_tab->cn_dev == NODEV\n");
     91      1.33       leo 	}
     92  1.40.4.4      fvdl 	rdev = vdev_rdev(devvp);
     93  1.40.4.4      fvdl 	if (rdev == cndev) {
     94      1.37       mrg 		/*
     95      1.37       mrg 		 * This causes cnopen() to be called recursively, which
     96      1.37       mrg 		 * is generally a bad thing.  It is often caused when
     97      1.37       mrg 		 * dev == 0 and cn_dev has not been set, but was probably
     98      1.37       mrg 		 * initialised to 0.
     99      1.37       mrg 		 */
    100      1.37       mrg 		panic("cnopen: cn_tab->cn_dev == dev\n");
    101      1.37       mrg 	}
    102      1.33       leo 
    103  1.40.4.2      fvdl 	if (vfinddev(cndev, VCHR, &vp) == 0) {
    104  1.40.4.2      fvdl 		error = cdevvp(cndev, &vp);
    105  1.40.4.2      fvdl 		if (error != 0)
    106  1.40.4.2      fvdl 			return error;
    107  1.40.4.3      fvdl 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    108  1.40.4.2      fvdl 	} else
    109  1.40.4.3      fvdl 		vget(vp, LK_EXCLUSIVE | LK_RETRY);
    110  1.40.4.4      fvdl 
    111  1.40.4.4      fvdl 	vdev_setprivdata(devvp, vp);
    112  1.40.4.2      fvdl 
    113  1.40.4.2      fvdl 	error = VOP_OPEN(vp, mode, p->p_ucred, p, NULL);
    114  1.40.4.2      fvdl 	VOP_UNLOCK(vp, 0);
    115  1.40.4.2      fvdl 	if (error != 0)
    116  1.40.4.2      fvdl 		vrele(vp);
    117  1.40.4.2      fvdl 	return error;
    118       1.1       cgd }
    119       1.1       cgd 
    120       1.7    andrew int
    121  1.40.4.1   thorpej cnclose(devvp, flag, mode, p)
    122  1.40.4.1   thorpej 	struct vnode *devvp;
    123       1.1       cgd 	int flag, mode;
    124       1.1       cgd 	struct proc *p;
    125       1.1       cgd {
    126      1.10       cgd 	struct vnode *vp;
    127  1.40.4.2      fvdl 	int error;
    128      1.10       cgd 
    129       1.1       cgd 	if (cn_tab == NULL)
    130       1.1       cgd 		return (0);
    131      1.10       cgd 
    132  1.40.4.4      fvdl 	vp = vdev_privdata(devvp);
    133  1.40.4.2      fvdl 
    134  1.40.4.2      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    135  1.40.4.2      fvdl 	error = VOP_CLOSE(vp, flag, p->p_ucred, p);
    136  1.40.4.2      fvdl 	vput(vp);
    137  1.40.4.2      fvdl 
    138  1.40.4.2      fvdl 	return error;
    139       1.1       cgd }
    140       1.1       cgd 
    141       1.7    andrew int
    142  1.40.4.1   thorpej cnread(devvp, uio, flag)
    143  1.40.4.1   thorpej 	struct vnode *devvp;
    144       1.1       cgd 	struct uio *uio;
    145      1.24       cgd 	int flag;
    146       1.1       cgd {
    147  1.40.4.2      fvdl 	int error;
    148  1.40.4.2      fvdl 	struct vnode *vp;
    149  1.40.4.2      fvdl 	struct proc *p = uio->uio_procp;
    150      1.21   mycroft 
    151      1.10       cgd 	/*
    152      1.10       cgd 	 * If we would redirect input, punt.  This will keep strange
    153      1.10       cgd 	 * things from happening to people who are using the real
    154      1.10       cgd 	 * console.  Nothing should be using /dev/console for
    155      1.10       cgd 	 * input (except a shell in single-user mode, but then,
    156      1.10       cgd 	 * one wouldn't TIOCCONS then).
    157      1.10       cgd 	 */
    158      1.10       cgd 	if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
    159      1.10       cgd 		return 0;
    160      1.10       cgd 	else if (cn_tab == NULL)
    161      1.10       cgd 		return ENXIO;
    162      1.10       cgd 
    163  1.40.4.4      fvdl 	vp = vdev_privdata(devvp);
    164  1.40.4.4      fvdl 
    165  1.40.4.2      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    166  1.40.4.2      fvdl 	error = VOP_READ(vp, uio, flag, p->p_ucred);
    167  1.40.4.2      fvdl 	VOP_UNLOCK(vp, 0);
    168  1.40.4.2      fvdl 
    169  1.40.4.2      fvdl 	return error;
    170       1.1       cgd }
    171       1.1       cgd 
    172       1.7    andrew int
    173  1.40.4.1   thorpej cnwrite(devvp, uio, flag)
    174  1.40.4.1   thorpej 	struct vnode *devvp;
    175       1.1       cgd 	struct uio *uio;
    176      1.24       cgd 	int flag;
    177       1.1       cgd {
    178  1.40.4.2      fvdl 	int error;
    179  1.40.4.2      fvdl 	struct vnode *vp;
    180  1.40.4.2      fvdl 	struct proc *p = uio->uio_procp;
    181      1.21   mycroft 
    182      1.10       cgd 	/*
    183      1.10       cgd 	 * Redirect output, if that's appropriate.
    184      1.10       cgd 	 * If there's no real console, return ENXIO.
    185      1.10       cgd 	 */
    186      1.10       cgd 	if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
    187  1.40.4.5      fvdl 		vp = consvp;
    188      1.10       cgd 	else if (cn_tab == NULL)
    189      1.10       cgd 		return ENXIO;
    190       1.2       cgd 	else
    191  1.40.4.4      fvdl 		vp = vdev_privdata(devvp);
    192  1.40.4.4      fvdl 
    193  1.40.4.5      fvdl 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    194  1.40.4.5      fvdl 	if (error != 0)
    195  1.40.4.5      fvdl 		return error;
    196  1.40.4.2      fvdl 	error = VOP_WRITE(vp, uio, flag, p->p_ucred);
    197  1.40.4.2      fvdl 	VOP_UNLOCK(vp, 0);
    198  1.40.4.2      fvdl 	return error;
    199      1.25   mycroft }
    200      1.25   mycroft 
    201      1.31   mycroft void
    202      1.25   mycroft cnstop(tp, flag)
    203      1.25   mycroft 	struct tty *tp;
    204      1.25   mycroft 	int flag;
    205      1.25   mycroft {
    206      1.31   mycroft 
    207       1.1       cgd }
    208       1.1       cgd 
    209       1.7    andrew int
    210  1.40.4.1   thorpej cnioctl(devvp, cmd, data, flag, p)
    211  1.40.4.1   thorpej 	struct vnode *devvp;
    212      1.20       cgd 	u_long cmd;
    213       1.1       cgd 	caddr_t data;
    214      1.20       cgd 	int flag;
    215       1.1       cgd 	struct proc *p;
    216       1.1       cgd {
    217       1.1       cgd 	int error;
    218  1.40.4.2      fvdl 	struct vnode *vp;
    219       1.1       cgd 
    220       1.1       cgd 	/*
    221       1.1       cgd 	 * Superuser can always use this to wrest control of console
    222       1.1       cgd 	 * output from the "virtual" console.
    223       1.1       cgd 	 */
    224      1.10       cgd 	if (cmd == TIOCCONS && constty != NULL) {
    225       1.1       cgd 		error = suser(p->p_ucred, (u_short *) NULL);
    226       1.1       cgd 		if (error)
    227       1.1       cgd 			return (error);
    228       1.1       cgd 		constty = NULL;
    229  1.40.4.5      fvdl 		vrele(consvp);
    230  1.40.4.5      fvdl 		consvp = NULL;
    231       1.1       cgd 		return (0);
    232       1.1       cgd 	}
    233      1.10       cgd 
    234      1.10       cgd 	/*
    235      1.10       cgd 	 * Redirect the ioctl, if that's appropriate.
    236      1.10       cgd 	 * Note that strange things can happen, if a program does
    237      1.10       cgd 	 * ioctls on /dev/console, then the console is redirected
    238      1.10       cgd 	 * out from under it.
    239      1.10       cgd 	 */
    240      1.12       cgd 	if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
    241  1.40.4.5      fvdl 		vp = consvp;
    242      1.10       cgd 	else if (cn_tab == NULL)
    243      1.10       cgd 		return ENXIO;
    244      1.10       cgd 	else
    245  1.40.4.4      fvdl 		vp = vdev_privdata(devvp);
    246  1.40.4.2      fvdl 
    247  1.40.4.2      fvdl 	return VOP_IOCTL(vp, cmd, data, flag, p->p_ucred, p);
    248       1.1       cgd }
    249       1.1       cgd 
    250       1.1       cgd /*ARGSUSED*/
    251       1.7    andrew int
    252  1.40.4.1   thorpej cnpoll(devvp, events, p)
    253  1.40.4.1   thorpej 	struct vnode *devvp;
    254      1.32   mycroft 	int events;
    255       1.1       cgd 	struct proc *p;
    256       1.1       cgd {
    257  1.40.4.2      fvdl 	struct vnode *vp;
    258      1.21   mycroft 
    259      1.10       cgd 	/*
    260      1.38     lukem 	 * Redirect the poll, if that's appropriate.
    261      1.10       cgd 	 * I don't want to think of the possible side effects
    262      1.10       cgd 	 * of console redirection here.
    263      1.10       cgd 	 */
    264      1.12       cgd 	if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
    265  1.40.4.5      fvdl 		vp = consvp;
    266      1.10       cgd 	else if (cn_tab == NULL)
    267      1.10       cgd 		return ENXIO;
    268      1.10       cgd 	else
    269  1.40.4.4      fvdl 		vp = vdev_privdata(devvp);
    270  1.40.4.2      fvdl 
    271  1.40.4.2      fvdl 	return VOP_POLL(vp, events, p);
    272       1.1       cgd }
    273       1.1       cgd 
    274       1.7    andrew int
    275       1.1       cgd cngetc()
    276       1.1       cgd {
    277      1.21   mycroft 
    278       1.1       cgd 	if (cn_tab == NULL)
    279       1.1       cgd 		return (0);
    280       1.1       cgd 	return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
    281      1.36    itojun }
    282      1.36    itojun 
    283      1.36    itojun int
    284      1.36    itojun cngetsn(cp, size)
    285      1.36    itojun 	char *cp;
    286      1.36    itojun 	int size;
    287      1.36    itojun {
    288      1.36    itojun 	char *lp;
    289      1.36    itojun 	int c, len;
    290      1.36    itojun 
    291      1.36    itojun 	cnpollc(1);
    292      1.36    itojun 
    293      1.36    itojun 	lp = cp;
    294      1.36    itojun 	len = 0;
    295      1.36    itojun 	for (;;) {
    296      1.36    itojun 		c = cngetc();
    297      1.36    itojun 		switch (c) {
    298      1.36    itojun 		case '\n':
    299      1.36    itojun 		case '\r':
    300      1.36    itojun 			printf("\n");
    301      1.36    itojun 			*lp++ = '\0';
    302      1.36    itojun 			cnpollc(0);
    303      1.36    itojun 			return (len);
    304      1.36    itojun 		case '\b':
    305      1.36    itojun 		case '\177':
    306      1.36    itojun 		case '#':
    307      1.36    itojun 			if (len) {
    308      1.36    itojun 				--len;
    309      1.36    itojun 				--lp;
    310      1.36    itojun 				printf("\b \b");
    311      1.36    itojun 			}
    312      1.36    itojun 			continue;
    313      1.36    itojun 		case '@':
    314      1.40  jdolecek 		case 'u'&037:	/* CTRL-u */
    315      1.36    itojun 			len = 0;
    316      1.36    itojun 			lp = cp;
    317      1.36    itojun 			printf("\n");
    318      1.36    itojun 			continue;
    319      1.36    itojun 		default:
    320      1.36    itojun 			if (len + 1 >= size || c < ' ') {
    321      1.40  jdolecek 				printf("\007");
    322      1.36    itojun 				continue;
    323      1.36    itojun 			}
    324      1.36    itojun 			printf("%c", c);
    325      1.36    itojun 			++len;
    326      1.36    itojun 			*lp++ = c;
    327      1.36    itojun 		}
    328      1.36    itojun 	}
    329       1.1       cgd }
    330       1.1       cgd 
    331      1.29  christos void
    332       1.1       cgd cnputc(c)
    333      1.35  augustss 	int c;
    334       1.1       cgd {
    335      1.21   mycroft 
    336       1.1       cgd 	if (cn_tab == NULL)
    337      1.29  christos 		return;
    338      1.29  christos 
    339       1.1       cgd 	if (c) {
    340       1.1       cgd 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
    341       1.1       cgd 		if (c == '\n')
    342       1.1       cgd 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
    343       1.1       cgd 	}
    344      1.19   mycroft }
    345      1.19   mycroft 
    346      1.19   mycroft void
    347      1.19   mycroft cnpollc(on)
    348      1.19   mycroft 	int on;
    349      1.19   mycroft {
    350      1.19   mycroft 	static int refcount = 0;
    351      1.19   mycroft 
    352      1.19   mycroft 	if (cn_tab == NULL)
    353      1.19   mycroft 		return;
    354      1.19   mycroft 	if (!on)
    355      1.19   mycroft 		--refcount;
    356      1.19   mycroft 	if (refcount == 0)
    357      1.19   mycroft 		(*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
    358      1.19   mycroft 	if (on)
    359      1.19   mycroft 		++refcount;
    360      1.21   mycroft }
    361      1.21   mycroft 
    362      1.21   mycroft void
    363      1.28       cgd nullcnpollc(dev, on)
    364      1.28       cgd 	dev_t dev;
    365      1.21   mycroft 	int on;
    366      1.21   mycroft {
    367      1.21   mycroft 
    368      1.34   thorpej }
    369      1.34   thorpej 
    370      1.34   thorpej void
    371      1.34   thorpej cnbell(pitch, period, volume)
    372      1.34   thorpej 	u_int pitch, period, volume;
    373      1.34   thorpej {
    374      1.34   thorpej 
    375      1.34   thorpej 	if (cn_tab == NULL || cn_tab->cn_bell == NULL)
    376      1.34   thorpej 		return;
    377      1.34   thorpej 	(*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
    378       1.2       cgd }
    379