]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/i386/cons.c
577768fb0fc83a024b2d3b39003a54901c7a3a3f
[apple/xnu.git] / bsd / dev / i386 / cons.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * Copyright (c) 1987, 1988 NeXT, Inc.
32 *
33 * HISTORY
34 * 7-Jan-93 Mac Gillon (mgillon) at NeXT
35 * Integrated POSIX support
36 *
37 * 12-Aug-87 John Seamons (jks) at NeXT
38 * Ported to NeXT.
39 */
40
41 /*
42 * Indirect driver for console.
43 */
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/conf.h>
47 #include <sys/ioctl.h>
48 #include <sys/tty.h>
49 #include <sys/proc.h>
50 #include <sys/uio.h>
51
52 struct tty cons;
53 struct tty *constty; /* current console device */
54
55 int cnopen(__unused dev_t dev, int flag, int devtype, struct proc *pp);
56 int cnclose(__unused dev_t dev, int flag, int mode, struct proc *pp);
57 int cnread(__unused dev_t dev, struct uio *uio, int ioflag);
58 int cnwrite(__unused dev_t dev, struct uio *uio, int ioflag);
59 int cnioctl(__unused dev_t dev, int cmd, caddr_t addr, int flg, struct proc *p);
60 int cnselect(__unused dev_t dev, int flag, void * wql, struct proc *p);
61
62 void slave_cnenable(void);
63
64 int alert(
65 __unused int width,
66 __unused int height,
67 __unused const char *title,
68 const char *msg,
69 int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8);
70 int alert_done(void);
71
72 /*ARGSUSED*/
73 int
74 cnopen(__unused dev_t dev, int flag, int devtype, struct proc *pp)
75 {
76 dev_t device;
77 boolean_t funnel_state;
78 int error;
79
80 funnel_state = thread_funnel_set(kernel_flock, TRUE);
81
82 if (constty)
83 device = constty->t_dev;
84 else
85 device = cons.t_dev;
86 error = (*cdevsw[major(device)].d_open)(device, flag, devtype, pp);
87 thread_funnel_set(kernel_flock, funnel_state);
88
89 return(error);
90 }
91
92 /*ARGSUSED*/
93 int
94 cnclose(__unused dev_t dev, int flag, int mode, struct proc *pp)
95 {
96 dev_t device;
97 boolean_t funnel_state;
98 int error;
99
100 funnel_state = thread_funnel_set(kernel_flock, TRUE);
101 if (constty)
102 device = constty->t_dev;
103 else
104 device = cons.t_dev;
105 error = (*cdevsw[major(device)].d_close)(device, flag, mode, pp);
106 thread_funnel_set(kernel_flock, funnel_state);
107
108 return(error);
109
110
111 }
112
113 /*ARGSUSED*/
114 int
115 cnread(__unused dev_t dev, struct uio *uio, int ioflag)
116 {
117 dev_t device;
118 boolean_t funnel_state;
119 int error;
120
121 funnel_state = thread_funnel_set(kernel_flock, TRUE);
122 if (constty)
123 device = constty->t_dev;
124 else
125 device = cons.t_dev;
126 error = (*cdevsw[major(device)].d_read)(device, uio, ioflag);
127 thread_funnel_set(kernel_flock, funnel_state);
128
129 return(error);
130 }
131
132 /*ARGSUSED*/
133 int
134 cnwrite(__unused dev_t dev, struct uio *uio, int ioflag)
135 {
136 dev_t device;
137 boolean_t funnel_state;
138 int error;
139
140 funnel_state = thread_funnel_set(kernel_flock, TRUE);
141 if (constty)
142 device = constty->t_dev;
143 else
144 device = cons.t_dev;
145 error = (*cdevsw[major(device)].d_write)(device, uio, ioflag);
146 thread_funnel_set(kernel_flock, funnel_state);
147
148 return(error);
149 }
150
151 /*ARGSUSED*/
152 int
153 cnioctl(__unused dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p)
154 {
155 dev_t device;
156 boolean_t funnel_state;
157 int error;
158
159 funnel_state = thread_funnel_set(kernel_flock, TRUE);
160
161 if (constty)
162 device = constty->t_dev;
163 else
164 device = cons.t_dev;
165 /*
166 * Superuser can always use this to wrest control of console
167 * output from the "virtual" console.
168 */
169 if ((unsigned) cmd == TIOCCONS && constty) {
170 error = proc_suser(p);
171 if (error) {
172 goto out;
173 }
174 constty = NULL;
175 error = 0;
176 goto out;
177 }
178 error = (*cdevsw[major(device)].d_ioctl)(device, cmd, addr, flag, p);
179 out:
180 thread_funnel_set(kernel_flock, funnel_state);
181
182 return(error);
183 }
184
185 /*ARGSUSED*/
186 /* called with funnel held */
187 int
188 cnselect(__unused dev_t dev, int flag, void * wql, struct proc *p)
189 {
190 dev_t device;
191
192 if (constty)
193 device = constty->t_dev;
194 else
195 device = cons.t_dev;
196 return ((*cdevsw[major(device)].d_select)(device, flag, wql, p));
197 }
198
199 #if 0 /* FIXME - using OSFMK console driver for the moment */
200 int
201 cngetc()
202 {
203 dev_t device;
204 boolean_t funnel_state;
205 int error;
206
207 funnel_state = thread_funnel_set(kernel_flock, TRUE);
208 if (constty)
209 device = constty->t_dev;
210 else
211 device = cons.t_dev;
212 error = (*cdevsw[major(device)].d_getc)(device);
213 thread_funnel_set(kernel_flock, funnel_state);
214
215 return(error);
216 }
217
218 /*ARGSUSED*/
219 int
220 cnputc(c)
221 char c;
222 {
223 dev_t device;
224 boolean_t funnel_state;
225 int error;
226
227 funnel_state = thread_funnel_set(kernel_flock, TRUE);
228 if (constty)
229 device = constty->t_dev;
230 else
231 device = cons.t_dev;
232 error = (*cdevsw[major(device)].d_putc)(device, c);
233 thread_funnel_set(kernel_flock, funnel_state);
234
235 return(error);
236 }
237 #endif
238
239 void
240 slave_cnenable(void)
241 {
242 /* FIXME: what to do here? */
243 }
244
245 #if 0
246 void
247 kprintf( const char *format, ...)
248 {
249 /* on PPC this outputs to the serial line */
250 /* nop on intel ... umeshv@apple.com */
251
252 }
253 #endif
254
255 /*
256 * Write message to console; create an alert panel if no text-type window
257 * currently exists. Caller must call alert_done() when finished.
258 * The height and width arguments are not used; they are provided for
259 * compatibility with the 68k version of alert().
260 */
261 int
262 alert(
263 __unused int width,
264 __unused int height,
265 __unused const char *title,
266 const char *msg,
267 int p1,
268 int p2,
269 int p3,
270 int p4,
271 int p5,
272 int p6,
273 int p7,
274 int p8)
275 {
276 char smsg[200];
277
278 sprintf(smsg, msg, p1, p2, p3, p4, p5, p6, p7, p8);
279 #if FIXME /* [ */
280 /* DoAlert(title, smsg); */
281 #else
282 printf("%s\n",smsg);
283 #endif /* FIXME ] */
284
285 return 0;
286 }
287
288 int
289 alert_done(void)
290 {
291 /* DoRestore(); */
292 return 0;
293 }
294