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