xref: /netbsd-src/sys/dev/nullcons_subr.c (revision da9817918ec7e88db2912a2882967c7570a83f47)
1 /*	$NetBSD: nullcons_subr.c,v 1.9 2009/04/16 12:57:22 tsutsui Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: nullcons_subr.c,v 1.9 2009/04/16 12:57:22 tsutsui Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/proc.h>
34 #include <sys/user.h>
35 #include <sys/systm.h>
36 #include <sys/buf.h>
37 #include <sys/ioctl.h>
38 #include <sys/tty.h>
39 #include <sys/file.h>
40 #include <sys/conf.h>
41 #include <sys/vnode.h>
42 
43 #include <dev/cons.h>
44 
45 static struct tty *nulltty;		/* null console tty */
46 
47 cons_decl(null);
48 
49 dev_type_read(nullcndev_read);
50 dev_type_ioctl(nullcndev_ioctl);
51 dev_type_tty(nullcndev_tty);
52 
53 static int	nullcons_newdev(struct consdev *);
54 
55 const struct cdevsw nullcn_devsw = {
56 	nullopen, nullclose, nullcndev_read, nullwrite, nullcndev_ioctl,
57 	nullstop, nullcndev_tty, nopoll, nommap, ttykqfilter, D_TTY
58 };
59 
60 /*
61  * null console device. We need it because of the ioctl() it handles,
62  * which in particular allows control terminal allocation through
63  * TIOCSCTTY ioctl. Without the latter, system won't even boot past init(8)
64  * invocation.
65  */
66 int
67 nullcndev_read(dev_t dev, struct uio *uio, int flag)
68 {
69 
70 	return EIO;
71 }
72 
73 int
74 nullcndev_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
75 {
76 	int error;
77 
78 	error = (*nulltty->t_linesw->l_ioctl)(nulltty, cmd, data, flag, l);
79 	if (error != EPASSTHROUGH)
80 		return error;
81 
82 	error = ttioctl(nulltty, cmd, data, flag, l);
83 	if (error != EPASSTHROUGH)
84 		return error;
85 
86 	return 0;
87 }
88 
89 struct tty*
90 nullcndev_tty(dev_t dev)
91 {
92 
93 	return nulltty;
94 }
95 
96 /*
97  * Mark console as no-op (null) console. Proper initialization is deferred
98  * to nullconsattach().
99  */
100 void
101 nullcnprobe(struct consdev *cn)
102 {
103 
104 	cn->cn_pri = CN_NULL;
105 	cn->cn_dev = NODEV;
106 }
107 
108 /*
109  * null console initialization. This includes allocation of a new device and
110  * a new tty.
111  */
112 void
113 nullcninit(struct consdev *cn)
114 {
115 	static struct consdev nullcn = cons_init(null);
116 
117 	nullcnprobe(&nullcn);
118 	cn_tab = &nullcn;
119 }
120 
121 /*
122  * Dumb getc() implementation. Simply blocks on call.
123  */
124 int
125 nullcngetc(dev_t dev)
126 {
127 
128 	for (;;)
129 		;
130 	return 0;
131 }
132 
133 /*
134  * Dumb putc() implementation.
135  */
136 void
137 nullcnputc(dev_t dev, int c)
138 {
139 
140 }
141 
142 /*
143  * Allocate a new console device and a tty to handle console ioctls.
144  */
145 int
146 nullcons_newdev(struct consdev *cn)
147 {
148 	int error;
149 	int bmajor = -1, cmajor = -1;
150 
151 	if ((cn == NULL) || (cn->cn_pri != CN_NULL) || (cn->cn_dev != NODEV))
152 		return 0;
153 
154 	/*
155 	 * Attach no-op device to the device list.
156 	 */
157 	error = devsw_attach("nullcn", NULL, &bmajor, &nullcn_devsw, &cmajor);
158 	if (error != 0)
159 		return error;
160 
161 	/*
162 	 * Allocate tty (mostly to have sane ioctl()).
163 	 */
164 	nulltty = ttymalloc();
165 	nulltty->t_dev = makedev(cmajor, 0);
166 	tty_attach(nulltty);
167 	cn->cn_dev = nulltty->t_dev;
168 
169 	return 0;
170 }
171 
172 /*
173  * Pseudo-device attach function -- it's the right time to do the rest of
174  * initialization.
175  */
176 void
177 nullconsattach(int pdev_count)
178 {
179 
180 	nullcons_newdev(cn_tab);
181 }
182