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