#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kd.c,v 1.54 2014/07/25 08:10:34 dholland Exp $");
#include "opt_kgdb.h"
#include "fb.h"
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/file.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/kauth.h>
#include <machine/bsd_openprom.h>
#include <machine/promlib.h>
#include <machine/eeprom.h>
#include <machine/psl.h>
#include <machine/cpu.h>
#include <machine/kbd.h>
#include <machine/autoconf.h>
#if defined(RASTERCONSOLE) && NFB > 0
#include <dev/sun/fbio.h>
#include <dev/sun/fbvar.h>
#endif
#include <dev/cons.h>
#include <sparc/dev/cons.h>
#include <dev/sun/event_var.h>
#include <dev/sun/kbd_xlate.h>
#include <dev/sun/kbdvar.h>
#define PUT_WSIZE 64
struct kd_softc {
struct tty *kd_tty;
int rows, cols;
struct cons_channel *kd_in;
};
static struct kd_softc kd_softc;
void kd_attach_input(struct cons_channel *);
static void kd_init(struct kd_softc *);
static void kdstart(struct tty *);
static void kd_later(void *);
static void kd_putfb(struct tty *);
static int kdparam(struct tty *, struct termios *);
static void kd_cons_input(int);
dev_type_open(kdopen);
dev_type_close(kdclose);
dev_type_read(kdread);
dev_type_write(kdwrite);
dev_type_ioctl(kdioctl);
dev_type_tty(kdtty);
dev_type_poll(kdpoll);
const struct cdevsw kd_cdevsw = {
.d_open = kdopen,
.d_close = kdclose,
.d_read = kdread,
.d_write = kdwrite,
.d_ioctl = kdioctl,
.d_stop = nostop,
.d_tty = kdtty,
.d_poll = kdpoll,
.d_mmap = nommap,
.d_kqfilter = ttykqfilter,
.d_discard = nodiscard,
.d_flag = D_TTY
};
static void
kd_init(struct kd_softc *kd)
{
struct tty *tp;
tp = tty_alloc();
callout_setfunc(&tp->t_rstrt_ch, kd_later, tp);
tp->t_oproc = kdstart;
tp->t_param = kdparam;
tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
tty_attach(tp);
kd->kd_tty = tp;
#if defined(RASTERCONSOLE) && NFB > 0
kd->rows = fbrcons_rows();
kd->cols = fbrcons_cols();
rcons_ttyinit(tp);
#endif
switch (prom_version()) {
char prop[6+1];
struct eeprom *ep;
case PROM_OLDMON:
if ((ep = (struct eeprom *)eeprom_va) == NULL)
break;
if (kd->rows == 0)
kd->rows = (u_short)ep->eeTtyRows;
if (kd->cols == 0)
kd->cols = (u_short)ep->eeTtyCols;
break;
case PROM_OBP_V0:
case PROM_OBP_V2:
case PROM_OBP_V3:
case PROM_OPENFIRM:
if (kd->rows == 0 &&
prom_getoption("screen-#rows", prop, sizeof prop) == 0)
kd->rows = strtoul(prop, NULL, 10);
if (kd->cols == 0 &&
prom_getoption("screen-#columns", prop, sizeof prop) == 0)
kd->cols = strtoul(prop, NULL, 10);
break;
}
return;
}
struct tty *
kdtty(dev_t dev)
{
struct kd_softc *kd;
kd = &kd_softc;
return (kd->kd_tty);
}
int
kdopen(dev_t dev, int flag, int mode, struct lwp *l)
{
struct kd_softc *kd;
int error, s, unit;
struct tty *tp;
static int firstopen = 1;
unit = minor(dev);
if (unit != 0)
return ENXIO;
kd = &kd_softc;
if (firstopen) {
kd_init(kd);
firstopen = 0;
}
tp = kd->kd_tty;
if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
return (EBUSY);
s = spltty();
if ((tp->t_state & TS_ISOPEN) == 0) {
struct cons_channel *cc = kd->kd_in;
if (cc != NULL &&
(error = (*cc->cc_iopen)(cc)) != 0) {
return (error);
}
ttychars(tp);
tp->t_iflag = TTYDEF_IFLAG;
tp->t_oflag = TTYDEF_OFLAG;
tp->t_cflag = TTYDEF_CFLAG;
tp->t_lflag = TTYDEF_LFLAG;
tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
(void) kdparam(tp, &tp->t_termios);
ttsetwater(tp);
tp->t_winsize.ws_row = kd->rows;
tp->t_winsize.ws_col = kd->cols;
tp->t_state |= TS_CARR_ON;
}
splx(s);
return ((*tp->t_linesw->l_open)(dev, tp));
}
int
kdclose(dev_t dev, int flag, int mode, struct lwp *l)
{
struct kd_softc *kd;
struct tty *tp;
struct cons_channel *cc;
kd = &kd_softc;
tp = kd->kd_tty;
if ((tp->t_state & TS_ISOPEN) == 0)
return 0;
(*tp->t_linesw->l_close)(tp, flag);
ttyclose(tp);
if ((cc = kd->kd_in) != NULL)
(void)(*cc->cc_iclose)(cc);
return (0);
}
int
kdread(dev_t dev, struct uio *uio, int flag)
{
struct kd_softc *kd;
struct tty *tp;
kd = &kd_softc;
tp = kd->kd_tty;
return ((*tp->t_linesw->l_read)(tp, uio, flag));
}
int
kdwrite(dev_t dev, struct uio *uio, int flag)
{
struct kd_softc *kd;
struct tty *tp;
kd = &kd_softc;
tp = kd->kd_tty;
return ((*tp->t_linesw->l_write)(tp, uio, flag));
}
int
kdpoll(dev_t dev, int events, struct lwp *l)
{
struct kd_softc *kd;
struct tty *tp;
kd = &kd_softc;
tp = kd->kd_tty;
return ((*tp->t_linesw->l_poll)(tp, events, l));
}
int
kdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
struct kd_softc *kd;
struct tty *tp;
int error;
kd = &kd_softc;
tp = kd->kd_tty;
error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
if (error != EPASSTHROUGH)
return error;
error = ttioctl(tp, cmd, data, flag, l);
if (error != EPASSTHROUGH)
return error;
return EPASSTHROUGH;
}
static int
kdparam(struct tty *tp, struct termios *t)
{
tp->t_ispeed = t->c_ispeed;
tp->t_ospeed = t->c_ospeed;
tp->t_cflag = t->c_cflag;
return 0;
}
static void
kdstart(struct tty *tp)
{
int s1, s2;
s1 = splsoftclock();
s2 = spltty();
if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
goto out;
if (ttypull(tp)) {
tp->t_state |= TS_BUSY;
if ((s1 & PSR_PIL) == 0) {
splx(s2);
kd_putfb(tp);
s2 = spltty();
tp->t_state &= ~TS_BUSY;
} else {
callout_schedule(&tp->t_rstrt_ch, 0);
}
}
out:
splx(s2);
splx(s1);
}
static void
kd_later(void *arg)
{
struct tty *tp = arg;
int s;
kd_putfb(tp);
s = spltty();
tp->t_state &= ~TS_BUSY;
(*tp->t_linesw->l_start)(tp);
splx(s);
}
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) {
p = buf;
end = buf + len;
while (p < end)
*p++ &= 0x7f;
prom_putstr(buf, len);
}
}
static void
kd_cons_input(int c)
{
struct kd_softc *kd = &kd_softc;
struct tty *tp;
tp = kd->kd_tty;
if (tp == NULL)
return;
if ((tp->t_state & TS_ISOPEN) == 0)
return;
(*tp->t_linesw->l_rint)(c, tp);
}
void
cons_attach_input(struct cons_channel *cc, struct consdev *cn)
{
struct kd_softc *kd = &kd_softc;
kd->kd_in = cc;
cc->cc_upstream = kd_cons_input;
}
void
kd_attach_input(struct cons_channel *cc)
{
struct kd_softc *kd = &kd_softc;
kd->kd_in = cc;
cc->cc_upstream = kd_cons_input;
}
static int kd_rom_iopen(struct cons_channel *);
static int kd_rom_iclose(struct cons_channel *);
static void kd_rom_intr(void *);
static struct cons_channel prom_cons_channel = {
NULL,
kd_rom_iopen,
kd_rom_iclose,
NULL
};
static struct callout prom_cons_callout;
static int
kd_rom_iopen(struct cons_channel *cc)
{
static bool callo;
if (!callo) {
callout_init(&prom_cons_callout, 0);
callo = true;
}
callout_reset(&prom_cons_callout, hz / 4, kd_rom_intr, cc);
return (0);
}
static int
kd_rom_iclose(struct cons_channel *cc)
{
callout_stop(&prom_cons_callout);
return (0);
}
static void
kd_rom_intr(void *arg)
{
struct cons_channel *cc = arg;
int s, c;
callout_schedule(&prom_cons_callout, hz / 4);
s = spltty();
while ((c = prom_peekchar()) >= 0)
(*cc->cc_upstream)(c);
splx(s);
}
int prom_stdin_node;
int prom_stdout_node;
char prom_stdin_args[16];
char prom_stdout_args[16];
static void prom_cnprobe(struct consdev *);
static void prom_cninit(struct consdev *);
int prom_cngetc(dev_t);
static void prom_cnputc(dev_t, int);
static void prom_cnpollc(dev_t, int);
struct consdev consdev_prom = {
prom_cnprobe,
prom_cninit,
prom_cngetc,
prom_cnputc,
prom_cnpollc,
NULL,
};
#if 0
struct consdev *cn_tab = &consdev_prom;
#endif
static void
prom_cnprobe(struct consdev *cn)
{
}
static void
prom_cninit(struct consdev *cn)
{
}
static void
prom_cnpollc(dev_t dev, int on)
{
if (on) {
#if NFB > 0
fb_unblank();
#endif
} else {
}
}
int
prom_cngetc(dev_t dev)
{
int s, c;
s = splhigh();
c = prom_getchar();
splx(s);
return (c);
}
static void
prom_cnputc(dev_t dev, int c)
{
prom_putchar(c);
}
static void prom_get_device_args(const char *, char *, unsigned int);
static void
prom_get_device_args(const char *prop, char *args, unsigned int sz)
{
const char *cp;
char buffer[128];
cp = prom_getpropstringA(findroot(), prop, buffer, sizeof buffer);
cp = buffer + strlen(buffer);
while (cp >= buffer) {
if (*cp == ':') {
strncpy(args, cp+1, sz);
break;
}
cp--;
}
}
void
consinit(void)
{
#if 0
int inSource, outSink;
#endif
switch (prom_version()) {
#if 0
case PROM_OLDMON:
case PROM_OBP_V0:
inSource = prom_stdin();
outSink = prom_stdout();
break;
#endif
case PROM_OBP_V2:
case PROM_OBP_V3:
case PROM_OPENFIRM:
prom_get_device_args("stdin-path", prom_stdin_args,
sizeof(prom_stdin_args));
prom_get_device_args("stdout-path", prom_stdout_args,
sizeof(prom_stdout_args));
prom_stdin_node = prom_instance_to_package(prom_stdin());
if (prom_stdin_node == 0)
printf("consinit: cannot convert stdin ihandle\n");
prom_stdout_node = prom_instance_to_package(prom_stdout());
if (prom_stdout_node == 0) {
printf("consinit: cannot convert stdout ihandle\n");
break;
}
break;
default:
break;
}
cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
cn_tab->cn_pri = CN_INTERNAL;
cons_attach_input(&prom_cons_channel, cn_tab);
#ifdef KGDB
zs_kgdb_init();
#endif
}