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