xref: /netbsd-src/sys/dev/nullcons_subr.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: nullcons_subr.c,v 1.6 2008/04/28 20:23:47 martin 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.6 2008/04/28 20:23:47 martin 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 	for(;;);
77 	return (0);
78 }
79 
80 int
81 nullcndev_ioctl(dev, cmd, data, flag, l)
82 	dev_t dev;
83 	u_long cmd;
84 	void *data;
85 	int flag;
86 	struct lwp *l;
87 {
88 	int error;
89 
90 	error = (*nulltty->t_linesw->l_ioctl)(nulltty, cmd, data, flag, l);
91 	if (error != EPASSTHROUGH)
92 		return (error);
93 
94 	error = ttioctl(nulltty, cmd, data, flag, l);
95 	if (error != EPASSTHROUGH)
96 		return (error);
97 
98 	return (0);
99 }
100 
101 struct tty*
102 nullcndev_tty(dev)
103 	dev_t dev;
104 {
105 
106 	return nulltty;
107 }
108 
109 /*
110  * Mark console as no-op (null) console. Proper initialization is deferred
111  * to nullconsattach().
112  */
113 void
114 nullcnprobe(cn)
115 	struct consdev *cn;
116 {
117 
118 	cn->cn_pri = CN_NULL;
119 	cn->cn_dev = NODEV;
120 }
121 
122 /*
123  * null console initialization. This includes allocation of a new device and
124  * a new tty.
125  */
126 void
127 nullcninit(cn)
128 	struct consdev *cn;
129 {
130 	static struct consdev nullcn = cons_init(null);
131 
132 	nullcnprobe(&nullcn);
133 	cn_tab = &nullcn;
134 }
135 
136 /*
137  * Dumb getc() implementation. Simply blocks on call.
138  */
139 int
140 nullcngetc(dev)
141 	dev_t dev;
142 {
143 
144 	for(;;);
145 	return (0);
146 }
147 
148 /*
149  * Dumb putc() implementation.
150  */
151 void
152 nullcnputc(dev, c)
153 	dev_t dev;
154 	int c;
155 {
156 
157 }
158 
159 /*
160  * Allocate a new console device and a tty to handle console ioctls.
161  */
162 int
163 nullcons_newdev(cn)
164 	struct consdev *cn;
165 {
166 	int error;
167 	int bmajor = -1, cmajor = -1;
168 
169 	if ((cn == NULL) || (cn->cn_pri != CN_NULL) || (cn->cn_dev != NODEV))
170 		return (0);
171 
172 	/*
173 	 * Attach no-op device to the device list.
174 	 */
175 	error = devsw_attach("nullcn", NULL, &bmajor, &nullcn_devsw, &cmajor);
176 	if (error != 0)
177 		return (error);
178 
179 	/*
180 	 * Allocate tty (mostly to have sane ioctl()).
181 	 */
182 	nulltty = ttymalloc();
183 	nulltty->t_dev = makedev(cmajor, 0);
184 	tty_attach(nulltty);
185 	cn->cn_dev = nulltty->t_dev;
186 
187 	return (0);
188 }
189 
190 /*
191  * Pseudo-device attach function -- it's the right time to do the rest of
192  * initialization.
193  */
194 void
195 nullconsattach(pdev_count)
196 	int pdev_count;
197 {
198 
199 	nullcons_newdev(cn_tab);
200 }
201