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