Home | History | Annotate | Line # | Download | only in dev
cons.c revision 1.64.14.1
      1  1.64.14.1       mjf /*	$NetBSD: cons.c,v 1.64.14.1 2008/02/18 21:05:31 mjf Exp $	*/
      2       1.18       cgd 
      3        1.1       cgd /*
      4       1.17       cgd  * Copyright (c) 1990, 1993
      5       1.17       cgd  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * This code is derived from software contributed to Berkeley by
      8        1.1       cgd  * the Systems Programming Group of the University of Utah Computer
      9        1.1       cgd  * Science Department.
     10        1.1       cgd  *
     11        1.1       cgd  * Redistribution and use in source and binary forms, with or without
     12        1.1       cgd  * modification, are permitted provided that the following conditions
     13        1.1       cgd  * are met:
     14        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     15        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     16        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     18        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     19       1.49       agc  * 3. Neither the name of the University nor the names of its contributors
     20       1.49       agc  *    may be used to endorse or promote products derived from this software
     21       1.49       agc  *    without specific prior written permission.
     22       1.49       agc  *
     23       1.49       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.49       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.49       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.49       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.49       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.49       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.49       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.49       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.49       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.49       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.49       agc  * SUCH DAMAGE.
     34       1.49       agc  *
     35       1.49       agc  * from: Utah $Hdr: cons.c 1.7 92/01/21$
     36       1.49       agc  *
     37       1.49       agc  *	@(#)cons.c	8.2 (Berkeley) 1/12/94
     38       1.49       agc  */
     39       1.49       agc 
     40       1.49       agc /*
     41       1.49       agc  * Copyright (c) 1988 University of Utah.
     42       1.49       agc  *
     43       1.49       agc  * This code is derived from software contributed to Berkeley by
     44       1.49       agc  * the Systems Programming Group of the University of Utah Computer
     45       1.49       agc  * Science Department.
     46       1.49       agc  *
     47       1.49       agc  * Redistribution and use in source and binary forms, with or without
     48       1.49       agc  * modification, are permitted provided that the following conditions
     49       1.49       agc  * are met:
     50       1.49       agc  * 1. Redistributions of source code must retain the above copyright
     51       1.49       agc  *    notice, this list of conditions and the following disclaimer.
     52       1.49       agc  * 2. Redistributions in binary form must reproduce the above copyright
     53       1.49       agc  *    notice, this list of conditions and the following disclaimer in the
     54       1.49       agc  *    documentation and/or other materials provided with the distribution.
     55        1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     56        1.1       cgd  *    must display the following acknowledgement:
     57        1.1       cgd  *	This product includes software developed by the University of
     58        1.1       cgd  *	California, Berkeley and its contributors.
     59        1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     60        1.1       cgd  *    may be used to endorse or promote products derived from this software
     61        1.1       cgd  *    without specific prior written permission.
     62        1.1       cgd  *
     63        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73        1.1       cgd  * SUCH DAMAGE.
     74        1.1       cgd  *
     75       1.17       cgd  * from: Utah $Hdr: cons.c 1.7 92/01/21$
     76       1.17       cgd  *
     77       1.17       cgd  *	@(#)cons.c	8.2 (Berkeley) 1/12/94
     78        1.1       cgd  */
     79       1.41     lukem 
     80       1.41     lukem #include <sys/cdefs.h>
     81  1.64.14.1       mjf __KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.64.14.1 2008/02/18 21:05:31 mjf Exp $");
     82        1.1       cgd 
     83       1.10       cgd #include <sys/param.h>
     84       1.10       cgd #include <sys/proc.h>
     85       1.10       cgd #include <sys/user.h>
     86       1.10       cgd #include <sys/systm.h>
     87       1.10       cgd #include <sys/buf.h>
     88       1.10       cgd #include <sys/ioctl.h>
     89       1.56        ws #include <sys/poll.h>
     90       1.10       cgd #include <sys/tty.h>
     91       1.10       cgd #include <sys/file.h>
     92       1.10       cgd #include <sys/conf.h>
     93       1.10       cgd #include <sys/vnode.h>
     94       1.58      elad #include <sys/kauth.h>
     95       1.62        ad #include <sys/mutex.h>
     96        1.1       cgd 
     97       1.11       cgd #include <dev/cons.h>
     98        1.1       cgd 
     99       1.43   gehenna dev_type_open(cnopen);
    100       1.43   gehenna dev_type_close(cnclose);
    101       1.43   gehenna dev_type_read(cnread);
    102       1.43   gehenna dev_type_write(cnwrite);
    103       1.43   gehenna dev_type_ioctl(cnioctl);
    104       1.43   gehenna dev_type_poll(cnpoll);
    105       1.45  jdolecek dev_type_kqfilter(cnkqfilter);
    106       1.43   gehenna 
    107       1.64        ad static bool cn_redirect(dev_t *, int, int *);
    108       1.50       dsl 
    109       1.43   gehenna const struct cdevsw cons_cdevsw = {
    110       1.43   gehenna 	cnopen, cnclose, cnread, cnwrite, cnioctl,
    111       1.45  jdolecek 	nostop, notty, cnpoll, nommap, cnkqfilter, D_TTY
    112       1.43   gehenna };
    113       1.43   gehenna 
    114       1.10       cgd struct	tty *constty = NULL;	/* virtual console output device */
    115       1.52       cdi struct	consdev *cn_tab;	/* physical console device info */
    116       1.50       dsl struct	vnode *cn_devvp[2];	/* vnode for underlying device. */
    117        1.1       cgd 
    118        1.7    andrew int
    119       1.57  christos cnopen(dev_t dev, int flag, int mode, struct lwp *l)
    120        1.1       cgd {
    121       1.37       mrg 	dev_t cndev;
    122  1.64.14.1       mjf 	int unit, error;
    123       1.50       dsl 
    124       1.50       dsl 	unit = minor(dev);
    125       1.50       dsl 	if (unit > 1)
    126       1.50       dsl 		return ENODEV;
    127       1.21   mycroft 
    128        1.1       cgd 	if (cn_tab == NULL)
    129        1.1       cgd 		return (0);
    130       1.10       cgd 
    131       1.10       cgd 	/*
    132       1.10       cgd 	 * always open the 'real' console device, so we don't get nailed
    133       1.10       cgd 	 * later.  This follows normal device semantics; they always get
    134       1.10       cgd 	 * open() calls.
    135       1.10       cgd 	 */
    136       1.37       mrg 	cndev = cn_tab->cn_dev;
    137       1.37       mrg 	if (cndev == NODEV) {
    138       1.33       leo 		/*
    139       1.33       leo 		 * This is most likely an error in the console attach
    140       1.53       wiz 		 * code. Panicking looks better than jumping into nowhere
    141       1.33       leo 		 * through cdevsw below....
    142       1.33       leo 		 */
    143       1.44    provos 		panic("cnopen: no console device");
    144       1.33       leo 	}
    145       1.37       mrg 	if (dev == cndev) {
    146       1.37       mrg 		/*
    147       1.37       mrg 		 * This causes cnopen() to be called recursively, which
    148       1.37       mrg 		 * is generally a bad thing.  It is often caused when
    149       1.37       mrg 		 * dev == 0 and cn_dev has not been set, but was probably
    150       1.37       mrg 		 * initialised to 0.
    151       1.37       mrg 		 */
    152       1.44    provos 		panic("cnopen: cn_tab->cn_dev == dev");
    153       1.37       mrg 	}
    154  1.64.14.1       mjf 	if (cn_devvp[unit] != NULLVP)
    155  1.64.14.1       mjf 		return 0;
    156  1.64.14.1       mjf 	if ((error = cdevvp(cndev, &cn_devvp[unit])) != 0)
    157  1.64.14.1       mjf 		printf("cnopen: unable to get vnode reference\n");
    158  1.64.14.1       mjf 	error = vn_lock(cn_devvp[unit], LK_EXCLUSIVE | LK_RETRY);
    159  1.64.14.1       mjf 	if (error == 0) {
    160  1.64.14.1       mjf 		error = VOP_OPEN(cn_devvp[unit], flag, kauth_cred_get());
    161  1.64.14.1       mjf 		VOP_UNLOCK(cn_devvp[unit], 0);
    162       1.13       cgd 	}
    163  1.64.14.1       mjf 	return error;
    164        1.1       cgd }
    165       1.54     perry 
    166        1.7    andrew int
    167       1.57  christos cnclose(dev_t dev, int flag, int mode, struct lwp *l)
    168        1.1       cgd {
    169       1.10       cgd 	struct vnode *vp;
    170  1.64.14.1       mjf 	int unit, error;
    171       1.50       dsl 
    172       1.50       dsl 	unit = minor(dev);
    173       1.10       cgd 
    174        1.1       cgd 	if (cn_tab == NULL)
    175        1.1       cgd 		return (0);
    176       1.10       cgd 
    177  1.64.14.1       mjf 	vp = cn_devvp[unit];
    178  1.64.14.1       mjf 	cn_devvp[unit] = NULL;
    179  1.64.14.1       mjf 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    180  1.64.14.1       mjf 	if (error == 0) {
    181  1.64.14.1       mjf 		error = VOP_CLOSE(vp, flag, kauth_cred_get());
    182  1.64.14.1       mjf 		VOP_UNLOCK(vp, 0);
    183       1.13       cgd 	}
    184  1.64.14.1       mjf 	return error;
    185        1.1       cgd }
    186       1.54     perry 
    187        1.7    andrew int
    188       1.46      matt cnread(dev_t dev, struct uio *uio, int flag)
    189        1.1       cgd {
    190       1.50       dsl 	int error;
    191       1.21   mycroft 
    192       1.10       cgd 	/*
    193       1.10       cgd 	 * If we would redirect input, punt.  This will keep strange
    194       1.10       cgd 	 * things from happening to people who are using the real
    195       1.10       cgd 	 * console.  Nothing should be using /dev/console for
    196       1.10       cgd 	 * input (except a shell in single-user mode, but then,
    197       1.10       cgd 	 * one wouldn't TIOCCONS then).
    198       1.10       cgd 	 */
    199       1.64        ad 	if (!cn_redirect(&dev, 1, &error))
    200       1.50       dsl 		return error;
    201       1.64        ad 	return cdev_read(dev, uio, flag);
    202        1.1       cgd }
    203       1.54     perry 
    204        1.7    andrew int
    205       1.46      matt cnwrite(dev_t dev, struct uio *uio, int flag)
    206        1.1       cgd {
    207       1.50       dsl 	int error;
    208       1.21   mycroft 
    209       1.50       dsl 	/* Redirect output, if that's appropriate. */
    210       1.64        ad 	if (!cn_redirect(&dev, 0, &error))
    211       1.50       dsl 		return error;
    212       1.64        ad 	return cdev_write(dev, uio, flag);
    213       1.25   mycroft }
    214       1.25   mycroft 
    215        1.7    andrew int
    216       1.63  christos cnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    217        1.1       cgd {
    218        1.1       cgd 	int error;
    219        1.1       cgd 
    220       1.62        ad 	error = 0;
    221       1.62        ad 
    222        1.1       cgd 	/*
    223        1.1       cgd 	 * Superuser can always use this to wrest control of console
    224        1.1       cgd 	 * output from the "virtual" console.
    225        1.1       cgd 	 */
    226       1.10       cgd 	if (cmd == TIOCCONS && constty != NULL) {
    227       1.59        ad 		error = kauth_authorize_generic(l->l_cred,
    228       1.59        ad 		    KAUTH_GENERIC_ISSUSER, NULL);
    229       1.62        ad 		if (!error)
    230       1.62        ad 			constty = NULL;
    231       1.62        ad 		return (error);
    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.64        ad 	if (!cn_redirect(&dev, 0, &error))
    241       1.64        ad 		return error;
    242       1.64        ad 	return cdev_ioctl(dev, cmd, data, flag, l);
    243        1.1       cgd }
    244        1.1       cgd 
    245        1.1       cgd /*ARGSUSED*/
    246        1.7    andrew int
    247       1.57  christos cnpoll(dev_t dev, int events, struct lwp *l)
    248        1.1       cgd {
    249       1.50       dsl 	int error;
    250       1.21   mycroft 
    251       1.10       cgd 	/*
    252       1.38     lukem 	 * Redirect the poll, if that's appropriate.
    253       1.10       cgd 	 * I don't want to think of the possible side effects
    254       1.10       cgd 	 * of console redirection here.
    255       1.10       cgd 	 */
    256       1.64        ad 	if (!cn_redirect(&dev, 0, &error))
    257       1.56        ws 		return POLLHUP;
    258       1.64        ad 	return cdev_poll(dev, events, l);
    259       1.45  jdolecek }
    260       1.45  jdolecek 
    261       1.45  jdolecek /*ARGSUSED*/
    262       1.45  jdolecek int
    263       1.46      matt cnkqfilter(dev_t dev, struct knote *kn)
    264       1.45  jdolecek {
    265       1.50       dsl 	int error;
    266       1.45  jdolecek 
    267       1.45  jdolecek 	/*
    268       1.45  jdolecek 	 * Redirect the kqfilter, if that's appropriate.
    269       1.45  jdolecek 	 * I don't want to think of the possible side effects
    270       1.45  jdolecek 	 * of console redirection here.
    271       1.45  jdolecek 	 */
    272       1.64        ad 	if (!cn_redirect(&dev, 0, &error))
    273       1.50       dsl 		return error;
    274       1.64        ad 	return cdev_kqfilter(dev, kn);
    275        1.1       cgd }
    276        1.1       cgd 
    277        1.7    andrew int
    278       1.46      matt cngetc(void)
    279        1.1       cgd {
    280        1.1       cgd 	if (cn_tab == NULL)
    281        1.1       cgd 		return (0);
    282        1.1       cgd 	return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
    283       1.36    itojun }
    284       1.36    itojun 
    285       1.36    itojun int
    286       1.46      matt cngetsn(char *cp, 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.46      matt cnputc(int c)
    333        1.1       cgd {
    334       1.21   mycroft 
    335        1.1       cgd 	if (cn_tab == NULL)
    336       1.54     perry 		return;
    337       1.29  christos 
    338        1.1       cgd 	if (c) {
    339        1.1       cgd 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
    340        1.1       cgd 		if (c == '\n')
    341        1.1       cgd 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
    342        1.1       cgd 	}
    343       1.19   mycroft }
    344       1.19   mycroft 
    345       1.19   mycroft void
    346       1.46      matt cnpollc(int on)
    347       1.19   mycroft {
    348       1.19   mycroft 	static int refcount = 0;
    349       1.19   mycroft 
    350       1.19   mycroft 	if (cn_tab == NULL)
    351       1.19   mycroft 		return;
    352       1.19   mycroft 	if (!on)
    353       1.19   mycroft 		--refcount;
    354       1.19   mycroft 	if (refcount == 0)
    355       1.19   mycroft 		(*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
    356       1.19   mycroft 	if (on)
    357       1.19   mycroft 		++refcount;
    358       1.21   mycroft }
    359       1.21   mycroft 
    360       1.21   mycroft void
    361       1.61  christos nullcnpollc(dev_t dev, int on)
    362       1.21   mycroft {
    363       1.21   mycroft 
    364       1.34   thorpej }
    365       1.34   thorpej 
    366       1.34   thorpej void
    367       1.46      matt cnbell(u_int pitch, u_int period, u_int volume)
    368       1.34   thorpej {
    369       1.34   thorpej 
    370       1.34   thorpej 	if (cn_tab == NULL || cn_tab->cn_bell == NULL)
    371       1.34   thorpej 		return;
    372       1.34   thorpej 	(*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
    373       1.46      matt }
    374       1.46      matt 
    375       1.46      matt void
    376       1.46      matt cnflush(void)
    377       1.46      matt {
    378       1.46      matt 	if (cn_tab == NULL || cn_tab->cn_flush == NULL)
    379       1.46      matt 		return;
    380       1.46      matt 	(*cn_tab->cn_flush)(cn_tab->cn_dev);
    381       1.46      matt }
    382       1.54     perry 
    383       1.46      matt void
    384       1.46      matt cnhalt(void)
    385       1.46      matt {
    386       1.46      matt 	if (cn_tab == NULL || cn_tab->cn_halt == NULL)
    387       1.46      matt 		return;
    388       1.46      matt 	(*cn_tab->cn_halt)(cn_tab->cn_dev);
    389       1.50       dsl }
    390       1.50       dsl 
    391       1.62        ad /*
    392       1.62        ad  * Redirect output, if that's appropriate.  If there's no real console,
    393       1.62        ad  * return ENXIO.
    394       1.62        ad  *
    395       1.62        ad  * Call with tty_mutex held.
    396       1.62        ad  */
    397       1.64        ad static bool
    398       1.50       dsl cn_redirect(dev_t *devp, int is_read, int *error)
    399       1.50       dsl {
    400       1.50       dsl 	dev_t dev = *devp;
    401       1.50       dsl 
    402       1.50       dsl 	*error = ENXIO;
    403       1.50       dsl 	if (constty != NULL && minor(dev) == 0 &&
    404       1.55    martin 	    (cn_tab == NULL || (cn_tab->cn_pri != CN_REMOTE))) {
    405       1.50       dsl 		if (is_read) {
    406       1.50       dsl 			*error = 0;
    407       1.64        ad 			return false;
    408       1.50       dsl 		}
    409       1.50       dsl 		dev = constty->t_dev;
    410       1.50       dsl 	} else if (cn_tab == NULL)
    411       1.64        ad 		return false;
    412       1.50       dsl 	else
    413       1.50       dsl 		dev = cn_tab->cn_dev;
    414       1.50       dsl 	*devp = dev;
    415       1.64        ad 	return true;
    416        1.2       cgd }
    417