xref: /netbsd-src/sys/arch/sun3/dev/kd.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: kd.c,v 1.58 2014/03/16 05:20:26 dholland Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Keyboard/Display device.
34  *
35  * This driver exists simply to provide a tty device that
36  * the indirect console driver can point to.
37  * The kbd driver sends its input here.
38  * Output goes to the screen via PROM printf.
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: kd.c,v 1.58 2014/03/16 05:20:26 dholland Exp $");
43 
44 #include <sys/param.h>
45 #include <sys/proc.h>
46 #include <sys/systm.h>
47 #include <sys/ioctl.h>
48 #include <sys/tty.h>
49 #include <sys/file.h>
50 #include <sys/conf.h>
51 #include <sys/device.h>
52 #include <sys/kauth.h>
53 
54 #include <machine/autoconf.h>
55 #include <machine/cpu.h>
56 #include <machine/mon.h>
57 #include <machine/psl.h>
58 
59 #include <dev/cons.h>
60 #include <dev/sun/event_var.h>
61 #include <dev/sun/kbd_xlate.h>
62 #include <dev/sun/kbdvar.h>
63 #include <sun3/dev/zs_cons.h>
64 
65 #include "fb.h"
66 
67 #define PUT_WSIZE	64
68 
69 struct kd_softc {
70 	struct  tty *kd_tty;
71 
72 	/* Console input hook */
73 	struct cons_channel *kd_in;
74 };
75 
76 /*
77  * There is no point in pretending there might be
78  * more than one keyboard/display device.
79  */
80 static struct kd_softc kd_softc;
81 static int kd_is_console;
82 
83 static int kdparam(struct tty *, struct termios *);
84 static void kdstart(struct tty *);
85 static void kd_init(struct kd_softc *);
86 static void kd_cons_input(int);
87 static void kd_later(void *);
88 
89 dev_type_open(kdopen);
90 dev_type_close(kdclose);
91 dev_type_read(kdread);
92 dev_type_write(kdwrite);
93 dev_type_ioctl(kdioctl);
94 dev_type_tty(kdtty);
95 dev_type_poll(kdpoll);
96 
97 const struct cdevsw kd_cdevsw = {
98 	.d_open = kdopen,
99 	.d_close = kdclose,
100 	.d_read = kdread,
101 	.d_write = kdwrite,
102 	.d_ioctl = kdioctl,
103 	.d_stop = nostop,
104 	.d_tty = kdtty,
105 	.d_poll = kdpoll,
106 	.d_mmap = nommap,
107 	.d_kqfilter = ttykqfilter,
108 	.d_flag = D_TTY
109 };
110 
111 /*
112  * Prepare the console tty; called on first open of /dev/console
113  */
114 void
115 kd_init(struct kd_softc *kd)
116 {
117 	struct tty *tp;
118 
119 	tp = tty_alloc();
120 	callout_setfunc(&tp->t_rstrt_ch, kd_later, tp);
121 
122 	tp->t_oproc = kdstart;
123 	tp->t_param = kdparam;
124 	tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
125 
126 	tty_attach(tp);
127 	kd->kd_tty = tp;
128 
129 	return;
130 }
131 
132 struct tty *
133 kdtty(dev_t dev)
134 {
135 	struct kd_softc *kd;
136 
137 	kd = &kd_softc; 	/* XXX */
138 	return (kd->kd_tty);
139 }
140 
141 int
142 kdopen(dev_t dev, int flag, int mode, struct lwp *l)
143 {
144 	struct kd_softc *kd;
145 	int error, s, unit;
146 	struct tty *tp;
147 static	int firstopen = 1;
148 
149 	unit = minor(dev);
150 	if (unit != 0)
151 		return ENXIO;
152 	kd = &kd_softc; 	/* XXX */
153 	if (firstopen) {
154 		kd_init(kd);
155 		firstopen = 0;
156 	}
157 	tp = kd->kd_tty;
158 
159 	/* It's simpler to do this up here. */
160 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
161 		return (EBUSY);
162 
163 	s = spltty();
164 
165 	if ((tp->t_state & TS_ISOPEN) == 0) {
166 		/* First open. */
167 
168 		/* Notify the input device that serves us */
169 		struct cons_channel *cc = kd->kd_in;
170 		if (cc != NULL &&
171 		    (error = (*cc->cc_iopen)(cc)) != 0) {
172 			return (error);
173 		}
174 
175 		ttychars(tp);
176 		tp->t_iflag = TTYDEF_IFLAG;
177 		tp->t_oflag = TTYDEF_OFLAG;
178 		tp->t_cflag = TTYDEF_CFLAG;
179 		tp->t_lflag = TTYDEF_LFLAG;
180 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
181 		(void) kdparam(tp, &tp->t_termios);
182 		ttsetwater(tp);
183 		/* Flush pending input?  Clear translator? */
184 		/* This (pseudo)device always has SOFTCAR */
185 		tp->t_state |= TS_CARR_ON;
186 	}
187 
188 	splx(s);
189 
190 	return ((*tp->t_linesw->l_open)(dev, tp));
191 }
192 
193 int
194 kdclose(dev_t dev, int flag, int mode, struct lwp *l)
195 {
196 	struct kd_softc *kd;
197 	struct tty *tp;
198 	struct cons_channel *cc;
199 
200 	kd = &kd_softc; 	/* XXX */
201 	tp = kd->kd_tty;
202 
203 	/* XXX This is for cons.c. */
204 	if ((tp->t_state & TS_ISOPEN) == 0)
205 		return 0;
206 
207 	(*tp->t_linesw->l_close)(tp, flag);
208 	ttyclose(tp);
209 	if ((cc = kd->kd_in) != NULL)
210 		(void)(*cc->cc_iclose)(cc);
211 	return (0);
212 }
213 
214 int
215 kdread(dev_t dev, struct uio *uio, int flag)
216 {
217 	struct kd_softc *kd;
218 	struct tty *tp;
219 
220 	kd = &kd_softc; 	/* XXX */
221 	tp = kd->kd_tty;
222 
223 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
224 }
225 
226 int
227 kdwrite(dev_t dev, struct uio *uio, int flag)
228 {
229 	struct kd_softc *kd;
230 	struct tty *tp;
231 
232 	kd = &kd_softc; 	/* XXX */
233 	tp = kd->kd_tty;
234 
235 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
236 }
237 
238 int
239 kdpoll(dev_t dev, int events, struct lwp *l)
240 {
241 	struct kd_softc *kd;
242 	struct tty *tp;
243 
244 	kd = &kd_softc; 	/* XXX */
245 	tp = kd->kd_tty;
246 
247 	return ((*tp->t_linesw->l_poll)(tp, events, l));
248 }
249 
250 int
251 kdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
252 {
253 	struct kd_softc *kd;
254 	struct tty *tp;
255 	int error;
256 
257 	kd = &kd_softc; 	/* XXX */
258 	tp = kd->kd_tty;
259 
260 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
261 	if (error != EPASSTHROUGH)
262 		return error;
263 
264 	error = ttioctl(tp, cmd, data, flag, l);
265 	if (error != EPASSTHROUGH)
266 		return error;
267 
268 	/* Handle any ioctl commands specific to kbd/display. */
269 	/* XXX - Send KB* ioctls to kbd module? */
270 	/* XXX - Send FB* ioctls to fb module?  */
271 
272 	return EPASSTHROUGH;
273 }
274 
275 static int
276 kdparam(struct tty *tp, struct termios *t)
277 {
278 	/* XXX - These are ignored... */
279 	tp->t_ispeed = t->c_ispeed;
280 	tp->t_ospeed = t->c_ospeed;
281 	tp->t_cflag = t->c_cflag;
282 	return 0;
283 }
284 
285 
286 static void kd_putfb(struct tty *);
287 
288 static void
289 kdstart(struct tty *tp)
290 {
291 	struct clist *cl;
292 	int s1, s2;
293 
294 	s1 = splsoftclock();
295 	s2 = spltty();
296 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
297 		goto out;
298 
299 	cl = &tp->t_outq;
300 	if (ttypull(tp)) {
301 		if (kd_is_console) {
302 			tp->t_state |= TS_BUSY;
303 			if ((s1 & PSL_IPL) == 0) {
304 				/* called at level zero - update screen now. */
305 				splx(s2);
306 				kd_putfb(tp);
307 				s2 = spltty();
308 				tp->t_state &= ~TS_BUSY;
309 			} else {
310 				/* called at interrupt level - do it later */
311 				callout_schedule(&tp->t_rstrt_ch, 0);
312 			}
313 		} else {
314 			/*
315 			 * This driver uses the PROM for writing the screen,
316 			 * and that only works if this is the console device.
317 			 * If this is not the console, just flush the output.
318 			 * Sorry.  (In that case, use xdm instead of getty.)
319 			 */
320 			ndflush(cl, cl->c_cc);
321 		}
322 	}
323 out:
324 	splx(s2);
325 	splx(s1);
326 }
327 
328 /*
329  * Timeout function to do delayed writes to the screen.
330  * Called at splsoftclock when requested by kdstart.
331  */
332 static void
333 kd_later(void *tpaddr)
334 {
335 	struct tty *tp = tpaddr;
336 	int s;
337 
338 	kd_putfb(tp);
339 
340 	s = spltty();
341 	tp->t_state &= ~TS_BUSY;
342 	(*tp->t_linesw->l_start)(tp);
343 	splx(s);
344 }
345 
346 /*
347  * Put text on the screen using the PROM monitor.
348  * This can take a while, so to avoid missing
349  * interrupts, this is called at splsoftclock.
350  */
351 static void
352 kd_putfb(struct tty *tp)
353 {
354 	char buf[PUT_WSIZE];
355 	struct clist *cl = &tp->t_outq;
356 	char *p, *end;
357 	int len;
358 
359 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
360 		/* PROM will barf if high bits are set. */
361 		p = buf;
362 		end = buf + len;
363 		while (p < end)
364 			*p++ &= 0x7f;
365 		(romVectorPtr->fbWriteStr)(buf, len);
366 	}
367 }
368 
369 void
370 cons_attach_input(struct cons_channel *cc, struct consdev *cn)
371 {
372 	struct kd_softc *kd = &kd_softc;
373 
374 	kd->kd_in = cc;
375 	cc->cc_upstream = kd_cons_input;
376 }
377 
378 /*
379  * Default PROM-based console input stream
380  */
381 static int kd_rom_iopen(struct cons_channel *);
382 static int kd_rom_iclose(struct cons_channel *);
383 
384 static struct cons_channel prom_cons_channel;
385 
386 int
387 kd_rom_iopen(struct cons_channel *cc)
388 {
389 	/* No-op */
390 	return (0);
391 }
392 
393 int
394 kd_rom_iclose(struct cons_channel *cc)
395 {
396 	/* No-op */
397 	return (0);
398 }
399 
400 /*
401  * Our "interrupt" routine for input. This is called by
402  * the keyboard driver (dev/sun/kbd.c) at spltty.
403  */
404 void
405 kd_cons_input(int c)
406 {
407 	struct kd_softc *kd = &kd_softc;
408 	struct tty *tp;
409 
410 	/* XXX: Make sure the device is open. */
411 	tp = kd->kd_tty;
412 	if (tp == NULL)
413 		return;
414 	if ((tp->t_state & TS_ISOPEN) == 0)
415 		return;
416 
417 	(*tp->t_linesw->l_rint)(c, tp);
418 }
419 
420 
421 /****************************************************************
422  * kd console support
423  ****************************************************************/
424 
425 /* The debugger gets its own key translation state. */
426 static struct kbd_state kdcn_state;
427 
428 static void kdcnprobe(struct consdev *);
429 static void kdcninit(struct consdev *);
430 static int  kdcngetc(dev_t);
431 static void kdcnputc(dev_t, int);
432 static void kdcnpollc(dev_t, int);
433 
434 struct consdev consdev_kd = {
435 	kdcnprobe,
436 	kdcninit,
437 	kdcngetc,
438 	kdcnputc,
439 	kdcnpollc,
440 	NULL,
441 };
442 
443 /* We never call this. */
444 static void
445 kdcnprobe(struct consdev *cn)
446 {
447 }
448 
449 static void
450 kdcninit(struct consdev *cn)
451 {
452 	struct kbd_state *ks = &kdcn_state;
453 
454 	cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
455 	cn->cn_pri = CN_INTERNAL;
456 
457 	/* This prepares kbd_translate() */
458 	ks->kbd_id = KBD_MIN_TYPE;
459 	kbd_xlate_init(ks);
460 
461 	/* Set up initial PROM input channel for /dev/console */
462 	prom_cons_channel.cc_private = NULL;
463 	prom_cons_channel.cc_iopen = kd_rom_iopen;
464 	prom_cons_channel.cc_iclose = kd_rom_iclose;
465 	cons_attach_input(&prom_cons_channel, cn);
466 
467 	/* Indicate that it is OK to use the PROM fbwrite */
468 	kd_is_console = 1;
469 }
470 
471 static int
472 kdcngetc(dev_t dev)
473 {
474 	struct kbd_state *ks = &kdcn_state;
475 	int code, class, data, keysym;
476 
477 	for (;;) {
478 		code = zs_getc(zs_conschan);
479 		keysym = kbd_code_to_keysym(ks, code);
480 		class = KEYSYM_CLASS(keysym);
481 
482 		switch (class) {
483 		case KEYSYM_ASCII:
484 			goto out;
485 
486 		case KEYSYM_CLRMOD:
487 		case KEYSYM_SETMOD:
488 			data = (keysym & 0x1F);
489 			/* Only allow ctrl or shift. */
490 			if (data > KBMOD_SHIFT_R)
491 				break;
492 			data = 1 << data;
493 			if (class == KEYSYM_SETMOD)
494 				ks->kbd_modbits |= data;
495 			else
496 				ks->kbd_modbits &= ~data;
497 			break;
498 
499 		case KEYSYM_ALL_UP:
500 			/* No toggle keys here. */
501 			ks->kbd_modbits = 0;
502 			break;
503 
504 		default:	/* ignore all other keysyms */
505 			break;
506 		}
507 	}
508 out:
509 	return (keysym);
510 }
511 
512 static void
513 kdcnputc(dev_t dev, int c)
514 {
515 	(romVectorPtr->fbWriteChar)(c & 0x7f);
516 }
517 
518 static void
519 kdcnpollc(dev_t dev, int on)
520 {
521 	struct kbd_state *ks = &kdcn_state;
522 
523 	if (on) {
524 		/* Entering debugger. */
525 #if NFB > 0
526 		fb_unblank();
527 #endif
528 		/* Clear shift keys too. */
529 		ks->kbd_modbits = 0;
530 	} else {
531 		/* Resuming kernel. */
532 	}
533 }
534 
535