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