Home | History | Annotate | Line # | Download | only in dev
kd.c revision 1.20.4.3
      1  1.20.4.3  nathanw /*	$NetBSD: kd.c,v 1.20.4.3 2002/09/17 21:17:43 nathanw Exp $	*/
      2  1.20.4.2  nathanw 
      3  1.20.4.2  nathanw /*-
      4  1.20.4.2  nathanw  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  1.20.4.2  nathanw  * All rights reserved.
      6  1.20.4.2  nathanw  *
      7  1.20.4.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.20.4.2  nathanw  * by Gordon W. Ross.
      9  1.20.4.2  nathanw  *
     10  1.20.4.2  nathanw  * Redistribution and use in source and binary forms, with or without
     11  1.20.4.2  nathanw  * modification, are permitted provided that the following conditions
     12  1.20.4.2  nathanw  * are met:
     13  1.20.4.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     14  1.20.4.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     15  1.20.4.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.20.4.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     17  1.20.4.2  nathanw  *    documentation and/or other materials provided with the distribution.
     18  1.20.4.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     19  1.20.4.2  nathanw  *    must display the following acknowledgement:
     20  1.20.4.2  nathanw  *        This product includes software developed by the NetBSD
     21  1.20.4.2  nathanw  *        Foundation, Inc. and its contributors.
     22  1.20.4.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.20.4.2  nathanw  *    contributors may be used to endorse or promote products derived
     24  1.20.4.2  nathanw  *    from this software without specific prior written permission.
     25  1.20.4.2  nathanw  *
     26  1.20.4.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.20.4.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.20.4.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.20.4.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.20.4.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.20.4.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.20.4.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.20.4.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.20.4.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.20.4.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.20.4.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     37  1.20.4.2  nathanw  */
     38  1.20.4.2  nathanw 
     39  1.20.4.2  nathanw /*
     40  1.20.4.2  nathanw  * Console driver based on PROM primitives.
     41  1.20.4.2  nathanw  *
     42  1.20.4.2  nathanw  * This driver exists to provide a tty device that the indirect
     43  1.20.4.2  nathanw  * console driver can point to. It also provides a hook that
     44  1.20.4.2  nathanw  * allows another device to serve console input. This will normally
     45  1.20.4.2  nathanw  * be a keyboard driver (see sys/dev/sun/kbd.c)
     46  1.20.4.2  nathanw  */
     47  1.20.4.2  nathanw 
     48  1.20.4.2  nathanw #include "opt_kgdb.h"
     49  1.20.4.2  nathanw #include "fb.h"
     50  1.20.4.2  nathanw 
     51  1.20.4.2  nathanw #include <sys/param.h>
     52  1.20.4.2  nathanw #include <sys/proc.h>
     53  1.20.4.2  nathanw #include <sys/systm.h>
     54  1.20.4.2  nathanw #include <sys/kernel.h>
     55  1.20.4.2  nathanw #include <sys/ioctl.h>
     56  1.20.4.2  nathanw #include <sys/tty.h>
     57  1.20.4.2  nathanw #include <sys/file.h>
     58  1.20.4.2  nathanw #include <sys/conf.h>
     59  1.20.4.2  nathanw #include <sys/device.h>
     60  1.20.4.2  nathanw 
     61  1.20.4.2  nathanw #include <machine/bsd_openprom.h>
     62  1.20.4.2  nathanw #include <machine/promlib.h>
     63  1.20.4.2  nathanw #include <machine/eeprom.h>
     64  1.20.4.2  nathanw #include <machine/psl.h>
     65  1.20.4.2  nathanw #include <machine/cpu.h>
     66  1.20.4.2  nathanw #include <machine/kbd.h>
     67  1.20.4.2  nathanw #include <machine/autoconf.h>
     68  1.20.4.2  nathanw 
     69  1.20.4.2  nathanw #if defined(RASTERCONSOLE) && NFB > 0
     70  1.20.4.2  nathanw #include <dev/sun/fbio.h>
     71  1.20.4.2  nathanw #include <dev/sun/fbvar.h>
     72  1.20.4.2  nathanw #endif
     73  1.20.4.2  nathanw 
     74  1.20.4.2  nathanw #include <dev/cons.h>
     75  1.20.4.2  nathanw #include <sparc/dev/cons.h>
     76  1.20.4.2  nathanw 
     77  1.20.4.2  nathanw #include <dev/sun/event_var.h>
     78  1.20.4.2  nathanw #include <dev/sun/kbd_xlate.h>
     79  1.20.4.2  nathanw #include <dev/sun/kbdvar.h>
     80  1.20.4.2  nathanw 
     81  1.20.4.2  nathanw #define PUT_WSIZE	64
     82  1.20.4.2  nathanw 
     83  1.20.4.2  nathanw struct kd_softc {
     84  1.20.4.2  nathanw 	struct	device kd_dev;		/* required first: base device */
     85  1.20.4.2  nathanw 	struct  tty *kd_tty;
     86  1.20.4.2  nathanw 	int rows, cols;
     87  1.20.4.2  nathanw 
     88  1.20.4.2  nathanw 	/* Console input hook */
     89  1.20.4.2  nathanw 	struct cons_channel *kd_in;
     90  1.20.4.2  nathanw };
     91  1.20.4.2  nathanw 
     92  1.20.4.2  nathanw /*
     93  1.20.4.2  nathanw  * There is no point in pretending there might be
     94  1.20.4.2  nathanw  * more than one keyboard/display device.
     95  1.20.4.2  nathanw  */
     96  1.20.4.2  nathanw static struct kd_softc kd_softc;
     97  1.20.4.2  nathanw 
     98  1.20.4.2  nathanw static int kdparam(struct tty *, struct termios *);
     99  1.20.4.2  nathanw static void kdstart(struct tty *);
    100  1.20.4.2  nathanw static void kd_init __P((struct kd_softc *));
    101  1.20.4.2  nathanw static void kd_cons_input __P((int));
    102  1.20.4.2  nathanw 
    103  1.20.4.3  nathanw dev_type_open(kdopen);
    104  1.20.4.3  nathanw dev_type_close(kdclose);
    105  1.20.4.3  nathanw dev_type_read(kdread);
    106  1.20.4.3  nathanw dev_type_write(kdwrite);
    107  1.20.4.3  nathanw dev_type_ioctl(kdioctl);
    108  1.20.4.3  nathanw dev_type_tty(kdtty);
    109  1.20.4.3  nathanw dev_type_poll(kdpoll);
    110  1.20.4.3  nathanw 
    111  1.20.4.3  nathanw const struct cdevsw kd_cdevsw = {
    112  1.20.4.3  nathanw 	kdopen, kdclose, kdread, kdwrite, kdioctl,
    113  1.20.4.3  nathanw 	nostop, kdtty, kdpoll, nommap, D_TTY
    114  1.20.4.3  nathanw };
    115  1.20.4.3  nathanw 
    116  1.20.4.2  nathanw /*
    117  1.20.4.2  nathanw  * Prepare the console tty; called on first open of /dev/console
    118  1.20.4.2  nathanw  */
    119  1.20.4.2  nathanw void
    120  1.20.4.2  nathanw kd_init(kd)
    121  1.20.4.2  nathanw 	struct kd_softc *kd;
    122  1.20.4.2  nathanw {
    123  1.20.4.2  nathanw 	struct tty *tp;
    124  1.20.4.2  nathanw 
    125  1.20.4.2  nathanw 	tp = ttymalloc();
    126  1.20.4.2  nathanw 	tp->t_oproc = kdstart;
    127  1.20.4.2  nathanw 	tp->t_param = kdparam;
    128  1.20.4.3  nathanw 	tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
    129  1.20.4.2  nathanw 
    130  1.20.4.2  nathanw 	tty_attach(tp);
    131  1.20.4.2  nathanw 	kd->kd_tty = tp;
    132  1.20.4.2  nathanw 
    133  1.20.4.2  nathanw 	/*
    134  1.20.4.2  nathanw 	 * Get the console struct winsize.
    135  1.20.4.2  nathanw 	 */
    136  1.20.4.2  nathanw #if defined(RASTERCONSOLE) && NFB > 0
    137  1.20.4.2  nathanw 	/* If the raster console driver is attached, copy its size */
    138  1.20.4.2  nathanw 	kd->rows = fbrcons_rows();
    139  1.20.4.2  nathanw 	kd->cols = fbrcons_cols();
    140  1.20.4.2  nathanw 	rcons_ttyinit(tp);
    141  1.20.4.2  nathanw #endif
    142  1.20.4.2  nathanw 
    143  1.20.4.2  nathanw 	/* else, consult the PROM */
    144  1.20.4.2  nathanw 	switch (prom_version()) {
    145  1.20.4.2  nathanw 	char *prop;
    146  1.20.4.2  nathanw 	struct eeprom *ep;
    147  1.20.4.2  nathanw 	case PROM_OLDMON:
    148  1.20.4.2  nathanw 		if ((ep = (struct eeprom *)eeprom_va) == NULL)
    149  1.20.4.2  nathanw 			break;
    150  1.20.4.2  nathanw 		if (kd->rows == 0)
    151  1.20.4.2  nathanw 			kd->rows = (u_short)ep->eeTtyRows;
    152  1.20.4.2  nathanw 		if (kd->cols == 0)
    153  1.20.4.2  nathanw 			kd->cols = (u_short)ep->eeTtyCols;
    154  1.20.4.2  nathanw 		break;
    155  1.20.4.2  nathanw 	case PROM_OBP_V0:
    156  1.20.4.2  nathanw 	case PROM_OBP_V2:
    157  1.20.4.2  nathanw 	case PROM_OBP_V3:
    158  1.20.4.2  nathanw 	case PROM_OPENFIRM:
    159  1.20.4.2  nathanw 
    160  1.20.4.2  nathanw 		if (kd->rows == 0 &&
    161  1.20.4.2  nathanw 		    (prop = PROM_getpropstring(optionsnode, "screen-#rows"))) {
    162  1.20.4.2  nathanw 			int i = 0;
    163  1.20.4.2  nathanw 
    164  1.20.4.2  nathanw 			while (*prop != '\0')
    165  1.20.4.2  nathanw 				i = i * 10 + *prop++ - '0';
    166  1.20.4.2  nathanw 			kd->rows = (unsigned short)i;
    167  1.20.4.2  nathanw 		}
    168  1.20.4.2  nathanw 		if (kd->cols == 0 &&
    169  1.20.4.2  nathanw 		    (prop = PROM_getpropstring(optionsnode, "screen-#columns"))) {
    170  1.20.4.2  nathanw 			int i = 0;
    171  1.20.4.2  nathanw 
    172  1.20.4.2  nathanw 			while (*prop != '\0')
    173  1.20.4.2  nathanw 				i = i * 10 + *prop++ - '0';
    174  1.20.4.2  nathanw 			kd->cols = (unsigned short)i;
    175  1.20.4.2  nathanw 		}
    176  1.20.4.2  nathanw 		break;
    177  1.20.4.2  nathanw 	}
    178  1.20.4.2  nathanw 
    179  1.20.4.2  nathanw 	return;
    180  1.20.4.2  nathanw }
    181  1.20.4.2  nathanw 
    182  1.20.4.2  nathanw struct tty *
    183  1.20.4.2  nathanw kdtty(dev)
    184  1.20.4.2  nathanw 	dev_t dev;
    185  1.20.4.2  nathanw {
    186  1.20.4.2  nathanw 	struct kd_softc *kd;
    187  1.20.4.2  nathanw 
    188  1.20.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    189  1.20.4.2  nathanw 	return (kd->kd_tty);
    190  1.20.4.2  nathanw }
    191  1.20.4.2  nathanw 
    192  1.20.4.2  nathanw int
    193  1.20.4.2  nathanw kdopen(dev, flag, mode, p)
    194  1.20.4.2  nathanw 	dev_t dev;
    195  1.20.4.2  nathanw 	int flag, mode;
    196  1.20.4.2  nathanw 	struct proc *p;
    197  1.20.4.2  nathanw {
    198  1.20.4.2  nathanw 	struct kd_softc *kd;
    199  1.20.4.2  nathanw 	int error, s, unit;
    200  1.20.4.2  nathanw 	struct tty *tp;
    201  1.20.4.2  nathanw static	int firstopen = 1;
    202  1.20.4.2  nathanw 
    203  1.20.4.2  nathanw 	unit = minor(dev);
    204  1.20.4.2  nathanw 	if (unit != 0)
    205  1.20.4.2  nathanw 		return ENXIO;
    206  1.20.4.2  nathanw 
    207  1.20.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    208  1.20.4.2  nathanw 	if (firstopen) {
    209  1.20.4.2  nathanw 		kd_init(kd);
    210  1.20.4.2  nathanw 		firstopen = 0;
    211  1.20.4.2  nathanw 	}
    212  1.20.4.2  nathanw 
    213  1.20.4.2  nathanw 	tp = kd->kd_tty;
    214  1.20.4.2  nathanw 
    215  1.20.4.2  nathanw 	/* It's simpler to do this up here. */
    216  1.20.4.2  nathanw 	if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
    217  1.20.4.2  nathanw 	     ==             (TS_ISOPEN | TS_XCLUDE))
    218  1.20.4.2  nathanw 	    && (p->p_ucred->cr_uid != 0) )
    219  1.20.4.2  nathanw 	{
    220  1.20.4.2  nathanw 		return (EBUSY);
    221  1.20.4.2  nathanw 	}
    222  1.20.4.2  nathanw 
    223  1.20.4.2  nathanw 	s = spltty();
    224  1.20.4.2  nathanw 	if ((tp->t_state & TS_ISOPEN) == 0) {
    225  1.20.4.2  nathanw 		/* First open. */
    226  1.20.4.2  nathanw 
    227  1.20.4.2  nathanw 		/* Notify the input device that serves us */
    228  1.20.4.2  nathanw 		struct cons_channel *cc = kd->kd_in;
    229  1.20.4.2  nathanw 		if (cc != NULL &&
    230  1.20.4.2  nathanw 		    (error = (*cc->cc_iopen)(cc)) != 0) {
    231  1.20.4.2  nathanw 			return (error);
    232  1.20.4.2  nathanw 		}
    233  1.20.4.2  nathanw 
    234  1.20.4.2  nathanw 		ttychars(tp);
    235  1.20.4.2  nathanw 		tp->t_iflag = TTYDEF_IFLAG;
    236  1.20.4.2  nathanw 		tp->t_oflag = TTYDEF_OFLAG;
    237  1.20.4.2  nathanw 		tp->t_cflag = TTYDEF_CFLAG;
    238  1.20.4.2  nathanw 		tp->t_lflag = TTYDEF_LFLAG;
    239  1.20.4.2  nathanw 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    240  1.20.4.2  nathanw 		(void) kdparam(tp, &tp->t_termios);
    241  1.20.4.2  nathanw 		ttsetwater(tp);
    242  1.20.4.2  nathanw 		tp->t_winsize.ws_row = kd->rows;
    243  1.20.4.2  nathanw 		tp->t_winsize.ws_col = kd->cols;
    244  1.20.4.2  nathanw 		/* Flush pending input?  Clear translator? */
    245  1.20.4.2  nathanw 		/* This (pseudo)device always has SOFTCAR */
    246  1.20.4.2  nathanw 		tp->t_state |= TS_CARR_ON;
    247  1.20.4.2  nathanw 	}
    248  1.20.4.2  nathanw 
    249  1.20.4.2  nathanw 	splx(s);
    250  1.20.4.2  nathanw 
    251  1.20.4.2  nathanw 	return ((*tp->t_linesw->l_open)(dev, tp));
    252  1.20.4.2  nathanw }
    253  1.20.4.2  nathanw 
    254  1.20.4.2  nathanw int
    255  1.20.4.2  nathanw kdclose(dev, flag, mode, p)
    256  1.20.4.2  nathanw 	dev_t dev;
    257  1.20.4.2  nathanw 	int flag, mode;
    258  1.20.4.2  nathanw 	struct proc *p;
    259  1.20.4.2  nathanw {
    260  1.20.4.2  nathanw 	struct kd_softc *kd;
    261  1.20.4.2  nathanw 	struct tty *tp;
    262  1.20.4.2  nathanw 	struct cons_channel *cc;
    263  1.20.4.2  nathanw 
    264  1.20.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    265  1.20.4.2  nathanw 	tp = kd->kd_tty;
    266  1.20.4.2  nathanw 
    267  1.20.4.2  nathanw 	/* XXX This is for cons.c. */
    268  1.20.4.2  nathanw 	if ((tp->t_state & TS_ISOPEN) == 0)
    269  1.20.4.2  nathanw 		return 0;
    270  1.20.4.2  nathanw 
    271  1.20.4.2  nathanw 	(*tp->t_linesw->l_close)(tp, flag);
    272  1.20.4.2  nathanw 	ttyclose(tp);
    273  1.20.4.2  nathanw 
    274  1.20.4.2  nathanw 	if ((cc = kd->kd_in) != NULL)
    275  1.20.4.2  nathanw 		(void)(*cc->cc_iclose)(cc);
    276  1.20.4.2  nathanw 
    277  1.20.4.2  nathanw 	return (0);
    278  1.20.4.2  nathanw }
    279  1.20.4.2  nathanw 
    280  1.20.4.2  nathanw int
    281  1.20.4.2  nathanw kdread(dev, uio, flag)
    282  1.20.4.2  nathanw 	dev_t dev;
    283  1.20.4.2  nathanw 	struct uio *uio;
    284  1.20.4.2  nathanw 	int flag;
    285  1.20.4.2  nathanw {
    286  1.20.4.2  nathanw 	struct kd_softc *kd;
    287  1.20.4.2  nathanw 	struct tty *tp;
    288  1.20.4.2  nathanw 
    289  1.20.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    290  1.20.4.2  nathanw 	tp = kd->kd_tty;
    291  1.20.4.2  nathanw 
    292  1.20.4.2  nathanw 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    293  1.20.4.2  nathanw }
    294  1.20.4.2  nathanw 
    295  1.20.4.2  nathanw int
    296  1.20.4.2  nathanw kdwrite(dev, uio, flag)
    297  1.20.4.2  nathanw 	dev_t dev;
    298  1.20.4.2  nathanw 	struct uio *uio;
    299  1.20.4.2  nathanw 	int flag;
    300  1.20.4.2  nathanw {
    301  1.20.4.2  nathanw 	struct kd_softc *kd;
    302  1.20.4.2  nathanw 	struct tty *tp;
    303  1.20.4.2  nathanw 
    304  1.20.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    305  1.20.4.2  nathanw 	tp = kd->kd_tty;
    306  1.20.4.2  nathanw 
    307  1.20.4.2  nathanw 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    308  1.20.4.2  nathanw }
    309  1.20.4.2  nathanw 
    310  1.20.4.2  nathanw int
    311  1.20.4.2  nathanw kdpoll(dev, events, p)
    312  1.20.4.2  nathanw 	dev_t dev;
    313  1.20.4.2  nathanw 	int events;
    314  1.20.4.2  nathanw 	struct proc *p;
    315  1.20.4.2  nathanw {
    316  1.20.4.2  nathanw 	struct kd_softc *kd;
    317  1.20.4.2  nathanw 	struct tty *tp;
    318  1.20.4.2  nathanw 
    319  1.20.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    320  1.20.4.2  nathanw 	tp = kd->kd_tty;
    321  1.20.4.2  nathanw 
    322  1.20.4.2  nathanw 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    323  1.20.4.2  nathanw }
    324  1.20.4.2  nathanw 
    325  1.20.4.2  nathanw int
    326  1.20.4.2  nathanw kdioctl(dev, cmd, data, flag, p)
    327  1.20.4.2  nathanw 	dev_t dev;
    328  1.20.4.2  nathanw 	u_long cmd;
    329  1.20.4.2  nathanw 	caddr_t data;
    330  1.20.4.2  nathanw 	int flag;
    331  1.20.4.2  nathanw 	struct proc *p;
    332  1.20.4.2  nathanw {
    333  1.20.4.2  nathanw 	struct kd_softc *kd;
    334  1.20.4.2  nathanw 	struct tty *tp;
    335  1.20.4.2  nathanw 	int error;
    336  1.20.4.2  nathanw 
    337  1.20.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    338  1.20.4.2  nathanw 	tp = kd->kd_tty;
    339  1.20.4.2  nathanw 
    340  1.20.4.2  nathanw 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    341  1.20.4.2  nathanw 	if (error != EPASSTHROUGH)
    342  1.20.4.2  nathanw 		return error;
    343  1.20.4.2  nathanw 
    344  1.20.4.2  nathanw 	error = ttioctl(tp, cmd, data, flag, p);
    345  1.20.4.2  nathanw 	if (error != EPASSTHROUGH)
    346  1.20.4.2  nathanw 		return error;
    347  1.20.4.2  nathanw 
    348  1.20.4.2  nathanw 	/* Handle any ioctl commands specific to kbd/display. */
    349  1.20.4.2  nathanw 	/* XXX - Send KB* ioctls to kbd module? */
    350  1.20.4.2  nathanw 	/* XXX - Send FB* ioctls to fb module?  */
    351  1.20.4.2  nathanw 
    352  1.20.4.2  nathanw 	return EPASSTHROUGH;
    353  1.20.4.2  nathanw }
    354  1.20.4.2  nathanw 
    355  1.20.4.2  nathanw static int
    356  1.20.4.2  nathanw kdparam(tp, t)
    357  1.20.4.2  nathanw 	struct tty *tp;
    358  1.20.4.2  nathanw 	struct termios *t;
    359  1.20.4.2  nathanw {
    360  1.20.4.2  nathanw 	/* XXX - These are ignored... */
    361  1.20.4.2  nathanw 	tp->t_ispeed = t->c_ispeed;
    362  1.20.4.2  nathanw 	tp->t_ospeed = t->c_ospeed;
    363  1.20.4.2  nathanw 	tp->t_cflag = t->c_cflag;
    364  1.20.4.2  nathanw 	return 0;
    365  1.20.4.2  nathanw }
    366  1.20.4.2  nathanw 
    367  1.20.4.2  nathanw 
    368  1.20.4.2  nathanw static void kd_later(void*);
    369  1.20.4.2  nathanw static void kd_putfb(struct tty *);
    370  1.20.4.2  nathanw 
    371  1.20.4.2  nathanw static void
    372  1.20.4.2  nathanw kdstart(tp)
    373  1.20.4.2  nathanw 	struct tty *tp;
    374  1.20.4.2  nathanw {
    375  1.20.4.2  nathanw 	struct clist *cl;
    376  1.20.4.2  nathanw 	int s;
    377  1.20.4.2  nathanw 
    378  1.20.4.2  nathanw 	s = spltty();
    379  1.20.4.2  nathanw 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
    380  1.20.4.2  nathanw 		goto out;
    381  1.20.4.2  nathanw 
    382  1.20.4.2  nathanw 	cl = &tp->t_outq;
    383  1.20.4.2  nathanw 	if (cl->c_cc) {
    384  1.20.4.2  nathanw 		tp->t_state |= TS_BUSY;
    385  1.20.4.2  nathanw 		if ((s & PSR_PIL) == 0) {
    386  1.20.4.2  nathanw 			/* called at level zero - update screen now. */
    387  1.20.4.2  nathanw 			(void) spllowersoftclock();
    388  1.20.4.2  nathanw 			kd_putfb(tp);
    389  1.20.4.2  nathanw 			(void) spltty();
    390  1.20.4.2  nathanw 			tp->t_state &= ~TS_BUSY;
    391  1.20.4.2  nathanw 		} else {
    392  1.20.4.2  nathanw 			/* called at interrupt level - do it later */
    393  1.20.4.2  nathanw 			callout_reset(&tp->t_rstrt_ch, 0, kd_later, tp);
    394  1.20.4.2  nathanw 		}
    395  1.20.4.2  nathanw 	}
    396  1.20.4.2  nathanw 	if (cl->c_cc <= tp->t_lowat) {
    397  1.20.4.2  nathanw 		if (tp->t_state & TS_ASLEEP) {
    398  1.20.4.2  nathanw 			tp->t_state &= ~TS_ASLEEP;
    399  1.20.4.2  nathanw 			wakeup((caddr_t)cl);
    400  1.20.4.2  nathanw 		}
    401  1.20.4.2  nathanw 		selwakeup(&tp->t_wsel);
    402  1.20.4.2  nathanw 	}
    403  1.20.4.2  nathanw out:
    404  1.20.4.2  nathanw 	splx(s);
    405  1.20.4.2  nathanw }
    406  1.20.4.2  nathanw 
    407  1.20.4.2  nathanw /*
    408  1.20.4.2  nathanw  * Timeout function to do delayed writes to the screen.
    409  1.20.4.2  nathanw  * Called at splsoftclock when requested by kdstart.
    410  1.20.4.2  nathanw  */
    411  1.20.4.2  nathanw static void
    412  1.20.4.2  nathanw kd_later(arg)
    413  1.20.4.2  nathanw 	void *arg;
    414  1.20.4.2  nathanw {
    415  1.20.4.2  nathanw 	struct tty *tp = arg;
    416  1.20.4.2  nathanw 	int s;
    417  1.20.4.2  nathanw 
    418  1.20.4.2  nathanw 	kd_putfb(tp);
    419  1.20.4.2  nathanw 
    420  1.20.4.2  nathanw 	s = spltty();
    421  1.20.4.2  nathanw 	tp->t_state &= ~TS_BUSY;
    422  1.20.4.2  nathanw 	(*tp->t_linesw->l_start)(tp);
    423  1.20.4.2  nathanw 	splx(s);
    424  1.20.4.2  nathanw }
    425  1.20.4.2  nathanw 
    426  1.20.4.2  nathanw /*
    427  1.20.4.2  nathanw  * Put text on the screen using the PROM monitor.
    428  1.20.4.2  nathanw  * This can take a while, so to avoid missing
    429  1.20.4.2  nathanw  * interrupts, this is called at splsoftclock.
    430  1.20.4.2  nathanw  */
    431  1.20.4.2  nathanw static void
    432  1.20.4.2  nathanw kd_putfb(tp)
    433  1.20.4.2  nathanw 	struct tty *tp;
    434  1.20.4.2  nathanw {
    435  1.20.4.2  nathanw 	char buf[PUT_WSIZE];
    436  1.20.4.2  nathanw 	struct clist *cl = &tp->t_outq;
    437  1.20.4.2  nathanw 	char *p, *end;
    438  1.20.4.2  nathanw 	int len;
    439  1.20.4.2  nathanw 
    440  1.20.4.2  nathanw 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
    441  1.20.4.2  nathanw 		/* PROM will barf if high bits are set. */
    442  1.20.4.2  nathanw 		p = buf;
    443  1.20.4.2  nathanw 		end = buf + len;
    444  1.20.4.2  nathanw 		while (p < end)
    445  1.20.4.2  nathanw 			*p++ &= 0x7f;
    446  1.20.4.2  nathanw 
    447  1.20.4.2  nathanw 		/* Now let the PROM print it. */
    448  1.20.4.2  nathanw 		prom_putstr(buf, len);
    449  1.20.4.2  nathanw 	}
    450  1.20.4.2  nathanw }
    451  1.20.4.2  nathanw 
    452  1.20.4.2  nathanw /*
    453  1.20.4.2  nathanw  * Our "interrupt" routine for input. This is called by
    454  1.20.4.2  nathanw  * the keyboard driver (dev/sun/kbd.c) at spltty.
    455  1.20.4.2  nathanw  */
    456  1.20.4.2  nathanw void
    457  1.20.4.2  nathanw kd_cons_input(c)
    458  1.20.4.2  nathanw 	int c;
    459  1.20.4.2  nathanw {
    460  1.20.4.2  nathanw 	struct kd_softc *kd = &kd_softc;
    461  1.20.4.2  nathanw 	struct tty *tp;
    462  1.20.4.2  nathanw 
    463  1.20.4.2  nathanw 	/* XXX: Make sure the device is open. */
    464  1.20.4.2  nathanw 	tp = kd->kd_tty;
    465  1.20.4.2  nathanw 	if (tp == NULL)
    466  1.20.4.2  nathanw 		return;
    467  1.20.4.2  nathanw 	if ((tp->t_state & TS_ISOPEN) == 0)
    468  1.20.4.2  nathanw 		return;
    469  1.20.4.2  nathanw 
    470  1.20.4.2  nathanw 	(*tp->t_linesw->l_rint)(c, tp);
    471  1.20.4.2  nathanw }
    472  1.20.4.2  nathanw 
    473  1.20.4.2  nathanw void
    474  1.20.4.2  nathanw cons_attach_input(cc, cn)
    475  1.20.4.2  nathanw 	struct cons_channel *cc;
    476  1.20.4.2  nathanw 	struct consdev *cn;
    477  1.20.4.2  nathanw {
    478  1.20.4.2  nathanw 	struct kd_softc *kd = &kd_softc;
    479  1.20.4.2  nathanw 
    480  1.20.4.2  nathanw 	kd->kd_in = cc;
    481  1.20.4.2  nathanw 	cc->cc_upstream = kd_cons_input;
    482  1.20.4.2  nathanw }
    483  1.20.4.2  nathanw 
    484  1.20.4.2  nathanw void kd_attach_input(struct cons_channel *);
    485  1.20.4.2  nathanw void
    486  1.20.4.2  nathanw kd_attach_input(cc)
    487  1.20.4.2  nathanw 	struct cons_channel *cc;
    488  1.20.4.2  nathanw {
    489  1.20.4.2  nathanw 	struct kd_softc *kd = &kd_softc;
    490  1.20.4.2  nathanw 
    491  1.20.4.2  nathanw 	kd->kd_in = cc;
    492  1.20.4.2  nathanw 	cc->cc_upstream = kd_cons_input;
    493  1.20.4.2  nathanw }
    494  1.20.4.2  nathanw 
    495  1.20.4.2  nathanw /*
    496  1.20.4.2  nathanw  * Default PROM-based console input stream
    497  1.20.4.2  nathanw  * Since the PROM does not notify us when data is available on the
    498  1.20.4.2  nathanw  * input channel these functions periodically poll the PROM.
    499  1.20.4.2  nathanw  */
    500  1.20.4.2  nathanw static int kd_rom_iopen __P((struct cons_channel *));
    501  1.20.4.2  nathanw static int kd_rom_iclose __P((struct cons_channel *));
    502  1.20.4.2  nathanw static void kd_rom_intr __P((void *));
    503  1.20.4.2  nathanw 
    504  1.20.4.2  nathanw static struct cons_channel prom_cons_channel;
    505  1.20.4.2  nathanw 
    506  1.20.4.2  nathanw int
    507  1.20.4.2  nathanw kd_rom_iopen(cc)
    508  1.20.4.2  nathanw 	struct cons_channel *cc;
    509  1.20.4.2  nathanw {
    510  1.20.4.2  nathanw 	/* Poll for ROM input 4 times per second */
    511  1.20.4.2  nathanw 	callout_reset(&cc->cc_callout, hz / 4, kd_rom_intr, cc);
    512  1.20.4.2  nathanw 	return (0);
    513  1.20.4.2  nathanw }
    514  1.20.4.2  nathanw 
    515  1.20.4.2  nathanw int
    516  1.20.4.2  nathanw kd_rom_iclose(cc)
    517  1.20.4.2  nathanw 	struct cons_channel *cc;
    518  1.20.4.2  nathanw {
    519  1.20.4.2  nathanw 
    520  1.20.4.2  nathanw 	callout_stop(&cc->cc_callout);
    521  1.20.4.2  nathanw 	return (0);
    522  1.20.4.2  nathanw }
    523  1.20.4.2  nathanw 
    524  1.20.4.2  nathanw /*
    525  1.20.4.2  nathanw  * "Interrupt" routine for input through ROM vectors
    526  1.20.4.2  nathanw  */
    527  1.20.4.2  nathanw void
    528  1.20.4.2  nathanw kd_rom_intr(arg)
    529  1.20.4.2  nathanw 	void *arg;
    530  1.20.4.2  nathanw {
    531  1.20.4.2  nathanw 	struct cons_channel *cc = arg;
    532  1.20.4.2  nathanw 	int s, c;
    533  1.20.4.2  nathanw 
    534  1.20.4.2  nathanw 	/* Re-schedule */
    535  1.20.4.2  nathanw 	callout_reset(&cc->cc_callout, hz / 4, kd_rom_intr, cc);
    536  1.20.4.2  nathanw 
    537  1.20.4.2  nathanw 	s = spltty();
    538  1.20.4.2  nathanw 
    539  1.20.4.2  nathanw 	while ((c = prom_peekchar()) >= 0)
    540  1.20.4.2  nathanw 		(*cc->cc_upstream)(c);
    541  1.20.4.2  nathanw 
    542  1.20.4.2  nathanw 	splx(s);
    543  1.20.4.2  nathanw }
    544  1.20.4.2  nathanw 
    545  1.20.4.2  nathanw /*****************************************************************/
    546  1.20.4.2  nathanw 
    547  1.20.4.2  nathanw int prom_stdin_node;
    548  1.20.4.2  nathanw int prom_stdout_node;
    549  1.20.4.2  nathanw char prom_stdin_args[16];
    550  1.20.4.2  nathanw char prom_stdout_args[16];
    551  1.20.4.2  nathanw 
    552  1.20.4.2  nathanw extern void prom_cnprobe __P((struct consdev *));
    553  1.20.4.2  nathanw static void prom_cninit __P((struct consdev *));
    554  1.20.4.2  nathanw static int  prom_cngetc __P((dev_t));
    555  1.20.4.2  nathanw static void prom_cnputc __P((dev_t, int));
    556  1.20.4.2  nathanw extern void prom_cnpollc __P((dev_t, int));
    557  1.20.4.2  nathanw 
    558  1.20.4.2  nathanw /*
    559  1.20.4.2  nathanw  * The console is set to this one initially,
    560  1.20.4.2  nathanw  * which lets us use the PROM until consinit()
    561  1.20.4.2  nathanw  * is called to select a real console.
    562  1.20.4.2  nathanw  */
    563  1.20.4.2  nathanw struct consdev consdev_prom = {
    564  1.20.4.2  nathanw 	prom_cnprobe,
    565  1.20.4.2  nathanw 	prom_cninit,
    566  1.20.4.2  nathanw 	prom_cngetc,
    567  1.20.4.2  nathanw 	prom_cnputc,
    568  1.20.4.2  nathanw 	prom_cnpollc,
    569  1.20.4.2  nathanw 	NULL,
    570  1.20.4.2  nathanw };
    571  1.20.4.2  nathanw 
    572  1.20.4.2  nathanw /*
    573  1.20.4.2  nathanw  * The console table pointer is statically initialized
    574  1.20.4.2  nathanw  * to point to the PROM table, so that early calls to printf will work.
    575  1.20.4.2  nathanw  */
    576  1.20.4.2  nathanw struct consdev *cn_tab = &consdev_prom;
    577  1.20.4.2  nathanw 
    578  1.20.4.2  nathanw void
    579  1.20.4.2  nathanw prom_cnprobe(cn)
    580  1.20.4.2  nathanw 	struct consdev *cn;
    581  1.20.4.2  nathanw {
    582  1.20.4.2  nathanw }
    583  1.20.4.2  nathanw 
    584  1.20.4.2  nathanw static void
    585  1.20.4.2  nathanw prom_cninit(cn)
    586  1.20.4.2  nathanw 	struct consdev *cn;
    587  1.20.4.2  nathanw {
    588  1.20.4.2  nathanw }
    589  1.20.4.2  nathanw 
    590  1.20.4.2  nathanw void
    591  1.20.4.2  nathanw prom_cnpollc(dev, on)
    592  1.20.4.2  nathanw 	dev_t dev;
    593  1.20.4.2  nathanw 	int on;
    594  1.20.4.2  nathanw {
    595  1.20.4.2  nathanw 
    596  1.20.4.2  nathanw 	if (on) {
    597  1.20.4.2  nathanw 		/* Entering debugger. */
    598  1.20.4.2  nathanw #if NFB > 0
    599  1.20.4.2  nathanw 		fb_unblank();
    600  1.20.4.2  nathanw #endif
    601  1.20.4.2  nathanw 	} else {
    602  1.20.4.2  nathanw 		/* Resuming kernel. */
    603  1.20.4.2  nathanw 	}
    604  1.20.4.2  nathanw }
    605  1.20.4.2  nathanw 
    606  1.20.4.2  nathanw 
    607  1.20.4.2  nathanw /*
    608  1.20.4.2  nathanw  * PROM console input putchar.
    609  1.20.4.2  nathanw  */
    610  1.20.4.2  nathanw static int
    611  1.20.4.2  nathanw prom_cngetc(dev)
    612  1.20.4.2  nathanw 	dev_t dev;
    613  1.20.4.2  nathanw {
    614  1.20.4.2  nathanw 	int s, c;
    615  1.20.4.2  nathanw 
    616  1.20.4.2  nathanw 	s = splhigh();
    617  1.20.4.2  nathanw 	c = prom_getchar();
    618  1.20.4.2  nathanw 	splx(s);
    619  1.20.4.2  nathanw 	return (c);
    620  1.20.4.2  nathanw }
    621  1.20.4.2  nathanw 
    622  1.20.4.2  nathanw /*
    623  1.20.4.2  nathanw  * PROM console output putchar.
    624  1.20.4.2  nathanw  */
    625  1.20.4.2  nathanw static void
    626  1.20.4.2  nathanw prom_cnputc(dev, c)
    627  1.20.4.2  nathanw 	dev_t dev;
    628  1.20.4.2  nathanw 	int c;
    629  1.20.4.2  nathanw {
    630  1.20.4.2  nathanw 
    631  1.20.4.2  nathanw 	prom_putchar(c);
    632  1.20.4.2  nathanw }
    633  1.20.4.2  nathanw 
    634  1.20.4.2  nathanw 
    635  1.20.4.2  nathanw /*****************************************************************/
    636  1.20.4.2  nathanw 
    637  1.20.4.2  nathanw static void prom_get_device_args __P((const char *, char *, unsigned int));
    638  1.20.4.2  nathanw 
    639  1.20.4.2  nathanw void
    640  1.20.4.2  nathanw prom_get_device_args(prop, args, sz)
    641  1.20.4.2  nathanw 	const char *prop;
    642  1.20.4.2  nathanw 	char *args;
    643  1.20.4.2  nathanw 	unsigned int sz;
    644  1.20.4.2  nathanw {
    645  1.20.4.2  nathanw 	char *cp, buffer[128];
    646  1.20.4.2  nathanw 
    647  1.20.4.2  nathanw 	cp = PROM_getpropstringA(findroot(), (char *)prop, buffer, sizeof buffer);
    648  1.20.4.2  nathanw 
    649  1.20.4.2  nathanw 	/*
    650  1.20.4.2  nathanw 	 * Extract device-specific arguments from a PROM device path (if any)
    651  1.20.4.2  nathanw 	 */
    652  1.20.4.2  nathanw 	cp = buffer + strlen(buffer);
    653  1.20.4.2  nathanw 	while (cp >= buffer) {
    654  1.20.4.2  nathanw 		if (*cp == ':') {
    655  1.20.4.2  nathanw 			strncpy(args, cp+1, sz);
    656  1.20.4.2  nathanw 			break;
    657  1.20.4.2  nathanw 		}
    658  1.20.4.2  nathanw 		cp--;
    659  1.20.4.2  nathanw 	}
    660  1.20.4.2  nathanw }
    661  1.20.4.2  nathanw 
    662  1.20.4.2  nathanw /*
    663  1.20.4.2  nathanw  *
    664  1.20.4.2  nathanw  */
    665  1.20.4.2  nathanw void
    666  1.20.4.2  nathanw consinit()
    667  1.20.4.2  nathanw {
    668  1.20.4.2  nathanw 	int inSource, outSink;
    669  1.20.4.2  nathanw 
    670  1.20.4.2  nathanw 	switch (prom_version()) {
    671  1.20.4.2  nathanw 	case PROM_OLDMON:
    672  1.20.4.2  nathanw 	case PROM_OBP_V0:
    673  1.20.4.2  nathanw 		/* The stdio handles identify the device type */
    674  1.20.4.2  nathanw 		inSource = prom_stdin();
    675  1.20.4.2  nathanw 		outSink  = prom_stdout();
    676  1.20.4.2  nathanw 		break;
    677  1.20.4.2  nathanw 
    678  1.20.4.2  nathanw 	case PROM_OBP_V2:
    679  1.20.4.2  nathanw 	case PROM_OBP_V3:
    680  1.20.4.2  nathanw 	case PROM_OPENFIRM:
    681  1.20.4.2  nathanw 
    682  1.20.4.2  nathanw 		/* Save PROM arguments for device matching */
    683  1.20.4.2  nathanw 		prom_get_device_args("stdin-path", prom_stdin_args,
    684  1.20.4.2  nathanw 				     sizeof(prom_stdin_args));
    685  1.20.4.2  nathanw 		prom_get_device_args("stdout-path", prom_stdout_args,
    686  1.20.4.2  nathanw 				    sizeof(prom_stdout_args));
    687  1.20.4.2  nathanw 
    688  1.20.4.2  nathanw 		/*
    689  1.20.4.2  nathanw 		 * Translate the STDIO package instance (`ihandle') -- that
    690  1.20.4.2  nathanw 		 * the PROM has already opened for us -- to a device tree
    691  1.20.4.2  nathanw 		 * node (i.e. a `phandle').
    692  1.20.4.2  nathanw 		 */
    693  1.20.4.2  nathanw 
    694  1.20.4.2  nathanw 		prom_stdin_node = prom_instance_to_package(prom_stdin());
    695  1.20.4.2  nathanw 		if (prom_stdin_node == 0)
    696  1.20.4.2  nathanw 			printf("consinit: cannot convert stdin ihandle\n");
    697  1.20.4.2  nathanw 
    698  1.20.4.2  nathanw 		prom_stdout_node = prom_instance_to_package(prom_stdout());
    699  1.20.4.2  nathanw 		if (prom_stdout_node == 0) {
    700  1.20.4.2  nathanw 			printf("consinit: cannot convert stdout ihandle\n");
    701  1.20.4.2  nathanw 			break;
    702  1.20.4.2  nathanw 		}
    703  1.20.4.2  nathanw 
    704  1.20.4.2  nathanw 		break;
    705  1.20.4.2  nathanw 
    706  1.20.4.2  nathanw 	default:
    707  1.20.4.2  nathanw 		break;
    708  1.20.4.2  nathanw 	}
    709  1.20.4.2  nathanw 
    710  1.20.4.2  nathanw 	/* Wire up /dev/console */
    711  1.20.4.3  nathanw 	cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
    712  1.20.4.2  nathanw 	cn_tab->cn_pri = CN_INTERNAL;
    713  1.20.4.2  nathanw 
    714  1.20.4.2  nathanw 	/* Set up initial PROM input channel for /dev/console */
    715  1.20.4.2  nathanw 	prom_cons_channel.cc_dev = NULL;
    716  1.20.4.2  nathanw 	prom_cons_channel.cc_iopen = kd_rom_iopen;
    717  1.20.4.2  nathanw 	prom_cons_channel.cc_iclose = kd_rom_iclose;
    718  1.20.4.2  nathanw 	cons_attach_input(&prom_cons_channel, cn_tab);
    719  1.20.4.2  nathanw 
    720  1.20.4.2  nathanw #ifdef	KGDB
    721  1.20.4.2  nathanw 	zs_kgdb_init();	/* XXX */
    722  1.20.4.2  nathanw #endif
    723  1.20.4.2  nathanw }
    724