xref: /netbsd-src/sys/arch/sun2/dev/kd.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: kd.c,v 1.24 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.24 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/promlib.h>
55 #include <machine/eeprom.h>
56 #include <machine/psl.h>
57 #include <machine/cpu.h>
58 #include <machine/kbd.h>
59 #include <machine/autoconf.h>
60 
61 #ifdef RASTERCONSOLE
62 #include <dev/sun/fbio.h>
63 #include <machine/fbvar.h>
64 #endif
65 
66 
67 #include <dev/cons.h>
68 #include <dev/sun/event_var.h>
69 #include <dev/sun/kbd_xlate.h>
70 #include <dev/sun/kbdvar.h>
71 #include <sun2/dev/cons.h>
72 
73 struct	tty *fbconstty = 0;	/* tty structure for frame buffer console */
74 
75 #define PUT_WSIZE	64
76 
77 struct kd_softc {
78 	struct  tty *kd_tty;
79 	int rows, cols;
80 
81 	/* Console input hook */
82 	struct cons_channel *kd_in;
83 };
84 
85 /*
86  * There is no point in pretending there might be
87  * more than one keyboard/display device.
88  */
89 static struct kd_softc kd_softc;
90 static int kd_is_console;
91 
92 static int kdparam(struct tty *, struct termios *);
93 static void kdstart(struct tty *);
94 static void kd_init(struct kd_softc *);
95 static void kd_cons_input(int);
96 static int  kdcngetc(dev_t);
97 static void kd_later(void*);
98 
99 dev_type_open(kdopen);
100 dev_type_close(kdclose);
101 dev_type_read(kdread);
102 dev_type_write(kdwrite);
103 dev_type_ioctl(kdioctl);
104 dev_type_tty(kdtty);
105 dev_type_poll(kdpoll);
106 
107 const struct cdevsw kd_cdevsw = {
108 	.d_open = kdopen,
109 	.d_close = kdclose,
110 	.d_read = kdread,
111 	.d_write = kdwrite,
112 	.d_ioctl = kdioctl,
113 	.d_stop = nostop,
114 	.d_tty = kdtty,
115 	.d_poll = kdpoll,
116 	.d_mmap = nommap,
117 	.d_kqfilter = ttykqfilter,
118 	.d_flag = D_TTY
119 };
120 
121 /*
122  * This is called by kbd_attach()
123  * XXX - Make this a proper child of kbd?
124  */
125 void
126 kd_init(struct kd_softc *kd)
127 {
128 	struct tty *tp;
129 #ifdef	PROM_OLDMON
130 	struct eeprom *ep;
131 #endif
132 #ifdef	notyet /* PROM_OBP_V2 */
133 	int i;
134 	char *prop;
135 #endif
136 
137 	kd = &kd_softc; 	/* XXX */
138 
139 	tp = tty_alloc();
140 	callout_setfunc(&tp->t_rstrt_ch, kd_later, tp);
141 	tp->t_oproc = kdstart;
142 	tp->t_param = kdparam;
143 	tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
144 
145 	tty_attach(tp);
146 	kd->kd_tty = tp;
147 
148 	/*
149 	 * get the console struct winsize.
150 	 */
151 	if (kd_is_console) {
152 		fbconstty = tp;
153 #ifdef RASTERCONSOLE
154 		kd->rows = fbrcons_rows();
155 		kd->cols = fbrcons_cols();
156 		rcons_ttyinit(tp);
157 #endif
158 	}
159 
160 	/* else, consult the PROM */
161 	switch (prom_version()) {
162 #ifdef	PROM_OLDMON
163 	case PROM_OLDMON:
164 		if ((ep = (struct eeprom *)eeprom_va) == NULL)
165 			break;
166 		if (kd->rows == 0)
167 			kd->rows = (u_short)ep->eeTtyRows;
168 		if (kd->cols == 0)
169 			kd->cols = (u_short)ep->eeTtyCols;
170 		break;
171 #endif	/* PROM_OLDMON */
172 #ifdef	notyet /* PROM_OBP_V2 */
173 	case PROM_OBP_V0:
174 	case PROM_OBP_V2:
175 	case PROM_OBP_V3:
176 	case PROM_OPENFIRM:
177 
178 		if (kd->rows == 0 &&
179 		    (prop = PROM_getpropstring(optionsnode, "screen-#rows"))) {
180 			i = 0;
181 			while (*prop != '\0')
182 				i = i * 10 + *prop++ - '0';
183 			kd->rows = (unsigned short)i;
184 		}
185 		if (kd->cols == 0 &&
186 		    (prop = PROM_getpropstring(optionsnode, "screen-#columns"))) {
187 			i = 0;
188 			while (*prop != '\0')
189 				i = i * 10 + *prop++ - '0';
190 			kd->cols = (unsigned short)i;
191 		}
192 		break;
193 #endif	/* PROM_OBP_V2 */
194 	}
195 	return;
196 }
197 
198 struct tty *
199 kdtty(dev_t dev)
200 {
201 	struct kd_softc *kd;
202 
203 	kd = &kd_softc; 	/* XXX */
204 	return (kd->kd_tty);
205 }
206 
207 int
208 kdopen(dev_t dev, int flag, int mode, struct lwp *l)
209 {
210 	struct kd_softc *kd;
211 	int error, s, unit;
212 	struct tty *tp;
213 static	int firstopen = 1;
214 
215 	unit = minor(dev);
216 	if (unit != 0)
217 		return ENXIO;
218 	kd = &kd_softc; 	/* XXX */
219 
220 	if (firstopen) {
221 		kd_init(kd);
222 		firstopen = 0;
223 	}
224 	tp = kd->kd_tty;
225 
226 	/* It's simpler to do this up here. */
227 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
228 		return (EBUSY);
229 
230 	s = spltty();
231 
232 	if ((tp->t_state & TS_ISOPEN) == 0) {
233 		/* First open. */
234 
235 		/* Notify the input device that serves us */
236 		struct cons_channel *cc = kd->kd_in;
237 		if (cc != NULL &&
238 		    (error = (*cc->cc_iopen)(cc)) != 0) {
239 			splx(s);
240 			return (error);
241 		}
242 
243 		ttychars(tp);
244 		tp->t_iflag = TTYDEF_IFLAG;
245 		tp->t_oflag = TTYDEF_OFLAG;
246 		tp->t_cflag = TTYDEF_CFLAG;
247 		tp->t_lflag = TTYDEF_LFLAG;
248 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
249 		(void) kdparam(tp, &tp->t_termios);
250 		ttsetwater(tp);
251 		tp->t_winsize.ws_row = kd->rows;
252 		tp->t_winsize.ws_col = kd->cols;
253 		/* Flush pending input?  Clear translator? */
254 		/* This (pseudo)device always has SOFTCAR */
255 		tp->t_state |= TS_CARR_ON;
256 	}
257 
258 	splx(s);
259 
260 	return ((*tp->t_linesw->l_open)(dev, tp));
261 }
262 
263 int
264 kdclose(dev_t dev, int flag, int mode, struct lwp *l)
265 {
266 	struct kd_softc *kd;
267 	struct tty *tp;
268 	struct cons_channel *cc;
269 
270 	kd = &kd_softc; 	/* XXX */
271 	tp = kd->kd_tty;
272 
273 	/* XXX This is for cons.c. */
274 	if ((tp->t_state & TS_ISOPEN) == 0)
275 		return 0;
276 
277 	(*tp->t_linesw->l_close)(tp, flag);
278 	ttyclose(tp);
279 
280 	if ((cc = kd->kd_in) != NULL)
281 		(void)(*cc->cc_iclose)(cc);
282 
283 	return (0);
284 }
285 
286 int
287 kdread(dev_t dev, struct uio *uio, int flag)
288 {
289 	struct kd_softc *kd;
290 	struct tty *tp;
291 
292 	kd = &kd_softc; 	/* XXX */
293 	tp = kd->kd_tty;
294 
295 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
296 }
297 
298 int
299 kdwrite(dev_t dev, struct uio *uio, int flag)
300 {
301 	struct kd_softc *kd;
302 	struct tty *tp;
303 
304 	kd = &kd_softc; 	/* XXX */
305 	tp = kd->kd_tty;
306 
307 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
308 }
309 
310 int
311 kdpoll(dev_t dev, int events, struct lwp *l)
312 {
313 	struct kd_softc *kd;
314 	struct tty *tp;
315 
316 	kd = &kd_softc; 	/* XXX */
317 	tp = kd->kd_tty;
318 
319 	return ((*tp->t_linesw->l_poll)(tp, events, l));
320 }
321 
322 int
323 kdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
324 {
325 	struct kd_softc *kd;
326 	struct tty *tp;
327 	int error;
328 
329 	kd = &kd_softc; 	/* XXX */
330 	tp = kd->kd_tty;
331 
332 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
333 	if (error != EPASSTHROUGH)
334 		return error;
335 
336 	error = ttioctl(tp, cmd, data, flag, l);
337 	if (error != EPASSTHROUGH)
338 		return error;
339 
340 	/* Handle any ioctl commands specific to kbd/display. */
341 	/* XXX - Send KB* ioctls to kbd module? */
342 	/* XXX - Send FB* ioctls to fb module?  */
343 
344 	return EPASSTHROUGH;
345 }
346 
347 static int
348 kdparam(struct tty *tp, struct termios *t)
349 {
350 	/* XXX - These are ignored... */
351 	tp->t_ispeed = t->c_ispeed;
352 	tp->t_ospeed = t->c_ospeed;
353 	tp->t_cflag = t->c_cflag;
354 	return 0;
355 }
356 
357 
358 static void kd_putfb(struct tty *);
359 
360 static void
361 kdstart(struct tty *tp)
362 {
363 	struct clist *cl;
364 	int s1, s2;
365 
366 	s1 = splsoftclock();
367 	s2 = spltty();
368 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
369 		goto out;
370 
371 	cl = &tp->t_outq;
372 	if (ttypull(tp)) {
373 		if (kd_is_console) {
374 			tp->t_state |= TS_BUSY;
375 			if (is_spl0(s1)) {
376 				/* called at level zero - update screen now. */
377 				splx(s2);
378 				kd_putfb(tp);
379 				s2 = spltty();
380 				tp->t_state &= ~TS_BUSY;
381 			} else {
382 				/* called at interrupt level - do it later */
383 				callout_schedule(&tp->t_rstrt_ch, 0);
384 			}
385 		} else {
386 			/*
387 			 * This driver uses the PROM for writing the screen,
388 			 * and that only works if this is the console device.
389 			 * If this is not the console, just flush the output.
390 			 * Sorry.  (In that case, use xdm instead of getty.)
391 			 */
392 			ndflush(cl, cl->c_cc);
393 		}
394 	}
395 out:
396 	splx(s2);
397 	splx(s1);
398 }
399 
400 /*
401  * Timeout function to do delayed writes to the screen.
402  * Called at splsoftclock when requested by kdstart.
403  */
404 static void
405 kd_later(void *tpaddr)
406 {
407 	struct tty *tp = tpaddr;
408 	int s;
409 
410 	kd_putfb(tp);
411 
412 	s = spltty();
413 	tp->t_state &= ~TS_BUSY;
414 	(*tp->t_linesw->l_start)(tp);
415 	splx(s);
416 }
417 
418 /*
419  * Put text on the screen using the PROM monitor.
420  * This can take a while, so to avoid missing
421  * interrupts, this is called at splsoftclock.
422  */
423 static void
424 kd_putfb(struct tty *tp)
425 {
426 	char buf[PUT_WSIZE];
427 	struct clist *cl = &tp->t_outq;
428 	char *p, *end;
429 	int len;
430 
431 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
432 		/* PROM will barf if high bits are set. */
433 		p = buf;
434 		end = buf + len;
435 		while (p < end)
436 			*p++ &= 0x7f;
437 		/* Now let the PROM print it. */
438 		prom_putstr(buf, len);
439 	}
440 }
441 
442 /*
443  * Default PROM-based console input stream
444  */
445 static int kd_rom_iopen(struct cons_channel *);
446 static int kd_rom_iclose(struct cons_channel *);
447 
448 static struct cons_channel prom_cons_channel;
449 
450 int
451 kd_rom_iopen(struct cons_channel *cc)
452 {
453 	return (0);
454 }
455 
456 int
457 kd_rom_iclose(struct cons_channel *cc)
458 {
459 	return (0);
460 }
461 
462 /*
463  * Our "interrupt" routine for input. This is called by
464  * the keyboard driver (dev/sun/kbd.c) at spltty.
465  */
466 void
467 kd_cons_input(int c)
468 {
469 	struct kd_softc *kd = &kd_softc;
470 	struct tty *tp;
471 
472 	/* XXX: Make sure the device is open. */
473 	tp = kd->kd_tty;
474 	if (tp == NULL)
475 		return;
476 	if ((tp->t_state & TS_ISOPEN) == 0)
477 		return;
478 
479 	(*tp->t_linesw->l_rint)(c, tp);
480 }
481 
482 
483 /****************************************************************
484  * kd console support
485  ****************************************************************/
486 
487 /* The debugger gets its own key translation state. */
488 static struct kbd_state *kdcn_state;
489 
490 static void kdcnprobe(struct consdev *);
491 static void kdcninit(struct consdev *);
492 static void kdcnputc(dev_t, int);
493 static void kdcnpollc(dev_t, int);
494 
495 /* The keyboard driver uses cn_hw to access the real console driver */
496 extern struct consdev consdev_prom;
497 struct consdev consdev_kd = {
498 	kdcnprobe,
499 	kdcninit,
500 	kdcngetc,
501 	kdcnputc,
502 	kdcnpollc,
503 	NULL,
504 };
505 struct consdev *cn_hw = &consdev_kd;
506 
507 void
508 cons_attach_input(struct cons_channel *cc, struct consdev *cn)
509 {
510 	struct kd_softc *kd = &kd_softc;
511 	struct kbd_softc *kds = cc->cc_private;
512 	struct kbd_state *ks;
513 
514 	/* Share the keyboard state */
515 	kdcn_state = ks = &kds->k_state;
516 
517 	kd->kd_in = cc;
518 	cc->cc_upstream = kd_cons_input;
519 
520 	/* Attach lower level. */
521 	cn_hw->cn_dev = cn->cn_dev;
522 	cn_hw->cn_pollc = cn->cn_pollc;
523 	cn_hw->cn_getc = cn->cn_getc;
524 
525 	/* Attach us as console. */
526 	cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
527 	cn_tab->cn_probe = kdcnprobe;
528 	cn_tab->cn_init = kdcninit;
529 	cn_tab->cn_getc = kdcngetc;
530 	cn_tab->cn_pollc = kdcnpollc;
531 	cn_tab->cn_pri = CN_INTERNAL;
532 
533 	/* Set up initial PROM input channel for /dev/console */
534 	prom_cons_channel.cc_private = NULL;
535 	prom_cons_channel.cc_iopen = kd_rom_iopen;
536 	prom_cons_channel.cc_iclose = kd_rom_iclose;
537 
538 	/* Indicate that it is OK to use the PROM fbwrite */
539 	kd_is_console = 1;
540 }
541 
542 
543 void kd_attach_input(struct cons_channel *);
544 void
545 kd_attach_input(struct cons_channel *cc)
546 {
547 	struct kd_softc *kd = &kd_softc;
548 
549 	kd->kd_in = cc;
550 	cc->cc_upstream = kd_cons_input;
551 }
552 
553 
554 /* We never call this. */
555 static void
556 kdcnprobe(struct consdev *cn)
557 {
558 }
559 
560 static void
561 kdcninit(struct consdev *cn)
562 {
563 #if 0
564 	struct kbd_state *ks = kdcn_state;
565 
566 	cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
567 	cn->cn_pri = CN_INTERNAL;
568 
569 	/* This prepares kbd_translate() */
570 	ks->kbd_id = KBD_MIN_TYPE;
571 	kbd_xlate_init(ks);
572 
573 	/* Set up initial PROM input channel for /dev/console */
574 	prom_cons_channel.cc_private = NULL;
575 	prom_cons_channel.cc_iopen = kd_rom_iopen;
576 	prom_cons_channel.cc_iclose = kd_rom_iclose;
577 	cons_attach_input(&prom_cons_channel);
578 
579 	/* Indicate that it is OK to use the PROM fbwrite */
580 	kd_is_console = 1;
581 #endif
582 }
583 
584 static int
585 kdcngetc(dev_t dev)
586 {
587 	struct kbd_state *ks = kdcn_state;
588 	int code, class, data, keysym;
589 	extern int prom_cngetc(dev_t);
590 
591 
592 	if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev);
593 	for (;;) {
594 		code = (*cn_hw->cn_getc)(dev);
595 		keysym = kbd_code_to_keysym(ks, code);
596 		class = KEYSYM_CLASS(keysym);
597 
598 		switch (class) {
599 		case KEYSYM_ASCII:
600 			goto out;
601 
602 		case KEYSYM_CLRMOD:
603 		case KEYSYM_SETMOD:
604 			data = (keysym & 0x1F);
605 			/* Only allow ctrl or shift. */
606 			if (data > KBMOD_SHIFT_R)
607 				break;
608 			data = 1 << data;
609 			if (class == KEYSYM_SETMOD)
610 				ks->kbd_modbits |= data;
611 			else
612 				ks->kbd_modbits &= ~data;
613 			break;
614 
615 		case KEYSYM_ALL_UP:
616 			/* No toggle keys here. */
617 			ks->kbd_modbits = 0;
618 			break;
619 
620 		default:	/* ignore all other keysyms */
621 			break;
622 		}
623 	}
624 out:
625 	return (keysym);
626 }
627 
628 static void
629 kdcnputc(dev_t dev, int c)
630 {
631 	int s;
632 
633 	s = splhigh();
634 	prom_putchar(c);
635 	splx(s);
636 }
637 
638 static void
639 kdcnpollc(dev_t dev, int on)
640 {
641 	struct kbd_state *ks = kdcn_state;
642 
643 	if (on) {
644 		/* Entering debugger. */
645 #if NFB > 0
646 		fb_unblank();
647 #endif
648 		/* Clear shift keys too. */
649 		ks->kbd_modbits = 0;
650 	} else {
651 		/* Resuming kernel. */
652 	}
653 	(*cn_hw->cn_pollc)(dev, on);
654 }
655