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