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