Home | History | Annotate | Line # | Download | only in dev
cons.c revision 1.94
      1  1.94  riastrad /*	$NetBSD: cons.c,v 1.94 2023/09/02 17:44:12 riastradh Exp $	*/
      2  1.18       cgd 
      3   1.1       cgd /*
      4  1.68     rmind  * 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.49       agc  * 3. Neither the name of the University nor the names of its contributors
     21  1.49       agc  *    may be used to endorse or promote products derived from this software
     22  1.49       agc  *    without specific prior written permission.
     23  1.49       agc  *
     24  1.49       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.49       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.49       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.49       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.49       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.49       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.49       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.49       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.49       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.49       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.49       agc  * SUCH DAMAGE.
     35  1.49       agc  *
     36  1.77  riastrad  * from: Utah $Hdr: cons.c 1.7 92/01/21$
     37  1.49       agc  *
     38  1.49       agc  *	@(#)cons.c	8.2 (Berkeley) 1/12/94
     39  1.49       agc  */
     40  1.49       agc 
     41  1.41     lukem #include <sys/cdefs.h>
     42  1.94  riastrad __KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.94 2023/09/02 17:44:12 riastradh Exp $");
     43  1.94  riastrad 
     44  1.94  riastrad #ifdef _KERNEL_OPT
     45  1.94  riastrad #include "opt_heartbeat.h"
     46  1.94  riastrad #endif
     47   1.1       cgd 
     48  1.10       cgd #include <sys/param.h>
     49  1.93  riastrad 
     50  1.93  riastrad #include <sys/atomic.h>
     51  1.10       cgd #include <sys/buf.h>
     52  1.93  riastrad #include <sys/conf.h>
     53  1.93  riastrad #include <sys/file.h>
     54  1.94  riastrad #include <sys/heartbeat.h>
     55  1.10       cgd #include <sys/ioctl.h>
     56  1.93  riastrad #include <sys/kauth.h>
     57  1.93  riastrad #include <sys/module.h>
     58  1.93  riastrad #include <sys/mutex.h>
     59  1.56        ws #include <sys/poll.h>
     60  1.93  riastrad #include <sys/proc.h>
     61  1.93  riastrad #include <sys/pserialize.h>
     62  1.93  riastrad #include <sys/systm.h>
     63  1.10       cgd #include <sys/tty.h>
     64  1.10       cgd #include <sys/vnode.h>
     65   1.1       cgd 
     66  1.11       cgd #include <dev/cons.h>
     67   1.1       cgd 
     68  1.73   mlelstv #include "nullcons.h"
     69  1.73   mlelstv 
     70  1.43   gehenna dev_type_open(cnopen);
     71  1.43   gehenna dev_type_close(cnclose);
     72  1.43   gehenna dev_type_read(cnread);
     73  1.43   gehenna dev_type_write(cnwrite);
     74  1.43   gehenna dev_type_ioctl(cnioctl);
     75  1.43   gehenna dev_type_poll(cnpoll);
     76  1.45  jdolecek dev_type_kqfilter(cnkqfilter);
     77  1.43   gehenna 
     78  1.91  riastrad static bool cn_redirect(dev_t *, int, int *, struct tty **);
     79  1.91  riastrad static void cn_release(struct tty *);
     80  1.50       dsl 
     81  1.43   gehenna const struct cdevsw cons_cdevsw = {
     82  1.71  dholland 	.d_open = cnopen,
     83  1.71  dholland 	.d_close = cnclose,
     84  1.71  dholland 	.d_read = cnread,
     85  1.71  dholland 	.d_write = cnwrite,
     86  1.71  dholland 	.d_ioctl = cnioctl,
     87  1.71  dholland 	.d_stop = nostop,
     88  1.71  dholland 	.d_tty = notty,
     89  1.71  dholland 	.d_poll = cnpoll,
     90  1.71  dholland 	.d_mmap = nommap,
     91  1.71  dholland 	.d_kqfilter = cnkqfilter,
     92  1.72  dholland 	.d_discard = nodiscard,
     93  1.92  riastrad 	.d_flag = D_TTY|D_MPSAFE,
     94  1.43   gehenna };
     95  1.43   gehenna 
     96  1.81  riastrad static struct kmutex cn_lock;
     97  1.81  riastrad 
     98  1.91  riastrad struct	tty *volatile constty;	/* virtual console output device */
     99  1.52       cdi struct	consdev *cn_tab;	/* physical console device info */
    100  1.50       dsl struct	vnode *cn_devvp[2];	/* vnode for underlying device. */
    101   1.1       cgd 
    102  1.80  riastrad void
    103  1.80  riastrad cn_set_tab(struct consdev *tab)
    104  1.80  riastrad {
    105  1.80  riastrad 
    106  1.80  riastrad 	/*
    107  1.80  riastrad 	 * This is a point that we should have KASSERT(cold) or add
    108  1.80  riastrad 	 * synchronization in case this can happen after cold boot.
    109  1.80  riastrad 	 * However, cn_tab initialization is so critical to any
    110  1.80  riastrad 	 * diagnostics or debugging that we need to tread carefully
    111  1.80  riastrad 	 * about introducing new ways to crash.  So let's put the
    112  1.80  riastrad 	 * assertion in only after we've audited most or all of the
    113  1.80  riastrad 	 * cn_tab updates.
    114  1.80  riastrad 	 */
    115  1.80  riastrad 	cn_tab = tab;
    116  1.80  riastrad }
    117  1.80  riastrad 
    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.65        ad 	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.81  riastrad 	mutex_enter(&cn_lock);
    129  1.81  riastrad 
    130  1.81  riastrad 	if (cn_tab == NULL) {
    131  1.81  riastrad 		error = 0;
    132  1.81  riastrad 		goto out;
    133  1.81  riastrad 	}
    134  1.10       cgd 
    135  1.10       cgd 	/*
    136  1.10       cgd 	 * always open the 'real' console device, so we don't get nailed
    137  1.10       cgd 	 * later.  This follows normal device semantics; they always get
    138  1.10       cgd 	 * open() calls.
    139  1.10       cgd 	 */
    140  1.37       mrg 	cndev = cn_tab->cn_dev;
    141  1.73   mlelstv #if NNULLCONS > 0
    142  1.73   mlelstv 	if (cndev == NODEV) {
    143  1.73   mlelstv 		nullconsattach(0);
    144  1.73   mlelstv 	}
    145  1.73   mlelstv #else /* NNULLCONS > 0 */
    146  1.37       mrg 	if (cndev == NODEV) {
    147  1.33       leo 		/*
    148  1.33       leo 		 * This is most likely an error in the console attach
    149  1.53       wiz 		 * code. Panicking looks better than jumping into nowhere
    150  1.33       leo 		 * through cdevsw below....
    151  1.33       leo 		 */
    152  1.44    provos 		panic("cnopen: no console device");
    153  1.33       leo 	}
    154  1.73   mlelstv #endif /* NNULLCONS > 0 */
    155  1.37       mrg 	if (dev == cndev) {
    156  1.37       mrg 		/*
    157  1.37       mrg 		 * This causes cnopen() to be called recursively, which
    158  1.37       mrg 		 * is generally a bad thing.  It is often caused when
    159  1.37       mrg 		 * dev == 0 and cn_dev has not been set, but was probably
    160  1.37       mrg 		 * initialised to 0.
    161  1.37       mrg 		 */
    162  1.44    provos 		panic("cnopen: cn_tab->cn_dev == dev");
    163  1.37       mrg 	}
    164  1.81  riastrad 	if (cn_devvp[unit] != NULLVP) {
    165  1.81  riastrad 		error = 0;
    166  1.81  riastrad 		goto out;
    167  1.81  riastrad 	}
    168  1.79  riastrad 	if ((error = cdevvp(cndev, &cn_devvp[unit])) != 0) {
    169  1.65        ad 		printf("cnopen: unable to get vnode reference\n");
    170  1.81  riastrad 		goto out;
    171  1.79  riastrad 	}
    172  1.78  riastrad 	vn_lock(cn_devvp[unit], LK_EXCLUSIVE | LK_RETRY);
    173  1.78  riastrad 	error = VOP_OPEN(cn_devvp[unit], flag, kauth_cred_get());
    174  1.78  riastrad 	VOP_UNLOCK(cn_devvp[unit]);
    175  1.81  riastrad 
    176  1.81  riastrad out:	mutex_exit(&cn_lock);
    177  1.65        ad 	return error;
    178   1.1       cgd }
    179  1.54     perry 
    180   1.7    andrew int
    181  1.57  christos cnclose(dev_t dev, int flag, int mode, struct lwp *l)
    182   1.1       cgd {
    183  1.10       cgd 	struct vnode *vp;
    184  1.65        ad 	int unit, error;
    185  1.50       dsl 
    186  1.50       dsl 	unit = minor(dev);
    187  1.82  riastrad 	if (unit > 1)
    188  1.82  riastrad 		return ENODEV;
    189  1.10       cgd 
    190  1.81  riastrad 	mutex_enter(&cn_lock);
    191  1.81  riastrad 
    192  1.81  riastrad 	if (cn_tab == NULL) {
    193  1.81  riastrad 		error = 0;
    194  1.81  riastrad 		goto out;
    195  1.81  riastrad 	}
    196  1.10       cgd 
    197  1.65        ad 	vp = cn_devvp[unit];
    198  1.65        ad 	cn_devvp[unit] = NULL;
    199  1.78  riastrad 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    200  1.78  riastrad 	error = VOP_CLOSE(vp, flag, kauth_cred_get());
    201  1.78  riastrad 	VOP_UNLOCK(vp);
    202  1.78  riastrad 	vrele(vp);
    203  1.81  riastrad 
    204  1.81  riastrad out:	mutex_exit(&cn_lock);
    205  1.65        ad 	return error;
    206   1.1       cgd }
    207  1.54     perry 
    208   1.7    andrew int
    209  1.46      matt cnread(dev_t dev, struct uio *uio, int flag)
    210   1.1       cgd {
    211  1.91  riastrad 	struct tty *ctp = NULL;
    212  1.50       dsl 	int error;
    213  1.21   mycroft 
    214  1.10       cgd 	/*
    215  1.10       cgd 	 * If we would redirect input, punt.  This will keep strange
    216  1.10       cgd 	 * things from happening to people who are using the real
    217  1.10       cgd 	 * console.  Nothing should be using /dev/console for
    218  1.10       cgd 	 * input (except a shell in single-user mode, but then,
    219  1.10       cgd 	 * one wouldn't TIOCCONS then).
    220  1.10       cgd 	 */
    221  1.91  riastrad 	if (!cn_redirect(&dev, 1, &error, &ctp))
    222  1.50       dsl 		return error;
    223  1.91  riastrad 	error = cdev_read(dev, uio, flag);
    224  1.91  riastrad 	cn_release(ctp);
    225  1.91  riastrad 	return error;
    226   1.1       cgd }
    227  1.54     perry 
    228   1.7    andrew int
    229  1.46      matt cnwrite(dev_t dev, struct uio *uio, int flag)
    230   1.1       cgd {
    231  1.91  riastrad 	struct tty *ctp = NULL;
    232  1.50       dsl 	int error;
    233  1.21   mycroft 
    234  1.50       dsl 	/* Redirect output, if that's appropriate. */
    235  1.91  riastrad 	if (!cn_redirect(&dev, 0, &error, &ctp))
    236  1.50       dsl 		return error;
    237  1.91  riastrad 	error = cdev_write(dev, uio, flag);
    238  1.91  riastrad 	cn_release(ctp);
    239  1.91  riastrad 	return error;
    240  1.25   mycroft }
    241  1.25   mycroft 
    242   1.7    andrew int
    243  1.63  christos cnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    244   1.1       cgd {
    245  1.91  riastrad 	struct tty *ctp = NULL;
    246   1.1       cgd 	int error;
    247   1.1       cgd 
    248  1.62        ad 	error = 0;
    249  1.62        ad 
    250   1.1       cgd 	/*
    251   1.1       cgd 	 * Superuser can always use this to wrest control of console
    252   1.1       cgd 	 * output from the "virtual" console.
    253   1.1       cgd 	 */
    254  1.91  riastrad 	if (cmd == TIOCCONS) {
    255  1.91  riastrad 		struct tty *tp;
    256  1.91  riastrad 
    257  1.91  riastrad 		mutex_enter(&constty_lock);
    258  1.91  riastrad 		tp = atomic_load_relaxed(&constty);
    259  1.91  riastrad 		if (tp == NULL) {
    260  1.91  riastrad 			mutex_exit(&constty_lock);
    261  1.91  riastrad 			goto passthrough; /* XXX ??? */
    262  1.91  riastrad 		}
    263  1.69      elad 		error = kauth_authorize_device_tty(l->l_cred,
    264  1.91  riastrad 		    KAUTH_DEVICE_TTY_VIRTUAL, tp);
    265  1.62        ad 		if (!error)
    266  1.91  riastrad 			atomic_store_relaxed(&constty, NULL);
    267  1.91  riastrad 		mutex_exit(&constty_lock);
    268  1.91  riastrad 		return error;
    269   1.1       cgd 	}
    270  1.91  riastrad passthrough:
    271  1.10       cgd 	/*
    272  1.10       cgd 	 * Redirect the ioctl, if that's appropriate.
    273  1.10       cgd 	 * Note that strange things can happen, if a program does
    274  1.10       cgd 	 * ioctls on /dev/console, then the console is redirected
    275  1.10       cgd 	 * out from under it.
    276  1.10       cgd 	 */
    277  1.91  riastrad 	if (!cn_redirect(&dev, 0, &error, &ctp))
    278  1.64        ad 		return error;
    279  1.91  riastrad 	error = cdev_ioctl(dev, cmd, data, flag, l);
    280  1.91  riastrad 	cn_release(ctp);
    281  1.91  riastrad 	return error;
    282   1.1       cgd }
    283   1.1       cgd 
    284   1.1       cgd /*ARGSUSED*/
    285   1.7    andrew int
    286  1.57  christos cnpoll(dev_t dev, int events, struct lwp *l)
    287   1.1       cgd {
    288  1.91  riastrad 	struct tty *ctp = NULL;
    289  1.50       dsl 	int error;
    290  1.21   mycroft 
    291  1.10       cgd 	/*
    292  1.38     lukem 	 * Redirect the poll, if that's appropriate.
    293  1.10       cgd 	 * I don't want to think of the possible side effects
    294  1.10       cgd 	 * of console redirection here.
    295  1.10       cgd 	 */
    296  1.91  riastrad 	if (!cn_redirect(&dev, 0, &error, &ctp))
    297  1.56        ws 		return POLLHUP;
    298  1.91  riastrad 	error = cdev_poll(dev, events, l);
    299  1.91  riastrad 	cn_release(ctp);
    300  1.91  riastrad 	return error;
    301  1.45  jdolecek }
    302  1.45  jdolecek 
    303  1.45  jdolecek /*ARGSUSED*/
    304  1.45  jdolecek int
    305  1.46      matt cnkqfilter(dev_t dev, struct knote *kn)
    306  1.45  jdolecek {
    307  1.91  riastrad 	struct tty *ctp = NULL;
    308  1.50       dsl 	int error;
    309  1.45  jdolecek 
    310  1.45  jdolecek 	/*
    311  1.45  jdolecek 	 * Redirect the kqfilter, if that's appropriate.
    312  1.45  jdolecek 	 * I don't want to think of the possible side effects
    313  1.45  jdolecek 	 * of console redirection here.
    314  1.45  jdolecek 	 */
    315  1.91  riastrad 	if (!cn_redirect(&dev, 0, &error, &ctp))
    316  1.50       dsl 		return error;
    317  1.91  riastrad 	error = cdev_kqfilter(dev, kn);
    318  1.91  riastrad 	cn_release(ctp);
    319  1.91  riastrad 	return error;
    320   1.1       cgd }
    321   1.1       cgd 
    322   1.7    andrew int
    323  1.46      matt cngetc(void)
    324   1.1       cgd {
    325   1.1       cgd 	if (cn_tab == NULL)
    326   1.1       cgd 		return (0);
    327  1.70      matt 	int s = splhigh();
    328  1.70      matt 	for (;;) {
    329  1.70      matt 		const int rv = (*cn_tab->cn_getc)(cn_tab->cn_dev);
    330  1.70      matt 		if (rv >= 0) {
    331  1.70      matt 			splx(s);
    332  1.70      matt 			return rv;
    333  1.70      matt 		}
    334  1.70      matt 		docritpollhooks();
    335  1.70      matt 	}
    336  1.36    itojun }
    337  1.36    itojun 
    338  1.36    itojun int
    339  1.46      matt cngetsn(char *cp, int size)
    340  1.36    itojun {
    341  1.36    itojun 	char *lp;
    342  1.36    itojun 	int c, len;
    343  1.36    itojun 
    344  1.36    itojun 	cnpollc(1);
    345  1.36    itojun 
    346  1.36    itojun 	lp = cp;
    347  1.36    itojun 	len = 0;
    348  1.36    itojun 	for (;;) {
    349  1.36    itojun 		c = cngetc();
    350  1.36    itojun 		switch (c) {
    351  1.36    itojun 		case '\n':
    352  1.36    itojun 		case '\r':
    353  1.36    itojun 			printf("\n");
    354  1.36    itojun 			*lp++ = '\0';
    355  1.36    itojun 			cnpollc(0);
    356  1.36    itojun 			return (len);
    357  1.36    itojun 		case '\b':
    358  1.36    itojun 		case '\177':
    359  1.36    itojun 		case '#':
    360  1.36    itojun 			if (len) {
    361  1.36    itojun 				--len;
    362  1.36    itojun 				--lp;
    363  1.36    itojun 				printf("\b \b");
    364  1.36    itojun 			}
    365  1.36    itojun 			continue;
    366  1.36    itojun 		case '@':
    367  1.40  jdolecek 		case 'u'&037:	/* CTRL-u */
    368  1.36    itojun 			len = 0;
    369  1.36    itojun 			lp = cp;
    370  1.36    itojun 			printf("\n");
    371  1.36    itojun 			continue;
    372  1.36    itojun 		default:
    373  1.36    itojun 			if (len + 1 >= size || c < ' ') {
    374  1.40  jdolecek 				printf("\007");
    375  1.36    itojun 				continue;
    376  1.36    itojun 			}
    377  1.36    itojun 			printf("%c", c);
    378  1.36    itojun 			++len;
    379  1.36    itojun 			*lp++ = c;
    380  1.36    itojun 		}
    381  1.36    itojun 	}
    382   1.1       cgd }
    383   1.1       cgd 
    384  1.29  christos void
    385  1.46      matt cnputc(int c)
    386   1.1       cgd {
    387  1.21   mycroft 
    388   1.1       cgd 	if (cn_tab == NULL)
    389  1.54     perry 		return;
    390  1.29  christos 
    391  1.75  macallan /*
    392  1.75  macallan  * XXX
    393  1.75  macallan  * for some reason this causes ARCS firmware to output an endless stream of
    394  1.75  macallan  * whitespaces with n32 kernels, so use the pre-1.74 code for now until I can
    395  1.75  macallan  * figure out why this happens
    396  1.75  macallan  */
    397  1.75  macallan #ifndef sgimips
    398   1.1       cgd 	if (c) {
    399  1.70      matt 		if (c == '\n') {
    400  1.74  nakayama 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
    401  1.70      matt 			docritpollhooks();
    402  1.70      matt 		}
    403  1.74  nakayama 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
    404   1.1       cgd 	}
    405  1.75  macallan #else
    406  1.75  macallan 	if (c) {
    407  1.75  macallan 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
    408  1.75  macallan 		if (c == '\n') {
    409  1.75  macallan 			docritpollhooks();
    410  1.75  macallan 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
    411  1.75  macallan 		}
    412  1.75  macallan 	}
    413  1.75  macallan #endif
    414  1.19   mycroft }
    415  1.19   mycroft 
    416  1.19   mycroft void
    417  1.46      matt cnpollc(int on)
    418  1.19   mycroft {
    419  1.19   mycroft 	static int refcount = 0;
    420  1.19   mycroft 
    421  1.19   mycroft 	if (cn_tab == NULL)
    422  1.19   mycroft 		return;
    423  1.19   mycroft 	if (!on)
    424  1.19   mycroft 		--refcount;
    425  1.94  riastrad 	if (refcount == 0) {
    426  1.94  riastrad #ifdef HEARTBEAT
    427  1.94  riastrad 		if (on) {
    428  1.94  riastrad 			/*
    429  1.94  riastrad 			 * Bind to the current CPU by disabling
    430  1.94  riastrad 			 * preemption (more convenient than finding a
    431  1.94  riastrad 			 * place to store a stack to unwind for
    432  1.94  riastrad 			 * curlwp_bind/bindx, and preemption wouldn't
    433  1.94  riastrad 			 * happen anyway while spinning at high IPL in
    434  1.94  riastrad 			 * cngetc) so that curcpu() is stable so that
    435  1.94  riastrad 			 * we can suspend heartbeat checks for it.
    436  1.94  riastrad 			 */
    437  1.94  riastrad 			kpreempt_disable();
    438  1.94  riastrad 			heartbeat_suspend();
    439  1.94  riastrad 		}
    440  1.94  riastrad #endif
    441  1.19   mycroft 		(*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
    442  1.94  riastrad #ifdef HEARTBEAT
    443  1.94  riastrad 		if (!on) {
    444  1.94  riastrad 			heartbeat_resume();
    445  1.94  riastrad 			kpreempt_enable();
    446  1.94  riastrad 		}
    447  1.94  riastrad #endif
    448  1.94  riastrad 	}
    449  1.19   mycroft 	if (on)
    450  1.19   mycroft 		++refcount;
    451  1.21   mycroft }
    452  1.21   mycroft 
    453  1.21   mycroft void
    454  1.61  christos nullcnpollc(dev_t dev, int on)
    455  1.21   mycroft {
    456  1.21   mycroft 
    457  1.34   thorpej }
    458  1.34   thorpej 
    459  1.34   thorpej void
    460  1.46      matt cnbell(u_int pitch, u_int period, u_int volume)
    461  1.34   thorpej {
    462  1.34   thorpej 
    463  1.34   thorpej 	if (cn_tab == NULL || cn_tab->cn_bell == NULL)
    464  1.34   thorpej 		return;
    465  1.34   thorpej 	(*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
    466  1.46      matt }
    467  1.46      matt 
    468  1.46      matt void
    469  1.46      matt cnflush(void)
    470  1.46      matt {
    471  1.46      matt 	if (cn_tab == NULL || cn_tab->cn_flush == NULL)
    472  1.46      matt 		return;
    473  1.46      matt 	(*cn_tab->cn_flush)(cn_tab->cn_dev);
    474  1.46      matt }
    475  1.54     perry 
    476  1.46      matt void
    477  1.46      matt cnhalt(void)
    478  1.46      matt {
    479  1.46      matt 	if (cn_tab == NULL || cn_tab->cn_halt == NULL)
    480  1.46      matt 		return;
    481  1.46      matt 	(*cn_tab->cn_halt)(cn_tab->cn_dev);
    482  1.50       dsl }
    483  1.50       dsl 
    484  1.62        ad /*
    485  1.62        ad  * Redirect output, if that's appropriate.  If there's no real console,
    486  1.62        ad  * return ENXIO.
    487  1.62        ad  */
    488  1.64        ad static bool
    489  1.91  riastrad cn_redirect(dev_t *devp, int is_read, int *error, struct tty **ctpp)
    490  1.50       dsl {
    491  1.50       dsl 	dev_t dev = *devp;
    492  1.91  riastrad 	struct tty *ctp;
    493  1.91  riastrad 	int s;
    494  1.91  riastrad 	bool ok = false;
    495  1.50       dsl 
    496  1.50       dsl 	*error = ENXIO;
    497  1.91  riastrad 	*ctpp = NULL;
    498  1.91  riastrad 	s = pserialize_read_enter();
    499  1.91  riastrad 	if ((ctp = atomic_load_consume(&constty)) != NULL && minor(dev) == 0 &&
    500  1.55    martin 	    (cn_tab == NULL || (cn_tab->cn_pri != CN_REMOTE))) {
    501  1.50       dsl 		if (is_read) {
    502  1.50       dsl 			*error = 0;
    503  1.91  riastrad 			goto out;
    504  1.50       dsl 		}
    505  1.91  riastrad 		tty_acquire(ctp);
    506  1.91  riastrad 		*ctpp = ctp;
    507  1.91  riastrad 		dev = ctp->t_dev;
    508  1.86  riastrad 	} else if (cn_tab == NULL)
    509  1.91  riastrad 		goto out;
    510  1.86  riastrad 	else
    511  1.50       dsl 		dev = cn_tab->cn_dev;
    512  1.91  riastrad 	ok = true;
    513  1.50       dsl 	*devp = dev;
    514  1.91  riastrad out:	pserialize_read_exit(s);
    515  1.91  riastrad 	return ok;
    516  1.91  riastrad }
    517  1.91  riastrad 
    518  1.91  riastrad static void
    519  1.91  riastrad cn_release(struct tty *ctp)
    520  1.91  riastrad {
    521  1.91  riastrad 
    522  1.91  riastrad 	if (ctp == NULL)
    523  1.91  riastrad 		return;
    524  1.91  riastrad 	tty_release(ctp);
    525   1.2       cgd }
    526  1.81  riastrad 
    527  1.81  riastrad MODULE(MODULE_CLASS_DRIVER, cons, NULL);
    528  1.81  riastrad 
    529  1.81  riastrad static int
    530  1.81  riastrad cons_modcmd(modcmd_t cmd, void *arg)
    531  1.81  riastrad {
    532  1.81  riastrad 
    533  1.81  riastrad 	switch (cmd) {
    534  1.81  riastrad 	case MODULE_CMD_INIT:
    535  1.81  riastrad 		mutex_init(&cn_lock, MUTEX_DEFAULT, IPL_NONE);
    536  1.81  riastrad 		return 0;
    537  1.81  riastrad 	case MODULE_CMD_FINI:
    538  1.81  riastrad 		mutex_destroy(&cn_lock);
    539  1.81  riastrad 		return 0;
    540  1.81  riastrad 	default:
    541  1.81  riastrad 		return ENOTTY;
    542  1.81  riastrad 	}
    543  1.81  riastrad }
    544