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