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