xref: /netbsd/sys/dev/nullcons_subr.c (revision 1a918832)
1*1a918832Sdholland /*	$NetBSD: nullcons_subr.c,v 1.13 2014/07/25 08:10:35 dholland Exp $	*/
261da4642Scdi 
361da4642Scdi /*-
461da4642Scdi  * Copyright (c) 2003 The NetBSD Foundation, Inc.
561da4642Scdi  * All rights reserved.
661da4642Scdi  *
761da4642Scdi  * Redistribution and use in source and binary forms, with or without
861da4642Scdi  * modification, are permitted provided that the following conditions
961da4642Scdi  * are met:
1061da4642Scdi  * 1. Redistributions of source code must retain the above copyright
1161da4642Scdi  *    notice, this list of conditions and the following disclaimer.
1261da4642Scdi  * 2. Redistributions in binary form must reproduce the above copyright
1361da4642Scdi  *    notice, this list of conditions and the following disclaimer in the
1461da4642Scdi  *    documentation and/or other materials provided with the distribution.
1561da4642Scdi  *
1661da4642Scdi  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1761da4642Scdi  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1861da4642Scdi  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1961da4642Scdi  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2061da4642Scdi  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2161da4642Scdi  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2261da4642Scdi  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2361da4642Scdi  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2461da4642Scdi  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2561da4642Scdi  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2661da4642Scdi  * POSSIBILITY OF SUCH DAMAGE.
2761da4642Scdi  */
2861da4642Scdi 
2961da4642Scdi #include <sys/cdefs.h>
30*1a918832Sdholland __KERNEL_RCSID(0, "$NetBSD: nullcons_subr.c,v 1.13 2014/07/25 08:10:35 dholland Exp $");
3161da4642Scdi 
3261da4642Scdi #include <sys/param.h>
3361da4642Scdi #include <sys/proc.h>
3461da4642Scdi #include <sys/systm.h>
3561da4642Scdi #include <sys/buf.h>
3661da4642Scdi #include <sys/ioctl.h>
3761da4642Scdi #include <sys/tty.h>
3861da4642Scdi #include <sys/file.h>
3961da4642Scdi #include <sys/conf.h>
4061da4642Scdi #include <sys/vnode.h>
4161da4642Scdi 
4261da4642Scdi #include <dev/cons.h>
4361da4642Scdi 
4461da4642Scdi static struct tty *nulltty;		/* null console tty */
4561da4642Scdi 
4661da4642Scdi cons_decl(null);
4761da4642Scdi 
4861da4642Scdi dev_type_read(nullcndev_read);
4961da4642Scdi dev_type_ioctl(nullcndev_ioctl);
5061da4642Scdi dev_type_tty(nullcndev_tty);
5161da4642Scdi 
5218db93c7Sperry static int	nullcons_newdev(struct consdev *);
5361da4642Scdi 
5461da4642Scdi const struct cdevsw nullcn_devsw = {
5576258fa0Sdholland 	.d_open = nullopen,
5676258fa0Sdholland 	.d_close = nullclose,
5776258fa0Sdholland 	.d_read = nullcndev_read,
5876258fa0Sdholland 	.d_write = nullwrite,
5976258fa0Sdholland 	.d_ioctl = nullcndev_ioctl,
6076258fa0Sdholland 	.d_stop = nullstop,
6176258fa0Sdholland 	.d_tty = nullcndev_tty,
6276258fa0Sdholland 	.d_poll = nopoll,
6376258fa0Sdholland 	.d_mmap = nommap,
6476258fa0Sdholland 	.d_kqfilter = ttykqfilter,
65*1a918832Sdholland 	.d_discard = nodiscard,
6676258fa0Sdholland 	.d_flag = D_TTY
6761da4642Scdi };
6861da4642Scdi 
6961da4642Scdi /*
7061da4642Scdi  * null console device. We need it because of the ioctl() it handles,
7161da4642Scdi  * which in particular allows control terminal allocation through
7261da4642Scdi  * TIOCSCTTY ioctl. Without the latter, system won't even boot past init(8)
7361da4642Scdi  * invocation.
7461da4642Scdi  */
7561da4642Scdi int
nullcndev_read(dev_t dev,struct uio * uio,int flag)76454af1c0Sdsl nullcndev_read(dev_t dev, struct uio *uio, int flag)
7761da4642Scdi {
7861da4642Scdi 
7913a1c5a1Stsutsui 	return EIO;
8061da4642Scdi }
8161da4642Scdi 
8261da4642Scdi int
nullcndev_ioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)83454af1c0Sdsl nullcndev_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
8461da4642Scdi {
8561da4642Scdi 	int error;
8661da4642Scdi 
8795e1ffb1Schristos 	error = (*nulltty->t_linesw->l_ioctl)(nulltty, cmd, data, flag, l);
8861da4642Scdi 	if (error != EPASSTHROUGH)
8939014001Stsutsui 		return error;
9061da4642Scdi 
9195e1ffb1Schristos 	error = ttioctl(nulltty, cmd, data, flag, l);
9261da4642Scdi 	if (error != EPASSTHROUGH)
9339014001Stsutsui 		return error;
9461da4642Scdi 
9539014001Stsutsui 	return 0;
9661da4642Scdi }
9761da4642Scdi 
9861da4642Scdi struct tty*
nullcndev_tty(dev_t dev)99454af1c0Sdsl nullcndev_tty(dev_t dev)
10061da4642Scdi {
10161da4642Scdi 
10261da4642Scdi 	return nulltty;
10361da4642Scdi }
10461da4642Scdi 
10561da4642Scdi /*
10661da4642Scdi  * Mark console as no-op (null) console. Proper initialization is deferred
10761da4642Scdi  * to nullconsattach().
10861da4642Scdi  */
10961da4642Scdi void
nullcnprobe(struct consdev * cn)110454af1c0Sdsl nullcnprobe(struct consdev *cn)
11161da4642Scdi {
11261da4642Scdi 
11361da4642Scdi 	cn->cn_pri = CN_NULL;
11461da4642Scdi 	cn->cn_dev = NODEV;
11561da4642Scdi }
11661da4642Scdi 
11761da4642Scdi /*
11861da4642Scdi  * null console initialization. This includes allocation of a new device and
11961da4642Scdi  * a new tty.
12061da4642Scdi  */
12161da4642Scdi void
nullcninit(struct consdev * cn)122454af1c0Sdsl nullcninit(struct consdev *cn)
12361da4642Scdi {
12461da4642Scdi 	static struct consdev nullcn = cons_init(null);
12561da4642Scdi 
12661da4642Scdi 	nullcnprobe(&nullcn);
12761da4642Scdi 	cn_tab = &nullcn;
12861da4642Scdi }
12961da4642Scdi 
13061da4642Scdi /*
13161da4642Scdi  * Dumb getc() implementation. Simply blocks on call.
13261da4642Scdi  */
13361da4642Scdi int
nullcngetc(dev_t dev)134454af1c0Sdsl nullcngetc(dev_t dev)
13561da4642Scdi {
13661da4642Scdi 
13739014001Stsutsui 	for (;;)
13839014001Stsutsui 		;
13939014001Stsutsui 	return 0;
14061da4642Scdi }
14161da4642Scdi 
14261da4642Scdi /*
14361da4642Scdi  * Dumb putc() implementation.
14461da4642Scdi  */
14561da4642Scdi void
nullcnputc(dev_t dev,int c)146454af1c0Sdsl nullcnputc(dev_t dev, int c)
14761da4642Scdi {
14861da4642Scdi 
14961da4642Scdi }
15061da4642Scdi 
15161da4642Scdi /*
15261da4642Scdi  * Allocate a new console device and a tty to handle console ioctls.
15361da4642Scdi  */
15461da4642Scdi int
nullcons_newdev(struct consdev * cn)155454af1c0Sdsl nullcons_newdev(struct consdev *cn)
15661da4642Scdi {
15761da4642Scdi 	int error;
15861da4642Scdi 	int bmajor = -1, cmajor = -1;
15961da4642Scdi 
16061da4642Scdi 	if ((cn == NULL) || (cn->cn_pri != CN_NULL) || (cn->cn_dev != NODEV))
16139014001Stsutsui 		return 0;
16261da4642Scdi 
16361da4642Scdi 	/*
16461da4642Scdi 	 * Attach no-op device to the device list.
16561da4642Scdi 	 */
16661da4642Scdi 	error = devsw_attach("nullcn", NULL, &bmajor, &nullcn_devsw, &cmajor);
16761da4642Scdi 	if (error != 0)
16839014001Stsutsui 		return error;
16961da4642Scdi 
17061da4642Scdi 	/*
17161da4642Scdi 	 * Allocate tty (mostly to have sane ioctl()).
17261da4642Scdi 	 */
17380f6fc52Srmind 	nulltty = tty_alloc();
17461da4642Scdi 	nulltty->t_dev = makedev(cmajor, 0);
17561da4642Scdi 	tty_attach(nulltty);
17661da4642Scdi 	cn->cn_dev = nulltty->t_dev;
17761da4642Scdi 
17839014001Stsutsui 	return 0;
17961da4642Scdi }
18061da4642Scdi 
18161da4642Scdi /*
18261da4642Scdi  * Pseudo-device attach function -- it's the right time to do the rest of
18361da4642Scdi  * initialization.
18461da4642Scdi  */
18561da4642Scdi void
nullconsattach(int pdev_count)186454af1c0Sdsl nullconsattach(int pdev_count)
18761da4642Scdi {
18861da4642Scdi 
18961da4642Scdi 	nullcons_newdev(cn_tab);
19061da4642Scdi }
191