xref: /netbsd-src/sys/arch/sparc/dev/kd.c (revision f9228f42259a421502f04b1ddb2df91c2ecbc519)
1*f9228f42Sdholland /*	$NetBSD: kd.c,v 1.54 2014/07/25 08:10:34 dholland Exp $	*/
20308bf1aSgwr 
30308bf1aSgwr /*-
40308bf1aSgwr  * Copyright (c) 1996 The NetBSD Foundation, Inc.
50308bf1aSgwr  * All rights reserved.
60308bf1aSgwr  *
70308bf1aSgwr  * This code is derived from software contributed to The NetBSD Foundation
80308bf1aSgwr  * by Gordon W. Ross.
90308bf1aSgwr  *
100308bf1aSgwr  * Redistribution and use in source and binary forms, with or without
110308bf1aSgwr  * modification, are permitted provided that the following conditions
120308bf1aSgwr  * are met:
130308bf1aSgwr  * 1. Redistributions of source code must retain the above copyright
140308bf1aSgwr  *    notice, this list of conditions and the following disclaimer.
150308bf1aSgwr  * 2. Redistributions in binary form must reproduce the above copyright
160308bf1aSgwr  *    notice, this list of conditions and the following disclaimer in the
170308bf1aSgwr  *    documentation and/or other materials provided with the distribution.
180308bf1aSgwr  *
190308bf1aSgwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
200308bf1aSgwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
210308bf1aSgwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220308bf1aSgwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
230308bf1aSgwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
240308bf1aSgwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
250308bf1aSgwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
260308bf1aSgwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
270308bf1aSgwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
280308bf1aSgwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
290308bf1aSgwr  * POSSIBILITY OF SUCH DAMAGE.
300308bf1aSgwr  */
310308bf1aSgwr 
320308bf1aSgwr /*
3341fb5989Spk  * Console driver based on PROM primitives.
340308bf1aSgwr  *
3541fb5989Spk  * This driver exists to provide a tty device that the indirect
3641fb5989Spk  * console driver can point to. It also provides a hook that
3741fb5989Spk  * allows another device to serve console input. This will normally
3841fb5989Spk  * be a keyboard driver (see sys/dev/sun/kbd.c)
390308bf1aSgwr  */
400308bf1aSgwr 
41a4183603Slukem #include <sys/cdefs.h>
42*f9228f42Sdholland __KERNEL_RCSID(0, "$NetBSD: kd.c,v 1.54 2014/07/25 08:10:34 dholland Exp $");
43a4183603Slukem 
44e95e804cSpk #include "opt_kgdb.h"
45c522c184Spk #include "fb.h"
46e95e804cSpk 
470308bf1aSgwr #include <sys/param.h>
480308bf1aSgwr #include <sys/proc.h>
490308bf1aSgwr #include <sys/systm.h>
5041fb5989Spk #include <sys/kernel.h>
510308bf1aSgwr #include <sys/ioctl.h>
520308bf1aSgwr #include <sys/tty.h>
530308bf1aSgwr #include <sys/file.h>
540308bf1aSgwr #include <sys/conf.h>
550308bf1aSgwr #include <sys/device.h>
568ccb6c93Selad #include <sys/kauth.h>
570308bf1aSgwr 
580308bf1aSgwr #include <machine/bsd_openprom.h>
598edcd039Spk #include <machine/promlib.h>
600308bf1aSgwr #include <machine/eeprom.h>
610308bf1aSgwr #include <machine/psl.h>
620308bf1aSgwr #include <machine/cpu.h>
630308bf1aSgwr #include <machine/kbd.h>
640308bf1aSgwr #include <machine/autoconf.h>
650308bf1aSgwr 
66c522c184Spk #if defined(RASTERCONSOLE) && NFB > 0
67702e1dd9Spk #include <dev/sun/fbio.h>
68702e1dd9Spk #include <dev/sun/fbvar.h>
690308bf1aSgwr #endif
700308bf1aSgwr 
710308bf1aSgwr #include <dev/cons.h>
720308bf1aSgwr #include <sparc/dev/cons.h>
730308bf1aSgwr 
7441fb5989Spk #include <dev/sun/event_var.h>
7541fb5989Spk #include <dev/sun/kbd_xlate.h>
7641fb5989Spk #include <dev/sun/kbdvar.h>
7741fb5989Spk 
780308bf1aSgwr #define PUT_WSIZE	64
790308bf1aSgwr 
800308bf1aSgwr struct kd_softc {
810308bf1aSgwr 	struct  tty *kd_tty;
820308bf1aSgwr 	int rows, cols;
8341fb5989Spk 
8441fb5989Spk 	/* Console input hook */
8541fb5989Spk 	struct cons_channel *kd_in;
860308bf1aSgwr };
870308bf1aSgwr 
880308bf1aSgwr /*
890308bf1aSgwr  * There is no point in pretending there might be
900308bf1aSgwr  * more than one keyboard/display device.
910308bf1aSgwr  */
920308bf1aSgwr static struct kd_softc kd_softc;
930308bf1aSgwr 
940ec5f35bSuwe /* For keyboard driver to register itself as console input */
950ec5f35bSuwe void kd_attach_input(struct cons_channel *);
960ec5f35bSuwe 
9768f362b7Suwe static void kd_init(struct kd_softc *);
980ec5f35bSuwe static void kdstart(struct tty *);
990ec5f35bSuwe static void kd_later(void *);
1000ec5f35bSuwe static void kd_putfb(struct tty *);
1010ec5f35bSuwe static int kdparam(struct tty *, struct termios *);
10268f362b7Suwe static void kd_cons_input(int);
1030308bf1aSgwr 
10477a6b82bSgehenna dev_type_open(kdopen);
10577a6b82bSgehenna dev_type_close(kdclose);
10677a6b82bSgehenna dev_type_read(kdread);
10777a6b82bSgehenna dev_type_write(kdwrite);
10877a6b82bSgehenna dev_type_ioctl(kdioctl);
10977a6b82bSgehenna dev_type_tty(kdtty);
11077a6b82bSgehenna dev_type_poll(kdpoll);
11177a6b82bSgehenna 
11277a6b82bSgehenna const struct cdevsw kd_cdevsw = {
113a68f9396Sdholland 	.d_open = kdopen,
114a68f9396Sdholland 	.d_close = kdclose,
115a68f9396Sdholland 	.d_read = kdread,
116a68f9396Sdholland 	.d_write = kdwrite,
117a68f9396Sdholland 	.d_ioctl = kdioctl,
118a68f9396Sdholland 	.d_stop = nostop,
119a68f9396Sdholland 	.d_tty = kdtty,
120a68f9396Sdholland 	.d_poll = kdpoll,
121a68f9396Sdholland 	.d_mmap = nommap,
122a68f9396Sdholland 	.d_kqfilter = ttykqfilter,
123*f9228f42Sdholland 	.d_discard = nodiscard,
124a68f9396Sdholland 	.d_flag = D_TTY
12577a6b82bSgehenna };
12677a6b82bSgehenna 
1270308bf1aSgwr /*
12841fb5989Spk  * Prepare the console tty; called on first open of /dev/console
1290308bf1aSgwr  */
1300ec5f35bSuwe static void
kd_init(struct kd_softc * kd)1310ec5f35bSuwe kd_init(struct kd_softc *kd)
13241fb5989Spk {
1330308bf1aSgwr 	struct tty *tp;
1340308bf1aSgwr 
1352626d576Srmind 	tp = tty_alloc();
136d238692cSjoerg 	callout_setfunc(&tp->t_rstrt_ch, kd_later, tp);
1370308bf1aSgwr 	tp->t_oproc = kdstart;
1380308bf1aSgwr 	tp->t_param = kdparam;
13977a6b82bSgehenna 	tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
1400308bf1aSgwr 
1410308bf1aSgwr 	tty_attach(tp);
1420308bf1aSgwr 	kd->kd_tty = tp;
1430308bf1aSgwr 
1440308bf1aSgwr 	/*
14541fb5989Spk 	 * Get the console struct winsize.
1460308bf1aSgwr 	 */
147c522c184Spk #if defined(RASTERCONSOLE) && NFB > 0
14841fb5989Spk 	/* If the raster console driver is attached, copy its size */
1490308bf1aSgwr 	kd->rows = fbrcons_rows();
1500308bf1aSgwr 	kd->cols = fbrcons_cols();
151436638abSpk 	rcons_ttyinit(tp);
1520308bf1aSgwr #endif
1530308bf1aSgwr 
15441fb5989Spk 	/* else, consult the PROM */
15541fb5989Spk 	switch (prom_version()) {
1569166dfe5Spk 	char prop[6+1];		/* Enough for six digits */
15741fb5989Spk 	struct eeprom *ep;
15841fb5989Spk 	case PROM_OLDMON:
15941fb5989Spk 		if ((ep = (struct eeprom *)eeprom_va) == NULL)
16041fb5989Spk 			break;
16141fb5989Spk 		if (kd->rows == 0)
16241fb5989Spk 			kd->rows = (u_short)ep->eeTtyRows;
16341fb5989Spk 		if (kd->cols == 0)
16441fb5989Spk 			kd->cols = (u_short)ep->eeTtyCols;
16541fb5989Spk 		break;
1669166dfe5Spk 
16741fb5989Spk 	case PROM_OBP_V0:
16841fb5989Spk 	case PROM_OBP_V2:
16941fb5989Spk 	case PROM_OBP_V3:
17041fb5989Spk 	case PROM_OPENFIRM:
1710308bf1aSgwr 		if (kd->rows == 0 &&
1729166dfe5Spk 		    prom_getoption("screen-#rows", prop, sizeof prop) == 0)
1739166dfe5Spk 			kd->rows = strtoul(prop, NULL, 10);
17441fb5989Spk 
1750308bf1aSgwr 		if (kd->cols == 0 &&
1769166dfe5Spk 		    prom_getoption("screen-#columns", prop, sizeof prop) == 0)
1779166dfe5Spk 			kd->cols = strtoul(prop, NULL, 10);
17841fb5989Spk 
17941fb5989Spk 		break;
1800308bf1aSgwr 	}
1810308bf1aSgwr 
1820308bf1aSgwr 	return;
1830308bf1aSgwr }
1840308bf1aSgwr 
1850308bf1aSgwr struct tty *
kdtty(dev_t dev)1860ec5f35bSuwe kdtty(dev_t dev)
1870308bf1aSgwr {
1880308bf1aSgwr 	struct kd_softc *kd;
1890308bf1aSgwr 
1900308bf1aSgwr 	kd = &kd_softc; 	/* XXX */
1910308bf1aSgwr 	return (kd->kd_tty);
1920308bf1aSgwr }
1930308bf1aSgwr 
1940308bf1aSgwr int
kdopen(dev_t dev,int flag,int mode,struct lwp * l)19595e1ffb1Schristos kdopen(dev_t dev, int flag, int mode, struct lwp *l)
1960308bf1aSgwr {
1970308bf1aSgwr 	struct kd_softc *kd;
1980308bf1aSgwr 	int error, s, unit;
1990308bf1aSgwr 	struct tty *tp;
20041fb5989Spk static	int firstopen = 1;
2010308bf1aSgwr 
2020308bf1aSgwr 	unit = minor(dev);
2030308bf1aSgwr 	if (unit != 0)
2040308bf1aSgwr 		return ENXIO;
2050308bf1aSgwr 
20641fb5989Spk 	kd = &kd_softc; 	/* XXX */
20741fb5989Spk 	if (firstopen) {
20841fb5989Spk 		kd_init(kd);
20941fb5989Spk 		firstopen = 0;
2100308bf1aSgwr 	}
2110308bf1aSgwr 
21241fb5989Spk 	tp = kd->kd_tty;
21341fb5989Spk 
2140308bf1aSgwr 	/* It's simpler to do this up here. */
215e8373398Selad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
2160308bf1aSgwr 		return (EBUSY);
2170308bf1aSgwr 
2180308bf1aSgwr 	s = spltty();
2190308bf1aSgwr 	if ((tp->t_state & TS_ISOPEN) == 0) {
2200308bf1aSgwr 		/* First open. */
22141fb5989Spk 
22241fb5989Spk 		/* Notify the input device that serves us */
22341fb5989Spk 		struct cons_channel *cc = kd->kd_in;
22441fb5989Spk 		if (cc != NULL &&
22541fb5989Spk 		    (error = (*cc->cc_iopen)(cc)) != 0) {
22641fb5989Spk 			return (error);
22741fb5989Spk 		}
22841fb5989Spk 
2290308bf1aSgwr 		ttychars(tp);
2300308bf1aSgwr 		tp->t_iflag = TTYDEF_IFLAG;
2310308bf1aSgwr 		tp->t_oflag = TTYDEF_OFLAG;
2320308bf1aSgwr 		tp->t_cflag = TTYDEF_CFLAG;
2330308bf1aSgwr 		tp->t_lflag = TTYDEF_LFLAG;
2340308bf1aSgwr 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
2350308bf1aSgwr 		(void) kdparam(tp, &tp->t_termios);
2360308bf1aSgwr 		ttsetwater(tp);
2370308bf1aSgwr 		tp->t_winsize.ws_row = kd->rows;
2380308bf1aSgwr 		tp->t_winsize.ws_col = kd->cols;
2390308bf1aSgwr 		/* Flush pending input?  Clear translator? */
2400308bf1aSgwr 		/* This (pseudo)device always has SOFTCAR */
2410308bf1aSgwr 		tp->t_state |= TS_CARR_ON;
2420308bf1aSgwr 	}
2430308bf1aSgwr 
2440308bf1aSgwr 	splx(s);
2450308bf1aSgwr 
2468c3f6a0dSeeh 	return ((*tp->t_linesw->l_open)(dev, tp));
2470308bf1aSgwr }
2480308bf1aSgwr 
2490308bf1aSgwr int
kdclose(dev_t dev,int flag,int mode,struct lwp * l)25095e1ffb1Schristos kdclose(dev_t dev, int flag, int mode, struct lwp *l)
2510308bf1aSgwr {
2520308bf1aSgwr 	struct kd_softc *kd;
2530308bf1aSgwr 	struct tty *tp;
25441fb5989Spk 	struct cons_channel *cc;
2550308bf1aSgwr 
2560308bf1aSgwr 	kd = &kd_softc; 	/* XXX */
2570308bf1aSgwr 	tp = kd->kd_tty;
2580308bf1aSgwr 
2590308bf1aSgwr 	/* XXX This is for cons.c. */
2600308bf1aSgwr 	if ((tp->t_state & TS_ISOPEN) == 0)
2610308bf1aSgwr 		return 0;
2620308bf1aSgwr 
2638c3f6a0dSeeh 	(*tp->t_linesw->l_close)(tp, flag);
2640308bf1aSgwr 	ttyclose(tp);
26541fb5989Spk 
26641fb5989Spk 	if ((cc = kd->kd_in) != NULL)
2673cffbb81Spk 		(void)(*cc->cc_iclose)(cc);
26841fb5989Spk 
2690308bf1aSgwr 	return (0);
2700308bf1aSgwr }
2710308bf1aSgwr 
2720308bf1aSgwr int
kdread(dev_t dev,struct uio * uio,int flag)2730ec5f35bSuwe kdread(dev_t dev, struct uio *uio, int flag)
2740308bf1aSgwr {
2750308bf1aSgwr 	struct kd_softc *kd;
2760308bf1aSgwr 	struct tty *tp;
2770308bf1aSgwr 
2780308bf1aSgwr 	kd = &kd_softc; 	/* XXX */
2790308bf1aSgwr 	tp = kd->kd_tty;
2800308bf1aSgwr 
2818c3f6a0dSeeh 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
2820308bf1aSgwr }
2830308bf1aSgwr 
2840308bf1aSgwr int
kdwrite(dev_t dev,struct uio * uio,int flag)2850ec5f35bSuwe kdwrite(dev_t dev, struct uio *uio, int flag)
2860308bf1aSgwr {
2870308bf1aSgwr 	struct kd_softc *kd;
2880308bf1aSgwr 	struct tty *tp;
2890308bf1aSgwr 
2900308bf1aSgwr 	kd = &kd_softc; 	/* XXX */
2910308bf1aSgwr 	tp = kd->kd_tty;
2920308bf1aSgwr 
2938c3f6a0dSeeh 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
2940308bf1aSgwr }
2950308bf1aSgwr 
2960308bf1aSgwr int
kdpoll(dev_t dev,int events,struct lwp * l)29795e1ffb1Schristos kdpoll(dev_t dev, int events, struct lwp *l)
2982963ff5cSscw {
2992963ff5cSscw 	struct kd_softc *kd;
3002963ff5cSscw 	struct tty *tp;
3012963ff5cSscw 
3022963ff5cSscw 	kd = &kd_softc; 	/* XXX */
3032963ff5cSscw 	tp = kd->kd_tty;
3042963ff5cSscw 
30595e1ffb1Schristos 	return ((*tp->t_linesw->l_poll)(tp, events, l));
3062963ff5cSscw }
3072963ff5cSscw 
3082963ff5cSscw int
kdioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)30953524e44Schristos kdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
3100308bf1aSgwr {
3110308bf1aSgwr 	struct kd_softc *kd;
3120308bf1aSgwr 	struct tty *tp;
3130308bf1aSgwr 	int error;
3140308bf1aSgwr 
3150308bf1aSgwr 	kd = &kd_softc; 	/* XXX */
3160308bf1aSgwr 	tp = kd->kd_tty;
3170308bf1aSgwr 
31895e1ffb1Schristos 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
31931144d99Satatat 	if (error != EPASSTHROUGH)
3200308bf1aSgwr 		return error;
32131144d99Satatat 
32295e1ffb1Schristos 	error = ttioctl(tp, cmd, data, flag, l);
32331144d99Satatat 	if (error != EPASSTHROUGH)
3240308bf1aSgwr 		return error;
3250308bf1aSgwr 
3260308bf1aSgwr 	/* Handle any ioctl commands specific to kbd/display. */
3270308bf1aSgwr 	/* XXX - Send KB* ioctls to kbd module? */
3280308bf1aSgwr 	/* XXX - Send FB* ioctls to fb module?  */
3290308bf1aSgwr 
33031144d99Satatat 	return EPASSTHROUGH;
3310308bf1aSgwr }
3320308bf1aSgwr 
3330308bf1aSgwr static int
kdparam(struct tty * tp,struct termios * t)3340ec5f35bSuwe kdparam(struct tty *tp, struct termios *t)
3350308bf1aSgwr {
3360ec5f35bSuwe 
3370308bf1aSgwr 	/* XXX - These are ignored... */
3380308bf1aSgwr 	tp->t_ispeed = t->c_ispeed;
3390308bf1aSgwr 	tp->t_ospeed = t->c_ospeed;
3400308bf1aSgwr 	tp->t_cflag = t->c_cflag;
3410308bf1aSgwr 	return 0;
3420308bf1aSgwr }
3430308bf1aSgwr 
3440308bf1aSgwr static void
kdstart(struct tty * tp)3450ec5f35bSuwe kdstart(struct tty *tp)
3460308bf1aSgwr {
34736bb413eSad 	int s1, s2;
3480308bf1aSgwr 
34936bb413eSad 	s1 = splsoftclock();
35036bb413eSad 	s2 = spltty();
3510308bf1aSgwr 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
3520308bf1aSgwr 		goto out;
3530308bf1aSgwr 
354dc26833bSad 	if (ttypull(tp)) {
3550308bf1aSgwr 		tp->t_state |= TS_BUSY;
35636bb413eSad 		if ((s1 & PSR_PIL) == 0) {
3570308bf1aSgwr 			/* called at level zero - update screen now. */
35836bb413eSad 			splx(s2);
3590308bf1aSgwr 			kd_putfb(tp);
36036bb413eSad 			s2 = spltty();
3610308bf1aSgwr 			tp->t_state &= ~TS_BUSY;
3620308bf1aSgwr 		} else {
3630308bf1aSgwr 			/* called at interrupt level - do it later */
364d238692cSjoerg 			callout_schedule(&tp->t_rstrt_ch, 0);
3650308bf1aSgwr 		}
3660308bf1aSgwr 	}
3670308bf1aSgwr out:
36836bb413eSad 	splx(s2);
36936bb413eSad 	splx(s1);
3700308bf1aSgwr }
3710308bf1aSgwr 
3720308bf1aSgwr /*
3730308bf1aSgwr  * Timeout function to do delayed writes to the screen.
3740308bf1aSgwr  * Called at splsoftclock when requested by kdstart.
3750308bf1aSgwr  */
3760308bf1aSgwr static void
kd_later(void * arg)3770ec5f35bSuwe kd_later(void *arg)
3780308bf1aSgwr {
37941fb5989Spk 	struct tty *tp = arg;
38041fb5989Spk 	int s;
3810308bf1aSgwr 
3820308bf1aSgwr 	kd_putfb(tp);
3830308bf1aSgwr 
3840308bf1aSgwr 	s = spltty();
3850308bf1aSgwr 	tp->t_state &= ~TS_BUSY;
3868c3f6a0dSeeh 	(*tp->t_linesw->l_start)(tp);
3870308bf1aSgwr 	splx(s);
3880308bf1aSgwr }
3890308bf1aSgwr 
3900308bf1aSgwr /*
3910308bf1aSgwr  * Put text on the screen using the PROM monitor.
3920308bf1aSgwr  * This can take a while, so to avoid missing
3930308bf1aSgwr  * interrupts, this is called at splsoftclock.
3940308bf1aSgwr  */
3950308bf1aSgwr static void
kd_putfb(struct tty * tp)3960ec5f35bSuwe kd_putfb(struct tty *tp)
3970308bf1aSgwr {
3980308bf1aSgwr 	char buf[PUT_WSIZE];
3990308bf1aSgwr 	struct clist *cl = &tp->t_outq;
4000308bf1aSgwr 	char *p, *end;
4010308bf1aSgwr 	int len;
4020308bf1aSgwr 
4030308bf1aSgwr 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
4040308bf1aSgwr 		/* PROM will barf if high bits are set. */
4050308bf1aSgwr 		p = buf;
4060308bf1aSgwr 		end = buf + len;
4070308bf1aSgwr 		while (p < end)
4080308bf1aSgwr 			*p++ &= 0x7f;
4098edcd039Spk 
4100308bf1aSgwr 		/* Now let the PROM print it. */
4118edcd039Spk 		prom_putstr(buf, len);
4120308bf1aSgwr 	}
4130308bf1aSgwr }
4140308bf1aSgwr 
4150308bf1aSgwr /*
4160308bf1aSgwr  * Our "interrupt" routine for input. This is called by
4170308bf1aSgwr  * the keyboard driver (dev/sun/kbd.c) at spltty.
4180308bf1aSgwr  */
4190ec5f35bSuwe static void
kd_cons_input(int c)4200ec5f35bSuwe kd_cons_input(int c)
4210308bf1aSgwr {
4220308bf1aSgwr 	struct kd_softc *kd = &kd_softc;
4230308bf1aSgwr 	struct tty *tp;
4240308bf1aSgwr 
4250308bf1aSgwr 	/* XXX: Make sure the device is open. */
4260308bf1aSgwr 	tp = kd->kd_tty;
4270308bf1aSgwr 	if (tp == NULL)
4280308bf1aSgwr 		return;
4290308bf1aSgwr 	if ((tp->t_state & TS_ISOPEN) == 0)
4300308bf1aSgwr 		return;
4310308bf1aSgwr 
4328c3f6a0dSeeh 	(*tp->t_linesw->l_rint)(c, tp);
4330308bf1aSgwr }
4340308bf1aSgwr 
43541fb5989Spk void
cons_attach_input(struct cons_channel * cc,struct consdev * cn)4360ec5f35bSuwe cons_attach_input(struct cons_channel *cc, struct consdev *cn)
43741fb5989Spk {
43841fb5989Spk 	struct kd_softc *kd = &kd_softc;
4390308bf1aSgwr 
44041fb5989Spk 	kd->kd_in = cc;
44141fb5989Spk 	cc->cc_upstream = kd_cons_input;
44241fb5989Spk }
4430308bf1aSgwr 
4446708aee3Schristos void
kd_attach_input(struct cons_channel * cc)4450ec5f35bSuwe kd_attach_input(struct cons_channel *cc)
4466708aee3Schristos {
4476708aee3Schristos 	struct kd_softc *kd = &kd_softc;
4486708aee3Schristos 
4496708aee3Schristos 	kd->kd_in = cc;
4506708aee3Schristos 	cc->cc_upstream = kd_cons_input;
4516708aee3Schristos }
4520308bf1aSgwr 
45341fb5989Spk /*
45441fb5989Spk  * Default PROM-based console input stream
45541fb5989Spk  * Since the PROM does not notify us when data is available on the
45641fb5989Spk  * input channel these functions periodically poll the PROM.
45741fb5989Spk  */
45868f362b7Suwe static int kd_rom_iopen(struct cons_channel *);
45968f362b7Suwe static int kd_rom_iclose(struct cons_channel *);
46068f362b7Suwe static void kd_rom_intr(void *);
4610308bf1aSgwr 
4623a942f10Suwe static struct cons_channel prom_cons_channel = {
4633a942f10Suwe 	NULL,			/* no private data */
4643a942f10Suwe 	kd_rom_iopen,
4653a942f10Suwe 	kd_rom_iclose,
4663a942f10Suwe 	NULL			/* will be set by kd driver */
4673a942f10Suwe };
4683a942f10Suwe 
46988ab7da9Sad static struct callout prom_cons_callout;
47041fb5989Spk 
4710ec5f35bSuwe static int
kd_rom_iopen(struct cons_channel * cc)4720ec5f35bSuwe kd_rom_iopen(struct cons_channel *cc)
47341fb5989Spk {
47488ab7da9Sad 	static bool callo;
47588ab7da9Sad 
47688ab7da9Sad 	if (!callo) {
47788ab7da9Sad 		callout_init(&prom_cons_callout, 0);
47888ab7da9Sad 		callo = true;
47988ab7da9Sad 	}
4803a942f10Suwe 
48141fb5989Spk 	/* Poll for ROM input 4 times per second */
4823a942f10Suwe 	callout_reset(&prom_cons_callout, hz / 4, kd_rom_intr, cc);
48341fb5989Spk 	return (0);
48441fb5989Spk }
48541fb5989Spk 
4860ec5f35bSuwe static int
kd_rom_iclose(struct cons_channel * cc)4870ec5f35bSuwe kd_rom_iclose(struct cons_channel *cc)
48841fb5989Spk {
48941fb5989Spk 
4903a942f10Suwe 	callout_stop(&prom_cons_callout);
49141fb5989Spk 	return (0);
49241fb5989Spk }
49341fb5989Spk 
49441fb5989Spk /*
49541fb5989Spk  * "Interrupt" routine for input through ROM vectors
49641fb5989Spk  */
4970ec5f35bSuwe static void
kd_rom_intr(void * arg)4980ec5f35bSuwe kd_rom_intr(void *arg)
49941fb5989Spk {
50041fb5989Spk 	struct cons_channel *cc = arg;
50141fb5989Spk 	int s, c;
50241fb5989Spk 
5033a942f10Suwe 	callout_schedule(&prom_cons_callout, hz / 4);
50441fb5989Spk 
50541fb5989Spk 	s = spltty();
50641fb5989Spk 
50741fb5989Spk 	while ((c = prom_peekchar()) >= 0)
50841fb5989Spk 		(*cc->cc_upstream)(c);
50941fb5989Spk 
51041fb5989Spk 	splx(s);
51141fb5989Spk }
51241fb5989Spk 
51341fb5989Spk /*****************************************************************/
51441fb5989Spk 
51541fb5989Spk int prom_stdin_node;
51641fb5989Spk int prom_stdout_node;
51741fb5989Spk char prom_stdin_args[16];
51841fb5989Spk char prom_stdout_args[16];
51941fb5989Spk 
5200ec5f35bSuwe static void prom_cnprobe(struct consdev *);
52168f362b7Suwe static void prom_cninit(struct consdev *);
5220ec5f35bSuwe int  prom_cngetc(dev_t);	/* XXX: for sunkbd_wskbd_cngetc */
52368f362b7Suwe static void prom_cnputc(dev_t, int);
5240ec5f35bSuwe static void prom_cnpollc(dev_t, int);
52541fb5989Spk 
52641fb5989Spk /*
52741fb5989Spk  * The console is set to this one initially,
52841fb5989Spk  * which lets us use the PROM until consinit()
52941fb5989Spk  * is called to select a real console.
53041fb5989Spk  */
53141fb5989Spk struct consdev consdev_prom = {
53241fb5989Spk 	prom_cnprobe,
53341fb5989Spk 	prom_cninit,
53441fb5989Spk 	prom_cngetc,
53541fb5989Spk 	prom_cnputc,
53641fb5989Spk 	prom_cnpollc,
537a183d34fSthorpej 	NULL,
5380308bf1aSgwr };
5390308bf1aSgwr 
54041fb5989Spk /*
54141fb5989Spk  * The console table pointer is statically initialized
54241fb5989Spk  * to point to the PROM table, so that early calls to printf will work.
5434362d3edSchristos  * this has been moved to cpu_startup()
54441fb5989Spk  */
5454362d3edSchristos #if 0
54641fb5989Spk struct consdev *cn_tab = &consdev_prom;
5474362d3edSchristos #endif
54841fb5989Spk 
5490ec5f35bSuwe static void
prom_cnprobe(struct consdev * cn)5500ec5f35bSuwe prom_cnprobe(struct consdev *cn)
5510308bf1aSgwr {
5520308bf1aSgwr }
5530308bf1aSgwr 
5540308bf1aSgwr static void
prom_cninit(struct consdev * cn)5550ec5f35bSuwe prom_cninit(struct consdev *cn)
5560308bf1aSgwr {
5570308bf1aSgwr }
5580308bf1aSgwr 
5590ec5f35bSuwe static void
prom_cnpollc(dev_t dev,int on)5600ec5f35bSuwe prom_cnpollc(dev_t dev, int on)
56141fb5989Spk {
56241fb5989Spk 
56341fb5989Spk 	if (on) {
56441fb5989Spk 		/* Entering debugger. */
56541fb5989Spk #if NFB > 0
56641fb5989Spk 		fb_unblank();
56741fb5989Spk #endif
56841fb5989Spk 	} else {
56941fb5989Spk 		/* Resuming kernel. */
57041fb5989Spk 	}
57141fb5989Spk }
57241fb5989Spk 
57341fb5989Spk 
57441fb5989Spk /*
57541fb5989Spk  * PROM console input putchar.
57641fb5989Spk  */
577754d7940Smacallan int
prom_cngetc(dev_t dev)5780ec5f35bSuwe prom_cngetc(dev_t dev)
5790308bf1aSgwr {
58041fb5989Spk 	int s, c;
5810308bf1aSgwr 
58241fb5989Spk 	s = splhigh();
58341fb5989Spk 	c = prom_getchar();
58441fb5989Spk 	splx(s);
58541fb5989Spk 	return (c);
5860308bf1aSgwr }
5870308bf1aSgwr 
58841fb5989Spk /*
58941fb5989Spk  * PROM console output putchar.
59041fb5989Spk  */
5910308bf1aSgwr static void
prom_cnputc(dev_t dev,int c)5920ec5f35bSuwe prom_cnputc(dev_t dev, int c)
5930308bf1aSgwr {
5940308bf1aSgwr 
5958edcd039Spk 	prom_putchar(c);
5960308bf1aSgwr }
5970308bf1aSgwr 
59841fb5989Spk 
59941fb5989Spk /*****************************************************************/
60041fb5989Spk 
60168f362b7Suwe static void prom_get_device_args(const char *, char *, unsigned int);
60241fb5989Spk 
6030ec5f35bSuwe static void
prom_get_device_args(const char * prop,char * args,unsigned int sz)6040ec5f35bSuwe prom_get_device_args(const char *prop, char *args, unsigned int sz)
6050308bf1aSgwr {
60632c7c3a6Smartin 	const char *cp;
60732c7c3a6Smartin 	char buffer[128];
6080308bf1aSgwr 
60932c7c3a6Smartin 	cp = prom_getpropstringA(findroot(), prop, buffer, sizeof buffer);
61041fb5989Spk 
61141fb5989Spk 	/*
61241fb5989Spk 	 * Extract device-specific arguments from a PROM device path (if any)
61341fb5989Spk 	 */
61441fb5989Spk 	cp = buffer + strlen(buffer);
61541fb5989Spk 	while (cp >= buffer) {
61641fb5989Spk 		if (*cp == ':') {
61741fb5989Spk 			strncpy(args, cp+1, sz);
61841fb5989Spk 			break;
61941fb5989Spk 		}
62041fb5989Spk 		cp--;
62141fb5989Spk 	}
62241fb5989Spk }
62341fb5989Spk 
62441fb5989Spk /*
62541fb5989Spk  *
62641fb5989Spk  */
62741fb5989Spk void
consinit(void)6280ec5f35bSuwe consinit(void)
62941fb5989Spk {
630fa33d35fSmrg #if 0
63141fb5989Spk 	int inSource, outSink;
632fa33d35fSmrg #endif
63341fb5989Spk 
63441fb5989Spk 	switch (prom_version()) {
635fa33d35fSmrg #if 0
63641fb5989Spk 	case PROM_OLDMON:
63741fb5989Spk 	case PROM_OBP_V0:
63841fb5989Spk 		/* The stdio handles identify the device type */
63941fb5989Spk 		inSource = prom_stdin();
64041fb5989Spk 		outSink  = prom_stdout();
64141fb5989Spk 		break;
642fa33d35fSmrg 	// XXXMRG  should these just set prom_stdin_node / prom_stdout_node?
643fa33d35fSmrg #endif
64441fb5989Spk 
64541fb5989Spk 	case PROM_OBP_V2:
64641fb5989Spk 	case PROM_OBP_V3:
64741fb5989Spk 	case PROM_OPENFIRM:
64841fb5989Spk 
64941fb5989Spk 		/* Save PROM arguments for device matching */
65041fb5989Spk 		prom_get_device_args("stdin-path", prom_stdin_args,
65141fb5989Spk 				     sizeof(prom_stdin_args));
65241fb5989Spk 		prom_get_device_args("stdout-path", prom_stdout_args,
65341fb5989Spk 				    sizeof(prom_stdout_args));
65441fb5989Spk 
65541fb5989Spk 		/*
65641fb5989Spk 		 * Translate the STDIO package instance (`ihandle') -- that
65741fb5989Spk 		 * the PROM has already opened for us -- to a device tree
65841fb5989Spk 		 * node (i.e. a `phandle').
65941fb5989Spk 		 */
66041fb5989Spk 
66141fb5989Spk 		prom_stdin_node = prom_instance_to_package(prom_stdin());
66241fb5989Spk 		if (prom_stdin_node == 0)
66341fb5989Spk 			printf("consinit: cannot convert stdin ihandle\n");
66441fb5989Spk 
66541fb5989Spk 		prom_stdout_node = prom_instance_to_package(prom_stdout());
66641fb5989Spk 		if (prom_stdout_node == 0) {
66741fb5989Spk 			printf("consinit: cannot convert stdout ihandle\n");
66841fb5989Spk 			break;
66941fb5989Spk 		}
67041fb5989Spk 
67141fb5989Spk 		break;
67241fb5989Spk 
67341fb5989Spk 	default:
67441fb5989Spk 		break;
67541fb5989Spk 	}
67641fb5989Spk 
67741fb5989Spk 	/* Wire up /dev/console */
67877a6b82bSgehenna 	cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
67941fb5989Spk 	cn_tab->cn_pri = CN_INTERNAL;
68041fb5989Spk 
68141fb5989Spk 	/* Set up initial PROM input channel for /dev/console */
6823cee90e0Seeh 	cons_attach_input(&prom_cons_channel, cn_tab);
68341fb5989Spk 
68441fb5989Spk #ifdef	KGDB
68541fb5989Spk 	zs_kgdb_init();	/* XXX */
6867069ab99Smrg #endif
6870308bf1aSgwr }
688