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