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