xref: /netbsd/sys/arch/sun2/dev/kd.c (revision c4a72b64)
1 /*	$NetBSD: kd.c,v 1.5 2002/10/23 19:48:59 jdolecek 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 
64 #ifdef RASTERCONSOLE
65 #include <dev/sun/fbio.h>
66 #include <machine/fbvar.h>
67 #endif
68 
69 
70 #include <dev/cons.h>
71 #include <dev/sun/event_var.h>
72 #include <dev/sun/kbd_xlate.h>
73 #include <dev/sun/kbdvar.h>
74 #include <sun2/dev/cons.h>
75 
76 struct	tty *fbconstty = 0;	/* tty structure for frame buffer console */
77 
78 #define PUT_WSIZE	64
79 
80 struct kd_softc {
81 	struct	device kd_dev;		/* required first: base device */
82 	struct  tty *kd_tty;
83 	int rows, cols;
84 
85 	/* Console input hook */
86 	struct cons_channel *kd_in;
87 };
88 
89 /*
90  * There is no point in pretending there might be
91  * more than one keyboard/display device.
92  */
93 static struct kd_softc kd_softc;
94 static int kd_is_console;
95 
96 static int kdparam(struct tty *, struct termios *);
97 static void kdstart(struct tty *);
98 static void kd_init __P((struct kd_softc *));
99 static void kd_cons_input __P((int));
100 static int  kdcngetc __P((dev_t));
101 
102 dev_type_open(kdopen);
103 dev_type_close(kdclose);
104 dev_type_read(kdread);
105 dev_type_write(kdwrite);
106 dev_type_ioctl(kdioctl);
107 dev_type_tty(kdtty);
108 dev_type_poll(kdpoll);
109 
110 const struct cdevsw kd_cdevsw = {
111 	kdopen, kdclose, kdread, kdwrite, kdioctl,
112 	nostop, kdtty, kdpoll, nommap, ttykqfilter, D_TTY
113 };
114 
115 /*
116  * This is called by kbd_attach()
117  * XXX - Make this a proper child of kbd?
118  */
119 void
120 kd_init(kd)
121 	struct kd_softc *kd;
122 {
123 	struct tty *tp;
124 #ifdef	PROM_OLDMON
125 	struct eeprom *ep;
126 #endif
127 #ifdef	notyet /* PROM_OBP_V2 */
128 	int i;
129 	char *prop;
130 #endif
131 
132 	kd = &kd_softc; 	/* XXX */
133 
134 	tp = ttymalloc();
135 	tp->t_oproc = kdstart;
136 	tp->t_param = kdparam;
137 	tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
138 
139 	tty_attach(tp);
140 	kd->kd_tty = tp;
141 
142 	/*
143 	 * get the console struct winsize.
144 	 */
145 	if (kd_is_console) {
146 		fbconstty = tp;
147 #ifdef RASTERCONSOLE
148 		kd->rows = fbrcons_rows();
149 		kd->cols = fbrcons_cols();
150 		rcons_ttyinit(tp);
151 #endif
152 	}
153 
154 	/* else, consult the PROM */
155 	switch (prom_version()) {
156 #ifdef	PROM_OLDMON
157 	case PROM_OLDMON:
158 		if ((ep = (struct eeprom *)eeprom_va) == NULL)
159 			break;
160 		if (kd->rows == 0)
161 			kd->rows = (u_short)ep->eeTtyRows;
162 		if (kd->cols == 0)
163 			kd->cols = (u_short)ep->eeTtyCols;
164 		break;
165 #endif	/* PROM_OLDMON */
166 #ifdef	notyet /* PROM_OBP_V2 */
167 	case PROM_OBP_V0:
168 	case PROM_OBP_V2:
169 	case PROM_OBP_V3:
170 	case PROM_OPENFIRM:
171 
172 		if (kd->rows == 0 &&
173 		    (prop = PROM_getpropstring(optionsnode, "screen-#rows"))) {
174 			i = 0;
175 			while (*prop != '\0')
176 				i = i * 10 + *prop++ - '0';
177 			kd->rows = (unsigned short)i;
178 		}
179 		if (kd->cols == 0 &&
180 		    (prop = PROM_getpropstring(optionsnode, "screen-#columns"))) {
181 			i = 0;
182 			while (*prop != '\0')
183 				i = i * 10 + *prop++ - '0';
184 			kd->cols = (unsigned short)i;
185 		}
186 		break;
187 #endif	/* PROM_OBP_V2 */
188 	}
189 	return;
190 }
191 
192 struct tty *
193 kdtty(dev)
194 	dev_t dev;
195 {
196 	struct kd_softc *kd;
197 
198 	kd = &kd_softc; 	/* XXX */
199 	return (kd->kd_tty);
200 }
201 
202 int
203 kdopen(dev, flag, mode, p)
204 	dev_t dev;
205 	int flag, mode;
206 	struct proc *p;
207 {
208 	struct kd_softc *kd;
209 	int error, s, unit;
210 	struct tty *tp;
211 static	int firstopen = 1;
212 
213 	unit = minor(dev);
214 	if (unit != 0)
215 		return ENXIO;
216 	kd = &kd_softc; 	/* XXX */
217 
218 	if (firstopen) {
219 		kd_init(kd);
220 		firstopen = 0;
221 	}
222 	tp = kd->kd_tty;
223 
224 	/* It's simpler to do this up here. */
225 	if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
226 	     ==             (TS_ISOPEN | TS_XCLUDE))
227 	    && (p->p_ucred->cr_uid != 0) )
228 	{
229 		return (EBUSY);
230 	}
231 
232 	s = spltty();
233 
234 	if ((tp->t_state & TS_ISOPEN) == 0) {
235 		/* First open. */
236 
237 		/* Notify the input device that serves us */
238 		struct cons_channel *cc = kd->kd_in;
239 		if (cc != NULL &&
240 		    (error = (*cc->cc_iopen)(cc)) != 0) {
241 			splx(s);
242 			return (error);
243 		}
244 
245 		ttychars(tp);
246 		tp->t_iflag = TTYDEF_IFLAG;
247 		tp->t_oflag = TTYDEF_OFLAG;
248 		tp->t_cflag = TTYDEF_CFLAG;
249 		tp->t_lflag = TTYDEF_LFLAG;
250 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
251 		(void) kdparam(tp, &tp->t_termios);
252 		ttsetwater(tp);
253 		tp->t_winsize.ws_row = kd->rows;
254 		tp->t_winsize.ws_col = kd->cols;
255 		/* Flush pending input?  Clear translator? */
256 		/* This (pseudo)device always has SOFTCAR */
257 		tp->t_state |= TS_CARR_ON;
258 	}
259 
260 	splx(s);
261 
262 	return ((*tp->t_linesw->l_open)(dev, tp));
263 }
264 
265 int
266 kdclose(dev, flag, mode, p)
267 	dev_t dev;
268 	int flag, mode;
269 	struct proc *p;
270 {
271 	struct kd_softc *kd;
272 	struct tty *tp;
273 	struct cons_channel *cc;
274 
275 	kd = &kd_softc; 	/* XXX */
276 	tp = kd->kd_tty;
277 
278 	/* XXX This is for cons.c. */
279 	if ((tp->t_state & TS_ISOPEN) == 0)
280 		return 0;
281 
282 	(*tp->t_linesw->l_close)(tp, flag);
283 	ttyclose(tp);
284 
285 	if ((cc = kd->kd_in) != NULL)
286 		(void)(*cc->cc_iclose)(cc);
287 
288 	return (0);
289 }
290 
291 int
292 kdread(dev, uio, flag)
293 	dev_t dev;
294 	struct uio *uio;
295 	int flag;
296 {
297 	struct kd_softc *kd;
298 	struct tty *tp;
299 
300 	kd = &kd_softc; 	/* XXX */
301 	tp = kd->kd_tty;
302 
303 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
304 }
305 
306 int
307 kdwrite(dev, uio, flag)
308 	dev_t dev;
309 	struct uio *uio;
310 	int flag;
311 {
312 	struct kd_softc *kd;
313 	struct tty *tp;
314 
315 	kd = &kd_softc; 	/* XXX */
316 	tp = kd->kd_tty;
317 
318 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
319 }
320 
321 int
322 kdpoll(dev, events, p)
323 	dev_t dev;
324 	int events;
325 	struct proc *p;
326 {
327 	struct kd_softc *kd;
328 	struct tty *tp;
329 
330 	kd = &kd_softc; 	/* XXX */
331 	tp = kd->kd_tty;
332 
333 	return ((*tp->t_linesw->l_poll)(tp, events, p));
334 }
335 
336 int
337 kdioctl(dev, cmd, data, flag, p)
338 	dev_t dev;
339 	u_long cmd;
340 	caddr_t data;
341 	int flag;
342 	struct proc *p;
343 {
344 	struct kd_softc *kd;
345 	struct tty *tp;
346 	int error;
347 
348 	kd = &kd_softc; 	/* XXX */
349 	tp = kd->kd_tty;
350 
351 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
352 	if (error != EPASSTHROUGH)
353 		return error;
354 
355 	error = ttioctl(tp, cmd, data, flag, p);
356 	if (error != EPASSTHROUGH)
357 		return error;
358 
359 	/* Handle any ioctl commands specific to kbd/display. */
360 	/* XXX - Send KB* ioctls to kbd module? */
361 	/* XXX - Send FB* ioctls to fb module?  */
362 
363 	return EPASSTHROUGH;
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(cdevsw_lookup_major(&kd_cdevsw), 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(cdevsw_lookup_major(&kd_cdevsw), 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