Home | History | Annotate | Line # | Download | only in dev
kd.c revision 1.18.4.2
      1  1.18.4.2  nathanw /*	$NetBSD: kd.c,v 1.18.4.2 2002/04/01 07:43:03 nathanw Exp $	*/
      2  1.18.4.2  nathanw 
      3  1.18.4.2  nathanw /*-
      4  1.18.4.2  nathanw  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  1.18.4.2  nathanw  * All rights reserved.
      6  1.18.4.2  nathanw  *
      7  1.18.4.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.18.4.2  nathanw  * by Gordon W. Ross.
      9  1.18.4.2  nathanw  *
     10  1.18.4.2  nathanw  * Redistribution and use in source and binary forms, with or without
     11  1.18.4.2  nathanw  * modification, are permitted provided that the following conditions
     12  1.18.4.2  nathanw  * are met:
     13  1.18.4.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     14  1.18.4.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     15  1.18.4.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.18.4.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     17  1.18.4.2  nathanw  *    documentation and/or other materials provided with the distribution.
     18  1.18.4.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     19  1.18.4.2  nathanw  *    must display the following acknowledgement:
     20  1.18.4.2  nathanw  *        This product includes software developed by the NetBSD
     21  1.18.4.2  nathanw  *        Foundation, Inc. and its contributors.
     22  1.18.4.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.18.4.2  nathanw  *    contributors may be used to endorse or promote products derived
     24  1.18.4.2  nathanw  *    from this software without specific prior written permission.
     25  1.18.4.2  nathanw  *
     26  1.18.4.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.18.4.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.18.4.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.18.4.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.18.4.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.18.4.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.18.4.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.18.4.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.18.4.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.18.4.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.18.4.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     37  1.18.4.2  nathanw  */
     38  1.18.4.2  nathanw 
     39  1.18.4.2  nathanw /*
     40  1.18.4.2  nathanw  * Keyboard/Display device.
     41  1.18.4.2  nathanw  *
     42  1.18.4.2  nathanw  * This driver exists simply to provide a tty device that
     43  1.18.4.2  nathanw  * the indirect console driver can point to.
     44  1.18.4.2  nathanw  * The kbd driver sends its input here.
     45  1.18.4.2  nathanw  * Output goes to the screen via PROM printf.
     46  1.18.4.2  nathanw  */
     47  1.18.4.2  nathanw 
     48  1.18.4.2  nathanw #include <sys/param.h>
     49  1.18.4.2  nathanw #include <sys/proc.h>
     50  1.18.4.2  nathanw #include <sys/systm.h>
     51  1.18.4.2  nathanw #include <sys/ioctl.h>
     52  1.18.4.2  nathanw #include <sys/tty.h>
     53  1.18.4.2  nathanw #include <sys/file.h>
     54  1.18.4.2  nathanw #include <sys/conf.h>
     55  1.18.4.2  nathanw #include <sys/device.h>
     56  1.18.4.2  nathanw 
     57  1.18.4.2  nathanw #include <machine/openfirm.h>
     58  1.18.4.2  nathanw #include <machine/eeprom.h>
     59  1.18.4.2  nathanw #include <machine/psl.h>
     60  1.18.4.2  nathanw #include <machine/cpu.h>
     61  1.18.4.2  nathanw #include <machine/kbd.h>
     62  1.18.4.2  nathanw #include <machine/autoconf.h>
     63  1.18.4.2  nathanw #include <machine/conf.h>
     64  1.18.4.2  nathanw 
     65  1.18.4.2  nathanw #ifdef RASTERCONSOLE
     66  1.18.4.2  nathanw #include <dev/sun/fbio.h>
     67  1.18.4.2  nathanw #include <machine/fbvar.h>
     68  1.18.4.2  nathanw #endif
     69  1.18.4.2  nathanw 
     70  1.18.4.2  nathanw 
     71  1.18.4.2  nathanw #include <dev/cons.h>
     72  1.18.4.2  nathanw #include <dev/sun/event_var.h>
     73  1.18.4.2  nathanw #include <dev/sun/kbd_xlate.h>
     74  1.18.4.2  nathanw #include <dev/sun/kbdvar.h>
     75  1.18.4.2  nathanw #include <sparc64/dev/cons.h>
     76  1.18.4.2  nathanw 
     77  1.18.4.2  nathanw struct	tty *fbconstty = 0;	/* tty structure for frame buffer console */
     78  1.18.4.2  nathanw 
     79  1.18.4.2  nathanw #define	KDMAJOR 1
     80  1.18.4.2  nathanw #define PUT_WSIZE	64
     81  1.18.4.2  nathanw 
     82  1.18.4.2  nathanw struct kd_softc {
     83  1.18.4.2  nathanw 	struct	device kd_dev;		/* required first: base device */
     84  1.18.4.2  nathanw 	struct  tty *kd_tty;
     85  1.18.4.2  nathanw 	int rows, cols;
     86  1.18.4.2  nathanw 
     87  1.18.4.2  nathanw 	/* Console input hook */
     88  1.18.4.2  nathanw 	struct cons_channel *kd_in;
     89  1.18.4.2  nathanw };
     90  1.18.4.2  nathanw 
     91  1.18.4.2  nathanw /*
     92  1.18.4.2  nathanw  * There is no point in pretending there might be
     93  1.18.4.2  nathanw  * more than one keyboard/display device.
     94  1.18.4.2  nathanw  */
     95  1.18.4.2  nathanw static struct kd_softc kd_softc;
     96  1.18.4.2  nathanw static int kd_is_console;
     97  1.18.4.2  nathanw 
     98  1.18.4.2  nathanw static int kdparam(struct tty *, struct termios *);
     99  1.18.4.2  nathanw static void kdstart(struct tty *);
    100  1.18.4.2  nathanw static void kd_init __P((struct kd_softc *));
    101  1.18.4.2  nathanw static void kd_cons_input __P((int));
    102  1.18.4.2  nathanw static int  kdcngetc __P((dev_t));
    103  1.18.4.2  nathanw 
    104  1.18.4.2  nathanw int	cons_ocount;		/* output byte count */
    105  1.18.4.2  nathanw 
    106  1.18.4.2  nathanw /*
    107  1.18.4.2  nathanw  * This is called by kbd_attach()
    108  1.18.4.2  nathanw  * XXX - Make this a proper child of kbd?
    109  1.18.4.2  nathanw  */
    110  1.18.4.2  nathanw void
    111  1.18.4.2  nathanw kd_init(kd)
    112  1.18.4.2  nathanw 	struct kd_softc *kd;
    113  1.18.4.2  nathanw {
    114  1.18.4.2  nathanw 	struct tty *tp;
    115  1.18.4.2  nathanw 	int i;
    116  1.18.4.2  nathanw 	char *prop;
    117  1.18.4.2  nathanw 
    118  1.18.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    119  1.18.4.2  nathanw 
    120  1.18.4.2  nathanw 	tp = ttymalloc();
    121  1.18.4.2  nathanw 	tp->t_oproc = kdstart;
    122  1.18.4.2  nathanw 	tp->t_param = kdparam;
    123  1.18.4.2  nathanw 	tp->t_dev = makedev(KDMAJOR, 0);
    124  1.18.4.2  nathanw 
    125  1.18.4.2  nathanw 	tty_attach(tp);
    126  1.18.4.2  nathanw 	kd->kd_tty = tp;
    127  1.18.4.2  nathanw 
    128  1.18.4.2  nathanw 	/*
    129  1.18.4.2  nathanw 	 * get the console struct winsize.
    130  1.18.4.2  nathanw 	 */
    131  1.18.4.2  nathanw 	if (kd_is_console) {
    132  1.18.4.2  nathanw 		fbconstty = tp;
    133  1.18.4.2  nathanw #ifdef RASTERCONSOLE
    134  1.18.4.2  nathanw 		kd->rows = fbrcons_rows();
    135  1.18.4.2  nathanw 		kd->cols = fbrcons_cols();
    136  1.18.4.2  nathanw 		rcons_ttyinit(tp);
    137  1.18.4.2  nathanw #endif
    138  1.18.4.2  nathanw 	}
    139  1.18.4.2  nathanw 
    140  1.18.4.2  nathanw 	if (kd->rows == 0 &&
    141  1.18.4.2  nathanw 	    (prop = PROM_getpropstring(optionsnode, "screen-#rows"))) {
    142  1.18.4.2  nathanw 		i = 0;
    143  1.18.4.2  nathanw 		while (*prop != '\0')
    144  1.18.4.2  nathanw 			i = i * 10 + *prop++ - '0';
    145  1.18.4.2  nathanw 		kd->rows = (unsigned short)i;
    146  1.18.4.2  nathanw 	}
    147  1.18.4.2  nathanw 	if (kd->cols == 0 &&
    148  1.18.4.2  nathanw 	    (prop = PROM_getpropstring(optionsnode, "screen-#columns"))) {
    149  1.18.4.2  nathanw 		i = 0;
    150  1.18.4.2  nathanw 		while (*prop != '\0')
    151  1.18.4.2  nathanw 			i = i * 10 + *prop++ - '0';
    152  1.18.4.2  nathanw 		kd->cols = (unsigned short)i;
    153  1.18.4.2  nathanw 	}
    154  1.18.4.2  nathanw 	return;
    155  1.18.4.2  nathanw }
    156  1.18.4.2  nathanw 
    157  1.18.4.2  nathanw struct tty *
    158  1.18.4.2  nathanw kdtty(dev)
    159  1.18.4.2  nathanw 	dev_t dev;
    160  1.18.4.2  nathanw {
    161  1.18.4.2  nathanw 	struct kd_softc *kd;
    162  1.18.4.2  nathanw 
    163  1.18.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    164  1.18.4.2  nathanw 	return (kd->kd_tty);
    165  1.18.4.2  nathanw }
    166  1.18.4.2  nathanw 
    167  1.18.4.2  nathanw int
    168  1.18.4.2  nathanw kdopen(dev, flag, mode, p)
    169  1.18.4.2  nathanw 	dev_t dev;
    170  1.18.4.2  nathanw 	int flag, mode;
    171  1.18.4.2  nathanw 	struct proc *p;
    172  1.18.4.2  nathanw {
    173  1.18.4.2  nathanw 	struct kd_softc *kd;
    174  1.18.4.2  nathanw 	int error, s, unit;
    175  1.18.4.2  nathanw 	struct tty *tp;
    176  1.18.4.2  nathanw static	int firstopen = 1;
    177  1.18.4.2  nathanw 
    178  1.18.4.2  nathanw 	unit = minor(dev);
    179  1.18.4.2  nathanw 	if (unit != 0)
    180  1.18.4.2  nathanw 		return ENXIO;
    181  1.18.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    182  1.18.4.2  nathanw 
    183  1.18.4.2  nathanw 	if (firstopen) {
    184  1.18.4.2  nathanw 		kd_init(kd);
    185  1.18.4.2  nathanw 		firstopen = 0;
    186  1.18.4.2  nathanw 	}
    187  1.18.4.2  nathanw 	tp = kd->kd_tty;
    188  1.18.4.2  nathanw 
    189  1.18.4.2  nathanw 	/* It's simpler to do this up here. */
    190  1.18.4.2  nathanw 	if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
    191  1.18.4.2  nathanw 	     ==             (TS_ISOPEN | TS_XCLUDE))
    192  1.18.4.2  nathanw 	    && (p->p_ucred->cr_uid != 0) )
    193  1.18.4.2  nathanw 	{
    194  1.18.4.2  nathanw 		return (EBUSY);
    195  1.18.4.2  nathanw 	}
    196  1.18.4.2  nathanw 
    197  1.18.4.2  nathanw 	s = spltty();
    198  1.18.4.2  nathanw 
    199  1.18.4.2  nathanw 	if ((tp->t_state & TS_ISOPEN) == 0) {
    200  1.18.4.2  nathanw 		/* First open. */
    201  1.18.4.2  nathanw 
    202  1.18.4.2  nathanw 		/* Notify the input device that serves us */
    203  1.18.4.2  nathanw 		struct cons_channel *cc = kd->kd_in;
    204  1.18.4.2  nathanw 		if (cc != NULL &&
    205  1.18.4.2  nathanw 		    (error = (*cc->cc_iopen)(cc)) != 0) {
    206  1.18.4.2  nathanw 			splx(s);
    207  1.18.4.2  nathanw 			return (error);
    208  1.18.4.2  nathanw 		}
    209  1.18.4.2  nathanw 
    210  1.18.4.2  nathanw 		ttychars(tp);
    211  1.18.4.2  nathanw 		tp->t_iflag = TTYDEF_IFLAG;
    212  1.18.4.2  nathanw 		tp->t_oflag = TTYDEF_OFLAG;
    213  1.18.4.2  nathanw 		tp->t_cflag = TTYDEF_CFLAG;
    214  1.18.4.2  nathanw 		tp->t_lflag = TTYDEF_LFLAG;
    215  1.18.4.2  nathanw 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    216  1.18.4.2  nathanw 		(void) kdparam(tp, &tp->t_termios);
    217  1.18.4.2  nathanw 		ttsetwater(tp);
    218  1.18.4.2  nathanw 		tp->t_winsize.ws_row = kd->rows;
    219  1.18.4.2  nathanw 		tp->t_winsize.ws_col = kd->cols;
    220  1.18.4.2  nathanw 		/* Flush pending input?  Clear translator? */
    221  1.18.4.2  nathanw 		/* This (pseudo)device always has SOFTCAR */
    222  1.18.4.2  nathanw 		tp->t_state |= TS_CARR_ON;
    223  1.18.4.2  nathanw 	}
    224  1.18.4.2  nathanw 
    225  1.18.4.2  nathanw 	splx(s);
    226  1.18.4.2  nathanw 
    227  1.18.4.2  nathanw 	return ((*tp->t_linesw->l_open)(dev, tp));
    228  1.18.4.2  nathanw }
    229  1.18.4.2  nathanw 
    230  1.18.4.2  nathanw int
    231  1.18.4.2  nathanw kdclose(dev, flag, mode, p)
    232  1.18.4.2  nathanw 	dev_t dev;
    233  1.18.4.2  nathanw 	int flag, mode;
    234  1.18.4.2  nathanw 	struct proc *p;
    235  1.18.4.2  nathanw {
    236  1.18.4.2  nathanw 	struct kd_softc *kd;
    237  1.18.4.2  nathanw 	struct tty *tp;
    238  1.18.4.2  nathanw 	struct cons_channel *cc;
    239  1.18.4.2  nathanw 
    240  1.18.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    241  1.18.4.2  nathanw 	tp = kd->kd_tty;
    242  1.18.4.2  nathanw 
    243  1.18.4.2  nathanw 	/* XXX This is for cons.c. */
    244  1.18.4.2  nathanw 	if ((tp->t_state & TS_ISOPEN) == 0)
    245  1.18.4.2  nathanw 		return 0;
    246  1.18.4.2  nathanw 
    247  1.18.4.2  nathanw 	(*tp->t_linesw->l_close)(tp, flag);
    248  1.18.4.2  nathanw 	ttyclose(tp);
    249  1.18.4.2  nathanw 
    250  1.18.4.2  nathanw 	if ((cc = kd->kd_in) != NULL)
    251  1.18.4.2  nathanw 		(void)(*cc->cc_iclose)(cc->cc_dev);
    252  1.18.4.2  nathanw 
    253  1.18.4.2  nathanw 	return (0);
    254  1.18.4.2  nathanw }
    255  1.18.4.2  nathanw 
    256  1.18.4.2  nathanw int
    257  1.18.4.2  nathanw kdread(dev, uio, flag)
    258  1.18.4.2  nathanw 	dev_t dev;
    259  1.18.4.2  nathanw 	struct uio *uio;
    260  1.18.4.2  nathanw 	int flag;
    261  1.18.4.2  nathanw {
    262  1.18.4.2  nathanw 	struct kd_softc *kd;
    263  1.18.4.2  nathanw 	struct tty *tp;
    264  1.18.4.2  nathanw 
    265  1.18.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    266  1.18.4.2  nathanw 	tp = kd->kd_tty;
    267  1.18.4.2  nathanw 
    268  1.18.4.2  nathanw 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    269  1.18.4.2  nathanw }
    270  1.18.4.2  nathanw 
    271  1.18.4.2  nathanw int
    272  1.18.4.2  nathanw kdwrite(dev, uio, flag)
    273  1.18.4.2  nathanw 	dev_t dev;
    274  1.18.4.2  nathanw 	struct uio *uio;
    275  1.18.4.2  nathanw 	int flag;
    276  1.18.4.2  nathanw {
    277  1.18.4.2  nathanw 	struct kd_softc *kd;
    278  1.18.4.2  nathanw 	struct tty *tp;
    279  1.18.4.2  nathanw 
    280  1.18.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    281  1.18.4.2  nathanw 	tp = kd->kd_tty;
    282  1.18.4.2  nathanw 
    283  1.18.4.2  nathanw 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    284  1.18.4.2  nathanw }
    285  1.18.4.2  nathanw 
    286  1.18.4.2  nathanw int
    287  1.18.4.2  nathanw kdpoll(dev, events, p)
    288  1.18.4.2  nathanw 	dev_t dev;
    289  1.18.4.2  nathanw 	int events;
    290  1.18.4.2  nathanw 	struct proc *p;
    291  1.18.4.2  nathanw {
    292  1.18.4.2  nathanw 	struct kd_softc *kd;
    293  1.18.4.2  nathanw 	struct tty *tp;
    294  1.18.4.2  nathanw 
    295  1.18.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    296  1.18.4.2  nathanw 	tp = kd->kd_tty;
    297  1.18.4.2  nathanw 
    298  1.18.4.2  nathanw 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    299  1.18.4.2  nathanw }
    300  1.18.4.2  nathanw 
    301  1.18.4.2  nathanw int
    302  1.18.4.2  nathanw kdioctl(dev, cmd, data, flag, p)
    303  1.18.4.2  nathanw 	dev_t dev;
    304  1.18.4.2  nathanw 	u_long cmd;
    305  1.18.4.2  nathanw 	caddr_t data;
    306  1.18.4.2  nathanw 	int flag;
    307  1.18.4.2  nathanw 	struct proc *p;
    308  1.18.4.2  nathanw {
    309  1.18.4.2  nathanw 	struct kd_softc *kd;
    310  1.18.4.2  nathanw 	struct tty *tp;
    311  1.18.4.2  nathanw 	int error;
    312  1.18.4.2  nathanw 
    313  1.18.4.2  nathanw 	kd = &kd_softc; 	/* XXX */
    314  1.18.4.2  nathanw 	tp = kd->kd_tty;
    315  1.18.4.2  nathanw 
    316  1.18.4.2  nathanw 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    317  1.18.4.2  nathanw 	if (error != EPASSTHROUGH)
    318  1.18.4.2  nathanw 		return error;
    319  1.18.4.2  nathanw 
    320  1.18.4.2  nathanw 	error = ttioctl(tp, cmd, data, flag, p);
    321  1.18.4.2  nathanw 	if (error != EPASSTHROUGH)
    322  1.18.4.2  nathanw 		return error;
    323  1.18.4.2  nathanw 
    324  1.18.4.2  nathanw 	/* Handle any ioctl commands specific to kbd/display. */
    325  1.18.4.2  nathanw 	/* XXX - Send KB* ioctls to kbd module? */
    326  1.18.4.2  nathanw 	/* XXX - Send FB* ioctls to fb module?  */
    327  1.18.4.2  nathanw 
    328  1.18.4.2  nathanw 	return EPASSTHROUGH;
    329  1.18.4.2  nathanw }
    330  1.18.4.2  nathanw 
    331  1.18.4.2  nathanw void
    332  1.18.4.2  nathanw kdstop(tp, flag)
    333  1.18.4.2  nathanw 	struct tty *tp;
    334  1.18.4.2  nathanw 	int flag;
    335  1.18.4.2  nathanw {
    336  1.18.4.2  nathanw 
    337  1.18.4.2  nathanw }
    338  1.18.4.2  nathanw 
    339  1.18.4.2  nathanw 
    340  1.18.4.2  nathanw static int
    341  1.18.4.2  nathanw kdparam(tp, t)
    342  1.18.4.2  nathanw 	struct tty *tp;
    343  1.18.4.2  nathanw 	struct termios *t;
    344  1.18.4.2  nathanw {
    345  1.18.4.2  nathanw 	/* XXX - These are ignored... */
    346  1.18.4.2  nathanw 	tp->t_ispeed = t->c_ispeed;
    347  1.18.4.2  nathanw 	tp->t_ospeed = t->c_ospeed;
    348  1.18.4.2  nathanw 	tp->t_cflag = t->c_cflag;
    349  1.18.4.2  nathanw 	return 0;
    350  1.18.4.2  nathanw }
    351  1.18.4.2  nathanw 
    352  1.18.4.2  nathanw 
    353  1.18.4.2  nathanw static void kd_later(void*);
    354  1.18.4.2  nathanw static void kd_putfb(struct tty *);
    355  1.18.4.2  nathanw 
    356  1.18.4.2  nathanw static void
    357  1.18.4.2  nathanw kdstart(tp)
    358  1.18.4.2  nathanw 	struct tty *tp;
    359  1.18.4.2  nathanw {
    360  1.18.4.2  nathanw 	struct clist *cl;
    361  1.18.4.2  nathanw 	register int s;
    362  1.18.4.2  nathanw 
    363  1.18.4.2  nathanw 	s = spltty();
    364  1.18.4.2  nathanw 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
    365  1.18.4.2  nathanw 		goto out;
    366  1.18.4.2  nathanw 
    367  1.18.4.2  nathanw 	cl = &tp->t_outq;
    368  1.18.4.2  nathanw 	if (cl->c_cc) {
    369  1.18.4.2  nathanw 		if (kd_is_console) {
    370  1.18.4.2  nathanw 			tp->t_state |= TS_BUSY;
    371  1.18.4.2  nathanw 			if (s == 0) {
    372  1.18.4.2  nathanw 				/* called at level zero - update screen now. */
    373  1.18.4.2  nathanw 				(void) spllowersoftclock();
    374  1.18.4.2  nathanw 				kd_putfb(tp);
    375  1.18.4.2  nathanw 				(void) spltty();
    376  1.18.4.2  nathanw 				tp->t_state &= ~TS_BUSY;
    377  1.18.4.2  nathanw 			} else {
    378  1.18.4.2  nathanw 				/* called at interrupt level - do it later */
    379  1.18.4.2  nathanw 				callout_reset(&tp->t_rstrt_ch, 0,
    380  1.18.4.2  nathanw 				    kd_later, tp);
    381  1.18.4.2  nathanw 			}
    382  1.18.4.2  nathanw 		} else {
    383  1.18.4.2  nathanw 			/*
    384  1.18.4.2  nathanw 			 * This driver uses the PROM for writing the screen,
    385  1.18.4.2  nathanw 			 * and that only works if this is the console device.
    386  1.18.4.2  nathanw 			 * If this is not the console, just flush the output.
    387  1.18.4.2  nathanw 			 * Sorry.  (In that case, use xdm instead of getty.)
    388  1.18.4.2  nathanw 			 */
    389  1.18.4.2  nathanw 			ndflush(cl, cl->c_cc);
    390  1.18.4.2  nathanw 		}
    391  1.18.4.2  nathanw 	}
    392  1.18.4.2  nathanw 	if (cl->c_cc <= tp->t_lowat) {
    393  1.18.4.2  nathanw 		if (tp->t_state & TS_ASLEEP) {
    394  1.18.4.2  nathanw 			tp->t_state &= ~TS_ASLEEP;
    395  1.18.4.2  nathanw 			wakeup((caddr_t)cl);
    396  1.18.4.2  nathanw 		}
    397  1.18.4.2  nathanw 		selwakeup(&tp->t_wsel);
    398  1.18.4.2  nathanw 	}
    399  1.18.4.2  nathanw out:
    400  1.18.4.2  nathanw 	splx(s);
    401  1.18.4.2  nathanw }
    402  1.18.4.2  nathanw 
    403  1.18.4.2  nathanw /*
    404  1.18.4.2  nathanw  * Timeout function to do delayed writes to the screen.
    405  1.18.4.2  nathanw  * Called at splsoftclock when requested by kdstart.
    406  1.18.4.2  nathanw  */
    407  1.18.4.2  nathanw static void
    408  1.18.4.2  nathanw kd_later(tpaddr)
    409  1.18.4.2  nathanw 	void *tpaddr;
    410  1.18.4.2  nathanw {
    411  1.18.4.2  nathanw 	struct tty *tp = tpaddr;
    412  1.18.4.2  nathanw 	register int s;
    413  1.18.4.2  nathanw 
    414  1.18.4.2  nathanw 	kd_putfb(tp);
    415  1.18.4.2  nathanw 
    416  1.18.4.2  nathanw 	s = spltty();
    417  1.18.4.2  nathanw 	tp->t_state &= ~TS_BUSY;
    418  1.18.4.2  nathanw 	(*tp->t_linesw->l_start)(tp);
    419  1.18.4.2  nathanw 	splx(s);
    420  1.18.4.2  nathanw }
    421  1.18.4.2  nathanw 
    422  1.18.4.2  nathanw /*
    423  1.18.4.2  nathanw  * Put text on the screen using the PROM monitor.
    424  1.18.4.2  nathanw  * This can take a while, so to avoid missing
    425  1.18.4.2  nathanw  * interrupts, this is called at splsoftclock.
    426  1.18.4.2  nathanw  */
    427  1.18.4.2  nathanw static void
    428  1.18.4.2  nathanw kd_putfb(tp)
    429  1.18.4.2  nathanw 	struct tty *tp;
    430  1.18.4.2  nathanw {
    431  1.18.4.2  nathanw 	char buf[PUT_WSIZE];
    432  1.18.4.2  nathanw 	struct clist *cl = &tp->t_outq;
    433  1.18.4.2  nathanw 	char *p, *end;
    434  1.18.4.2  nathanw 	int len;
    435  1.18.4.2  nathanw 
    436  1.18.4.2  nathanw 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
    437  1.18.4.2  nathanw 		/* PROM will barf if high bits are set. */
    438  1.18.4.2  nathanw 		p = buf;
    439  1.18.4.2  nathanw 		end = buf + len;
    440  1.18.4.2  nathanw 		while (p < end)
    441  1.18.4.2  nathanw 			*p++ &= 0x7f;
    442  1.18.4.2  nathanw 		/* Now let the PROM print it. */
    443  1.18.4.2  nathanw 		OF_write(OF_stdout(), buf, len);
    444  1.18.4.2  nathanw 	}
    445  1.18.4.2  nathanw }
    446  1.18.4.2  nathanw 
    447  1.18.4.2  nathanw /*
    448  1.18.4.2  nathanw  * Default PROM-based console input stream
    449  1.18.4.2  nathanw  */
    450  1.18.4.2  nathanw static int kd_rom_iopen __P((struct cons_channel *));
    451  1.18.4.2  nathanw static int kd_rom_iclose __P((struct cons_channel *));
    452  1.18.4.2  nathanw 
    453  1.18.4.2  nathanw static struct cons_channel prom_cons_channel;
    454  1.18.4.2  nathanw 
    455  1.18.4.2  nathanw int
    456  1.18.4.2  nathanw kd_rom_iopen(cc)
    457  1.18.4.2  nathanw 	struct cons_channel *cc;
    458  1.18.4.2  nathanw {
    459  1.18.4.2  nathanw 	return (0);
    460  1.18.4.2  nathanw }
    461  1.18.4.2  nathanw 
    462  1.18.4.2  nathanw int
    463  1.18.4.2  nathanw kd_rom_iclose(cc)
    464  1.18.4.2  nathanw 	struct cons_channel *cc;
    465  1.18.4.2  nathanw {
    466  1.18.4.2  nathanw 	return (0);
    467  1.18.4.2  nathanw }
    468  1.18.4.2  nathanw 
    469  1.18.4.2  nathanw /*
    470  1.18.4.2  nathanw  * Our "interrupt" routine for input. This is called by
    471  1.18.4.2  nathanw  * the keyboard driver (dev/sun/kbd.c) at spltty.
    472  1.18.4.2  nathanw  */
    473  1.18.4.2  nathanw void
    474  1.18.4.2  nathanw kd_cons_input(c)
    475  1.18.4.2  nathanw 	int c;
    476  1.18.4.2  nathanw {
    477  1.18.4.2  nathanw 	struct kd_softc *kd = &kd_softc;
    478  1.18.4.2  nathanw 	struct tty *tp;
    479  1.18.4.2  nathanw 
    480  1.18.4.2  nathanw 	/* XXX: Make sure the device is open. */
    481  1.18.4.2  nathanw 	tp = kd->kd_tty;
    482  1.18.4.2  nathanw 	if (tp == NULL)
    483  1.18.4.2  nathanw 		return;
    484  1.18.4.2  nathanw 	if ((tp->t_state & TS_ISOPEN) == 0)
    485  1.18.4.2  nathanw 		return;
    486  1.18.4.2  nathanw 
    487  1.18.4.2  nathanw 	(*tp->t_linesw->l_rint)(c, tp);
    488  1.18.4.2  nathanw }
    489  1.18.4.2  nathanw 
    490  1.18.4.2  nathanw 
    491  1.18.4.2  nathanw /****************************************************************
    492  1.18.4.2  nathanw  * kd console support
    493  1.18.4.2  nathanw  ****************************************************************/
    494  1.18.4.2  nathanw 
    495  1.18.4.2  nathanw /* The debugger gets its own key translation state. */
    496  1.18.4.2  nathanw static struct kbd_state *kdcn_state;
    497  1.18.4.2  nathanw 
    498  1.18.4.2  nathanw static void kdcnprobe __P((struct consdev *));
    499  1.18.4.2  nathanw static void kdcninit __P((struct consdev *));
    500  1.18.4.2  nathanw static void kdcnputc __P((dev_t, int));
    501  1.18.4.2  nathanw static void kdcnpollc __P((dev_t, int));
    502  1.18.4.2  nathanw 
    503  1.18.4.2  nathanw /* The keyboard driver uses cn_hw to access the real console driver */
    504  1.18.4.2  nathanw extern struct consdev consdev_prom;
    505  1.18.4.2  nathanw struct consdev consdev_kd = {
    506  1.18.4.2  nathanw 	kdcnprobe,
    507  1.18.4.2  nathanw 	kdcninit,
    508  1.18.4.2  nathanw 	kdcngetc,
    509  1.18.4.2  nathanw 	kdcnputc,
    510  1.18.4.2  nathanw 	kdcnpollc,
    511  1.18.4.2  nathanw 	NULL,
    512  1.18.4.2  nathanw };
    513  1.18.4.2  nathanw struct consdev *cn_hw = &consdev_kd;
    514  1.18.4.2  nathanw 
    515  1.18.4.2  nathanw void
    516  1.18.4.2  nathanw cons_attach_input(cc, cn)
    517  1.18.4.2  nathanw 	struct cons_channel *cc;
    518  1.18.4.2  nathanw 	struct consdev *cn;
    519  1.18.4.2  nathanw {
    520  1.18.4.2  nathanw 	struct kd_softc *kd = &kd_softc;
    521  1.18.4.2  nathanw 	struct kbd_softc *kds = cc->cc_dev;
    522  1.18.4.2  nathanw 	struct kbd_state *ks;
    523  1.18.4.2  nathanw 
    524  1.18.4.2  nathanw 	/* Share the keyboard state */
    525  1.18.4.2  nathanw 	kdcn_state = ks = &kds->k_state;
    526  1.18.4.2  nathanw 
    527  1.18.4.2  nathanw 	kd->kd_in = cc;
    528  1.18.4.2  nathanw 	cc->cc_upstream = kd_cons_input;
    529  1.18.4.2  nathanw 
    530  1.18.4.2  nathanw 	/* Attach lower level. */
    531  1.18.4.2  nathanw 	cn_hw->cn_dev = cn->cn_dev;
    532  1.18.4.2  nathanw 	cn_hw->cn_pollc = cn->cn_pollc;
    533  1.18.4.2  nathanw 	cn_hw->cn_getc = cn->cn_getc;
    534  1.18.4.2  nathanw 
    535  1.18.4.2  nathanw 	/* Attach us as console. */
    536  1.18.4.2  nathanw 	cn_tab->cn_dev = makedev(KDMAJOR, 0);
    537  1.18.4.2  nathanw 	cn_tab->cn_probe = kdcnprobe;
    538  1.18.4.2  nathanw 	cn_tab->cn_init = kdcninit;
    539  1.18.4.2  nathanw 	cn_tab->cn_getc = kdcngetc;
    540  1.18.4.2  nathanw 	cn_tab->cn_pollc = kdcnpollc;
    541  1.18.4.2  nathanw 	cn_tab->cn_pri = CN_INTERNAL;
    542  1.18.4.2  nathanw 
    543  1.18.4.2  nathanw 	/* Set up initial PROM input channel for /dev/console */
    544  1.18.4.2  nathanw 	prom_cons_channel.cc_dev = NULL;
    545  1.18.4.2  nathanw 	prom_cons_channel.cc_iopen = kd_rom_iopen;
    546  1.18.4.2  nathanw 	prom_cons_channel.cc_iclose = kd_rom_iclose;
    547  1.18.4.2  nathanw 
    548  1.18.4.2  nathanw 	/* Indicate that it is OK to use the PROM fbwrite */
    549  1.18.4.2  nathanw 	kd_is_console = 1;
    550  1.18.4.2  nathanw }
    551  1.18.4.2  nathanw 
    552  1.18.4.2  nathanw 
    553  1.18.4.2  nathanw void kd_attach_input(struct cons_channel *);
    554  1.18.4.2  nathanw void
    555  1.18.4.2  nathanw kd_attach_input(cc)
    556  1.18.4.2  nathanw 	struct cons_channel *cc;
    557  1.18.4.2  nathanw {
    558  1.18.4.2  nathanw 	struct kd_softc *kd = &kd_softc;
    559  1.18.4.2  nathanw 
    560  1.18.4.2  nathanw 	kd->kd_in = cc;
    561  1.18.4.2  nathanw 	cc->cc_upstream = kd_cons_input;
    562  1.18.4.2  nathanw }
    563  1.18.4.2  nathanw 
    564  1.18.4.2  nathanw 
    565  1.18.4.2  nathanw /* We never call this. */
    566  1.18.4.2  nathanw static void
    567  1.18.4.2  nathanw kdcnprobe(cn)
    568  1.18.4.2  nathanw 	struct consdev *cn;
    569  1.18.4.2  nathanw {
    570  1.18.4.2  nathanw }
    571  1.18.4.2  nathanw 
    572  1.18.4.2  nathanw static void
    573  1.18.4.2  nathanw kdcninit(cn)
    574  1.18.4.2  nathanw 	struct consdev *cn;
    575  1.18.4.2  nathanw {
    576  1.18.4.2  nathanw #if 0
    577  1.18.4.2  nathanw 	struct kbd_state *ks = kdcn_state;
    578  1.18.4.2  nathanw 
    579  1.18.4.2  nathanw 	cn->cn_dev = makedev(KDMAJOR, 0);
    580  1.18.4.2  nathanw 	cn->cn_pri = CN_INTERNAL;
    581  1.18.4.2  nathanw 
    582  1.18.4.2  nathanw 	/* This prepares kbd_translate() */
    583  1.18.4.2  nathanw 	ks->kbd_id = KBD_MIN_TYPE;
    584  1.18.4.2  nathanw 	kbd_xlate_init(ks);
    585  1.18.4.2  nathanw 
    586  1.18.4.2  nathanw 	/* Set up initial PROM input channel for /dev/console */
    587  1.18.4.2  nathanw 	prom_cons_channel.cc_dev = NULL;
    588  1.18.4.2  nathanw 	prom_cons_channel.cc_iopen = kd_rom_iopen;
    589  1.18.4.2  nathanw 	prom_cons_channel.cc_iclose = kd_rom_iclose;
    590  1.18.4.2  nathanw 	cons_attach_input(&prom_cons_channel);
    591  1.18.4.2  nathanw 
    592  1.18.4.2  nathanw 	/* Indicate that it is OK to use the PROM fbwrite */
    593  1.18.4.2  nathanw 	kd_is_console = 1;
    594  1.18.4.2  nathanw #endif
    595  1.18.4.2  nathanw }
    596  1.18.4.2  nathanw 
    597  1.18.4.2  nathanw static int
    598  1.18.4.2  nathanw kdcngetc(dev)
    599  1.18.4.2  nathanw 	dev_t dev;
    600  1.18.4.2  nathanw {
    601  1.18.4.2  nathanw 	struct kbd_state *ks = kdcn_state;
    602  1.18.4.2  nathanw 	int code, class, data, keysym;
    603  1.18.4.2  nathanw 	extern int prom_cngetc __P((dev_t));
    604  1.18.4.2  nathanw 
    605  1.18.4.2  nathanw 
    606  1.18.4.2  nathanw 	if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev);
    607  1.18.4.2  nathanw 	for (;;) {
    608  1.18.4.2  nathanw 		code = (*cn_hw->cn_getc)(dev);
    609  1.18.4.2  nathanw 		keysym = kbd_code_to_keysym(ks, code);
    610  1.18.4.2  nathanw 		class = KEYSYM_CLASS(keysym);
    611  1.18.4.2  nathanw 
    612  1.18.4.2  nathanw 		switch (class) {
    613  1.18.4.2  nathanw 		case KEYSYM_ASCII:
    614  1.18.4.2  nathanw 			goto out;
    615  1.18.4.2  nathanw 
    616  1.18.4.2  nathanw 		case KEYSYM_CLRMOD:
    617  1.18.4.2  nathanw 		case KEYSYM_SETMOD:
    618  1.18.4.2  nathanw 			data = (keysym & 0x1F);
    619  1.18.4.2  nathanw 			/* Only allow ctrl or shift. */
    620  1.18.4.2  nathanw 			if (data > KBMOD_SHIFT_R)
    621  1.18.4.2  nathanw 				break;
    622  1.18.4.2  nathanw 			data = 1 << data;
    623  1.18.4.2  nathanw 			if (class == KEYSYM_SETMOD)
    624  1.18.4.2  nathanw 				ks->kbd_modbits |= data;
    625  1.18.4.2  nathanw 			else
    626  1.18.4.2  nathanw 				ks->kbd_modbits &= ~data;
    627  1.18.4.2  nathanw 			break;
    628  1.18.4.2  nathanw 
    629  1.18.4.2  nathanw 		case KEYSYM_ALL_UP:
    630  1.18.4.2  nathanw 			/* No toggle keys here. */
    631  1.18.4.2  nathanw 			ks->kbd_modbits = 0;
    632  1.18.4.2  nathanw 			break;
    633  1.18.4.2  nathanw 
    634  1.18.4.2  nathanw 		default:	/* ignore all other keysyms */
    635  1.18.4.2  nathanw 			break;
    636  1.18.4.2  nathanw 		}
    637  1.18.4.2  nathanw 	}
    638  1.18.4.2  nathanw out:
    639  1.18.4.2  nathanw 	return (keysym);
    640  1.18.4.2  nathanw }
    641  1.18.4.2  nathanw 
    642  1.18.4.2  nathanw static void
    643  1.18.4.2  nathanw kdcnputc(dev, c)
    644  1.18.4.2  nathanw 	dev_t dev;
    645  1.18.4.2  nathanw 	int c;
    646  1.18.4.2  nathanw {
    647  1.18.4.2  nathanw 	int s;
    648  1.18.4.2  nathanw 	char c0 = (c & 0x7f);
    649  1.18.4.2  nathanw 
    650  1.18.4.2  nathanw 	s = splhigh();
    651  1.18.4.2  nathanw 	OF_write(OF_stdout(), &c0, 1);
    652  1.18.4.2  nathanw 	splx(s);
    653  1.18.4.2  nathanw }
    654  1.18.4.2  nathanw 
    655  1.18.4.2  nathanw static void
    656  1.18.4.2  nathanw kdcnpollc(dev, on)
    657  1.18.4.2  nathanw 	dev_t dev;
    658  1.18.4.2  nathanw 	int on;
    659  1.18.4.2  nathanw {
    660  1.18.4.2  nathanw 	struct kbd_state *ks = kdcn_state;
    661  1.18.4.2  nathanw 
    662  1.18.4.2  nathanw 	if (on) {
    663  1.18.4.2  nathanw 		/* Entering debugger. */
    664  1.18.4.2  nathanw #if NFB > 0
    665  1.18.4.2  nathanw 		fb_unblank();
    666  1.18.4.2  nathanw #endif
    667  1.18.4.2  nathanw 		/* Clear shift keys too. */
    668  1.18.4.2  nathanw 		ks->kbd_modbits = 0;
    669  1.18.4.2  nathanw 	} else {
    670  1.18.4.2  nathanw 		/* Resuming kernel. */
    671  1.18.4.2  nathanw 	}
    672  1.18.4.2  nathanw 	(*cn_hw->cn_pollc)(dev, on);
    673  1.18.4.2  nathanw }
    674