xref: /netbsd-src/sys/dev/nullcons_subr.c (revision 6a1508dad3515842aa76bf5ec8fc2daab5f5af02)
1 /*	$NetBSD: nullcons_subr.c,v 1.7 2009/02/28 00:40:47 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.7 2009/02/28 00:40:47 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 
46 extern struct consdev *cn_tab;		/* physical console device info */
47 
48 static struct tty *nulltty;		/* null console tty */
49 
50 cons_decl(null);
51 
52 dev_type_read(nullcndev_read);
53 dev_type_ioctl(nullcndev_ioctl);
54 dev_type_tty(nullcndev_tty);
55 
56 static int	nullcons_newdev(struct consdev *);
57 
58 const struct cdevsw nullcn_devsw = {
59 	nullopen, nullclose, nullcndev_read, nullwrite, nullcndev_ioctl,
60 	nullstop, nullcndev_tty, nopoll, nommap, ttykqfilter, D_TTY
61 };
62 
63 /*
64  * null console device. We need it because of the ioctl() it handles,
65  * which in particular allows control terminal allocation through
66  * TIOCSCTTY ioctl. Without the latter, system won't even boot past init(8)
67  * invocation.
68  */
69 int
70 nullcndev_read(dev, uio, flag)
71 	dev_t dev;
72 	struct uio *uio;
73 	int flag;
74 {
75 
76 	return EIO;
77 }
78 
79 int
80 nullcndev_ioctl(dev, cmd, data, flag, l)
81 	dev_t dev;
82 	u_long cmd;
83 	void *data;
84 	int flag;
85 	struct lwp *l;
86 {
87 	int error;
88 
89 	error = (*nulltty->t_linesw->l_ioctl)(nulltty, cmd, data, flag, l);
90 	if (error != EPASSTHROUGH)
91 		return (error);
92 
93 	error = ttioctl(nulltty, cmd, data, flag, l);
94 	if (error != EPASSTHROUGH)
95 		return (error);
96 
97 	return (0);
98 }
99 
100 struct tty*
101 nullcndev_tty(dev)
102 	dev_t dev;
103 {
104 
105 	return nulltty;
106 }
107 
108 /*
109  * Mark console as no-op (null) console. Proper initialization is deferred
110  * to nullconsattach().
111  */
112 void
113 nullcnprobe(cn)
114 	struct consdev *cn;
115 {
116 
117 	cn->cn_pri = CN_NULL;
118 	cn->cn_dev = NODEV;
119 }
120 
121 /*
122  * null console initialization. This includes allocation of a new device and
123  * a new tty.
124  */
125 void
126 nullcninit(cn)
127 	struct consdev *cn;
128 {
129 	static struct consdev nullcn = cons_init(null);
130 
131 	nullcnprobe(&nullcn);
132 	cn_tab = &nullcn;
133 }
134 
135 /*
136  * Dumb getc() implementation. Simply blocks on call.
137  */
138 int
139 nullcngetc(dev)
140 	dev_t dev;
141 {
142 
143 	for(;;);
144 	return (0);
145 }
146 
147 /*
148  * Dumb putc() implementation.
149  */
150 void
151 nullcnputc(dev, c)
152 	dev_t dev;
153 	int c;
154 {
155 
156 }
157 
158 /*
159  * Allocate a new console device and a tty to handle console ioctls.
160  */
161 int
162 nullcons_newdev(cn)
163 	struct consdev *cn;
164 {
165 	int error;
166 	int bmajor = -1, cmajor = -1;
167 
168 	if ((cn == NULL) || (cn->cn_pri != CN_NULL) || (cn->cn_dev != NODEV))
169 		return (0);
170 
171 	/*
172 	 * Attach no-op device to the device list.
173 	 */
174 	error = devsw_attach("nullcn", NULL, &bmajor, &nullcn_devsw, &cmajor);
175 	if (error != 0)
176 		return (error);
177 
178 	/*
179 	 * Allocate tty (mostly to have sane ioctl()).
180 	 */
181 	nulltty = ttymalloc();
182 	nulltty->t_dev = makedev(cmajor, 0);
183 	tty_attach(nulltty);
184 	cn->cn_dev = nulltty->t_dev;
185 
186 	return (0);
187 }
188 
189 /*
190  * Pseudo-device attach function -- it's the right time to do the rest of
191  * initialization.
192  */
193 void
194 nullconsattach(pdev_count)
195 	int pdev_count;
196 {
197 
198 	nullcons_newdev(cn_tab);
199 }
200