]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/i386/km.c
xnu-4903.221.2.tar.gz
[apple/xnu.git] / bsd / dev / i386 / km.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1992 NeXT Computer, Inc. All rights reserved.
29 *
30 * km.m - kernel keyboard/monitor module, procedural interface.
31 *
32 * HISTORY
33 */
34
35#include <sys/param.h>
36#include <sys/tty.h>
37
b0d623f7 38#include <machine/cons.h>
1c79356b
A
39#include <sys/conf.h>
40#include <sys/systm.h>
41#include <sys/uio.h>
42#include <sys/fcntl.h> /* for kmopen */
43#include <sys/errno.h>
44#include <sys/proc.h> /* for kmopen */
45#include <sys/msgbuf.h>
46#include <sys/time.h>
47#include <dev/kmreg_com.h>
48#include <pexpert/pexpert.h>
0c530ab8 49#include <pexpert/i386/boot.h>
1c79356b 50
91447636
A
51extern int hz;
52
53extern void cnputcusr(char);
5ba3f43e 54extern void cnputsusr(char *, int);
91447636
A
55extern int cngetc(void);
56
57void kminit(void);
91447636
A
58void cons_cinput(char ch);
59
1c79356b
A
60/*
61 * 'Global' variables, shared only by this file and conf.c.
62 */
b0d623f7 63struct tty *km_tty[1] = { 0 };
1c79356b
A
64
65/*
66 * this works early on, after initialize_screen() but before autoconf (and thus
67 * before we have a kmDevice).
68 */
69int disableConsoleOutput;
70
71/*
72 * 'Global' variables, shared only by this file and kmDevice.m.
73 */
74int initialized = 0;
75
76static int kmoutput(struct tty *tp);
1c79356b
A
77static void kmstart(struct tty *tp);
78
79extern void KeyboardOpen(void);
80
91447636
A
81void
82kminit(void)
1c79356b 83{
b0d623f7
A
84 km_tty[0] = ttymalloc();
85 km_tty[0]->t_dev = makedev(12, 0);
1c79356b
A
86 initialized = 1;
87}
b0d623f7 88
1c79356b
A
89/*
90 * cdevsw interface to km driver.
91 */
92int
b0d623f7 93kmopen(dev_t dev, int flag, __unused int devtype, proc_t pp)
1c79356b 94{
1c79356b
A
95 int unit;
96 struct tty *tp;
97 struct winsize *wp;
98 int ret;
99
100 unit = minor(dev);
101 if(unit >= 1)
102 return (ENXIO);
103
b0d623f7
A
104 tp = km_tty[unit];
105
106 tty_lock(tp);
107
1c79356b
A
108 tp->t_oproc = kmstart;
109 tp->t_param = NULL;
110 tp->t_dev = dev;
111
112 if ( !(tp->t_state & TS_ISOPEN) ) {
113 tp->t_iflag = TTYDEF_IFLAG;
114 tp->t_oflag = TTYDEF_OFLAG;
115 tp->t_cflag = (CREAD | CS8 | CLOCAL);
116 tp->t_lflag = TTYDEF_LFLAG;
117 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
118 termioschars(&tp->t_termios);
119 ttsetwater(tp);
b0d623f7
A
120 } else if ((tp->t_state & TS_XCLUDE) && proc_suser(pp)) {
121 ret = EBUSY;
122 goto out;
123 }
1c79356b
A
124
125 tp->t_state |= TS_CARR_ON; /* lie and say carrier exists and is on. */
b0d623f7 126
1c79356b
A
127 ret = ((*linesw[tp->t_line].l_open)(dev, tp));
128 {
129 PE_Video video;
130 wp = &tp->t_winsize;
b0d623f7
A
131 /*
132 * Magic numbers. These are CHARWIDTH and CHARHEIGHT
1c79356b
A
133 * from pexpert/i386/video_console.c
134 */
135 wp->ws_xpixel = 8;
136 wp->ws_ypixel = 16;
137
b0d623f7
A
138 tty_unlock(tp); /* XXX race window */
139
1c79356b
A
140 if (flag & O_POPUP)
141 PE_initialize_console(0, kPETextScreen);
142
143 bzero(&video, sizeof(video));
144 PE_current_console(&video);
b0d623f7
A
145
146 tty_lock(tp);
147
148 if( video.v_display == FB_TEXT_MODE && video.v_width != 0 && video.v_height != 0 ) {
1c79356b
A
149 wp->ws_col = video.v_width / wp->ws_xpixel;
150 wp->ws_row = video.v_height / wp->ws_ypixel;
151 } else {
152 wp->ws_col = 100;
153 wp->ws_row = 36;
154 }
155 }
b0d623f7
A
156
157out:
158 tty_unlock(tp);
159
1c79356b
A
160 return ret;
161}
162
163int
b0d623f7 164kmclose(dev_t dev, int flag, __unused int mode, __unused proc_t p)
1c79356b 165{
b0d623f7
A
166 int ret;
167 struct tty *tp = km_tty[minor(dev)];
1c79356b 168
b0d623f7
A
169 tty_lock(tp);
170 ret = (*linesw[tp->t_line].l_close)(tp,flag);
1c79356b 171 ttyclose(tp);
b0d623f7
A
172 tty_unlock(tp);
173
174 return (ret);
1c79356b
A
175}
176
177int
b0d623f7 178kmread(dev_t dev, struct uio *uio, int ioflag)
1c79356b 179{
b0d623f7
A
180 int ret;
181 struct tty *tp = km_tty[minor(dev)];
182
183 tty_lock(tp);
184 ret = (*linesw[tp->t_line].l_read)(tp, uio, ioflag);
185 tty_unlock(tp);
186
187 return (ret);
1c79356b
A
188}
189
190int
b0d623f7 191kmwrite(dev_t dev, struct uio *uio, int ioflag)
1c79356b 192{
b0d623f7
A
193 int ret;
194 struct tty *tp = km_tty[minor(dev)];
195
196 tty_lock(tp);
197 ret = (*linesw[tp->t_line].l_write)(tp, uio, ioflag);
198 tty_unlock(tp);
199
200 return (ret);
1c79356b
A
201}
202
203int
b0d623f7 204kmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, proc_t p)
1c79356b 205{
b0d623f7
A
206 int error = 0;
207 struct tty *tp = km_tty[minor(dev)];
1c79356b 208 struct winsize *wp;
b0d623f7
A
209
210 tty_lock(tp);
1c79356b
A
211
212 switch (cmd) {
1c79356b
A
213 case KMIOCSIZE:
214 wp = (struct winsize *)data;
215 *wp = tp->t_winsize;
b0d623f7 216 break;
1c79356b
A
217
218 case TIOCSWINSZ:
219 /* Prevent changing of console size --
220 * this ensures that login doesn't revert to the
221 * termcap-defined size
222 */
b0d623f7
A
223 error = EINVAL;
224 break;
1c79356b
A
225
226 /* Bodge in the CLOCAL flag as the km device is always local */
b0d623f7
A
227 case TIOCSETA_32:
228 case TIOCSETAW_32:
229 case TIOCSETAF_32:
230 {
231 struct termios32 *t = (struct termios32 *)data;
232 t->c_cflag |= CLOCAL;
233 /* No Break */
234 }
235 goto fallthrough;
236 case TIOCSETA_64:
237 case TIOCSETAW_64:
238 case TIOCSETAF_64:
239 {
240 struct user_termios *t = (struct user_termios *)data;
241 t->c_cflag |= CLOCAL;
242 /* No Break */
243 }
244fallthrough:
1c79356b
A
245 default:
246 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
91447636 247 if (ENOTTY != error)
b0d623f7
A
248 break;
249 error = ttioctl_locked(tp, cmd, data, flag, p);
250 break;
1c79356b 251 }
1c79356b 252
b0d623f7 253 tty_unlock(tp);
1c79356b 254
b0d623f7 255 return (error);
1c79356b
A
256}
257
b0d623f7
A
258/*
259 * kmputc
260 *
261 * Output a character to the serial console driver via cnputcusr(),
262 * which is exported by that driver.
263 *
264 * Locks: Assumes tp in the calling tty driver code is locked on
265 * entry, remains locked on exit
266 *
267 * Notes: Called from kmoutput(); giving the locking output
268 * assumptions here, this routine should be static (and
269 * inlined, given there is only one call site).
270 */
1c79356b 271int
b0d623f7 272kmputc(__unused dev_t dev, char c)
1c79356b 273{
b0d623f7
A
274 if(!disableConsoleOutput && initialized) {
275 /* OCRNL */
276 if(c == '\n')
277 cnputcusr('\r');
278 cnputcusr(c);
1c79356b 279 }
1c79356b 280
b0d623f7 281 return (0);
1c79356b
A
282}
283
b0d623f7 284
1c79356b
A
285/*
286 * Callouts from linesw.
287 */
288
289#define KM_LOWAT_DELAY ((ns_time_t)1000)
290
b0d623f7
A
291/*
292 * t_oproc for this driver; called from within the line discipline
293 *
294 * Locks: Assumes tp is locked on entry, remains locked on exit
295 */
1c79356b 296static void
b0d623f7 297kmstart(struct tty *tp)
1c79356b 298{
1c79356b
A
299 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
300 goto out;
301 if (tp->t_outq.c_cc == 0)
302 goto out;
303 tp->t_state |= TS_BUSY;
91447636
A
304 kmoutput(tp);
305 return;
306
1c79356b 307out:
91447636
A
308 (*linesw[tp->t_line].l_start)(tp);
309 return;
1c79356b
A
310}
311
b0d623f7
A
312/*
313 * One-shot output retry timeout from kmoutput(); re-calls kmoutput() at
314 * intervals until the output queue for the tty is empty, at which point
315 * the timeout is not rescheduled by kmoutput()
316 *
317 * This function must take the tty_lock() around the kmoutput() call; it
318 * ignores the return value.
319 */
1c79356b 320static void
55e303ae 321kmtimeout(void *arg)
1c79356b 322{
b0d623f7 323 struct tty *tp = (struct tty *)arg;
1c79356b 324
b0d623f7
A
325 tty_lock(tp);
326 (void)kmoutput(tp);
327 tty_unlock(tp);
1c79356b 328}
b0d623f7
A
329
330/*
331 * kmoutput
332 *
333 * Locks: Assumes tp is locked on entry, remains locked on exit
334 *
335 * Notes: Called from kmstart() and kmtimeout(); kmtimeout() is a
336 * timer initiated by this routine to deal with pending
337 * output not yet flushed (output is flushed at a maximum
338 * of sizeof(buf) charatcers at a time before dropping into
339 * the timeout code).
340 */
1c79356b 341static int
b0d623f7 342kmoutput(struct tty *tp)
1c79356b 343{
b0d623f7
A
344 unsigned char buf[80]; /* buffer; limits output per call */
345 unsigned char *cp;
1c79356b 346 int cc = -1;
1c79356b
A
347
348
b0d623f7 349 /* While there is data available to be output... */
1c79356b
A
350 while (tp->t_outq.c_cc > 0) {
351 cc = ndqb(&tp->t_outq, 0);
352 if (cc == 0)
353 break;
b0d623f7
A
354 /*
355 * attempt to output as many characters as are available,
356 * up to the available transfer buffer size.
357 */
358 cc = min(cc, sizeof(buf));
359 /* copy the output queue contents to the buffer */
1c79356b
A
360 (void) q_to_b(&tp->t_outq, buf, cc);
361 for (cp = buf; cp < &buf[cc]; cp++) {
b0d623f7 362 /* output the buffer one charatcer at a time */
5ba3f43e
A
363 *cp = *cp & 0x7f;
364 }
365
366 if (cc > 1) {
367 cnputsusr((char *)buf, cc);
368 } else {
369 kmputc(tp->t_dev, *buf);
1c79356b
A
370 }
371 }
b0d623f7
A
372 /*
373 * XXX This is likely not necessary, as the tty output queue is not
374 * XXX writeable while we hold the tty_lock().
375 */
1c79356b
A
376 if (tp->t_outq.c_cc > 0) {
377 timeout(kmtimeout, tp, hz);
378 }
379 tp->t_state &= ~TS_BUSY;
b0d623f7 380 /* Start the output processing for the line discipline */
91447636 381 (*linesw[tp->t_line].l_start)(tp);
1c79356b
A
382
383 return 0;
384}
91447636 385
b0d623f7
A
386
387/*
388 * cons_cinput
389 *
390 * Driver character input from the polled mode serial console driver calls
391 * this routine to input a character from the serial driver into the tty
392 * line discipline specific input processing receiv interrupt routine,
393 * l_rint().
394 *
395 * Locks: Assumes that the tty_lock() is NOT held on the tp, so a
396 * serial driver should NOT call this function as a result
397 * of being called from a function which already holds the
398 * lock; ECHOE will be handled at the line discipline, if
399 * output echo processing is going to occur.
400 */
91447636 401void
1c79356b
A
402cons_cinput(char ch)
403{
b0d623f7
A
404 struct tty *tp = km_tty[0]; /* XXX */
405
406 tty_lock(tp);
1c79356b 407 (*linesw[tp->t_line].l_rint) (ch, tp);
b0d623f7 408 tty_unlock(tp);
1c79356b 409}