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