xref: /netbsd/sys/arch/sun2/dev/kd.c (revision bf9ec67e)
1 /*	$NetBSD: kd.c,v 1.1 2002/03/22 00:22:44 fredette 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Keyboard/Display device.
41  *
42  * This driver exists simply to provide a tty device that
43  * the indirect console driver can point to.
44  * The kbd driver sends its input here.
45  * Output goes to the screen via PROM printf.
46  */
47 
48 #include <sys/param.h>
49 #include <sys/proc.h>
50 #include <sys/systm.h>
51 #include <sys/ioctl.h>
52 #include <sys/tty.h>
53 #include <sys/file.h>
54 #include <sys/conf.h>
55 #include <sys/device.h>
56 
57 #include <machine/promlib.h>
58 #include <machine/eeprom.h>
59 #include <machine/psl.h>
60 #include <machine/cpu.h>
61 #include <machine/kbd.h>
62 #include <machine/autoconf.h>
63 #include <machine/conf.h>
64 
65 #ifdef RASTERCONSOLE
66 #include <dev/sun/fbio.h>
67 #include <machine/fbvar.h>
68 #endif
69 
70 
71 #include <dev/cons.h>
72 #include <dev/sun/event_var.h>
73 #include <dev/sun/kbd_xlate.h>
74 #include <dev/sun/kbdvar.h>
75 #include <sun2/dev/cons.h>
76 
77 struct	tty *fbconstty = 0;	/* tty structure for frame buffer console */
78 
79 #define	KDMAJOR 1
80 #define PUT_WSIZE	64
81 
82 struct kd_softc {
83 	struct	device kd_dev;		/* required first: base device */
84 	struct  tty *kd_tty;
85 	int rows, cols;
86 
87 	/* Console input hook */
88 	struct cons_channel *kd_in;
89 };
90 
91 /*
92  * There is no point in pretending there might be
93  * more than one keyboard/display device.
94  */
95 static struct kd_softc kd_softc;
96 static int kd_is_console;
97 
98 static int kdparam(struct tty *, struct termios *);
99 static void kdstart(struct tty *);
100 static void kd_init __P((struct kd_softc *));
101 static void kd_cons_input __P((int));
102 static int  kdcngetc __P((dev_t));
103 
104 int	cons_ocount;		/* output byte count */
105 
106 /*
107  * This is called by kbd_attach()
108  * XXX - Make this a proper child of kbd?
109  */
110 void
111 kd_init(kd)
112 	struct kd_softc *kd;
113 {
114 	struct tty *tp;
115 #ifdef	PROM_OLDMON
116 	struct eeprom *ep;
117 #endif
118 #ifdef	notyet /* PROM_OBP_V2 */
119 	int i;
120 	char *prop;
121 #endif
122 
123 	kd = &kd_softc; 	/* XXX */
124 
125 	tp = ttymalloc();
126 	tp->t_oproc = kdstart;
127 	tp->t_param = kdparam;
128 	tp->t_dev = makedev(KDMAJOR, 0);
129 
130 	tty_attach(tp);
131 	kd->kd_tty = tp;
132 
133 	/*
134 	 * get the console struct winsize.
135 	 */
136 	if (kd_is_console) {
137 		fbconstty = tp;
138 #ifdef RASTERCONSOLE
139 		kd->rows = fbrcons_rows();
140 		kd->cols = fbrcons_cols();
141 		rcons_ttyinit(tp);
142 #endif
143 	}
144 
145 	/* else, consult the PROM */
146 	switch (prom_version()) {
147 #ifdef	PROM_OLDMON
148 	case PROM_OLDMON:
149 		if ((ep = (struct eeprom *)eeprom_va) == NULL)
150 			break;
151 		if (kd->rows == 0)
152 			kd->rows = (u_short)ep->eeTtyRows;
153 		if (kd->cols == 0)
154 			kd->cols = (u_short)ep->eeTtyCols;
155 		break;
156 #endif	/* PROM_OLDMON */
157 #ifdef	notyet /* PROM_OBP_V2 */
158 	case PROM_OBP_V0:
159 	case PROM_OBP_V2:
160 	case PROM_OBP_V3:
161 	case PROM_OPENFIRM:
162 
163 		if (kd->rows == 0 &&
164 		    (prop = PROM_getpropstring(optionsnode, "screen-#rows"))) {
165 			i = 0;
166 			while (*prop != '\0')
167 				i = i * 10 + *prop++ - '0';
168 			kd->rows = (unsigned short)i;
169 		}
170 		if (kd->cols == 0 &&
171 		    (prop = PROM_getpropstring(optionsnode, "screen-#columns"))) {
172 			i = 0;
173 			while (*prop != '\0')
174 				i = i * 10 + *prop++ - '0';
175 			kd->cols = (unsigned short)i;
176 		}
177 		break;
178 #endif	/* PROM_OBP_V2 */
179 	}
180 	return;
181 }
182 
183 struct tty *
184 kdtty(dev)
185 	dev_t dev;
186 {
187 	struct kd_softc *kd;
188 
189 	kd = &kd_softc; 	/* XXX */
190 	return (kd->kd_tty);
191 }
192 
193 int
194 kdopen(dev, flag, mode, p)
195 	dev_t dev;
196 	int flag, mode;
197 	struct proc *p;
198 {
199 	struct kd_softc *kd;
200 	int error, s, unit;
201 	struct tty *tp;
202 static	int firstopen = 1;
203 
204 	unit = minor(dev);
205 	if (unit != 0)
206 		return ENXIO;
207 	kd = &kd_softc; 	/* XXX */
208 
209 	if (firstopen) {
210 		kd_init(kd);
211 		firstopen = 0;
212 	}
213 	tp = kd->kd_tty;
214 
215 	/* It's simpler to do this up here. */
216 	if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
217 	     ==             (TS_ISOPEN | TS_XCLUDE))
218 	    && (p->p_ucred->cr_uid != 0) )
219 	{
220 		return (EBUSY);
221 	}
222 
223 	s = spltty();
224 
225 	if ((tp->t_state & TS_ISOPEN) == 0) {
226 		/* First open. */
227 
228 		/* Notify the input device that serves us */
229 		struct cons_channel *cc = kd->kd_in;
230 		if (cc != NULL &&
231 		    (error = (*cc->cc_iopen)(cc)) != 0) {
232 			splx(s);
233 			return (error);
234 		}
235 
236 		ttychars(tp);
237 		tp->t_iflag = TTYDEF_IFLAG;
238 		tp->t_oflag = TTYDEF_OFLAG;
239 		tp->t_cflag = TTYDEF_CFLAG;
240 		tp->t_lflag = TTYDEF_LFLAG;
241 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
242 		(void) kdparam(tp, &tp->t_termios);
243 		ttsetwater(tp);
244 		tp->t_winsize.ws_row = kd->rows;
245 		tp->t_winsize.ws_col = kd->cols;
246 		/* Flush pending input?  Clear translator? */
247 		/* This (pseudo)device always has SOFTCAR */
248 		tp->t_state |= TS_CARR_ON;
249 	}
250 
251 	splx(s);
252 
253 	return ((*tp->t_linesw->l_open)(dev, tp));
254 }
255 
256 int
257 kdclose(dev, flag, mode, p)
258 	dev_t dev;
259 	int flag, mode;
260 	struct proc *p;
261 {
262 	struct kd_softc *kd;
263 	struct tty *tp;
264 	struct cons_channel *cc;
265 
266 	kd = &kd_softc; 	/* XXX */
267 	tp = kd->kd_tty;
268 
269 	/* XXX This is for cons.c. */
270 	if ((tp->t_state & TS_ISOPEN) == 0)
271 		return 0;
272 
273 	(*tp->t_linesw->l_close)(tp, flag);
274 	ttyclose(tp);
275 
276 	if ((cc = kd->kd_in) != NULL)
277 		(void)(*cc->cc_iclose)(cc->cc_dev);
278 
279 	return (0);
280 }
281 
282 int
283 kdread(dev, uio, flag)
284 	dev_t dev;
285 	struct uio *uio;
286 	int flag;
287 {
288 	struct kd_softc *kd;
289 	struct tty *tp;
290 
291 	kd = &kd_softc; 	/* XXX */
292 	tp = kd->kd_tty;
293 
294 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
295 }
296 
297 int
298 kdwrite(dev, uio, flag)
299 	dev_t dev;
300 	struct uio *uio;
301 	int flag;
302 {
303 	struct kd_softc *kd;
304 	struct tty *tp;
305 
306 	kd = &kd_softc; 	/* XXX */
307 	tp = kd->kd_tty;
308 
309 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
310 }
311 
312 int
313 kdpoll(dev, events, p)
314 	dev_t dev;
315 	int events;
316 	struct proc *p;
317 {
318 	struct kd_softc *kd;
319 	struct tty *tp;
320 
321 	kd = &kd_softc; 	/* XXX */
322 	tp = kd->kd_tty;
323 
324 	return ((*tp->t_linesw->l_poll)(tp, events, p));
325 }
326 
327 int
328 kdioctl(dev, cmd, data, flag, p)
329 	dev_t dev;
330 	u_long cmd;
331 	caddr_t data;
332 	int flag;
333 	struct proc *p;
334 {
335 	struct kd_softc *kd;
336 	struct tty *tp;
337 	int error;
338 
339 	kd = &kd_softc; 	/* XXX */
340 	tp = kd->kd_tty;
341 
342 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
343 	if (error != EPASSTHROUGH)
344 		return error;
345 
346 	error = ttioctl(tp, cmd, data, flag, p);
347 	if (error != EPASSTHROUGH)
348 		return error;
349 
350 	/* Handle any ioctl commands specific to kbd/display. */
351 	/* XXX - Send KB* ioctls to kbd module? */
352 	/* XXX - Send FB* ioctls to fb module?  */
353 
354 	return EPASSTHROUGH;
355 }
356 
357 void
358 kdstop(tp, flag)
359 	struct tty *tp;
360 	int flag;
361 {
362 
363 }
364 
365 
366 static int
367 kdparam(tp, t)
368 	struct tty *tp;
369 	struct termios *t;
370 {
371 	/* XXX - These are ignored... */
372 	tp->t_ispeed = t->c_ispeed;
373 	tp->t_ospeed = t->c_ospeed;
374 	tp->t_cflag = t->c_cflag;
375 	return 0;
376 }
377 
378 
379 static void kd_later(void*);
380 static void kd_putfb(struct tty *);
381 
382 static void
383 kdstart(tp)
384 	struct tty *tp;
385 {
386 	struct clist *cl;
387 	register int s;
388 
389 	s = spltty();
390 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
391 		goto out;
392 
393 	cl = &tp->t_outq;
394 	if (cl->c_cc) {
395 		if (kd_is_console) {
396 			tp->t_state |= TS_BUSY;
397 			if (is_spl0(s)) {
398 				/* called at level zero - update screen now. */
399 				(void) spllowersoftclock();
400 				kd_putfb(tp);
401 				(void) spltty();
402 				tp->t_state &= ~TS_BUSY;
403 			} else {
404 				/* called at interrupt level - do it later */
405 				callout_reset(&tp->t_rstrt_ch, 0,
406 				    kd_later, tp);
407 			}
408 		} else {
409 			/*
410 			 * This driver uses the PROM for writing the screen,
411 			 * and that only works if this is the console device.
412 			 * If this is not the console, just flush the output.
413 			 * Sorry.  (In that case, use xdm instead of getty.)
414 			 */
415 			ndflush(cl, cl->c_cc);
416 		}
417 	}
418 	if (cl->c_cc <= tp->t_lowat) {
419 		if (tp->t_state & TS_ASLEEP) {
420 			tp->t_state &= ~TS_ASLEEP;
421 			wakeup((caddr_t)cl);
422 		}
423 		selwakeup(&tp->t_wsel);
424 	}
425 out:
426 	splx(s);
427 }
428 
429 /*
430  * Timeout function to do delayed writes to the screen.
431  * Called at splsoftclock when requested by kdstart.
432  */
433 static void
434 kd_later(tpaddr)
435 	void *tpaddr;
436 {
437 	struct tty *tp = tpaddr;
438 	register int s;
439 
440 	kd_putfb(tp);
441 
442 	s = spltty();
443 	tp->t_state &= ~TS_BUSY;
444 	(*tp->t_linesw->l_start)(tp);
445 	splx(s);
446 }
447 
448 /*
449  * Put text on the screen using the PROM monitor.
450  * This can take a while, so to avoid missing
451  * interrupts, this is called at splsoftclock.
452  */
453 static void
454 kd_putfb(tp)
455 	struct tty *tp;
456 {
457 	char buf[PUT_WSIZE];
458 	struct clist *cl = &tp->t_outq;
459 	char *p, *end;
460 	int len;
461 
462 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
463 		/* PROM will barf if high bits are set. */
464 		p = buf;
465 		end = buf + len;
466 		while (p < end)
467 			*p++ &= 0x7f;
468 		/* Now let the PROM print it. */
469 		prom_putstr(buf, len);
470 	}
471 }
472 
473 /*
474  * Default PROM-based console input stream
475  */
476 static int kd_rom_iopen __P((struct cons_channel *));
477 static int kd_rom_iclose __P((struct cons_channel *));
478 
479 static struct cons_channel prom_cons_channel;
480 
481 int
482 kd_rom_iopen(cc)
483 	struct cons_channel *cc;
484 {
485 	return (0);
486 }
487 
488 int
489 kd_rom_iclose(cc)
490 	struct cons_channel *cc;
491 {
492 	return (0);
493 }
494 
495 /*
496  * Our "interrupt" routine for input. This is called by
497  * the keyboard driver (dev/sun/kbd.c) at spltty.
498  */
499 void
500 kd_cons_input(c)
501 	int c;
502 {
503 	struct kd_softc *kd = &kd_softc;
504 	struct tty *tp;
505 
506 	/* XXX: Make sure the device is open. */
507 	tp = kd->kd_tty;
508 	if (tp == NULL)
509 		return;
510 	if ((tp->t_state & TS_ISOPEN) == 0)
511 		return;
512 
513 	(*tp->t_linesw->l_rint)(c, tp);
514 }
515 
516 
517 /****************************************************************
518  * kd console support
519  ****************************************************************/
520 
521 /* The debugger gets its own key translation state. */
522 static struct kbd_state *kdcn_state;
523 
524 static void kdcnprobe __P((struct consdev *));
525 static void kdcninit __P((struct consdev *));
526 static void kdcnputc __P((dev_t, int));
527 static void kdcnpollc __P((dev_t, int));
528 
529 /* The keyboard driver uses cn_hw to access the real console driver */
530 extern struct consdev consdev_prom;
531 struct consdev consdev_kd = {
532 	kdcnprobe,
533 	kdcninit,
534 	kdcngetc,
535 	kdcnputc,
536 	kdcnpollc,
537 	NULL,
538 };
539 struct consdev *cn_hw = &consdev_kd;
540 
541 void
542 cons_attach_input(cc, cn)
543 	struct cons_channel *cc;
544 	struct consdev *cn;
545 {
546 	struct kd_softc *kd = &kd_softc;
547 	struct kbd_softc *kds = cc->cc_dev;
548 	struct kbd_state *ks;
549 
550 	/* Share the keyboard state */
551 	kdcn_state = ks = &kds->k_state;
552 
553 	kd->kd_in = cc;
554 	cc->cc_upstream = kd_cons_input;
555 
556 	/* Attach lower level. */
557 	cn_hw->cn_dev = cn->cn_dev;
558 	cn_hw->cn_pollc = cn->cn_pollc;
559 	cn_hw->cn_getc = cn->cn_getc;
560 
561 	/* Attach us as console. */
562 	cn_tab->cn_dev = makedev(KDMAJOR, 0);
563 	cn_tab->cn_probe = kdcnprobe;
564 	cn_tab->cn_init = kdcninit;
565 	cn_tab->cn_getc = kdcngetc;
566 	cn_tab->cn_pollc = kdcnpollc;
567 	cn_tab->cn_pri = CN_INTERNAL;
568 
569 	/* Set up initial PROM input channel for /dev/console */
570 	prom_cons_channel.cc_dev = NULL;
571 	prom_cons_channel.cc_iopen = kd_rom_iopen;
572 	prom_cons_channel.cc_iclose = kd_rom_iclose;
573 
574 	/* Indicate that it is OK to use the PROM fbwrite */
575 	kd_is_console = 1;
576 }
577 
578 
579 void kd_attach_input(struct cons_channel *);
580 void
581 kd_attach_input(cc)
582 	struct cons_channel *cc;
583 {
584 	struct kd_softc *kd = &kd_softc;
585 
586 	kd->kd_in = cc;
587 	cc->cc_upstream = kd_cons_input;
588 }
589 
590 
591 /* We never call this. */
592 static void
593 kdcnprobe(cn)
594 	struct consdev *cn;
595 {
596 }
597 
598 static void
599 kdcninit(cn)
600 	struct consdev *cn;
601 {
602 #if 0
603 	struct kbd_state *ks = kdcn_state;
604 
605 	cn->cn_dev = makedev(KDMAJOR, 0);
606 	cn->cn_pri = CN_INTERNAL;
607 
608 	/* This prepares kbd_translate() */
609 	ks->kbd_id = KBD_MIN_TYPE;
610 	kbd_xlate_init(ks);
611 
612 	/* Set up initial PROM input channel for /dev/console */
613 	prom_cons_channel.cc_dev = NULL;
614 	prom_cons_channel.cc_iopen = kd_rom_iopen;
615 	prom_cons_channel.cc_iclose = kd_rom_iclose;
616 	cons_attach_input(&prom_cons_channel);
617 
618 	/* Indicate that it is OK to use the PROM fbwrite */
619 	kd_is_console = 1;
620 #endif
621 }
622 
623 static int
624 kdcngetc(dev)
625 	dev_t dev;
626 {
627 	struct kbd_state *ks = kdcn_state;
628 	int code, class, data, keysym;
629 	extern int prom_cngetc __P((dev_t));
630 
631 
632 	if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev);
633 	for (;;) {
634 		code = (*cn_hw->cn_getc)(dev);
635 		keysym = kbd_code_to_keysym(ks, code);
636 		class = KEYSYM_CLASS(keysym);
637 
638 		switch (class) {
639 		case KEYSYM_ASCII:
640 			goto out;
641 
642 		case KEYSYM_CLRMOD:
643 		case KEYSYM_SETMOD:
644 			data = (keysym & 0x1F);
645 			/* Only allow ctrl or shift. */
646 			if (data > KBMOD_SHIFT_R)
647 				break;
648 			data = 1 << data;
649 			if (class == KEYSYM_SETMOD)
650 				ks->kbd_modbits |= data;
651 			else
652 				ks->kbd_modbits &= ~data;
653 			break;
654 
655 		case KEYSYM_ALL_UP:
656 			/* No toggle keys here. */
657 			ks->kbd_modbits = 0;
658 			break;
659 
660 		default:	/* ignore all other keysyms */
661 			break;
662 		}
663 	}
664 out:
665 	return (keysym);
666 }
667 
668 static void
669 kdcnputc(dev, c)
670 	dev_t dev;
671 	int c;
672 {
673 	int s;
674 
675 	s = splhigh();
676 	prom_putchar(c);
677 	splx(s);
678 }
679 
680 static void
681 kdcnpollc(dev, on)
682 	dev_t dev;
683 	int on;
684 {
685 	struct kbd_state *ks = kdcn_state;
686 
687 	if (on) {
688 		/* Entering debugger. */
689 #if NFB > 0
690 		fb_unblank();
691 #endif
692 		/* Clear shift keys too. */
693 		ks->kbd_modbits = 0;
694 	} else {
695 		/* Resuming kernel. */
696 	}
697 	(*cn_hw->cn_pollc)(dev, on);
698 }
699