/*-
* Copyright (c) 1996 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Gordon W. Ross.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Keyboard/Display device.
*
* This driver exists simply to provide a tty device that
* the indirect console driver can point to.
* The kbd driver sends its input here.
* Output goes to the screen via PROM printf.
*/
/*
* There is no point in pretending there might be
* more than one keyboard/display device.
*/
static struct kd_softc kd_softc;
static int kd_is_console;
cl = &tp->t_outq;
if (cl->c_cc) {
if (kd_is_console) {
tp->t_state |= TS_BUSY;
if (s1 == 0) {
/* called at level zero - update screen now. */
splx(s2);
kd_putfb(tp);
s2 = spltty();
tp->t_state &= ~TS_BUSY;
} else {
/* called at interrupt level - do it later */
callout_schedule(&tp->t_rstrt_ch, 0);
}
} else {
/*
* This driver uses the PROM for writing the screen,
* and that only works if this is the console device.
* If this is not the console, just flush the output.
* Sorry. (In that case, use xdm instead of getty.)
*/
ndflush(cl, cl->c_cc);
}
}
ttypull(tp);
out:
splx(s2);
splx(s1);
}
/*
* Timeout function to do delayed writes to the screen.
* Called at splsoftclock when requested by kdstart.
*/
static void
kd_later(void *tpaddr)
{
struct tty *tp = tpaddr;
register int s;
kd_putfb(tp);
s = spltty();
tp->t_state &= ~TS_BUSY;
(*tp->t_linesw->l_start)(tp);
splx(s);
}
/*
* Put text on the screen using the PROM monitor.
* This can take a while, so to avoid missing
* interrupts, this is called at splsoftclock.
*/
static void
kd_putfb(struct tty *tp)
{
char buf[PUT_WSIZE];
struct clist *cl = &tp->t_outq;
char *p, *end;
int len;
while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
/* PROM will barf if high bits are set. */
p = buf;
end = buf + len;
while (p < end)
*p++ &= 0x7f;
/* Now let the PROM print it. */
prom_write(prom_stdout(), buf, len);
}
}
/*
* Default PROM-based console input stream
*/
static int kd_rom_iopen(struct cons_channel *);
static int kd_rom_iclose(struct cons_channel *);
static struct cons_channel prom_cons_channel;
int
kd_rom_iopen(struct cons_channel *cc)
{
return (0);
}
int
kd_rom_iclose(struct cons_channel *cc)
{
return (0);
}
/*
* Our "interrupt" routine for input. This is called by
* the keyboard driver (dev/sun/kbd.c) at spltty.
*/
void
kd_cons_input(int c)
{
struct kd_softc *kd = &kd_softc;
struct tty *tp;
/* XXX: Make sure the device is open. */
tp = kd->kd_tty;
if (tp == NULL)
return;
if ((tp->t_state & TS_ISOPEN) == 0)
return;
(*tp->t_linesw->l_rint)(c, tp);
}
/****************************************************************
* kd console support
****************************************************************/
/* The debugger gets its own key translation state. */
static struct kbd_state *kdcn_state;
/* This prepares kbd_translate() */
ks->kbd_id = KBD_MIN_TYPE;
kbd_xlate_init(ks);
/* Set up initial PROM input channel for /dev/console */
prom_cons_channel.cc_private = NULL;
prom_cons_channel.cc_iopen = kd_rom_iopen;
prom_cons_channel.cc_iclose = kd_rom_iclose;
cons_attach_input(&prom_cons_channel);
/* Indicate that it is OK to use the PROM fbwrite */
kd_is_console = 1;
#endif
}
static int
kdcngetc(dev_t dev)
{
struct kbd_state *ks = kdcn_state;
int code, class, data, keysym;
extern int prom_cngetc(dev_t);
if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev);
for (;;) {
code = (*cn_hw->cn_getc)(dev);
keysym = kbd_code_to_keysym(ks, code);
class = KEYSYM_CLASS(keysym);
switch (class) {
case KEYSYM_ASCII:
goto out;
case KEYSYM_CLRMOD:
case KEYSYM_SETMOD:
data = (keysym & 0x1F);
/* Only allow ctrl or shift. */
if (data > KBMOD_SHIFT_R)
break;
data = 1 << data;
if (class == KEYSYM_SETMOD)
ks->kbd_modbits |= data;
else
ks->kbd_modbits &= ~data;
break;
case KEYSYM_ALL_UP:
/* No toggle keys here. */
ks->kbd_modbits = 0;
break;
default: /* ignore all other keysyms */
break;
}
}
out:
return (keysym);
}
static void
kdcnputc(dev_t dev, int c)
{
int s;
char c0 = (c & 0x7f);
s = splhigh();
prom_write(prom_stdout(), &c0, 1);
splx(s);
}