#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: cd18xx.c,v 1.34 2025/12/16 18:24:47 andvar Exp $");
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/tty.h>
#include <sys/fcntl.h>
#include <sys/kauth.h>
#include <sys/intr.h>
#include <sys/bus.h>
#include <dev/ic/cd18xxvar.h>
#include <dev/ic/cd18xxreg.h>
#include "ioconf.h"
static void cdtty_attach(struct cd18xx_softc *, int);
static inline void cd18xx_rint(struct cd18xx_softc *, int *);
static inline void cd18xx_tint(struct cd18xx_softc *, int *);
static inline void cd18xx_mint(struct cd18xx_softc *, int *);
void cdtty_rxsoft(struct cd18xx_softc *, struct cdtty_port *, struct tty *);
void cdtty_txsoft(struct cd18xx_softc *, struct cdtty_port *, struct tty *);
void cdtty_stsoft(struct cd18xx_softc *, struct cdtty_port *, struct tty *);
void cd18xx_softintr(void *);
dev_type_open(cdttyopen);
dev_type_close(cdttyclose);
dev_type_read(cdttyread);
dev_type_write(cdttywrite);
dev_type_ioctl(cdttyioctl);
dev_type_stop(cdttystop);
dev_type_tty(cdttytty);
dev_type_poll(cdttypoll);
const struct cdevsw cdtty_cdevsw = {
.d_open = cdttyopen,
.d_close = cdttyclose,
.d_read = cdttyread,
.d_write = cdttywrite,
.d_ioctl = cdttyioctl,
.d_stop = cdttystop,
.d_tty = cdttytty,
.d_poll = cdttypoll,
.d_mmap = nommap,
.d_kqfilter = ttykqfilter,
.d_discard = nodiscard,
.d_flag = D_TTY
};
static void cdtty_shutdown(struct cd18xx_softc *, struct cdtty_port *);
static void cdttystart(struct tty *);
static int cdttyparam(struct tty *, struct termios *);
static void cdtty_break(struct cd18xx_softc *, struct cdtty_port *, int);
static void cdtty_modem(struct cd18xx_softc *, struct cdtty_port *, int);
static int cdttyhwiflow(struct tty *, int);
static void cdtty_hwiflow(struct cd18xx_softc *, struct cdtty_port *);
static void cdtty_loadchannelregs(struct cd18xx_softc *,
struct cdtty_port *);
u_int cdtty_rbuf_size = CDTTY_RING_SIZE;
u_int cdtty_rbuf_hiwat = (CDTTY_RING_SIZE * 1) / 4;
u_int cdtty_rbuf_lowat = (CDTTY_RING_SIZE * 3) / 4;
#define CD18XXDEBUG
#ifdef CD18XXDEBUG
#define CDD_INFO 0x0001
#define CDD_INTR 0x0002
int cd18xx_debug = CDD_INTR|CDD_INFO;
# define DPRINTF(l, x) if (cd18xx_debug & l) printf x
#else
# define DPRINTF(l, x)
#endif
struct cd18xx_revs {
u_char revision;
u_char onehundred_pin;
const char *name;
} cd18xx_revs[] = {
{ CD180_GFRCR_REV_B, 0, "CL-CD180 rev. B" },
{ CD180_GFRCR_REV_C, 0, "CL-CD180 rev. C" },
{ CD1864_GFRCR_REVISION_A, 1, "CL-CD1864 rev. A" },
{ CD1865_GFRCR_REVISION_A, 1, "CL-CD1865 rev. A" },
{ CD1865_GFRCR_REVISION_B, 1, "CL-CD1865 rev. B" },
{ CD1865_GFRCR_REVISION_C, 1, "CL-CD1865 rev. C" },
{ 0, 0, 0 }
};
static inline int cd18xx_wait_ccr(struct cd18xx_softc *);
static inline int
cd18xx_wait_ccr(struct cd18xx_softc *sc)
{
int i = 100000;
while (--i &&
bus_space_read_1(sc->sc_tag, sc->sc_handle, CD18xx_CCR) != 0)
;
return (i == 0);
}
void
cd18xx_attach(struct cd18xx_softc *sc)
{
static int chip_id_next = 1;
int onehundred_pin, revision, i, port;
revision = cd18xx_read(sc, CD18xx_GFRCR);
onehundred_pin = ISSET(cd18xx_read(sc, CD18xx_SRCR),CD18xx_SRCR_PKGTYP);
for (i = 0; cd18xx_revs[i].name; i++)
if (revision == cd18xx_revs[i].revision ||
onehundred_pin == cd18xx_revs[i].onehundred_pin) {
printf(": %s", cd18xx_revs[i].name);
break;
}
if (cd18xx_revs[i].name == NULL) {
aprint_error_dev(sc->sc_dev, "unknown revision, bailing.\n");
return;
}
cd18xx_set_car(sc, 0);
cd18xx_write(sc, CD18xx_GSVR, CD18xx_GSVR_CLEAR);
if (cd18xx_wait_ccr(sc)) {
printf("cd18xx_attach: reset change command timed out\n");
return;
}
cd18xx_write(sc, CD18xx_CCR,
CD18xx_CCR_RESET|CD18xx_CCR_RESET_HARD);
i = 100000;
while (--i && cd18xx_read(sc, CD18xx_GSVR) == CD18xx_GSVR_READY)
;
if (i == 0) {
aprint_normal("\n");
aprint_error_dev(sc->sc_dev, "did not reset!\n");
return;
}
sc->sc_chip_id = chip_id_next++;
#ifdef DIAGNOSTIC
if (sc->sc_chip_id > 31)
panic("more than 31 cd18xx's? help.");
#endif
cd18xx_write(sc, CD18xx_GSVR, CD18xx_GSVR_SETID(sc));
cd18xx_write(sc, CD18xx_MSMR, sc->sc_msmr | 0x80);
cd18xx_write(sc, CD18xx_TSMR, sc->sc_tsmr | 0x80);
cd18xx_write(sc, CD18xx_RSMR, sc->sc_rsmr | 0x80);
printf(", gsvr %x msmr %x tsmr %x rsmr %x",
cd18xx_read(sc, CD18xx_GSVR),
cd18xx_read(sc, CD18xx_MSMR),
cd18xx_read(sc, CD18xx_TSMR),
cd18xx_read(sc, CD18xx_RSMR));
sc->sc_pprh = 0xf0;
sc->sc_pprl = 0;
cd18xx_write(sc, CD18xx_PPRH, sc->sc_pprh);
cd18xx_write(sc, CD18xx_PPRL, sc->sc_pprl);
sc->sc_si = softint_establish(SOFTINT_SERIAL, cd18xx_softintr, sc);
printf(", 8 ports ready (chip id %d)\n", sc->sc_chip_id);
for (port = 0; port < 8; port++)
cdtty_attach(sc, port);
}
void
cdtty_attach(struct cd18xx_softc *sc, int port)
{
struct cdtty_port *p = &sc->sc_ports[port];
cd18xx_set_car(sc, port);
if (cd18xx_wait_ccr(sc)) {
printf("cd18xx_attach: change command timed out setting "
"CAR for port %d\n", port);
return;
}
cd18xx_write(sc, CD18xx_RTPR, 8);
sc->sc_ports[port].p_msvr = CD18xx_MSVR_RESET;
cd18xx_write(sc, CD18xx_SRER, 0);
SET(p->p_chanctl, CD18xx_CCR_CHANCTL |
CD18xx_CCR_CHANCTL_TxEN |
CD18xx_CCR_CHANCTL_RxEN);
p->p_tty = tty_alloc();
p->p_tty->t_oproc = cdttystart;
p->p_tty->t_param = cdttyparam;
p->p_tty->t_hwiflow = cdttyhwiflow;
p->p_rbuf = malloc(cdtty_rbuf_size << 1, M_DEVBUF, M_WAITOK);
p->p_rbput = p->p_rbget = p->p_rbuf;
p->p_rbavail = cdtty_rbuf_size;
if (p->p_rbuf == NULL) {
aprint_error_dev(sc->sc_dev, "unable to allocate ring buffer for tty %d\n", port);
return;
}
p->p_ebuf = p->p_rbuf + (cdtty_rbuf_size << 1);
tty_attach(p->p_tty);
}
void
cdtty_shutdown(struct cd18xx_softc *sc, struct cdtty_port *p)
{
struct tty *tp = p->p_tty;
int s;
s = splserial();
SET(p->p_rx_flags, RX_IBUF_BLOCKED);
cdtty_hwiflow(sc, p);
cdtty_break(sc, p, 0);
if (ISSET(tp->t_cflag, HUPCL)) {
cdtty_modem(sc, p, 0);
splx(s);
(void) tsleep(sc, TTIPRI, ttclos, hz);
s = splserial();
}
p->p_srer = 0;
cd18xx_write(sc, CD18xx_SRER, p->p_srer);
splx(s);
}
int
cdttyopen(dev_t dev, int flag, int mode, struct lwp *l)
{
struct tty *tp;
struct cd18xx_softc *sc;
struct cdtty_port *port;
int channel, instance, s, error;
channel = CD18XX_CHANNEL(dev);
instance = CD18XX_INSTANCE(dev);
if (instance >= clcd_cd.cd_ndevs)
return (ENXIO);
sc = device_lookup_private(&clcd_cd, instance);
if (sc == NULL)
return (ENXIO);
port = &sc->sc_ports[channel];
if (port == NULL || port->p_rbuf == NULL)
return (ENXIO);
tp = port->p_tty;
if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
return (EBUSY);
s = spltty();
if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
struct termios t;
tp->t_dev = dev;
t.c_ispeed = 0;
t.c_ospeed = TTYDEF_SPEED;
t.c_cflag = TTYDEF_CFLAG;
if (ISSET(port->p_swflags, TIOCFLAG_CLOCAL))
SET(t.c_cflag, CLOCAL);
if (ISSET(port->p_swflags, TIOCFLAG_CRTSCTS))
SET(t.c_cflag, CRTSCTS);
if (ISSET(port->p_swflags, TIOCFLAG_CDTRCTS))
SET(t.c_cflag, CDTRCTS);
if (ISSET(port->p_swflags, TIOCFLAG_MDMBUF))
SET(t.c_cflag, MDMBUF);
tp->t_ospeed = 0;
(void)cdttyparam(tp, &t);
tp->t_iflag = TTYDEF_IFLAG;
tp->t_oflag = TTYDEF_OFLAG;
tp->t_lflag = TTYDEF_LFLAG;
ttychars(tp);
ttsetwater(tp);
(void)splserial();
cd18xx_set_car(sc, CD18XX_CHANNEL(dev));
SET(port->p_srer, CD18xx_SRER_Rx |
CD18xx_SRER_RxSC |
CD18xx_SRER_CD);
cd18xx_write(sc, CD18xx_SRER, port->p_srer);
cdtty_modem(sc, port, 1);
port->p_rbget = port->p_rbput = port->p_rbuf;
port->p_rbavail = cdtty_rbuf_size;
CLR(port->p_rx_flags, RX_ANY_BLOCK);
cdtty_hwiflow(sc, port);
}
splx(s);
error = ttyopen(tp, CD18XX_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
if (error == 0)
error = (*tp->t_linesw->l_open)(dev, tp);
return (error);
}
int
cdttyclose(dev_t dev, int flag, int mode, struct lwp *l)
{
struct cd18xx_softc *sc;
struct cdtty_port *port;
struct tty *tp;
int channel, instance;
channel = CD18XX_CHANNEL(dev);
instance = CD18XX_INSTANCE(dev);
if (instance >= clcd_cd.cd_ndevs)
return (ENXIO);
sc = device_lookup_private(&clcd_cd, instance);
if (sc == NULL)
return (ENXIO);
port = &sc->sc_ports[channel];
tp = port->p_tty;
(*tp->t_linesw->l_close)(tp, flag);
ttyclose(tp);
if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
cdtty_shutdown(sc, port);
}
return (0);
}
int
cdttyread(dev_t dev, struct uio *uio, int flag)
{
struct cd18xx_softc *sc = device_lookup_private(&clcd_cd, CD18XX_INSTANCE(dev));
struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
struct tty *tp = port->p_tty;
return ((*tp->t_linesw->l_read)(tp, uio, flag));
}
int
cdttywrite(dev_t dev, struct uio *uio, int flag)
{
struct cd18xx_softc *sc = device_lookup_private(&clcd_cd, CD18XX_INSTANCE(dev));
struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
struct tty *tp = port->p_tty;
return ((*tp->t_linesw->l_write)(tp, uio, flag));
}
int
cdttypoll(dev_t dev, int events, struct lwp *l)
{
struct cd18xx_softc *sc = device_lookup_private(&clcd_cd, CD18XX_INSTANCE(dev));
struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
struct tty *tp = port->p_tty;
return ((*tp->t_linesw->l_poll)(tp, events, l));
}
struct tty *
cdttytty(dev_t dev)
{
struct cd18xx_softc *sc = device_lookup_private(&clcd_cd, CD18XX_INSTANCE(dev));
struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
return (port->p_tty);
}
int
cdttyioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
struct cd18xx_softc *sc = device_lookup_private(&clcd_cd, CD18XX_INSTANCE(dev));
struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
struct tty *tp = port->p_tty;
int error, s;
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);
s = splserial();
switch (cmd) {
case TIOCSBRK:
cdtty_break(sc, port, 1);
break;
case TIOCCBRK:
cdtty_break(sc, port, 0);
break;
case TIOCSDTR:
cdtty_modem(sc, port, 1);
break;
case TIOCCDTR:
cdtty_modem(sc, port, 0);
break;
case TIOCGFLAGS:
*(int *)data = port->p_swflags;
break;
case TIOCSFLAGS:
error = kauth_authorize_device_tty(l->l_cred,
KAUTH_DEVICE_TTY_PRIVSET, tp);
if (error)
return (error);
port->p_swflags = *(int *)data;
break;
case TIOCMSET:
case TIOCMBIS:
case TIOCMBIC:
case TIOCMGET:
default:
return (EPASSTHROUGH);
}
splx(s);
return (0);
}
static void
cdttystart(struct tty *tp)
{
struct cd18xx_softc *sc = device_lookup_private(&clcd_cd, CD18XX_INSTANCE(tp->t_dev));
struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
int s;
s = spltty();
if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
goto out;
if (p->p_tx_stopped)
goto out;
if (!ttypull(tp))
goto out;
{
u_char *tba;
int tbc;
tba = tp->t_outq.c_cf;
tbc = ndqb(&tp->t_outq, 0);
(void)splserial();
p->p_tba = tba;
p->p_tbc = tbc;
}
SET(tp->t_state, TS_BUSY);
p->p_tx_busy = 1;
if ((p->p_srer & CD18xx_SRER_Tx) == 0) {
cd18xx_set_car(sc, CD18XX_CHANNEL(tp->t_dev));
SET(p->p_srer, CD18xx_SRER_Tx);
cd18xx_write(sc, CD18xx_SRER, p->p_srer);
}
out:
splx(s);
return;
}
void
cdttystop(struct tty *tp, int flag)
{
struct cd18xx_softc *sc = device_lookup_private(&clcd_cd, CD18XX_INSTANCE(tp->t_dev));
struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
int s;
s = splserial();
if (ISSET(tp->t_state, TS_BUSY)) {
p->p_tbc = 0;
p->p_heldtbc = 0;
if (!ISSET(tp->t_state, TS_TTSTOP))
SET(tp->t_state, TS_FLUSH);
}
splx(s);
}
void
cdtty_loadchannelregs(struct cd18xx_softc *sc, struct cdtty_port *p)
{
cd18xx_set_car(sc, CD18XX_CHANNEL(p->p_tty->t_dev));
cd18xx_write(sc, CD18xx_SRER, p->p_srer);
cd18xx_write(sc, CD18xx_MSVR, p->p_msvr_active = p->p_msvr);
cd18xx_write(sc, CD18xx_COR1, p->p_cor1);
cd18xx_write(sc, CD18xx_COR2, p->p_cor2);
cd18xx_write(sc, CD18xx_COR3, p->p_cor3);
cd18xx_write(sc, CD18xx_CCR, CD18xx_CCR_CORCHG |
CD18xx_CCR_CORCHG_COR1 |
CD18xx_CCR_CORCHG_COR2 |
CD18xx_CCR_CORCHG_COR3);
cd18xx_write(sc, CD18xx_RBPRH, p->p_rbprh);
cd18xx_write(sc, CD18xx_RBPRL, p->p_rbprl);
cd18xx_write(sc, CD18xx_TBPRH, p->p_tbprh);
cd18xx_write(sc, CD18xx_TBPRL, p->p_tbprl);
if (cd18xx_wait_ccr(sc)) {
DPRINTF(CDD_INFO,
("%s: cdtty_loadchannelregs ccr wait timed out\n",
device_xname(sc->sc_dev)));
}
cd18xx_write(sc, CD18xx_CCR, p->p_chanctl);
}
static int
cdttyparam(struct tty *tp, struct termios *t)
{
struct cd18xx_softc *sc = device_lookup_private(&clcd_cd, CD18XX_INSTANCE(tp->t_dev));
struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
int s;
if (t->c_ospeed < 0)
return (EINVAL);
if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
return (EINVAL);
if (ISSET(p->p_swflags, TIOCFLAG_SOFTCAR)) {
SET(t->c_cflag, CLOCAL);
CLR(t->c_cflag, HUPCL);
}
if (tp->t_ospeed == t->c_ospeed &&
tp->t_cflag == t->c_cflag)
return (0);
s = splserial();
switch (t->c_cflag & CSIZE) {
case CS5:
p->p_cor1 = CD18xx_COR1_CS5;
break;
case CS6:
p->p_cor1 = CD18xx_COR1_CS6;
break;
case CS7:
p->p_cor1 = CD18xx_COR1_CS7;
break;
default:
p->p_cor1 = CD18xx_COR1_CS8;
break;
}
if (ISSET(t->c_cflag, PARENB)) {
SET(p->p_cor1, CD18xx_COR1_PARITY_NORMAL);
if (ISSET(t->c_cflag, PARODD))
SET(p->p_cor1, CD18xx_COR1_PARITY_ODD);
}
if (!ISSET(t->c_iflag, INPCK))
SET(p->p_cor1, CD18xx_COR1_IGNORE);
if (ISSET(t->c_cflag, CSTOPB))
SET(p->p_cor1, CD18xx_COR1_STOPBIT_2);
if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
p->p_msvr_dcd = 0;
else
p->p_msvr_dcd = CD18xx_MSVR_CD;
if (ISSET(t->c_cflag, CRTSCTS)) {
p->p_mcor1_dtr = CD18xx_MCOR1_DTR;
p->p_msvr_rts = CD18xx_MSVR_RTS;
p->p_msvr_cts = CD18xx_MSVR_CTS;
p->p_cor2 = CD18xx_COR2_RTSAOE|CD18xx_COR2_CTSAE;
} else if (ISSET(t->c_cflag, MDMBUF)) {
p->p_mcor1_dtr = 0;
p->p_msvr_rts = CD18xx_MSVR_DTR;
p->p_msvr_cts = CD18xx_MSVR_CD;
p->p_cor2 = 0;
} else {
p->p_mcor1_dtr = CD18xx_MSVR_DTR;
p->p_msvr_rts = 0;
p->p_msvr_cts = 0;
p->p_cor2 = 0;
}
p->p_msvr_mask = p->p_msvr_cts | p->p_msvr_dcd;
p->p_cor3 = (t->c_ospeed <= 1200 ? 1 : t->c_ospeed <= 38400 ? 8 : 4);
#define PORT_RATE(o, s) \
(((((o) + (s)/2) / (s)) + CD18xx_xBRPR_TPC/2) / CD18xx_xBRPR_TPC)
if (t->c_ospeed) {
u_int32_t tbpr = PORT_RATE(sc->sc_osc, t->c_ospeed);
if (tbpr == 0 || tbpr > 0xffff)
return (EINVAL);
p->p_tbprh = tbpr >> 8;
p->p_tbprl = tbpr & 0xff;
}
if (t->c_ispeed) {
u_int32_t rbpr = PORT_RATE(sc->sc_osc, t->c_ispeed);
if (rbpr == 0 || rbpr > 0xffff)
return (EINVAL);
p->p_rbprh = rbpr >> 8;
p->p_rbprl = rbpr & 0xff;
}
tp->t_ispeed = 0;
tp->t_ospeed = t->c_ospeed;
tp->t_cflag = t->c_cflag;
if (!p->p_heldchange) {
if (p->p_tx_busy) {
p->p_heldtbc = p->p_tbc;
p->p_tbc = 0;
p->p_heldchange = 1;
} else
cdtty_loadchannelregs(sc, p);
}
if (!ISSET(t->c_cflag, CHWFLOW)) {
p->p_r_hiwat = 0;
p->p_r_lowat = 0;
if (ISSET(p->p_rx_flags, RX_TTY_OVERFLOWED)) {
CLR(p->p_rx_flags, RX_TTY_OVERFLOWED);
softint_schedule(sc->sc_si);
}
if (ISSET(p->p_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
CLR(p->p_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
cdtty_hwiflow(sc, p);
}
} else {
p->p_r_hiwat = cdtty_rbuf_hiwat;
p->p_r_lowat = cdtty_rbuf_lowat;
}
splx(s);
(void) (*tp->t_linesw->l_modem)(tp, ISSET(p->p_msvr, CD18xx_MSVR_CD));
if (!ISSET(t->c_cflag, CHWFLOW)) {
if (p->p_tx_stopped) {
p->p_tx_stopped = 0;
cdttystart(tp);
}
}
return (0);
}
static void
cdtty_break(struct cd18xx_softc *sc, struct cdtty_port *p, int onoff)
{
p->p_needbreak = !!onoff;
if (p->p_needbreak != p->p_break)
SET(p->p_srer, CD18xx_SRER_Tx);
if (!p->p_heldchange) {
if (p->p_tx_busy) {
p->p_heldtbc = p->p_tbc;
p->p_tbc = 0;
p->p_heldchange = 1;
} else
cdtty_loadchannelregs(sc, p);
}
}
static void
cdtty_modem(struct cd18xx_softc *sc, struct cdtty_port *p, int onoff)
{
if (p->p_mcor1_dtr == 0)
return;
if (onoff)
CLR(p->p_mcor1, p->p_mcor1_dtr);
else
SET(p->p_mcor1, p->p_mcor1_dtr);
if (!p->p_heldchange) {
if (p->p_tx_busy) {
p->p_heldtbc = p->p_tbc;
p->p_tbc = 0;
p->p_heldchange = 1;
} else
cdtty_loadchannelregs(sc, p);
}
}
int
cdttyhwiflow(struct tty *tp, int block)
{
struct cd18xx_softc *sc = device_lookup_private(&clcd_cd, CD18XX_INSTANCE(tp->t_dev));
struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
int s;
if (p->p_msvr_rts == 0)
return (0);
s = splserial();
if (block) {
if (!ISSET(p->p_rx_flags, RX_TTY_BLOCKED)) {
SET(p->p_rx_flags, RX_TTY_BLOCKED);
cdtty_hwiflow(sc, p);
}
} else {
if (ISSET(p->p_rx_flags, RX_TTY_OVERFLOWED)) {
CLR(p->p_rx_flags, RX_TTY_OVERFLOWED);
softint_schedule(sc->sc_si);
}
if (ISSET(p->p_rx_flags, RX_TTY_BLOCKED)) {
CLR(p->p_rx_flags, RX_TTY_BLOCKED);
cdtty_hwiflow(sc, p);
}
}
splx(s);
return (1);
}
static void
cdtty_hwiflow(struct cd18xx_softc *sc, struct cdtty_port *p)
{
if (p->p_msvr_rts == 0)
return;
if (ISSET(p->p_rx_flags, RX_ANY_BLOCK)) {
CLR(p->p_msvr, p->p_msvr_rts);
CLR(p->p_msvr_active, p->p_msvr_rts);
} else {
SET(p->p_msvr, p->p_msvr_rts);
SET(p->p_msvr_active, p->p_msvr_rts);
}
cd18xx_set_car(sc, CD18XX_CHANNEL(p->p_tty->t_dev));
cd18xx_write(sc, CD18xx_MSVR, p->p_msvr_active);
}
#define INTR_MAX_ALLOWED 0
#if INTR_MAX_ALLOWED == 0
#define GOTINTR(sc, p)
#else
int intrcount;
#define GOTINTR(sc, p) \
do { \
if (intrcount++ == INTR_MAX_ALLOWED) { \
CLR(p->p_srer, CD18xx_SRER_Tx); \
cd18xx_write(sc, CD18xx_SRER, p->p_srer); \
} \
DPRINTF(CDD_INTR, (", intrcount %d srer %x", intrcount, p->p_srer)); \
} while (0)
#endif
static inline void
cd18xx_rint(struct cd18xx_softc *sc, int *ns)
{
struct cdtty_port *p;
u_int channel, count;
u_char *put, *end;
u_int cc;
channel = cd18xx_get_gscr1_channel(sc);
p = &sc->sc_ports[channel];
DPRINTF(CDD_INTR, ("%s: rint: channel %d", device_xname(sc->sc_dev), channel));
GOTINTR(sc, p);
end = p->p_ebuf;
put = p->p_rbput;
cc = p->p_rbavail;
count = cd18xx_read(sc, CD18xx_RDCR);
DPRINTF(CDD_INTR, (", %d bytes available: ", count));
while (cc > 0 && count > 0) {
u_char rcsr = cd18xx_read(sc, CD18xx_RCSR);
put[0] = cd18xx_read(sc, CD18xx_RDR);
put[1] = rcsr;
if (rcsr)
*ns = 1;
put += 2;
if (put >= end)
put = p->p_rbuf;
DPRINTF(CDD_INTR, ("."));
cc--;
count--;
}
DPRINTF(CDD_INTR, (" finished reading"));
p->p_rbput = put;
p->p_rbavail = cc;
if (!ISSET(p->p_rx_flags, RX_TTY_OVERFLOWED)) {
p->p_rx_ready = 1;
}
if (!cc) {
SET(p->p_rx_flags, RX_IBUF_OVERFLOWED);
CLR(p->p_srer, CD18xx_SRER_Rx |
CD18xx_SRER_RxSC |
CD18xx_SRER_CD);
cd18xx_write(sc, CD18xx_SRER, p->p_srer);
}
cd18xx_write(sc, CD18xx_EOSRR, 0);
DPRINTF(CDD_INTR, (", done\n"));
}
static inline void
cd18xx_tint(struct cd18xx_softc *sc, int *ns)
{
struct cdtty_port *p;
u_int channel;
channel = cd18xx_get_gscr1_channel(sc);
p = &sc->sc_ports[channel];
DPRINTF(CDD_INTR, ("%s: tint: channel %d", device_xname(sc->sc_dev),
channel));
GOTINTR(sc, p);
if (p->p_break != p->p_needbreak) {
u_char buf[2];
DPRINTF(CDD_INTR, (", changing break to %d", p->p_needbreak));
cd18xx_write(sc, CD18xx_COR2, p->p_cor2 | CD18xx_COR2_ETC);
buf[0] = CD18xx_TDR_ETC_BYTE;
buf[1] = p->p_needbreak ? CD18xx_TDR_BREAK_BYTE :
CD18xx_TDR_NOBREAK_BYTE;
cd18xx_write_multi(sc, CD18xx_TDR, buf, 2);
p->p_break = p->p_needbreak;
cd18xx_write(sc, CD18xx_COR2, p->p_cor2);
}
if (p->p_heldchange) {
cdtty_loadchannelregs(sc, p);
p->p_heldchange = 0;
p->p_tbc = p->p_heldtbc;
p->p_heldtbc = 0;
}
if (p->p_tbc > 0) {
int n;
n = p->p_tbc;
if (n > 8)
n = 8;
DPRINTF(CDD_INTR, (", writing %d bytes to TDR", n));
cd18xx_write_multi(sc, CD18xx_TDR, p->p_tba, n);
p->p_tbc -= n;
p->p_tba += n;
}
if (p->p_tbc == 0) {
if (ISSET(p->p_srer, CD18xx_SRER_Tx)) {
DPRINTF(CDD_INTR, (", disabling tx interrupts"));
CLR(p->p_srer, CD18xx_SRER_Tx);
cd18xx_write(sc, CD18xx_SRER, p->p_srer);
}
if (p->p_tx_busy) {
p->p_tx_busy = 0;
p->p_tx_done = 1;
}
}
*ns = 1;
cd18xx_write(sc, CD18xx_EOSRR, 0);
DPRINTF(CDD_INTR, (", done\n"));
}
static inline void
cd18xx_mint(struct cd18xx_softc *sc, int *ns)
{
struct cdtty_port *p;
u_int channel;
u_char msvr, delta;
channel = cd18xx_get_gscr1_channel(sc);
p = &sc->sc_ports[channel];
DPRINTF(CDD_INTR, ("%s: mint: channel %d", device_xname(sc->sc_dev), channel));
GOTINTR(sc, p);
msvr = cd18xx_read(sc, CD18xx_MSVR);
delta = msvr ^ p->p_msvr;
DPRINTF(CDD_INTR, (", msvr %d", msvr));
if (ISSET(delta, p->p_msvr_mask)) {
SET(p->p_msvr_delta, delta);
DPRINTF(CDD_INTR, (", status changed delta %d", delta));
if (ISSET(~msvr, p->p_msvr_mask)) {
p->p_tbc = 0;
p->p_heldtbc = 0;
}
p->p_st_check = 1;
*ns = 1;
}
cd18xx_write(sc, CD18xx_MCR, 0);
cd18xx_write(sc, CD18xx_EOSRR, 0);
DPRINTF(CDD_INTR, (", done\n"));
}
int
cd18xx_hardintr(void *v)
{
int i, rv = 0;
u_char ack;
DPRINTF(CDD_INTR, ("cd18xx_hardintr (ndevs %d):\n", clcd_cd.cd_ndevs));
for (i = 0; i < clcd_cd.cd_ndevs; i++)
{
struct cd18xx_softc *sc = device_lookup_private(&clcd_cd, i);
int status, ns = 0;
int count = 1;
if (sc == NULL)
continue;
DPRINTF(CDD_INTR, ("%s:", device_xname(sc->sc_dev)));
while (count-- &&
(status = (cd18xx_read(sc, CD18xx_SRSR) &
CD18xx_SRSR_PENDING))) {
rv = 1;
DPRINTF(CDD_INTR, (" status %x:", status));
if (ISSET(status, CD18xx_SRSR_RxPEND)) {
ack = (*sc->sc_ackfunc)(sc->sc_ackfunc_arg,
CD18xx_INTRACK_RxINT);
DPRINTF(CDD_INTR, (" rx: ack1 %x\n", ack));
cd18xx_rint(sc, &ns);
}
if (ISSET(status, CD18xx_SRSR_TxPEND)) {
ack = (*sc->sc_ackfunc)(sc->sc_ackfunc_arg,
CD18xx_INTRACK_TxINT);
DPRINTF(CDD_INTR, (" tx: ack1 %x\n", ack));
cd18xx_tint(sc, &ns);
}
if (ISSET(status, CD18xx_SRSR_MxPEND)) {
ack = (*sc->sc_ackfunc)(sc->sc_ackfunc_arg,
CD18xx_INTRACK_MxINT);
DPRINTF(CDD_INTR, (" mx: ack1 %x\n", ack));
cd18xx_mint(sc, &ns);
}
}
if (ns)
softint_schedule(sc->sc_si);
}
return (rv);
}
void
cdtty_rxsoft(struct cd18xx_softc *sc, struct cdtty_port *p, struct tty *tp)
{
u_char *get, *end;
u_int cc, scc;
u_char rcsr;
int code;
int s;
end = p->p_ebuf;
get = p->p_rbget;
scc = cc = cdtty_rbuf_size - p->p_rbavail;
if (cc == cdtty_rbuf_size) {
p->p_floods++;
#if 0
if (p->p_errors++ == 0)
callout_reset(&p->p_diag_callout, 60 * hz,
cdttydiag, p);
#endif
}
while (cc) {
code = get[0];
rcsr = get[1];
if (ISSET(rcsr, CD18xx_RCSR_OVERRUNERR | CD18xx_RCSR_BREAK |
CD18xx_RCSR_FRAMERR | CD18xx_RCSR_PARITYERR)) {
if (ISSET(rcsr, CD18xx_RCSR_OVERRUNERR)) {
p->p_overflows++;
#if 0
if (p->p_errors++ == 0)
callout_reset(&p->p_diag_callout,
60 * hz, cdttydiag, p);
#endif
}
if (ISSET(rcsr, CD18xx_RCSR_BREAK|CD18xx_RCSR_FRAMERR))
SET(code, TTY_FE);
if (ISSET(rcsr, CD18xx_RCSR_PARITYERR))
SET(code, TTY_PE);
}
if ((*tp->t_linesw->l_rint)(code, tp) == -1) {
if (!ISSET(p->p_rx_flags, RX_TTY_BLOCKED)) {
get += cc << 1;
if (get >= end)
get -= cdtty_rbuf_size << 1;
cc = 0;
} else {
SET(p->p_rx_flags, RX_TTY_OVERFLOWED);
}
break;
}
get += 2;
if (get >= end)
get = p->p_rbuf;
cc--;
}
if (cc != scc) {
p->p_rbget = get;
s = splserial();
cc = p->p_rbavail += scc - cc;
if (cc >= p->p_r_lowat) {
if (ISSET(p->p_rx_flags, RX_IBUF_OVERFLOWED)) {
CLR(p->p_rx_flags, RX_IBUF_OVERFLOWED);
cd18xx_set_car(sc, CD18XX_CHANNEL(tp->t_dev));
SET(p->p_srer, CD18xx_SRER_Rx |
CD18xx_SRER_RxSC |
CD18xx_SRER_CD);
cd18xx_write(sc, CD18xx_SRER, p->p_srer);
}
if (ISSET(p->p_rx_flags, RX_IBUF_BLOCKED)) {
CLR(p->p_rx_flags, RX_IBUF_BLOCKED);
cdtty_hwiflow(sc, p);
}
}
splx(s);
}
}
void
cdtty_txsoft(struct cd18xx_softc *sc, struct cdtty_port *p, struct tty *tp)
{
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)(p->p_tba - tp->t_outq.c_cf));
(*tp->t_linesw->l_start)(tp);
}
void
cdtty_stsoft(struct cd18xx_softc *sc, struct cdtty_port *p, struct tty *tp)
{
u_char msvr, delta;
int s;
s = splserial();
msvr = p->p_msvr;
delta = p->p_msvr_delta;
p->p_msvr_delta = 0;
splx(s);
if (ISSET(delta, p->p_msvr_dcd)) {
(void) (*tp->t_linesw->l_modem)(tp, ISSET(msvr, CD18xx_MSVR_CD));
}
if (ISSET(delta, p->p_msvr_cts)) {
if (ISSET(msvr, p->p_msvr_cts)) {
p->p_tx_stopped = 0;
(*tp->t_linesw->l_start)(tp);
} else {
p->p_tx_stopped = 1;
}
}
}
void
cd18xx_softintr(void *v)
{
struct cd18xx_softc *sc = v;
struct cdtty_port *p;
struct tty *tp;
int i;
for (i = 0; i < 8; i++) {
p = &sc->sc_ports[i];
tp = p->p_tty;
if (tp == NULL)
continue;
if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
continue;
if (p->p_rx_ready) {
p->p_rx_ready = 0;
cdtty_rxsoft(sc, p, tp);
}
if (p->p_st_check) {
p->p_st_check = 0;
cdtty_stsoft(sc, p, tp);
}
if (p->p_tx_done) {
p->p_tx_done = 0;
cdtty_txsoft(sc, p, tp);
}
}
}