1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * Indirect driver for console
31 *
32 * The purpose of this driver is to provide a device node indirection for
33 * the console device, which can be any tty class device. It does this by
34 * externalizing a global pointer "constty", which is then pointed at the
35 * console tty device.
36 *
37 * The default for this pointer is uninitialized; when it is NULL, we fall
38 * back to the "km" device, which is a tty BSD wrapper device for the
39 * Platform Expert console device. When it is non-NULL, we call through
40 * to the tty device device instead.
41 *
42 * The registration for this device node is static, and the devfs init
43 * code does not externalize a named device for it, to avoid software
44 * seeing the device and trying to open it.
45 *
46 * The upshot of this is that the console driver should not be set as your
47 * controlling tty, since you will get a reference to a device which does
48 * not have an actual device node in /dev, so its name cannot be looked up.
49 */
50 #include <machine/cons.h>
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/conf.h>
54 #include <sys/ioctl.h>
55 #include <sys/tty.h>
56 #include <sys/proc.h>
57 #include <sys/uio.h>
58
59 struct tty *_constty; /* current console device */
60 static LCK_GRP_DECLARE(constty_lock_grp, "constty");
61 static LCK_MTX_DECLARE(_constty_lock, &constty_lock_grp);
62
63 struct tty *
copy_constty(void)64 copy_constty(void)
65 {
66 struct tty *result = NULL;
67 lck_mtx_lock(&_constty_lock);
68 if (_constty != NULL) {
69 ttyhold(_constty);
70 result = _constty;
71 }
72 lck_mtx_unlock(&_constty_lock);
73 return result;
74 }
75
76 struct tty *
set_constty(struct tty * new_tty)77 set_constty(struct tty *new_tty)
78 {
79 struct tty *old_tty = NULL;
80 lck_mtx_lock(&_constty_lock);
81 old_tty = _constty;
82 _constty = new_tty;
83 if (_constty) {
84 ttyhold(_constty);
85 }
86 lck_mtx_unlock(&_constty_lock);
87
88 return old_tty;
89 }
90
91 /*
92 * The km driver supplied the default console device for the systems
93 * (usually a raw frame buffer driver, but potentially a serial driver).
94 */
95 extern struct tty *km_tty[1];
96
97 /*
98 * cdevsw[] entries for the console device driver
99 */
100 int cnopen(__unused dev_t dev, int flag, int devtype, proc_t pp);
101 int cnclose(__unused dev_t dev, int flag, int mode, proc_t pp);
102 int cnread(__unused dev_t dev, struct uio *uio, int ioflag);
103 int cnwrite(__unused dev_t dev, struct uio *uio, int ioflag);
104 int cnioctl(__unused dev_t dev, u_long cmd, caddr_t addr, int flg, proc_t p);
105 int cnselect(__unused dev_t dev, int flag, void * wql, proc_t p);
106
107 int
cnopen(__unused dev_t dev,int flag,int devtype,struct proc * pp)108 cnopen(__unused dev_t dev, int flag, int devtype, struct proc *pp)
109 {
110 int error;
111 struct tty *constty = copy_constty();
112 if (constty) {
113 dev = constty->t_dev;
114 } else {
115 dev = km_tty[0]->t_dev;
116 }
117 error = (*cdevsw[major(dev)].d_open)(dev, flag, devtype, pp);
118 if (constty != NULL) {
119 ttyfree(constty);
120 }
121 return error;
122 }
123
124
125 int
cnclose(__unused dev_t dev,int flag,int mode,struct proc * pp)126 cnclose(__unused dev_t dev, int flag, int mode, struct proc *pp)
127 {
128 int error;
129 struct tty *constty = copy_constty();
130 if (constty) {
131 dev = constty->t_dev;
132 } else {
133 dev = km_tty[0]->t_dev;
134 }
135 error = (*cdevsw[major(dev)].d_close)(dev, flag, mode, pp);
136 if (constty != NULL) {
137 ttyfree(constty);
138 }
139 return error;
140 }
141
142
143 int
cnread(__unused dev_t dev,struct uio * uio,int ioflag)144 cnread(__unused dev_t dev, struct uio *uio, int ioflag)
145 {
146 int error;
147 struct tty *constty = copy_constty();
148 if (constty) {
149 dev = constty->t_dev;
150 } else {
151 dev = km_tty[0]->t_dev;
152 }
153 error = (*cdevsw[major(dev)].d_read)(dev, uio, ioflag);
154 if (constty != NULL) {
155 ttyfree(constty);
156 }
157 return error;
158 }
159
160
161 int
cnwrite(__unused dev_t dev,struct uio * uio,int ioflag)162 cnwrite(__unused dev_t dev, struct uio *uio, int ioflag)
163 {
164 int error;
165 struct tty *constty = copy_constty();
166 if (constty) {
167 dev = constty->t_dev;
168 } else {
169 dev = km_tty[0]->t_dev;
170 }
171 error = (*cdevsw[major(dev)].d_write)(dev, uio, ioflag);
172 if (constty != NULL) {
173 ttyfree(constty);
174 }
175 return error;
176 }
177
178
179 int
cnioctl(__unused dev_t dev,u_long cmd,caddr_t addr,int flag,struct proc * p)180 cnioctl(__unused dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
181 {
182 int error;
183 struct tty *constty = copy_constty();
184 if (constty) {
185 dev = constty->t_dev;
186 } else {
187 dev = km_tty[0]->t_dev;
188 }
189 #if 0
190 /*
191 * Superuser can always use this to wrest control of console
192 * output from the "virtual" console.
193 *
194 * XXX Unfortunately, this code doesn't do what the author thougt
195 * XXX it did; use of the console device, a TIOCCONS would always
196 * XXX disassociate the console from a virtual terminal and send
197 * XXX it back to the fake tty.
198 */
199 if ((unsigned) cmd == TIOCCONS && constty) {
200 int error = proc_suser(p);
201 if (!error) {
202 constty = NULL;
203 }
204 return error;
205 }
206 #endif /* 0 */
207
208 error = (*cdevsw[major(dev)].d_ioctl)(dev, cmd, addr, flag, p);
209 if (constty != NULL) {
210 ttyfree(constty);
211 }
212 return error;
213 }
214
215
216 int
cnselect(__unused dev_t dev,int flag,void * wql,struct proc * p)217 cnselect(__unused dev_t dev, int flag, void *wql, struct proc *p)
218 {
219 int error;
220 struct tty *constty = copy_constty();
221 if (constty) {
222 dev = constty->t_dev;
223 } else {
224 dev = km_tty[0]->t_dev;
225 }
226 error = (*cdevsw[major(dev)].d_select)(dev, flag, wql, p);
227 if (constty != NULL) {
228 ttyfree(constty);
229 }
230 return error;
231 }
232