#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: z8530tty.c,v 1.135 2022/10/26 23:45:25 riastradh Exp $");
#include "opt_kgdb.h"
#include "opt_ntp.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <sys/timepps.h>
#include <sys/tty.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/syslog.h>
#include <sys/kauth.h>
#include <dev/ic/z8530reg.h>
#include <machine/z8530var.h>
#include <dev/cons.h>
#include "ioconf.h"
#include "locators.h"
#ifndef ZSTTY_RING_SIZE
#define ZSTTY_RING_SIZE 2048
#endif
static struct cnm_state zstty_cnm_state;
u_int zstty_rbuf_size = ZSTTY_RING_SIZE;
u_int zstty_rbuf_hiwat = (ZSTTY_RING_SIZE * 1) / 4;
u_int zstty_rbuf_lowat = (ZSTTY_RING_SIZE * 3) / 4;
struct zstty_softc {
device_t zst_dev;
struct tty *zst_tty;
struct zs_chanstate *zst_cs;
struct callout zst_diag_ch;
u_int zst_overflows,
zst_floods,
zst_errors;
int zst_hwflags,
zst_swflags;
u_int zst_r_hiwat,
zst_r_lowat;
uint8_t *volatile zst_rbget,
*volatile zst_rbput;
volatile u_int zst_rbavail;
uint8_t *zst_rbuf,
*zst_ebuf;
uint8_t *zst_tba;
u_int zst_tbc,
zst_heldtbc;
volatile uint8_t zst_rx_flags,
#define RX_TTY_BLOCKED 0x01
#define RX_TTY_OVERFLOWED 0x02
#define RX_IBUF_BLOCKED 0x04
#define RX_IBUF_OVERFLOWED 0x08
#define RX_ANY_BLOCK 0x0f
zst_tx_busy,
zst_tx_done,
zst_tx_stopped,
zst_st_check,
zst_rx_ready;
uint8_t zst_ppsmask;
struct pps_state zst_pps_state;
};
static int zstty_match(device_t, cfdata_t, void *);
static void zstty_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(zstty, sizeof(struct zstty_softc),
zstty_match, zstty_attach, NULL, NULL);
dev_type_open(zsopen);
dev_type_close(zsclose);
dev_type_read(zsread);
dev_type_write(zswrite);
dev_type_ioctl(zsioctl);
dev_type_stop(zsstop);
dev_type_tty(zstty);
dev_type_poll(zspoll);
const struct cdevsw zstty_cdevsw = {
.d_open = zsopen,
.d_close = zsclose,
.d_read = zsread,
.d_write = zswrite,
.d_ioctl = zsioctl,
.d_stop = zsstop,
.d_tty = zstty,
.d_poll = zspoll,
.d_mmap = nommap,
.d_kqfilter = ttykqfilter,
.d_discard = nodiscard,
.d_flag = D_TTY
};
struct zsops zsops_tty;
static void zs_shutdown(struct zstty_softc *);
static void zsstart(struct tty *);
static int zsparam(struct tty *, struct termios *);
static void zs_modem(struct zstty_softc *, int);
static void tiocm_to_zs(struct zstty_softc *, u_long, int);
static int zs_to_tiocm(struct zstty_softc *);
static int zshwiflow(struct tty *, int);
static void zs_hwiflow(struct zstty_softc *);
static void zs_maskintr(struct zstty_softc *);
static void zstty_rxint (struct zs_chanstate *);
static void zstty_stint (struct zs_chanstate *, int);
static void zstty_txint (struct zs_chanstate *);
static void zstty_softint(struct zs_chanstate *);
static void zstty_softint1(struct zs_chanstate *);
#define ZSUNIT(x) TTUNIT(x)
#define ZSDIALOUT(x) TTDIALOUT(x)
struct tty *zstty_get_tty_from_dev(device_t);
struct tty *
zstty_get_tty_from_dev(device_t dev)
{
struct zstty_softc *sc = device_private(dev);
return sc->zst_tty;
}
int
zstty_match(device_t parent, cfdata_t cf, void *aux)
{
struct zsc_attach_args *args = aux;
if (cf->zsccf_channel == args->channel)
return 2;
if (cf->zsccf_channel == ZSCCF_CHANNEL_DEFAULT)
return 1;
return 0;
}
void
zstty_attach(device_t parent, device_t self, void *aux)
{
struct zstty_softc *zst = device_private(self);
struct zsc_softc *zsc = device_private(parent);
cfdata_t cf = device_cfdata(self);
struct zsc_attach_args *args = aux;
struct zs_chanstate *cs;
struct tty *tp;
int channel, tty_unit;
dev_t dev;
const char *i, *o;
int dtr_on;
int resetbit;
zst->zst_dev = self;
callout_init(&zst->zst_diag_ch, 0);
cn_init_magic(&zstty_cnm_state);
tty_unit = device_unit(self);
channel = args->channel;
cs = zsc->zsc_cs[channel];
cs->cs_private = zst;
cs->cs_ops = &zsops_tty;
zst->zst_cs = cs;
zst->zst_swflags = cf->cf_flags;
zst->zst_hwflags = args->hwflags;
dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), tty_unit);
if (zst->zst_swflags)
aprint_normal(" flags 0x%x", zst->zst_swflags);
i = o = NULL;
if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
i = "input";
if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
args->consdev->cn_dev = dev;
cn_tab->cn_pollc = args->consdev->cn_pollc;
cn_tab->cn_getc = args->consdev->cn_getc;
}
cn_tab->cn_dev = dev;
cn_set_magic("\047\001");
}
if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
o = "output";
if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
cn_tab->cn_putc = args->consdev->cn_putc;
}
cn_tab->cn_dev = dev;
}
if (i != NULL || o != NULL)
aprint_normal(" (console %s)", i ? (o ? "i/o" : i) : o);
#ifdef KGDB
if (zs_check_kgdb(cs, dev)) {
aprint_normal(" (kgdb)\n");
return;
}
#endif
aprint_normal("\n");
tp = tty_alloc();
tp->t_dev = dev;
tp->t_oproc = zsstart;
tp->t_param = zsparam;
tp->t_hwiflow = zshwiflow;
tty_attach(tp);
zst->zst_tty = tp;
zst->zst_rbuf = malloc(zstty_rbuf_size << 1, M_DEVBUF, M_WAITOK);
zst->zst_ebuf = zst->zst_rbuf + (zstty_rbuf_size << 1);
zst->zst_r_hiwat = 0;
zst->zst_r_lowat = 0;
zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
zst->zst_rbavail = zstty_rbuf_size;
if (!cs->enable)
cs->enabled = 1;
dtr_on = 0;
resetbit = 0;
if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
struct termios t;
DELAY(10000);
t.c_ispeed = 0;
t.c_ospeed = cs->cs_defspeed;
t.c_cflag = cs->cs_defcflag;
SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
tp->t_ospeed = 0;
(void) zsparam(tp, &t);
dtr_on = 1;
} else if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_NORESET)) {
resetbit = (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET;
}
mutex_spin_enter(&cs->cs_lock);
if (resetbit)
zs_write_reg(cs, 9, resetbit);
zs_modem(zst, dtr_on);
mutex_spin_exit(&cs->cs_lock);
}
struct tty *
zstty(dev_t dev)
{
struct zstty_softc *zst;
zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
return (zst->zst_tty);
}
void
zs_shutdown(struct zstty_softc *zst)
{
struct zs_chanstate *cs = zst->zst_cs;
struct tty *tp = zst->zst_tty;
mutex_spin_enter(&cs->cs_lock);
SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
zs_hwiflow(zst);
zs_break(cs, 0);
if (ISSET(tp->t_cflag, HUPCL)) {
zs_modem(zst, 0);
mutex_spin_exit(&cs->cs_lock);
(void) tsleep(cs, TTIPRI, ttclos, hz);
if (ISSET(tp->t_state, TS_ISOPEN) || tp->t_wopen != 0)
return;
mutex_spin_enter(&cs->cs_lock);
}
if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
CLR(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
cs->cs_creg[1] = cs->cs_preg[1];
zs_write_reg(cs, 1, cs->cs_creg[1]);
}
if (cs->disable) {
#ifdef DIAGNOSTIC
if (!cs->enabled)
panic("%s: not enabled?", __func__);
#endif
(*cs->disable)(zst->zst_cs);
}
mutex_spin_exit(&cs->cs_lock);
}
int
zsopen(dev_t dev, int flags, int mode, struct lwp *l)
{
struct zstty_softc *zst;
struct zs_chanstate *cs;
struct tty *tp;
int error;
zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
if (zst == NULL)
return (ENXIO);
tp = zst->zst_tty;
cs = zst->zst_cs;
if (tp == NULL)
return (EBUSY);
if (ISSET(tp->t_state, TS_KERN_ONLY))
return (EBUSY);
if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
return (EBUSY);
ttylock(tp);
if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
struct termios t;
tp->t_dev = dev;
if (cs->enable) {
if ((*cs->enable)(cs)) {
ttyunlock(tp);
printf("%s: device enable failed\n",
device_xname(zst->zst_dev));
return (EIO);
}
}
t.c_ispeed = 0;
t.c_ospeed = cs->cs_defspeed;
t.c_cflag = cs->cs_defcflag;
if (ISSET(zst->zst_swflags, TIOCFLAG_CLOCAL))
SET(t.c_cflag, CLOCAL);
if (ISSET(zst->zst_swflags, TIOCFLAG_CRTSCTS))
SET(t.c_cflag, CRTSCTS);
if (ISSET(zst->zst_swflags, TIOCFLAG_CDTRCTS))
SET(t.c_cflag, CDTRCTS);
if (ISSET(zst->zst_swflags, TIOCFLAG_MDMBUF))
SET(t.c_cflag, MDMBUF);
mutex_spin_enter(&cs->cs_lock);
SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
mutex_spin_enter(&timecounter_lock);
zst->zst_ppsmask = 0;
memset(&zst->zst_pps_state, 0, sizeof(zst->zst_pps_state));
zst->zst_pps_state.ppscap =
PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
pps_init(&zst->zst_pps_state);
mutex_spin_exit(&timecounter_lock);
mutex_spin_exit(&cs->cs_lock);
tp->t_ospeed = 0;
ttyunlock(tp);
(void) zsparam(tp, &t);
ttylock(tp);
if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) {
tp->t_iflag = TTYDEF_IFLAG;
tp->t_oflag = TTYDEF_OFLAG;
tp->t_lflag = TTYDEF_LFLAG;
} else {
tp->t_iflag = 0;
tp->t_oflag = 0;
tp->t_lflag = 0;
}
ttychars(tp);
ttsetwater(tp);
mutex_spin_enter(&cs->cs_lock);
zs_modem(zst, 1);
zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
zst->zst_rbavail = zstty_rbuf_size;
zs_iflush(cs);
CLR(zst->zst_rx_flags, RX_ANY_BLOCK);
zs_hwiflow(zst);
mutex_spin_exit(&cs->cs_lock);
}
ttyunlock(tp);
error = ttyopen(tp, ZSDIALOUT(dev), ISSET(flags, O_NONBLOCK));
if (error)
goto bad;
error = (*tp->t_linesw->l_open)(dev, tp);
if (error)
goto bad;
return (0);
bad:
if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
zs_shutdown(zst);
}
return (error);
}
int
zsclose(dev_t dev, int flags, int mode, struct lwp *l)
{
struct zstty_softc *zst;
struct tty *tp;
zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
tp = zst->zst_tty;
if (!ISSET(tp->t_state, TS_ISOPEN))
return 0;
(*tp->t_linesw->l_close)(tp, flags);
ttyclose(tp);
if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
zs_shutdown(zst);
}
return (0);
}
int
zsread(dev_t dev, struct uio *uio, int flags)
{
struct zstty_softc *zst;
struct tty *tp;
zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
tp = zst->zst_tty;
return ((*tp->t_linesw->l_read)(tp, uio, flags));
}
int
zswrite(dev_t dev, struct uio *uio, int flags)
{
struct zstty_softc *zst;
struct tty *tp;
zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
tp = zst->zst_tty;
return ((*tp->t_linesw->l_write)(tp, uio, flags));
}
int
zspoll(dev_t dev, int events, struct lwp *l)
{
struct zstty_softc *zst;
struct tty *tp;
zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
tp = zst->zst_tty;
return ((*tp->t_linesw->l_poll)(tp, events, l));
}
int
zsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
struct zstty_softc *zst;
struct zs_chanstate *cs;
struct tty *tp;
int error;
zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
cs = zst->zst_cs;
tp = zst->zst_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);
#ifdef ZS_MD_IOCTL
error = ZS_MD_IOCTL(cs, cmd, data);
if (error != EPASSTHROUGH)
return (error);
#endif
error = 0;
mutex_spin_enter(&cs->cs_lock);
switch (cmd) {
case TIOCSBRK:
zs_break(cs, 1);
break;
case TIOCCBRK:
zs_break(cs, 0);
break;
case TIOCGFLAGS:
*(int *)data = zst->zst_swflags;
break;
case TIOCSFLAGS:
error = kauth_authorize_device_tty(l->l_cred,
KAUTH_DEVICE_TTY_PRIVSET, tp);
if (error)
break;
zst->zst_swflags = *(int *)data;
break;
case TIOCSDTR:
zs_modem(zst, 1);
break;
case TIOCCDTR:
zs_modem(zst, 0);
break;
case TIOCMSET:
case TIOCMBIS:
case TIOCMBIC:
tiocm_to_zs(zst, cmd, *(int *)data);
break;
case TIOCMGET:
*(int *)data = zs_to_tiocm(zst);
break;
case PPS_IOC_CREATE:
case PPS_IOC_DESTROY:
case PPS_IOC_GETPARAMS:
case PPS_IOC_SETPARAMS:
case PPS_IOC_GETCAP:
case PPS_IOC_FETCH:
#ifdef PPS_SYNC
case PPS_IOC_KCBIND:
#endif
mutex_spin_enter(&timecounter_lock);
error = pps_ioctl(cmd, data, &zst->zst_pps_state);
if (zst->zst_pps_state.ppsparam.mode & PPS_CAPTUREBOTH)
zst->zst_ppsmask = ZSRR0_DCD;
else
zst->zst_ppsmask = 0;
mutex_spin_exit(&timecounter_lock);
break;
case TIOCDCDTIMESTAMP:
if (cs->cs_rr0_pps == 0) {
error = EINVAL;
break;
}
mutex_spin_enter(&timecounter_lock);
#ifndef PPS_TRAILING_EDGE
TIMESPEC_TO_TIMEVAL((struct timeval *)data,
&zst->zst_pps_state.ppsinfo.assert_timestamp);
#else
TIMESPEC_TO_TIMEVAL((struct timeval *)data,
&zst->zst_pps_state.ppsinfo.clear_timestamp);
#endif
mutex_spin_exit(&timecounter_lock);
zs_maskintr(zst);
if (!cs->cs_heldchange) {
if (zst->zst_tx_busy) {
zst->zst_heldtbc = zst->zst_tbc;
zst->zst_tbc = 0;
cs->cs_heldchange = 1;
} else
zs_loadchannelregs(cs);
}
break;
default:
error = EPASSTHROUGH;
break;
}
mutex_spin_exit(&cs->cs_lock);
return (error);
}
static void
zsstart(struct tty *tp)
{
struct zstty_softc *zst;
struct zs_chanstate *cs;
u_char *tba;
int tbc;
zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
cs = zst->zst_cs;
if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
return;
if (zst->zst_tx_stopped)
return;
if (!ttypull(tp))
return;
tba = tp->t_outq.c_cf;
tbc = ndqb(&tp->t_outq, 0);
mutex_spin_enter(&cs->cs_lock);
zst->zst_tba = tba;
zst->zst_tbc = tbc;
SET(tp->t_state, TS_BUSY);
zst->zst_tx_busy = 1;
#ifdef ZS_TXDMA
if (zst->zst_tbc > 1) {
zs_dma_setup(cs, zst->zst_tba, zst->zst_tbc);
mutex_spin_exit(&cs->cs_lock);
return;
}
#endif
zs_write_data(cs, *zst->zst_tba);
zst->zst_tbc--;
zst->zst_tba++;
mutex_spin_exit(&cs->cs_lock);
}
void
zsstop(struct tty *tp, int flag)
{
struct zstty_softc *zst;
zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
mutex_spin_enter(&zst->zst_cs->cs_lock);
if (ISSET(tp->t_state, TS_BUSY)) {
zst->zst_tbc = 0;
zst->zst_heldtbc = 0;
if (!ISSET(tp->t_state, TS_TTSTOP))
SET(tp->t_state, TS_FLUSH);
}
mutex_spin_exit(&zst->zst_cs->cs_lock);
}
static int
zsparam(struct tty *tp, struct termios *t)
{
struct zstty_softc *zst;
struct zs_chanstate *cs;
int ospeed;
tcflag_t cflag;
uint8_t tmp3, tmp4, tmp5;
int error;
zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
cs = zst->zst_cs;
ospeed = t->c_ospeed;
cflag = t->c_cflag;
if (ospeed < 0)
return (EINVAL);
if (t->c_ispeed && t->c_ispeed != ospeed)
return (EINVAL);
if (ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR) ||
ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
SET(cflag, CLOCAL);
CLR(cflag, HUPCL);
}
if (tp->t_ospeed == ospeed &&
tp->t_cflag == cflag)
return (0);
error = zs_set_speed(cs, ospeed);
if (error)
return (error);
error = zs_set_modes(cs, cflag);
if (error)
return (error);
mutex_spin_enter(&cs->cs_lock);
zs_maskintr(zst);
tmp3 = cs->cs_preg[3];
tmp5 = cs->cs_preg[5];
CLR(tmp3, ZSWR3_RXSIZE);
CLR(tmp5, ZSWR5_TXSIZE);
switch (ISSET(cflag, CSIZE)) {
case CS5:
SET(tmp3, ZSWR3_RX_5);
SET(tmp5, ZSWR5_TX_5);
break;
case CS6:
SET(tmp3, ZSWR3_RX_6);
SET(tmp5, ZSWR5_TX_6);
break;
case CS7:
SET(tmp3, ZSWR3_RX_7);
SET(tmp5, ZSWR5_TX_7);
break;
case CS8:
SET(tmp3, ZSWR3_RX_8);
SET(tmp5, ZSWR5_TX_8);
break;
}
cs->cs_preg[3] = tmp3;
cs->cs_preg[5] = tmp5;
tmp4 = cs->cs_preg[4];
CLR(tmp4, ZSWR4_SBMASK | ZSWR4_PARMASK);
if (ISSET(cflag, CSTOPB))
SET(tmp4, ZSWR4_TWOSB);
else
SET(tmp4, ZSWR4_ONESB);
if (!ISSET(cflag, PARODD))
SET(tmp4, ZSWR4_EVENP);
if (ISSET(cflag, PARENB))
SET(tmp4, ZSWR4_PARENB);
cs->cs_preg[4] = tmp4;
tp->t_ispeed = 0;
tp->t_ospeed = ospeed;
tp->t_cflag = cflag;
if (!cs->cs_heldchange) {
if (zst->zst_tx_busy) {
zst->zst_heldtbc = zst->zst_tbc;
zst->zst_tbc = 0;
cs->cs_heldchange = 1;
} else
zs_loadchannelregs(cs);
}
if (!ISSET(cflag, CHWFLOW)) {
zst->zst_r_hiwat = 0;
zst->zst_r_lowat = 0;
if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
zst->zst_rx_ready = 1;
cs->cs_softreq = 1;
}
if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
zs_hwiflow(zst);
}
} else {
zst->zst_r_hiwat = zstty_rbuf_hiwat;
zst->zst_r_lowat = zstty_rbuf_lowat;
}
zstty_stint(cs, 1);
mutex_spin_exit(&cs->cs_lock);
if (!ISSET(cflag, CHWFLOW)) {
if (zst->zst_tx_stopped) {
zst->zst_tx_stopped = 0;
zsstart(tp);
}
}
zstty_softint1(cs);
return (0);
}
static void
zs_maskintr(struct zstty_softc *zst)
{
struct zs_chanstate *cs = zst->zst_cs;
uint8_t tmp15;
cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
if (zst->zst_ppsmask != 0)
cs->cs_rr0_mask |= cs->cs_rr0_pps;
tmp15 = cs->cs_preg[15];
if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD))
SET(tmp15, ZSWR15_DCD_IE);
else
CLR(tmp15, ZSWR15_DCD_IE);
if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS))
SET(tmp15, ZSWR15_CTS_IE);
else
CLR(tmp15, ZSWR15_CTS_IE);
cs->cs_preg[15] = tmp15;
}
static void
zs_modem(struct zstty_softc *zst, int onoff)
{
struct zs_chanstate *cs = zst->zst_cs, *ccs;
if (cs->cs_wr5_dtr == 0)
return;
ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
if (onoff)
SET(ccs->cs_preg[5], cs->cs_wr5_dtr);
else
CLR(ccs->cs_preg[5], cs->cs_wr5_dtr);
if (!cs->cs_heldchange) {
if (zst->zst_tx_busy) {
zst->zst_heldtbc = zst->zst_tbc;
zst->zst_tbc = 0;
cs->cs_heldchange = 1;
} else
zs_loadchannelregs(cs);
}
}
static void
tiocm_to_zs(struct zstty_softc *zst, u_long how, int ttybits)
{
struct zs_chanstate *cs = zst->zst_cs, *ccs;
uint8_t zsbits;
ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
zsbits = 0;
if (ISSET(ttybits, TIOCM_DTR))
SET(zsbits, ZSWR5_DTR);
if (ISSET(ttybits, TIOCM_RTS))
SET(zsbits, ZSWR5_RTS);
switch (how) {
case TIOCMBIC:
CLR(ccs->cs_preg[5], zsbits);
break;
case TIOCMBIS:
SET(ccs->cs_preg[5], zsbits);
break;
case TIOCMSET:
CLR(ccs->cs_preg[5], ZSWR5_RTS | ZSWR5_DTR);
SET(ccs->cs_preg[5], zsbits);
break;
}
if (!cs->cs_heldchange) {
if (zst->zst_tx_busy) {
zst->zst_heldtbc = zst->zst_tbc;
zst->zst_tbc = 0;
cs->cs_heldchange = 1;
} else
zs_loadchannelregs(cs);
}
}
static int
zs_to_tiocm(struct zstty_softc *zst)
{
struct zs_chanstate *cs = zst->zst_cs, *ccs;
uint8_t zsbits;
int ttybits = 0;
ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
zsbits = ccs->cs_preg[5];
if (ISSET(zsbits, ZSWR5_DTR))
SET(ttybits, TIOCM_DTR);
if (ISSET(zsbits, ZSWR5_RTS))
SET(ttybits, TIOCM_RTS);
zsbits = cs->cs_rr0;
if (ISSET(zsbits, ZSRR0_DCD))
SET(ttybits, TIOCM_CD);
if (ISSET(zsbits, ZSRR0_CTS))
SET(ttybits, TIOCM_CTS);
return (ttybits);
}
int
zshwiflow(struct tty *tp, int block)
{
struct zstty_softc *zst;
struct zs_chanstate *cs;
zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
cs = zst->zst_cs;
if (cs->cs_wr5_rts == 0)
return (0);
mutex_spin_enter(&cs->cs_lock);
if (block) {
if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
SET(zst->zst_rx_flags, RX_TTY_BLOCKED);
zs_hwiflow(zst);
}
} else {
if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
zst->zst_rx_ready = 1;
cs->cs_softreq = 1;
}
if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
CLR(zst->zst_rx_flags, RX_TTY_BLOCKED);
zs_hwiflow(zst);
}
}
mutex_spin_exit(&cs->cs_lock);
return (1);
}
static void
zs_hwiflow(struct zstty_softc *zst)
{
struct zs_chanstate *cs = zst->zst_cs, *ccs;
if (cs->cs_wr5_rts == 0)
return;
ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) {
CLR(ccs->cs_preg[5], cs->cs_wr5_rts);
CLR(ccs->cs_creg[5], cs->cs_wr5_rts);
} else {
SET(ccs->cs_preg[5], cs->cs_wr5_rts);
SET(ccs->cs_creg[5], cs->cs_wr5_rts);
}
zs_write_reg(ccs, 5, ccs->cs_creg[5]);
}
#define integrate static inline
integrate void zstty_rxsoft(struct zstty_softc *, struct tty *);
integrate void zstty_txsoft(struct zstty_softc *, struct tty *);
integrate void zstty_stsoft(struct zstty_softc *, struct tty *);
static void zstty_diag(void *);
static void
zstty_rxint(struct zs_chanstate *cs)
{
struct zstty_softc *zst = cs->cs_private;
uint8_t *put, *end;
u_int cc;
uint8_t rr0, rr1, c;
end = zst->zst_ebuf;
put = zst->zst_rbput;
cc = zst->zst_rbavail;
while (cc > 0) {
rr1 = zs_read_reg(cs, 1);
c = zs_read_data(cs);
if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
zs_write_csr(cs, ZSWR0_RESET_ERRORS);
}
cn_check_magic(zst->zst_tty->t_dev, c, zstty_cnm_state);
put[0] = c;
put[1] = rr1;
put += 2;
if (put >= end)
put = zst->zst_rbuf;
cc--;
rr0 = zs_read_csr(cs);
if (!ISSET(rr0, ZSRR0_RX_READY))
break;
}
zst->zst_rbput = put;
zst->zst_rbavail = cc;
if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
zst->zst_rx_ready = 1;
cs->cs_softreq = 1;
}
if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) &&
cc < zst->zst_r_hiwat) {
SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
zs_hwiflow(zst);
}
if (!cc) {
SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
CLR(cs->cs_preg[1], ZSWR1_RIE);
cs->cs_creg[1] = cs->cs_preg[1];
zs_write_reg(cs, 1, cs->cs_creg[1]);
}
#if 0
printf("%xH%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
#endif
}
static void
zstty_txint(struct zs_chanstate *cs)
{
struct zstty_softc *zst = cs->cs_private;
zs_write_csr(cs, ZSWR0_RESET_TXINT);
if (cs->cs_heldchange) {
zs_loadchannelregs(cs);
cs->cs_heldchange = 0;
zst->zst_tbc = zst->zst_heldtbc;
zst->zst_heldtbc = 0;
}
if (zst->zst_tbc > 0) {
zs_write_data(cs, *zst->zst_tba);
zst->zst_tbc--;
zst->zst_tba++;
} else {
if (zst->zst_tx_busy) {
zst->zst_tx_busy = 0;
zst->zst_tx_done = 1;
cs->cs_softreq = 1;
}
}
}
static void
zstty_stint(struct zs_chanstate *cs, int force)
{
struct zstty_softc *zst = cs->cs_private;
uint8_t rr0, delta;
rr0 = zs_read_csr(cs);
zs_write_csr(cs, ZSWR0_RESET_STATUS);
if (ISSET(rr0, ZSRR0_BREAK))
cn_check_magic(zst->zst_tty->t_dev, CNC_BREAK, zstty_cnm_state);
if (!force)
delta = rr0 ^ cs->cs_rr0;
else
delta = cs->cs_rr0_mask;
cs->cs_rr0 = rr0;
if (ISSET(delta, cs->cs_rr0_mask)) {
SET(cs->cs_rr0_delta, delta);
if (ISSET(delta, zst->zst_ppsmask)) {
if (zst->zst_pps_state.ppsparam.mode &
PPS_CAPTUREBOTH) {
mutex_spin_enter(&timecounter_lock);
pps_capture(&zst->zst_pps_state);
pps_event(&zst->zst_pps_state,
(ISSET(cs->cs_rr0, zst->zst_ppsmask))
? PPS_CAPTUREASSERT
: PPS_CAPTURECLEAR);
mutex_spin_exit(&timecounter_lock);
}
}
if (ISSET(~rr0, cs->cs_rr0_mask)) {
zst->zst_tbc = 0;
zst->zst_heldtbc = 0;
}
zst->zst_st_check = 1;
cs->cs_softreq = 1;
}
}
void
zstty_diag(void *arg)
{
struct zstty_softc *zst = arg;
int overflows, floods;
mutex_spin_enter(&zst->zst_cs->cs_lock);
overflows = zst->zst_overflows;
zst->zst_overflows = 0;
floods = zst->zst_floods;
zst->zst_floods = 0;
zst->zst_errors = 0;
mutex_spin_exit(&zst->zst_cs->cs_lock);
log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
device_xname(zst->zst_dev),
overflows, overflows == 1 ? "" : "s",
floods, floods == 1 ? "" : "s");
}
integrate void
zstty_rxsoft(struct zstty_softc *zst, struct tty *tp)
{
struct zs_chanstate *cs = zst->zst_cs;
int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
uint8_t *get, *end;
u_int cc, scc;
uint8_t rr1;
int code;
end = zst->zst_ebuf;
get = zst->zst_rbget;
scc = cc = zstty_rbuf_size - zst->zst_rbavail;
if (cc == zstty_rbuf_size) {
zst->zst_floods++;
if (zst->zst_errors++ == 0)
callout_reset(&zst->zst_diag_ch, 60 * hz,
zstty_diag, zst);
}
if (!ISSET(tp->t_state, TS_ISOPEN)) {
get += cc << 1;
if (get >= end)
get -= zstty_rbuf_size << 1;
cc = 0;
}
while (cc) {
code = get[0];
rr1 = get[1];
if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
if (ISSET(rr1, ZSRR1_DO)) {
zst->zst_overflows++;
if (zst->zst_errors++ == 0)
callout_reset(&zst->zst_diag_ch,
60 * hz, zstty_diag, zst);
}
if (ISSET(rr1, ZSRR1_FE))
SET(code, TTY_FE);
if (ISSET(rr1, ZSRR1_PE))
SET(code, TTY_PE);
}
if ((*rint)(code, tp) == -1) {
if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
get += cc << 1;
if (get >= end)
get -= zstty_rbuf_size << 1;
cc = 0;
} else {
SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
}
break;
}
get += 2;
if (get >= end)
get = zst->zst_rbuf;
cc--;
}
if (cc != scc) {
zst->zst_rbget = get;
mutex_spin_enter(&cs->cs_lock);
cc = zst->zst_rbavail += scc - cc;
if (cc >= zst->zst_r_lowat) {
if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
SET(cs->cs_preg[1], ZSWR1_RIE);
cs->cs_creg[1] = cs->cs_preg[1];
zs_write_reg(cs, 1, cs->cs_creg[1]);
}
if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
zs_hwiflow(zst);
}
}
mutex_spin_exit(&cs->cs_lock);
}
#if 0
printf("%xS%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
#endif
}
integrate void
zstty_txsoft(struct zstty_softc *zst, struct tty *tp)
{
struct zs_chanstate *cs = zst->zst_cs;
mutex_spin_enter(&cs->cs_lock);
CLR(tp->t_state, TS_BUSY);
if (ISSET(tp->t_state, TS_FLUSH))
CLR(tp->t_state, TS_FLUSH);
else
ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf));
mutex_spin_exit(&cs->cs_lock);
(*tp->t_linesw->l_start)(tp);
}
integrate void
zstty_stsoft(struct zstty_softc *zst, struct tty *tp)
{
struct zs_chanstate *cs = zst->zst_cs;
uint8_t rr0, delta;
mutex_spin_enter(&cs->cs_lock);
rr0 = cs->cs_rr0;
delta = cs->cs_rr0_delta;
cs->cs_rr0_delta = 0;
mutex_spin_exit(&cs->cs_lock);
if (ISSET(delta, cs->cs_rr0_dcd)) {
(void) (*tp->t_linesw->l_modem)(tp, ISSET(rr0, ZSRR0_DCD));
}
if (ISSET(delta, cs->cs_rr0_cts)) {
if (ISSET(rr0, cs->cs_rr0_cts)) {
zst->zst_tx_stopped = 0;
(*tp->t_linesw->l_start)(tp);
} else {
zst->zst_tx_stopped = 1;
}
}
}
static void
zstty_softint(struct zs_chanstate *cs)
{
zstty_softint1(cs);
}
static void
zstty_softint1(struct zs_chanstate *cs)
{
struct zstty_softc *zst = cs->cs_private;
struct tty *tp = zst->zst_tty;
if (zst->zst_rx_ready) {
zst->zst_rx_ready = 0;
zstty_rxsoft(zst, tp);
}
if (zst->zst_st_check) {
zst->zst_st_check = 0;
zstty_stsoft(zst, tp);
}
if (zst->zst_tx_done) {
zst->zst_tx_done = 0;
zstty_txsoft(zst, tp);
}
}
struct zsops zsops_tty = {
zstty_rxint,
zstty_stint,
zstty_txint,
zstty_softint,
};
#ifdef ZS_TXDMA
void
zstty_txdma_int(void *arg)
{
struct zs_chanstate *cs = arg;
struct zstty_softc *zst = cs->cs_private;
zst->zst_tba += zst->zst_tbc;
zst->zst_tbc = 0;
if (zst->zst_tx_busy) {
zst->zst_tx_busy = 0;
zst->zst_tx_done = 1;
cs->cs_softreq = 1;
}
}
#endif