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