Home | History | Annotate | Line # | Download | only in dev
kd.c revision 1.47.16.1
      1  1.47.16.1      yamt /*	$NetBSD: kd.c,v 1.47.16.1 2008/05/18 12:32:46 yamt Exp $	*/
      2        1.1       gwr 
      3        1.1       gwr /*-
      4        1.1       gwr  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5        1.1       gwr  * All rights reserved.
      6        1.1       gwr  *
      7        1.1       gwr  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1       gwr  * by Gordon W. Ross.
      9        1.1       gwr  *
     10        1.1       gwr  * Redistribution and use in source and binary forms, with or without
     11        1.1       gwr  * modification, are permitted provided that the following conditions
     12        1.1       gwr  * are met:
     13        1.1       gwr  * 1. Redistributions of source code must retain the above copyright
     14        1.1       gwr  *    notice, this list of conditions and the following disclaimer.
     15        1.1       gwr  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1       gwr  *    notice, this list of conditions and the following disclaimer in the
     17        1.1       gwr  *    documentation and/or other materials provided with the distribution.
     18        1.1       gwr  *
     19        1.1       gwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1       gwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1       gwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1       gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1       gwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1       gwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1       gwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1       gwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1       gwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1       gwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1       gwr  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1       gwr  */
     31        1.1       gwr 
     32        1.1       gwr /*
     33        1.7        pk  * Console driver based on PROM primitives.
     34        1.1       gwr  *
     35        1.7        pk  * This driver exists to provide a tty device that the indirect
     36        1.7        pk  * console driver can point to. It also provides a hook that
     37        1.7        pk  * allows another device to serve console input. This will normally
     38        1.7        pk  * be a keyboard driver (see sys/dev/sun/kbd.c)
     39        1.1       gwr  */
     40       1.26     lukem 
     41       1.26     lukem #include <sys/cdefs.h>
     42  1.47.16.1      yamt __KERNEL_RCSID(0, "$NetBSD: kd.c,v 1.47.16.1 2008/05/18 12:32:46 yamt Exp $");
     43       1.18        pk 
     44       1.18        pk #include "opt_kgdb.h"
     45       1.19        pk #include "fb.h"
     46        1.1       gwr 
     47        1.1       gwr #include <sys/param.h>
     48        1.1       gwr #include <sys/proc.h>
     49        1.1       gwr #include <sys/systm.h>
     50        1.7        pk #include <sys/kernel.h>
     51        1.1       gwr #include <sys/ioctl.h>
     52        1.1       gwr #include <sys/tty.h>
     53        1.1       gwr #include <sys/file.h>
     54        1.1       gwr #include <sys/conf.h>
     55        1.1       gwr #include <sys/device.h>
     56       1.39      elad #include <sys/kauth.h>
     57        1.1       gwr 
     58        1.1       gwr #include <machine/bsd_openprom.h>
     59        1.4        pk #include <machine/promlib.h>
     60        1.1       gwr #include <machine/eeprom.h>
     61        1.1       gwr #include <machine/psl.h>
     62        1.1       gwr #include <machine/cpu.h>
     63        1.1       gwr #include <machine/kbd.h>
     64        1.1       gwr #include <machine/autoconf.h>
     65        1.1       gwr 
     66       1.19        pk #if defined(RASTERCONSOLE) && NFB > 0
     67       1.14        pk #include <dev/sun/fbio.h>
     68       1.14        pk #include <dev/sun/fbvar.h>
     69        1.1       gwr #endif
     70        1.1       gwr 
     71        1.7        pk #include <dev/cons.h>
     72        1.7        pk #include <sparc/dev/cons.h>
     73        1.1       gwr 
     74        1.7        pk #include <dev/sun/event_var.h>
     75        1.1       gwr #include <dev/sun/kbd_xlate.h>
     76        1.7        pk #include <dev/sun/kbdvar.h>
     77        1.1       gwr 
     78        1.1       gwr #define PUT_WSIZE	64
     79        1.1       gwr 
     80        1.1       gwr struct kd_softc {
     81        1.1       gwr 	struct	device kd_dev;		/* required first: base device */
     82        1.1       gwr 	struct  tty *kd_tty;
     83        1.1       gwr 	int rows, cols;
     84        1.7        pk 
     85        1.7        pk 	/* Console input hook */
     86        1.7        pk 	struct cons_channel *kd_in;
     87        1.1       gwr };
     88        1.1       gwr 
     89        1.1       gwr /*
     90        1.1       gwr  * There is no point in pretending there might be
     91        1.1       gwr  * more than one keyboard/display device.
     92        1.1       gwr  */
     93        1.1       gwr static struct kd_softc kd_softc;
     94        1.1       gwr 
     95       1.36       uwe /* For keyboard driver to register itself as console input */
     96       1.36       uwe void kd_attach_input(struct cons_channel *);
     97       1.36       uwe 
     98       1.36       uwe static void kd_init(struct kd_softc *);
     99       1.36       uwe static void kdstart(struct tty *);
    100       1.36       uwe static void kd_later(void *);
    101       1.36       uwe static void kd_putfb(struct tty *);
    102        1.1       gwr static int kdparam(struct tty *, struct termios *);
    103       1.29       uwe static void kd_cons_input(int);
    104        1.1       gwr 
    105       1.22   gehenna dev_type_open(kdopen);
    106       1.22   gehenna dev_type_close(kdclose);
    107       1.22   gehenna dev_type_read(kdread);
    108       1.22   gehenna dev_type_write(kdwrite);
    109       1.22   gehenna dev_type_ioctl(kdioctl);
    110       1.22   gehenna dev_type_tty(kdtty);
    111       1.22   gehenna dev_type_poll(kdpoll);
    112       1.22   gehenna 
    113       1.22   gehenna const struct cdevsw kd_cdevsw = {
    114       1.22   gehenna 	kdopen, kdclose, kdread, kdwrite, kdioctl,
    115       1.23  jdolecek 	nostop, kdtty, kdpoll, nommap, ttykqfilter, D_TTY
    116       1.22   gehenna };
    117       1.22   gehenna 
    118        1.1       gwr /*
    119        1.7        pk  * Prepare the console tty; called on first open of /dev/console
    120        1.1       gwr  */
    121       1.36       uwe static void
    122       1.36       uwe kd_init(struct kd_softc *kd)
    123        1.1       gwr {
    124        1.1       gwr 	struct tty *tp;
    125        1.1       gwr 
    126        1.1       gwr 	tp = ttymalloc();
    127       1.46     joerg 	callout_setfunc(&tp->t_rstrt_ch, kd_later, tp);
    128        1.1       gwr 	tp->t_oproc = kdstart;
    129        1.1       gwr 	tp->t_param = kdparam;
    130       1.22   gehenna 	tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
    131        1.1       gwr 
    132        1.1       gwr 	tty_attach(tp);
    133        1.1       gwr 	kd->kd_tty = tp;
    134        1.1       gwr 
    135        1.1       gwr 	/*
    136        1.7        pk 	 * Get the console struct winsize.
    137        1.1       gwr 	 */
    138       1.19        pk #if defined(RASTERCONSOLE) && NFB > 0
    139        1.7        pk 	/* If the raster console driver is attached, copy its size */
    140        1.7        pk 	kd->rows = fbrcons_rows();
    141        1.7        pk 	kd->cols = fbrcons_cols();
    142        1.8        pk 	rcons_ttyinit(tp);
    143        1.1       gwr #endif
    144        1.1       gwr 
    145        1.7        pk 	/* else, consult the PROM */
    146        1.7        pk 	switch (prom_version()) {
    147       1.31        pk 	char prop[6+1];		/* Enough for six digits */
    148        1.7        pk 	struct eeprom *ep;
    149        1.7        pk 	case PROM_OLDMON:
    150        1.7        pk 		if ((ep = (struct eeprom *)eeprom_va) == NULL)
    151        1.7        pk 			break;
    152        1.7        pk 		if (kd->rows == 0)
    153        1.7        pk 			kd->rows = (u_short)ep->eeTtyRows;
    154        1.7        pk 		if (kd->cols == 0)
    155        1.7        pk 			kd->cols = (u_short)ep->eeTtyCols;
    156        1.7        pk 		break;
    157       1.31        pk 
    158        1.7        pk 	case PROM_OBP_V0:
    159        1.7        pk 	case PROM_OBP_V2:
    160        1.7        pk 	case PROM_OBP_V3:
    161        1.7        pk 	case PROM_OPENFIRM:
    162        1.1       gwr 		if (kd->rows == 0 &&
    163       1.31        pk 		    prom_getoption("screen-#rows", prop, sizeof prop) == 0)
    164       1.31        pk 			kd->rows = strtoul(prop, NULL, 10);
    165        1.7        pk 
    166        1.1       gwr 		if (kd->cols == 0 &&
    167       1.31        pk 		    prom_getoption("screen-#columns", prop, sizeof prop) == 0)
    168       1.31        pk 			kd->cols = strtoul(prop, NULL, 10);
    169        1.7        pk 
    170        1.7        pk 		break;
    171        1.1       gwr 	}
    172        1.1       gwr 
    173        1.1       gwr 	return;
    174        1.1       gwr }
    175        1.1       gwr 
    176        1.1       gwr struct tty *
    177       1.36       uwe kdtty(dev_t dev)
    178        1.1       gwr {
    179        1.1       gwr 	struct kd_softc *kd;
    180        1.1       gwr 
    181        1.1       gwr 	kd = &kd_softc; 	/* XXX */
    182        1.1       gwr 	return (kd->kd_tty);
    183        1.1       gwr }
    184        1.1       gwr 
    185        1.1       gwr int
    186       1.38  christos kdopen(dev_t dev, int flag, int mode, struct lwp *l)
    187        1.1       gwr {
    188        1.1       gwr 	struct kd_softc *kd;
    189        1.1       gwr 	int error, s, unit;
    190        1.1       gwr 	struct tty *tp;
    191        1.7        pk static	int firstopen = 1;
    192        1.7        pk 
    193        1.1       gwr 	unit = minor(dev);
    194        1.1       gwr 	if (unit != 0)
    195        1.1       gwr 		return ENXIO;
    196        1.7        pk 
    197        1.1       gwr 	kd = &kd_softc; 	/* XXX */
    198        1.7        pk 	if (firstopen) {
    199        1.7        pk 		kd_init(kd);
    200        1.7        pk 		firstopen = 0;
    201        1.7        pk 	}
    202        1.7        pk 
    203        1.1       gwr 	tp = kd->kd_tty;
    204        1.1       gwr 
    205        1.1       gwr 	/* It's simpler to do this up here. */
    206       1.41      elad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    207        1.1       gwr 		return (EBUSY);
    208        1.1       gwr 
    209        1.1       gwr 	s = spltty();
    210        1.1       gwr 	if ((tp->t_state & TS_ISOPEN) == 0) {
    211        1.1       gwr 		/* 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.7        pk 			return (error);
    218        1.7        pk 		}
    219        1.7        pk 
    220        1.1       gwr 		ttychars(tp);
    221        1.1       gwr 		tp->t_iflag = TTYDEF_IFLAG;
    222        1.1       gwr 		tp->t_oflag = TTYDEF_OFLAG;
    223        1.1       gwr 		tp->t_cflag = TTYDEF_CFLAG;
    224        1.1       gwr 		tp->t_lflag = TTYDEF_LFLAG;
    225        1.1       gwr 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    226        1.1       gwr 		(void) kdparam(tp, &tp->t_termios);
    227        1.1       gwr 		ttsetwater(tp);
    228        1.1       gwr 		tp->t_winsize.ws_row = kd->rows;
    229        1.1       gwr 		tp->t_winsize.ws_col = kd->cols;
    230        1.1       gwr 		/* Flush pending input?  Clear translator? */
    231        1.1       gwr 		/* This (pseudo)device always has SOFTCAR */
    232        1.1       gwr 		tp->t_state |= TS_CARR_ON;
    233        1.1       gwr 	}
    234        1.1       gwr 
    235        1.1       gwr 	splx(s);
    236        1.1       gwr 
    237       1.16       eeh 	return ((*tp->t_linesw->l_open)(dev, tp));
    238        1.1       gwr }
    239        1.1       gwr 
    240        1.1       gwr int
    241       1.38  christos kdclose(dev_t dev, int flag, int mode, struct lwp *l)
    242        1.1       gwr {
    243        1.1       gwr 	struct kd_softc *kd;
    244        1.1       gwr 	struct tty *tp;
    245        1.7        pk 	struct cons_channel *cc;
    246        1.1       gwr 
    247        1.1       gwr 	kd = &kd_softc; 	/* XXX */
    248        1.1       gwr 	tp = kd->kd_tty;
    249        1.1       gwr 
    250        1.1       gwr 	/* XXX This is for cons.c. */
    251        1.1       gwr 	if ((tp->t_state & TS_ISOPEN) == 0)
    252        1.1       gwr 		return 0;
    253        1.1       gwr 
    254       1.16       eeh 	(*tp->t_linesw->l_close)(tp, flag);
    255        1.1       gwr 	ttyclose(tp);
    256        1.7        pk 
    257        1.7        pk 	if ((cc = kd->kd_in) != NULL)
    258       1.13        pk 		(void)(*cc->cc_iclose)(cc);
    259        1.7        pk 
    260        1.1       gwr 	return (0);
    261        1.1       gwr }
    262        1.1       gwr 
    263        1.1       gwr int
    264       1.36       uwe kdread(dev_t dev, struct uio *uio, int flag)
    265        1.1       gwr {
    266        1.1       gwr 	struct kd_softc *kd;
    267        1.1       gwr 	struct tty *tp;
    268        1.1       gwr 
    269        1.1       gwr 	kd = &kd_softc; 	/* XXX */
    270        1.1       gwr 	tp = kd->kd_tty;
    271        1.1       gwr 
    272       1.16       eeh 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    273        1.1       gwr }
    274        1.1       gwr 
    275        1.1       gwr int
    276       1.36       uwe kdwrite(dev_t dev, struct uio *uio, int flag)
    277        1.1       gwr {
    278        1.1       gwr 	struct kd_softc *kd;
    279        1.1       gwr 	struct tty *tp;
    280        1.1       gwr 
    281        1.1       gwr 	kd = &kd_softc; 	/* XXX */
    282        1.1       gwr 	tp = kd->kd_tty;
    283        1.1       gwr 
    284       1.16       eeh 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    285       1.17       scw }
    286       1.17       scw 
    287       1.17       scw int
    288       1.38  christos kdpoll(dev_t dev, int events, struct lwp *l)
    289       1.17       scw {
    290       1.17       scw 	struct kd_softc *kd;
    291       1.17       scw 	struct tty *tp;
    292       1.17       scw 
    293       1.17       scw 	kd = &kd_softc; 	/* XXX */
    294       1.17       scw 	tp = kd->kd_tty;
    295       1.37       uwe 
    296       1.38  christos 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    297        1.1       gwr }
    298        1.1       gwr 
    299        1.1       gwr int
    300       1.43  christos kdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    301        1.1       gwr {
    302        1.1       gwr 	struct kd_softc *kd;
    303        1.1       gwr 	struct tty *tp;
    304        1.1       gwr 	int error;
    305        1.1       gwr 
    306        1.1       gwr 	kd = &kd_softc; 	/* XXX */
    307        1.1       gwr 	tp = kd->kd_tty;
    308        1.1       gwr 
    309       1.38  christos 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    310       1.21    atatat 	if (error != EPASSTHROUGH)
    311        1.1       gwr 		return error;
    312       1.21    atatat 
    313       1.38  christos 	error = ttioctl(tp, cmd, data, flag, l);
    314       1.21    atatat 	if (error != EPASSTHROUGH)
    315        1.1       gwr 		return error;
    316        1.1       gwr 
    317        1.1       gwr 	/* Handle any ioctl commands specific to kbd/display. */
    318        1.1       gwr 	/* XXX - Send KB* ioctls to kbd module? */
    319        1.1       gwr 	/* XXX - Send FB* ioctls to fb module?  */
    320        1.1       gwr 
    321       1.21    atatat 	return EPASSTHROUGH;
    322        1.1       gwr }
    323        1.1       gwr 
    324        1.1       gwr static int
    325       1.36       uwe kdparam(struct tty *tp, struct termios *t)
    326        1.1       gwr {
    327       1.36       uwe 
    328        1.1       gwr 	/* XXX - These are ignored... */
    329        1.1       gwr 	tp->t_ispeed = t->c_ispeed;
    330        1.1       gwr 	tp->t_ospeed = t->c_ospeed;
    331        1.1       gwr 	tp->t_cflag = t->c_cflag;
    332        1.1       gwr 	return 0;
    333        1.1       gwr }
    334        1.1       gwr 
    335        1.1       gwr static void
    336       1.36       uwe kdstart(struct tty *tp)
    337        1.1       gwr {
    338        1.1       gwr 	struct clist *cl;
    339       1.42        ad 	int s1, s2;
    340        1.1       gwr 
    341       1.42        ad 	s1 = splsoftclock();
    342       1.42        ad 	s2 = spltty();
    343        1.1       gwr 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
    344        1.1       gwr 		goto out;
    345        1.1       gwr 
    346        1.1       gwr 	cl = &tp->t_outq;
    347       1.47        ad 	if (ttypull(tp)) {
    348        1.7        pk 		tp->t_state |= TS_BUSY;
    349       1.42        ad 		if ((s1 & PSR_PIL) == 0) {
    350        1.7        pk 			/* called at level zero - update screen now. */
    351       1.42        ad 			splx(s2);
    352        1.7        pk 			kd_putfb(tp);
    353       1.42        ad 			s2 = spltty();
    354        1.7        pk 			tp->t_state &= ~TS_BUSY;
    355        1.1       gwr 		} else {
    356        1.7        pk 			/* called at interrupt level - do it later */
    357       1.46     joerg 			callout_schedule(&tp->t_rstrt_ch, 0);
    358        1.1       gwr 		}
    359        1.1       gwr 	}
    360        1.1       gwr out:
    361       1.42        ad 	splx(s2);
    362       1.42        ad 	splx(s1);
    363        1.1       gwr }
    364        1.1       gwr 
    365        1.1       gwr /*
    366        1.1       gwr  * Timeout function to do delayed writes to the screen.
    367        1.1       gwr  * Called at splsoftclock when requested by kdstart.
    368        1.1       gwr  */
    369        1.1       gwr static void
    370       1.36       uwe kd_later(void *arg)
    371        1.1       gwr {
    372        1.7        pk 	struct tty *tp = arg;
    373        1.7        pk 	int s;
    374        1.1       gwr 
    375        1.1       gwr 	kd_putfb(tp);
    376        1.1       gwr 
    377        1.1       gwr 	s = spltty();
    378        1.1       gwr 	tp->t_state &= ~TS_BUSY;
    379       1.16       eeh 	(*tp->t_linesw->l_start)(tp);
    380        1.1       gwr 	splx(s);
    381        1.1       gwr }
    382        1.1       gwr 
    383        1.1       gwr /*
    384        1.1       gwr  * Put text on the screen using the PROM monitor.
    385        1.1       gwr  * This can take a while, so to avoid missing
    386        1.1       gwr  * interrupts, this is called at splsoftclock.
    387        1.1       gwr  */
    388        1.1       gwr static void
    389       1.36       uwe kd_putfb(struct tty *tp)
    390        1.1       gwr {
    391        1.1       gwr 	char buf[PUT_WSIZE];
    392        1.1       gwr 	struct clist *cl = &tp->t_outq;
    393        1.1       gwr 	char *p, *end;
    394        1.1       gwr 	int len;
    395        1.1       gwr 
    396        1.1       gwr 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
    397        1.1       gwr 		/* PROM will barf if high bits are set. */
    398        1.1       gwr 		p = buf;
    399        1.1       gwr 		end = buf + len;
    400        1.1       gwr 		while (p < end)
    401        1.1       gwr 			*p++ &= 0x7f;
    402        1.4        pk 
    403        1.1       gwr 		/* Now let the PROM print it. */
    404        1.4        pk 		prom_putstr(buf, len);
    405        1.1       gwr 	}
    406        1.1       gwr }
    407        1.1       gwr 
    408        1.1       gwr /*
    409        1.1       gwr  * Our "interrupt" routine for input. This is called by
    410        1.1       gwr  * the keyboard driver (dev/sun/kbd.c) at spltty.
    411        1.1       gwr  */
    412       1.36       uwe static void
    413       1.36       uwe kd_cons_input(int c)
    414        1.1       gwr {
    415        1.1       gwr 	struct kd_softc *kd = &kd_softc;
    416        1.1       gwr 	struct tty *tp;
    417        1.1       gwr 
    418        1.1       gwr 	/* XXX: Make sure the device is open. */
    419        1.1       gwr 	tp = kd->kd_tty;
    420        1.1       gwr 	if (tp == NULL)
    421        1.1       gwr 		return;
    422        1.1       gwr 	if ((tp->t_state & TS_ISOPEN) == 0)
    423        1.1       gwr 		return;
    424        1.1       gwr 
    425       1.16       eeh 	(*tp->t_linesw->l_rint)(c, tp);
    426        1.1       gwr }
    427        1.1       gwr 
    428        1.7        pk void
    429       1.36       uwe cons_attach_input(struct cons_channel *cc, struct consdev *cn)
    430        1.7        pk {
    431        1.7        pk 	struct kd_softc *kd = &kd_softc;
    432        1.7        pk 
    433        1.7        pk 	kd->kd_in = cc;
    434        1.7        pk 	cc->cc_upstream = kd_cons_input;
    435        1.7        pk }
    436        1.7        pk 
    437       1.15  christos void
    438       1.36       uwe kd_attach_input(struct cons_channel *cc)
    439       1.15  christos {
    440       1.15  christos 	struct kd_softc *kd = &kd_softc;
    441       1.15  christos 
    442       1.15  christos 	kd->kd_in = cc;
    443       1.15  christos 	cc->cc_upstream = kd_cons_input;
    444       1.15  christos }
    445        1.7        pk 
    446        1.7        pk /*
    447        1.7        pk  * Default PROM-based console input stream
    448        1.7        pk  * Since the PROM does not notify us when data is available on the
    449        1.7        pk  * input channel these functions periodically poll the PROM.
    450        1.7        pk  */
    451       1.29       uwe static int kd_rom_iopen(struct cons_channel *);
    452       1.29       uwe static int kd_rom_iclose(struct cons_channel *);
    453       1.29       uwe static void kd_rom_intr(void *);
    454        1.7        pk 
    455       1.30       uwe static struct cons_channel prom_cons_channel = {
    456       1.30       uwe 	NULL,			/* no private data */
    457       1.30       uwe 	kd_rom_iopen,
    458       1.30       uwe 	kd_rom_iclose,
    459       1.30       uwe 	NULL			/* will be set by kd driver */
    460       1.30       uwe };
    461       1.30       uwe 
    462       1.44        ad static struct callout prom_cons_callout;
    463        1.7        pk 
    464       1.36       uwe static int
    465       1.36       uwe kd_rom_iopen(struct cons_channel *cc)
    466        1.7        pk {
    467       1.44        ad 	static bool callo;
    468       1.44        ad 
    469       1.44        ad 	if (!callo) {
    470       1.44        ad 		callout_init(&prom_cons_callout, 0);
    471       1.44        ad 		callo = true;
    472       1.44        ad 	}
    473       1.30       uwe 
    474        1.7        pk 	/* Poll for ROM input 4 times per second */
    475       1.30       uwe 	callout_reset(&prom_cons_callout, hz / 4, kd_rom_intr, cc);
    476        1.7        pk 	return (0);
    477        1.7        pk }
    478        1.7        pk 
    479       1.36       uwe static int
    480       1.36       uwe kd_rom_iclose(struct cons_channel *cc)
    481        1.7        pk {
    482        1.7        pk 
    483       1.30       uwe 	callout_stop(&prom_cons_callout);
    484        1.7        pk 	return (0);
    485        1.7        pk }
    486        1.7        pk 
    487        1.7        pk /*
    488        1.7        pk  * "Interrupt" routine for input through ROM vectors
    489        1.7        pk  */
    490       1.36       uwe static void
    491       1.36       uwe kd_rom_intr(void *arg)
    492        1.7        pk {
    493        1.7        pk 	struct cons_channel *cc = arg;
    494        1.7        pk 	int s, c;
    495        1.7        pk 
    496       1.30       uwe 	callout_schedule(&prom_cons_callout, hz / 4);
    497        1.7        pk 
    498        1.7        pk 	s = spltty();
    499        1.7        pk 
    500        1.7        pk 	while ((c = prom_peekchar()) >= 0)
    501        1.7        pk 		(*cc->cc_upstream)(c);
    502        1.7        pk 
    503        1.7        pk 	splx(s);
    504        1.7        pk }
    505        1.1       gwr 
    506        1.7        pk /*****************************************************************/
    507        1.7        pk 
    508        1.7        pk int prom_stdin_node;
    509        1.7        pk int prom_stdout_node;
    510        1.7        pk char prom_stdin_args[16];
    511        1.7        pk char prom_stdout_args[16];
    512        1.7        pk 
    513       1.36       uwe static void prom_cnprobe(struct consdev *);
    514       1.29       uwe static void prom_cninit(struct consdev *);
    515       1.36       uwe int  prom_cngetc(dev_t);	/* XXX: for sunkbd_wskbd_cngetc */
    516       1.29       uwe static void prom_cnputc(dev_t, int);
    517       1.36       uwe static void prom_cnpollc(dev_t, int);
    518        1.7        pk 
    519        1.7        pk /*
    520        1.7        pk  * The console is set to this one initially,
    521        1.7        pk  * which lets us use the PROM until consinit()
    522        1.7        pk  * is called to select a real console.
    523        1.7        pk  */
    524        1.7        pk struct consdev consdev_prom = {
    525        1.7        pk 	prom_cnprobe,
    526        1.7        pk 	prom_cninit,
    527        1.7        pk 	prom_cngetc,
    528        1.7        pk 	prom_cnputc,
    529        1.7        pk 	prom_cnpollc,
    530        1.6   thorpej 	NULL,
    531        1.1       gwr };
    532        1.1       gwr 
    533        1.7        pk /*
    534        1.7        pk  * The console table pointer is statically initialized
    535        1.7        pk  * to point to the PROM table, so that early calls to printf will work.
    536        1.7        pk  */
    537        1.7        pk struct consdev *cn_tab = &consdev_prom;
    538        1.7        pk 
    539       1.36       uwe static void
    540       1.36       uwe prom_cnprobe(struct consdev *cn)
    541        1.1       gwr {
    542        1.1       gwr }
    543        1.1       gwr 
    544        1.1       gwr static void
    545       1.36       uwe prom_cninit(struct consdev *cn)
    546        1.1       gwr {
    547        1.7        pk }
    548        1.1       gwr 
    549       1.36       uwe static void
    550       1.36       uwe prom_cnpollc(dev_t dev, int on)
    551        1.7        pk {
    552        1.1       gwr 
    553        1.7        pk 	if (on) {
    554        1.7        pk 		/* Entering debugger. */
    555        1.7        pk #if NFB > 0
    556        1.7        pk 		fb_unblank();
    557        1.7        pk #endif
    558        1.7        pk 	} else {
    559        1.7        pk 		/* Resuming kernel. */
    560        1.7        pk 	}
    561        1.7        pk }
    562        1.1       gwr 
    563        1.1       gwr 
    564        1.7        pk /*
    565        1.7        pk  * PROM console input putchar.
    566        1.7        pk  */
    567       1.34  macallan int
    568       1.36       uwe prom_cngetc(dev_t dev)
    569        1.1       gwr {
    570        1.7        pk 	int s, c;
    571        1.1       gwr 
    572        1.7        pk 	s = splhigh();
    573        1.7        pk 	c = prom_getchar();
    574        1.7        pk 	splx(s);
    575        1.7        pk 	return (c);
    576        1.1       gwr }
    577        1.1       gwr 
    578        1.7        pk /*
    579        1.7        pk  * PROM console output putchar.
    580        1.7        pk  */
    581        1.1       gwr static void
    582       1.36       uwe prom_cnputc(dev_t dev, int c)
    583        1.1       gwr {
    584        1.1       gwr 
    585        1.4        pk 	prom_putchar(c);
    586        1.1       gwr }
    587        1.1       gwr 
    588        1.7        pk 
    589        1.7        pk /*****************************************************************/
    590        1.7        pk 
    591       1.29       uwe static void prom_get_device_args(const char *, char *, unsigned int);
    592        1.7        pk 
    593       1.36       uwe static void
    594       1.36       uwe prom_get_device_args(const char *prop, char *args, unsigned int sz)
    595        1.1       gwr {
    596       1.33    martin 	const char *cp;
    597       1.33    martin 	char buffer[128];
    598        1.7        pk 
    599       1.33    martin 	cp = prom_getpropstringA(findroot(), prop, buffer, sizeof buffer);
    600        1.1       gwr 
    601        1.7        pk 	/*
    602        1.7        pk 	 * Extract device-specific arguments from a PROM device path (if any)
    603        1.7        pk 	 */
    604        1.7        pk 	cp = buffer + strlen(buffer);
    605        1.7        pk 	while (cp >= buffer) {
    606        1.7        pk 		if (*cp == ':') {
    607        1.7        pk 			strncpy(args, cp+1, sz);
    608        1.7        pk 			break;
    609        1.7        pk 		}
    610        1.7        pk 		cp--;
    611        1.1       gwr 	}
    612        1.1       gwr }
    613        1.1       gwr 
    614        1.7        pk /*
    615        1.7        pk  *
    616        1.7        pk  */
    617        1.7        pk void
    618       1.36       uwe consinit(void)
    619        1.7        pk {
    620        1.7        pk 	int inSource, outSink;
    621        1.7        pk 
    622        1.7        pk 	switch (prom_version()) {
    623        1.7        pk 	case PROM_OLDMON:
    624        1.7        pk 	case PROM_OBP_V0:
    625        1.7        pk 		/* The stdio handles identify the device type */
    626        1.7        pk 		inSource = prom_stdin();
    627        1.7        pk 		outSink  = prom_stdout();
    628        1.7        pk 		break;
    629        1.7        pk 
    630        1.7        pk 	case PROM_OBP_V2:
    631        1.7        pk 	case PROM_OBP_V3:
    632        1.7        pk 	case PROM_OPENFIRM:
    633        1.7        pk 
    634        1.7        pk 		/* Save PROM arguments for device matching */
    635        1.7        pk 		prom_get_device_args("stdin-path", prom_stdin_args,
    636        1.7        pk 				     sizeof(prom_stdin_args));
    637        1.7        pk 		prom_get_device_args("stdout-path", prom_stdout_args,
    638        1.7        pk 				    sizeof(prom_stdout_args));
    639        1.7        pk 
    640        1.7        pk 		/*
    641        1.7        pk 		 * Translate the STDIO package instance (`ihandle') -- that
    642        1.7        pk 		 * the PROM has already opened for us -- to a device tree
    643       1.37       uwe 		 * node (i.e. a `phandle').
    644        1.7        pk 		 */
    645        1.7        pk 
    646        1.7        pk 		prom_stdin_node = prom_instance_to_package(prom_stdin());
    647        1.7        pk 		if (prom_stdin_node == 0)
    648        1.7        pk 			printf("consinit: cannot convert stdin ihandle\n");
    649        1.7        pk 
    650        1.7        pk 		prom_stdout_node = prom_instance_to_package(prom_stdout());
    651        1.7        pk 		if (prom_stdout_node == 0) {
    652        1.7        pk 			printf("consinit: cannot convert stdout ihandle\n");
    653        1.7        pk 			break;
    654        1.7        pk 		}
    655        1.7        pk 
    656        1.7        pk 		break;
    657        1.7        pk 
    658        1.7        pk 	default:
    659        1.7        pk 		break;
    660        1.7        pk 	}
    661        1.7        pk 
    662        1.7        pk 	/* Wire up /dev/console */
    663       1.22   gehenna 	cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
    664        1.7        pk 	cn_tab->cn_pri = CN_INTERNAL;
    665        1.7        pk 
    666        1.7        pk 	/* Set up initial PROM input channel for /dev/console */
    667       1.12       eeh 	cons_attach_input(&prom_cons_channel, cn_tab);
    668        1.7        pk 
    669        1.7        pk #ifdef	KGDB
    670        1.7        pk 	zs_kgdb_init();	/* XXX */
    671        1.7        pk #endif
    672        1.7        pk }
    673