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