#include <sys/param.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/stream.h>
#include <sys/termio.h>
#include <sys/errno.h>
#include <sys/file.h>
#include <sys/cmn_err.h>
#include <sys/stropts.h>
#include <sys/strsubr.h>
#include <sys/strtty.h>
#include <sys/debug.h>
#include <sys/kbio.h>
#include <sys/cred.h>
#include <sys/stat.h>
#include <sys/consdev.h>
#include <sys/mkdev.h>
#include <sys/kmem.h>
#include <sys/cred.h>
#include <sys/strsun.h>
#ifdef DEBUG
#include <sys/promif.h>
#endif
#include <sys/modctl.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/pci.h>
#include <sys/asy.h>
#include <sys/policy.h>
#include <sys/sysmacros.h>
static int asy_trig_level = ASY_FCR_RHR_TRIG_8;
int asy_drain_check = 15000000;
int asy_min_dtr_low = 500000;
int asy_min_utbrk = 100000;
int asymaxchip = ASY_MAXCHIP;
int asy_fifo_test = 1;
int asy_scr_test = 1;
int asy_max_tx_fifo = 16;
#define async_stopc async_ttycommon.t_stopc
#define async_startc async_ttycommon.t_startc
#define ASY_INIT 1
#define ASY_NOINIT 0
typedef enum {
FLOW_CHECK,
FLOW_STOP,
FLOW_START
} async_flowc_action;
#ifdef DEBUG
typedef enum {
ASY_DEBUG_INIT = 1 << 0,
ASY_DEBUG_INPUT = 1 << 1,
ASY_DEBUG_EOT = 1 << 2,
ASY_DEBUG_CLOSE = 1 << 3,
ASY_DEBUG_HFLOW = 1 << 4,
ASY_DEBUG_PROCS = 1 << 5,
ASY_DEBUG_STATE = 1 << 6,
ASY_DEBUG_INTR = 1 << 7,
ASY_DEBUG_OUT = 1 << 8,
ASY_DEBUG_BUSY = 1 << 9,
ASY_DEBUG_MODEM = 1 << 10,
ASY_DEBUG_MODM2 = 1 << 11,
ASY_DEBUG_IOCTL = 1 << 12,
ASY_DEBUG_CHIP = 1 << 13,
ASY_DEBUG_SFLOW = 1 << 14,
} asy_debug_t;
static asy_debug_t debug = 0;
#define ASY_DEBUG(asy, x) (asy->asy_debug & (x))
#define ASY_DPRINTF(asy, fac, format, ...) \
if (ASY_DEBUG(asy, fac)) \
asyerror(asy, CE_CONT, "!%s: " format, __func__, ##__VA_ARGS__)
#else
#define ASY_DEBUG(asy, x) B_FALSE
#define ASY_DPRINTF(asy, fac, format, ...)
#endif
void ddi_hardpps(struct timeval *, int);
static struct ppsclockev asy_ppsev;
#ifdef PPSCLOCKLED
#define LED_ON
#define LED_OFF
#else
#define LED_ON
#define LED_OFF
#endif
static void asy_put_idx(const struct asycom *, asy_reg_t, uint8_t);
static uint8_t asy_get_idx(const struct asycom *, asy_reg_t);
static void asy_put_add(const struct asycom *, asy_reg_t, uint8_t);
static uint8_t asy_get_add(const struct asycom *, asy_reg_t);
static void asy_put_ext(const struct asycom *, asy_reg_t, uint8_t);
static uint8_t asy_get_ext(const struct asycom *, asy_reg_t);
static void asy_put_reg(const struct asycom *, asy_reg_t, uint8_t);
static uint8_t asy_get_reg(const struct asycom *, asy_reg_t);
static void asy_put(const struct asycom *, asy_reg_t, uint8_t);
static uint8_t asy_get(const struct asycom *, asy_reg_t);
static void asy_set(const struct asycom *, asy_reg_t, uint8_t);
static void asy_clr(const struct asycom *, asy_reg_t, uint8_t);
static void asy_enable_interrupts(const struct asycom *, uint8_t);
static void asy_disable_interrupts(const struct asycom *, uint8_t);
static void asy_set_baudrate(const struct asycom *, int);
static void asy_wait_baudrate(struct asycom *);
#define BAUDINDEX(cflg) (((cflg) & CBAUDEXT) ? \
(((cflg) & CBAUD) + CBAUD + 1) : ((cflg) & CBAUD))
static void asysetsoft(struct asycom *);
static uint_t asysoftintr(caddr_t, caddr_t);
static uint_t asyintr(caddr_t, caddr_t);
static boolean_t abort_charseq_recognize(uchar_t ch);
static void async_txint(struct asycom *asy);
static void async_rxint(struct asycom *asy, uchar_t lsr);
static void async_msint(struct asycom *asy);
static void async_softint(struct asycom *asy);
static void async_ioctl(struct asyncline *async, queue_t *q, mblk_t *mp);
static void async_reioctl(void *unit);
static void async_iocdata(queue_t *q, mblk_t *mp);
static void async_restart(void *arg);
static void async_start(struct asyncline *async);
static void async_resume(struct asyncline *async);
static void asy_program(struct asycom *asy, int mode);
static void asyinit(struct asycom *asy);
static void asy_waiteot(struct asycom *asy);
static void asyputchar(cons_polledio_arg_t, uchar_t c);
static int asygetchar(cons_polledio_arg_t);
static boolean_t asyischar(cons_polledio_arg_t);
static int asymctl(struct asycom *, int, int);
static int asytodm(int, int);
static int dmtoasy(struct asycom *, int);
static void asyerror(const struct asycom *, int, const char *, ...)
__KPRINTFLIKE(3);
static void asy_parse_mode(dev_info_t *devi, struct asycom *asy);
static void asy_soft_state_free(struct asycom *);
static char *asy_hw_name(struct asycom *asy);
static void async_hold_utbrk(void *arg);
static void async_resume_utbrk(struct asyncline *async);
static void async_dtr_free(struct asyncline *async);
static int asy_identify_chip(dev_info_t *devi, struct asycom *asy);
static void asy_reset_fifo(struct asycom *asy, uchar_t flags);
static void asy_carrier_check(struct asycom *);
static int asy_getproperty(dev_info_t *devi, struct asycom *asy,
const char *property);
static boolean_t async_flowcontrol_sw_input(struct asycom *asy,
async_flowc_action onoff, int type);
static void async_flowcontrol_sw_output(struct asycom *asy,
async_flowc_action onoff);
static void async_flowcontrol_hw_input(struct asycom *asy,
async_flowc_action onoff, int type);
static void async_flowcontrol_hw_output(struct asycom *asy,
async_flowc_action onoff);
#define GET_PROP(devi, pname, pflag, pval, plen) \
(ddi_prop_op(DDI_DEV_T_ANY, (devi), PROP_LEN_AND_VAL_BUF, \
(pflag), (pname), (caddr_t)(pval), (plen)))
kmutex_t asy_glob_lock;
void *asy_soft_state;
static const int standard_com_ports[] = {
COM1_IOADDR, COM2_IOADDR, COM3_IOADDR, COM4_IOADDR
};
static int *com_ports;
static uint_t num_com_ports;
#ifdef DEBUG
boolean_t asy_nosuspend = B_FALSE;
#endif
#define UNSUPPORTED 0x00, 0x00, 0x00
static struct {
uint8_t asy_dlh;
uint8_t asy_dll;
uint8_t asy_tcr;
} asy_baud_tab[] = {
[B0] = { UNSUPPORTED },
[B50] = { 0x09, 0x00, 0x00 },
[B75] = { 0x06, 0x00, 0x00 },
[B110] = { 0x04, 0x17, 0x00 },
[B134] = { 0x03, 0x59, 0x00 },
[B150] = { 0x03, 0x00, 0x00 },
[B200] = { 0x02, 0x40, 0x00 },
[B300] = { 0x01, 0x80, 0x00 },
[B600] = { 0x00, 0xc0, 0x00 },
[B1200] = { 0x00, 0x60, 0x00 },
[B1800] = { 0x00, 0x40, 0x00 },
[B2400] = { 0x00, 0x30, 0x00 },
[B4800] = { 0x00, 0x18, 0x00 },
[B9600] = { 0x00, 0x0c, 0x00 },
[B19200] = { 0x00, 0x06, 0x00 },
[B38400] = { 0x00, 0x03, 0x00 },
[B57600] = { 0x00, 0x02, 0x00 },
[B76800] = { 0x00, 0x06, 0x04 },
[B115200] = { 0x00, 0x01, 0x00 },
[B153600] = { 0x00, 0x03, 0x04 },
[B230400] = { 0x00, 0x02, 0x04 },
[B307200] = { 0x00, 0x01, 0x06 },
[B460800] = { 0x00, 0x01, 0x04 },
[B921600] = { 0x00, 0x02, 0x01 },
[B1000000] = { UNSUPPORTED },
[B1152000] = { UNSUPPORTED },
[B1500000] = { UNSUPPORTED },
[B2000000] = { UNSUPPORTED },
[B2500000] = { UNSUPPORTED },
[B3000000] = { UNSUPPORTED },
[B3500000] = { UNSUPPORTED },
[B4000000] = { UNSUPPORTED },
};
static struct {
int asy_min_hwtype;
int8_t asy_reg_off;
uint8_t (*asy_get_reg)(const struct asycom *, asy_reg_t);
void (*asy_put_reg)(const struct asycom *, asy_reg_t, uint8_t);
} asy_reg_table[] = {
[ASY_ILLEGAL] = { 0, -1, NULL, NULL },
[ASY_THR] = { ASY_8250A, 0, NULL, asy_put_reg },
[ASY_RHR] = { ASY_8250A, 0, asy_get_reg, NULL },
[ASY_IER] = { ASY_8250A, 1, asy_get_reg, asy_put_reg },
[ASY_FCR] = { ASY_16550, 2, NULL, asy_put_reg },
[ASY_ISR] = { ASY_8250A, 2, asy_get_reg, NULL },
[ASY_LCR] = { ASY_8250A, 3, asy_get_reg, asy_put_reg },
[ASY_MCR] = { ASY_8250A, 4, asy_get_reg, asy_put_reg },
[ASY_LSR] = { ASY_8250A, 5, asy_get_reg, NULL },
[ASY_MSR] = { ASY_8250A, 6, asy_get_reg, NULL },
[ASY_SPR] = { ASY_8250A, 7, asy_get_reg, asy_put_reg },
[ASY_DLL] = { ASY_8250A, 0, asy_get_reg, asy_put_reg },
[ASY_DLH] = { ASY_8250A, 1, asy_get_reg, asy_put_reg },
[ASY_EFR] = { ASY_16750, 2, asy_get_ext, asy_put_ext },
[ASY_XON1] = { ASY_16650, 4, asy_get_ext, asy_put_ext },
[ASY_XON2] = { ASY_16650, 5, asy_get_ext, asy_put_ext },
[ASY_XOFF1] = { ASY_16650, 6, asy_get_ext, asy_put_ext },
[ASY_XOFF2] = { ASY_16650, 7, asy_get_ext, asy_put_ext },
[ASY_ASR] = { ASY_16950, 1, asy_get_add, asy_put_add },
[ASY_RFL] = { ASY_16950, 3, asy_get_add, NULL },
[ASY_TFL] = { ASY_16950, 4, asy_get_add, NULL },
[ASY_ICR] = { ASY_16950, 5, asy_get_reg, asy_put_reg },
[ASY_ACR] = { ASY_16950, 0, asy_get_idx, asy_put_idx },
[ASY_CPR] = { ASY_16950, 1, asy_get_idx, asy_put_idx },
[ASY_TCR] = { ASY_16950, 2, asy_get_idx, asy_put_idx },
[ASY_CKS] = { ASY_16950, 3, asy_get_idx, asy_put_idx },
[ASY_TTL] = { ASY_16950, 4, asy_get_idx, asy_put_idx },
[ASY_RTL] = { ASY_16950, 5, asy_get_idx, asy_put_idx },
[ASY_FCL] = { ASY_16950, 6, asy_get_idx, asy_put_idx },
[ASY_FCH] = { ASY_16950, 7, asy_get_idx, asy_put_idx },
[ASY_ID1] = { ASY_16950, 8, asy_get_idx, NULL },
[ASY_ID2] = { ASY_16950, 9, asy_get_idx, NULL },
[ASY_ID3] = { ASY_16950, 10, asy_get_idx, NULL },
[ASY_REV] = { ASY_16950, 11, asy_get_idx, NULL },
[ASY_CSR] = { ASY_16950, 12, NULL, asy_put_idx },
[ASY_NMR] = { ASY_16950, 13, asy_get_idx, asy_put_idx },
};
static int asyrsrv(queue_t *q);
static int asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr);
static int asyclose(queue_t *q, int flag, cred_t *credp);
static int asywputdo(queue_t *q, mblk_t *mp, boolean_t);
static int asywput(queue_t *q, mblk_t *mp);
struct module_info asy_info = {
0,
"asy",
0,
INFPSZ,
4096,
128
};
static struct qinit asy_rint = {
putq,
asyrsrv,
asyopen,
asyclose,
NULL,
&asy_info,
NULL
};
static struct qinit asy_wint = {
asywput,
NULL,
NULL,
NULL,
NULL,
&asy_info,
NULL
};
struct streamtab asy_str_info = {
&asy_rint,
&asy_wint,
NULL,
NULL
};
static void asy_intr_free(struct asycom *);
static int asy_intr_setup(struct asycom *, int);
static void asy_softintr_free(struct asycom *);
static int asy_softintr_setup(struct asycom *);
static int asy_suspend(struct asycom *);
static int asy_resume(dev_info_t *);
static int asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
void **result);
static int asyprobe(dev_info_t *);
static int asyattach(dev_info_t *, ddi_attach_cmd_t);
static int asydetach(dev_info_t *, ddi_detach_cmd_t);
static int asyquiesce(dev_info_t *);
static struct cb_ops cb_asy_ops = {
nodev,
nodev,
nodev,
nodev,
nodev,
nodev,
nodev,
nodev,
nodev,
nodev,
nodev,
nochpoll,
ddi_prop_op,
&asy_str_info,
D_MP
};
struct dev_ops asy_ops = {
DEVO_REV,
0,
asyinfo,
nulldev,
asyprobe,
asyattach,
asydetach,
nodev,
&cb_asy_ops,
NULL,
NULL,
asyquiesce,
};
static struct modldrv modldrv = {
&mod_driverops,
"ASY driver",
&asy_ops,
};
static struct modlinkage modlinkage = {
MODREV_1,
(void *)&modldrv,
NULL
};
int
_init(void)
{
int i;
i = ddi_soft_state_init(&asy_soft_state, sizeof (struct asycom), 2);
if (i == 0) {
mutex_init(&asy_glob_lock, NULL, MUTEX_DRIVER, NULL);
if ((i = mod_install(&modlinkage)) != 0) {
mutex_destroy(&asy_glob_lock);
ddi_soft_state_fini(&asy_soft_state);
#ifdef DEBUG
} else {
if (debug & ASY_DEBUG_INIT)
cmn_err(CE_NOTE, "!%s, debug = %x",
modldrv.drv_linkinfo, debug);
#endif
}
}
return (i);
}
int
_fini(void)
{
int i;
if ((i = mod_remove(&modlinkage)) == 0) {
#ifdef DEBUG
if (debug & ASY_DEBUG_INIT)
cmn_err(CE_NOTE, "!%s unloading",
modldrv.drv_linkinfo);
#endif
mutex_destroy(&asy_glob_lock);
if (com_ports != NULL && com_ports != (int *)standard_com_ports)
ddi_prop_free(com_ports);
com_ports = NULL;
ddi_soft_state_fini(&asy_soft_state);
}
return (i);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static void
asy_put_idx(const struct asycom *asy, asy_reg_t reg, uint8_t val)
{
ASSERT(asy->asy_hwtype >= ASY_16950);
ASSERT(reg >= ASY_ACR);
ASSERT(reg <= ASY_NREG);
asy_put(asy, ASY_SPR, asy_reg_table[reg].asy_reg_off);
asy_put(asy, ASY_ICR, val);
}
static uint8_t
asy_get_idx(const struct asycom *asy, asy_reg_t reg)
{
uint8_t val;
ASSERT(asy->asy_hwtype >= ASY_16950);
ASSERT(reg >= ASY_ACR);
ASSERT(reg <= ASY_NREG);
asy_put(asy, ASY_ACR, ASY_ACR_ICR | asy->asy_acr);
asy_put(asy, ASY_SPR, asy_reg_table[reg].asy_reg_off);
val = asy_get(asy, ASY_ICR);
asy_put(asy, ASY_ACR, asy->asy_acr);
return (val);
}
static void
asy_put_add(const struct asycom *asy, asy_reg_t reg, uint8_t val)
{
ASSERT(asy->asy_hwtype >= ASY_16950);
ASSERT(reg == ASY_ASR);
ASSERT((val & ~(ASY_ASR_TD | ASY_ASR_RTD)) == 0);
asy_put(asy, ASY_ACR, ASY_ACR_ASR | asy->asy_acr);
asy_put_reg(asy, reg, val);
asy_put(asy, ASY_ACR, asy->asy_acr);
}
static uint8_t
asy_get_add(const struct asycom *asy, asy_reg_t reg)
{
uint8_t val;
ASSERT(asy->asy_hwtype >= ASY_16950);
ASSERT(reg >= ASY_ASR);
ASSERT(reg <= ASY_TFL);
asy_put(asy, ASY_ACR, ASY_ACR_ASR | asy->asy_acr);
val = asy_get_reg(asy, reg);
asy_put(asy, ASY_ACR, 0 | asy->asy_acr);
return (val);
}
static void
asy_put_ext(const struct asycom *asy, asy_reg_t reg, uint8_t val)
{
uint8_t lcr;
ASSERT(asy->asy_hwtype >= ASY_16650);
ASSERT(reg >= ASY_EFR);
ASSERT(reg <= ASY_XOFF2);
lcr = asy_get(asy, ASY_LCR);
asy_put(asy, ASY_LCR, ASY_LCR_EFRACCESS);
asy_put_reg(asy, reg, val);
asy_put(asy, ASY_LCR, lcr);
}
static uint8_t
asy_get_ext(const struct asycom *asy, asy_reg_t reg)
{
uint8_t lcr, val;
ASSERT(asy->asy_hwtype >= ASY_16650);
ASSERT(reg >= ASY_EFR);
ASSERT(reg <= ASY_XOFF2);
lcr = asy_get(asy, ASY_LCR);
asy_put(asy, ASY_LCR, ASY_LCR_EFRACCESS);
val = asy_get_reg(asy, reg);
asy_put(asy, ASY_LCR, lcr);
return (val);
}
static void
asy_put_reg(const struct asycom *asy, asy_reg_t reg, uint8_t val)
{
ASSERT(asy->asy_hwtype >= asy_reg_table[reg].asy_min_hwtype);
ddi_put8(asy->asy_iohandle,
asy->asy_ioaddr + asy_reg_table[reg].asy_reg_off, val);
}
static uint8_t
asy_get_reg(const struct asycom *asy, asy_reg_t reg)
{
ASSERT(asy->asy_hwtype >= asy_reg_table[reg].asy_min_hwtype);
return (ddi_get8(asy->asy_iohandle,
asy->asy_ioaddr + asy_reg_table[reg].asy_reg_off));
}
static void
asy_put(const struct asycom *asy, asy_reg_t reg, uint8_t val)
{
ASSERT(mutex_owned(&asy->asy_excl_hi));
ASSERT(reg > ASY_ILLEGAL);
ASSERT(reg < ASY_NREG);
ASSERT(asy->asy_hwtype >= asy_reg_table[reg].asy_min_hwtype);
ASSERT(asy_reg_table[reg].asy_put_reg != NULL);
asy_reg_table[reg].asy_put_reg(asy, reg, val);
}
static uint8_t
asy_get(const struct asycom *asy, asy_reg_t reg)
{
uint8_t val;
ASSERT(mutex_owned(&asy->asy_excl_hi));
ASSERT(reg > ASY_ILLEGAL);
ASSERT(reg < ASY_NREG);
ASSERT(asy->asy_hwtype >= asy_reg_table[reg].asy_min_hwtype);
ASSERT(asy_reg_table[reg].asy_get_reg != NULL);
val = asy_reg_table[reg].asy_get_reg(asy, reg);
return (val);
}
static void
asy_set(const struct asycom *asy, asy_reg_t reg, uint8_t bits)
{
uint8_t val = asy_get(asy, reg);
asy_put(asy, reg, val | bits);
}
static void
asy_clr(const struct asycom *asy, asy_reg_t reg, uint8_t bits)
{
uint8_t val = asy_get(asy, reg);
asy_put(asy, reg, val & ~bits);
}
static void
asy_enable_interrupts(const struct asycom *asy, uint8_t intr)
{
intr &= ASY_IER_ALL;
asy_set(asy, ASY_IER, intr);
}
static void
asy_disable_interrupts(const struct asycom *asy, uint8_t intr)
{
intr &= ASY_IER_ALL;
asy_clr(asy, ASY_IER, intr);
}
static void
asy_set_baudrate(const struct asycom *asy, int baudrate)
{
uint8_t tcr;
if (baudrate == 0)
return;
if (baudrate >= ARRAY_SIZE(asy_baud_tab))
return;
tcr = asy_baud_tab[baudrate].asy_tcr;
if (tcr != 0 && asy->asy_hwtype < ASY_16950)
return;
if (asy->asy_hwtype >= ASY_16950) {
if (tcr == 0x01) {
asy_put(asy, ASY_CKS,
ASY_CKS_RCLK_1X | ASY_CKS_TCLK_1X);
asy_put(asy, ASY_TCR, 0);
} else {
asy_put(asy, ASY_CKS, 0);
ASSERT(tcr == 0x00 || tcr >= 0x04 || tcr <= 0x0f);
asy_put(asy, ASY_TCR, tcr);
}
ASY_DPRINTF(asy, ASY_DEBUG_IOCTL,
"setting baudrate %d, CKS 0x%02x, TCR 0x%02x",
baudrate, asy_get(asy, ASY_CKS), asy_get(asy, ASY_TCR));
}
ASY_DPRINTF(asy, ASY_DEBUG_IOCTL,
"setting baudrate %d, divisor 0x%02x%02x",
baudrate, asy_baud_tab[baudrate].asy_dlh,
asy_baud_tab[baudrate].asy_dll);
asy_set(asy, ASY_LCR, ASY_LCR_DLAB);
asy_put(asy, ASY_DLL, asy_baud_tab[baudrate].asy_dll);
asy_put(asy, ASY_DLH, asy_baud_tab[baudrate].asy_dlh);
asy_clr(asy, ASY_LCR, ASY_LCR_DLAB);
}
static void
asy_wait_baudrate(struct asycom *asy)
{
struct asyncline *async = asy->asy_priv;
int rate = BAUDINDEX(async->async_ttycommon.t_cflag);
clock_t usec =
((((clock_t)asy_baud_tab[rate].asy_dlh) << 8) |
((clock_t)asy_baud_tab[rate].asy_dll)) * 16 * 2;
ASSERT(mutex_owned(&asy->asy_excl));
ASSERT(mutex_owned(&asy->asy_excl_hi));
while ((asy_get(asy, ASY_LSR) & ASY_LSR_TEMT) == 0) {
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
drv_usecwait(usec);
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
}
asy_set(asy, ASY_LCR, ASY_LCR_SETBRK);
}
void
async_put_suspq(struct asycom *asy, mblk_t *mp)
{
struct asyncline *async = asy->asy_priv;
ASSERT(mutex_owned(&asy->asy_excl));
if (async->async_suspqf == NULL)
async->async_suspqf = mp;
else
async->async_suspqb->b_next = mp;
async->async_suspqb = mp;
}
static mblk_t *
async_get_suspq(struct asycom *asy)
{
struct asyncline *async = asy->asy_priv;
mblk_t *mp;
ASSERT(mutex_owned(&asy->asy_excl));
if ((mp = async->async_suspqf) != NULL) {
async->async_suspqf = mp->b_next;
mp->b_next = NULL;
} else {
async->async_suspqb = NULL;
}
return (mp);
}
static void
async_process_suspq(struct asycom *asy)
{
struct asyncline *async = asy->asy_priv;
mblk_t *mp;
ASSERT(mutex_owned(&asy->asy_excl));
while ((mp = async_get_suspq(asy)) != NULL) {
queue_t *q;
q = async->async_ttycommon.t_writeq;
ASSERT(q != NULL);
mutex_exit(&asy->asy_excl);
(void) asywputdo(q, mp, B_FALSE);
mutex_enter(&asy->asy_excl);
}
async->async_flags &= ~ASYNC_DDI_SUSPENDED;
cv_broadcast(&async->async_flags_cv);
}
static int
asy_get_bus_type(dev_info_t *devinfo)
{
char *prop;
int bustype;
if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devinfo, 0, "device_type",
&prop) != DDI_PROP_SUCCESS &&
ddi_prop_lookup_string(DDI_DEV_T_ANY, devinfo, 0, "bus-type",
&prop) != DDI_PROP_SUCCESS) {
dev_err(devinfo, CE_WARN,
"!%s: can't figure out device type for parent \"%s\"",
__func__, ddi_get_name(ddi_get_parent(devinfo)));
return (ASY_BUS_UNKNOWN);
}
if (strcmp(prop, "isa") == 0)
bustype = ASY_BUS_ISA;
else if (strcmp(prop, "pci") == 0)
bustype = ASY_BUS_PCI;
else if (strcmp(prop, "pciex") == 0)
return (ASY_BUS_PCI);
else
bustype = ASY_BUS_UNKNOWN;
ddi_prop_free(prop);
return (bustype);
}
static int
asy_get_io_regnum_pci(dev_info_t *devi, struct asycom *asy)
{
int reglen, nregs;
int regnum, i;
uint64_t size;
struct pci_phys_spec *reglist;
if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
"reg", (caddr_t)®list, ®len) != DDI_PROP_SUCCESS) {
dev_err(devi, CE_WARN, "!%s: reg property"
" not found in devices property list", __func__);
return (-1);
}
regnum = -1;
nregs = reglen / sizeof (*reglist);
for (i = 0; i < nregs; i++) {
switch (reglist[i].pci_phys_hi & PCI_ADDR_MASK) {
case PCI_ADDR_IO:
if (regnum == -1)
regnum = i;
break;
default:
break;
}
}
if (regnum >= 0) {
size = ((uint64_t)reglist[regnum].pci_size_low) |
((uint64_t)reglist[regnum].pci_size_hi) << 32;
if (size < 8)
regnum = -1;
}
kmem_free(reglist, reglen);
return (regnum);
}
static int
asy_get_io_regnum_isa(dev_info_t *devi, struct asycom *asy)
{
int regnum = -1;
int reglen, nregs;
struct {
uint_t bustype;
int base;
int size;
} *reglist;
if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
"reg", (caddr_t)®list, ®len) != DDI_PROP_SUCCESS) {
dev_err(devi, CE_WARN, "!%s: reg property not found "
"in devices property list", __func__);
return (-1);
}
nregs = reglen / sizeof (*reglist);
for (int i = 0; i < nregs && regnum == -1; i++) {
if (reglist[i].bustype == 1) {
regnum = i;
break;
}
}
if ((regnum < 0) || (reglist[regnum].size < 8))
regnum = -1;
kmem_free(reglist, reglen);
return (regnum);
}
static int
asy_get_io_regnum(dev_info_t *devinfo, struct asycom *asy)
{
switch (asy_get_bus_type(devinfo)) {
case ASY_BUS_ISA:
return (asy_get_io_regnum_isa(devinfo, asy));
case ASY_BUS_PCI:
return (asy_get_io_regnum_pci(devinfo, asy));
default:
return (-1);
}
}
static void
asy_intr_free(struct asycom *asy)
{
int i;
for (i = 0; i < asy->asy_intr_cnt; i++) {
if (asy->asy_inth[i] == NULL)
break;
if ((asy->asy_intr_cap & DDI_INTR_FLAG_BLOCK) != 0)
(void) ddi_intr_block_disable(&asy->asy_inth[i], 1);
else
(void) ddi_intr_disable(asy->asy_inth[i]);
(void) ddi_intr_remove_handler(asy->asy_inth[i]);
(void) ddi_intr_free(asy->asy_inth[i]);
}
kmem_free(asy->asy_inth, asy->asy_inth_sz);
asy->asy_inth = NULL;
asy->asy_inth_sz = 0;
}
static int
asy_intr_setup(struct asycom *asy, int intr_type)
{
int nintrs, navail, count;
int ret;
int i;
if (asy->asy_intr_types == 0) {
ret = ddi_intr_get_supported_types(asy->asy_dip,
&asy->asy_intr_types);
if (ret != DDI_SUCCESS) {
asyerror(asy, CE_WARN,
"ddi_intr_get_supported_types failed");
return (ret);
}
}
if ((asy->asy_intr_types & intr_type) == 0)
return (DDI_FAILURE);
ret = ddi_intr_get_nintrs(asy->asy_dip, intr_type, &nintrs);
if (ret != DDI_SUCCESS) {
asyerror(asy, CE_WARN, "ddi_intr_get_nintrs failed, type %d",
intr_type);
return (ret);
}
if (nintrs < 1) {
asyerror(asy, CE_WARN, "no interrupts of type %d", intr_type);
return (DDI_FAILURE);
}
ret = ddi_intr_get_navail(asy->asy_dip, intr_type, &navail);
if (ret != DDI_SUCCESS) {
asyerror(asy, CE_WARN, "ddi_intr_get_navail failed, type %d",
intr_type);
return (ret);
}
if (navail < 1) {
asyerror(asy, CE_WARN, "no available interrupts, type %d",
intr_type);
return (DDI_FAILURE);
}
asy->asy_inth_sz = sizeof (ddi_intr_handle_t);
asy->asy_inth = kmem_zalloc(asy->asy_inth_sz, KM_SLEEP);
ret = ddi_intr_alloc(asy->asy_dip, asy->asy_inth, intr_type, 0, 1,
&count, 0);
if (ret != DDI_SUCCESS) {
asyerror(asy, CE_WARN, "ddi_intr_alloc failed, count %d, "
"type %d", navail, intr_type);
goto fail;
}
if (count != 1) {
asyerror(asy, CE_WARN, "ddi_intr_alloc returned not 1 but %d "
"interrupts of type %d", count, intr_type);
goto fail;
}
asy->asy_intr_cnt = count;
ret = ddi_intr_get_pri(asy->asy_inth[0], &asy->asy_intr_pri);
if (ret != DDI_SUCCESS) {
asyerror(asy, CE_WARN, "ddi_intr_get_pri failed, type %d",
intr_type);
goto fail;
}
for (i = 0; i < count; i++) {
ret = ddi_intr_add_handler(asy->asy_inth[i], asyintr,
(void *)asy, (void *)(uintptr_t)i);
if (ret != DDI_SUCCESS) {
asyerror(asy, CE_WARN, "ddi_intr_add_handler failed, "
"int %d, type %d", i, intr_type);
goto fail;
}
}
(void) ddi_intr_get_cap(asy->asy_inth[0], &asy->asy_intr_cap);
for (i = 0; i < count; i++) {
if (asy->asy_intr_cap & DDI_INTR_FLAG_BLOCK)
ret = ddi_intr_block_enable(&asy->asy_inth[i], 1);
else
ret = ddi_intr_enable(asy->asy_inth[i]);
if (ret != DDI_SUCCESS) {
asyerror(asy, CE_WARN,
"enabling interrupt %d failed, type %d",
i, intr_type);
goto fail;
}
}
asy->asy_intr_type = intr_type;
return (DDI_SUCCESS);
fail:
asy_intr_free(asy);
return (ret);
}
static void
asy_softintr_free(struct asycom *asy)
{
(void) ddi_intr_remove_softint(asy->asy_soft_inth);
}
static int
asy_softintr_setup(struct asycom *asy)
{
int ret;
ret = ddi_intr_add_softint(asy->asy_dip, &asy->asy_soft_inth,
ASY_SOFT_INT_PRI, asysoftintr, asy);
if (ret != DDI_SUCCESS) {
asyerror(asy, CE_WARN, "ddi_intr_add_softint failed");
return (ret);
}
ret = ddi_intr_get_softint_pri(asy->asy_soft_inth,
&asy->asy_soft_intr_pri);
if (ret != DDI_SUCCESS) {
asyerror(asy, CE_WARN, "ddi_intr_get_softint_pri failed");
return (ret);
}
return (DDI_SUCCESS);
}
static int
asy_resume(dev_info_t *devi)
{
struct asyncline *async;
struct asycom *asy;
int instance = ddi_get_instance(devi);
#ifdef DEBUG
if (asy_nosuspend)
return (DDI_SUCCESS);
#endif
asy = ddi_get_soft_state(asy_soft_state, instance);
if (asy == NULL)
return (DDI_FAILURE);
mutex_enter(&asy->asy_soft_sr);
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
async = asy->asy_priv;
asy_disable_interrupts(asy, ASY_IER_ALL);
if (asy_identify_chip(devi, asy) != DDI_SUCCESS) {
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
mutex_exit(&asy->asy_soft_sr);
ASY_DPRINTF(asy, ASY_DEBUG_INIT,
"Cannot identify UART chip at %p",
(void *)asy->asy_ioaddr);
return (DDI_FAILURE);
}
asy->asy_flags &= ~ASY_DDI_SUSPENDED;
if (async->async_flags & ASYNC_ISOPEN) {
asy_program(asy, ASY_INIT);
if (async->async_ocnt > 0) {
async_resume(async);
} else {
mutex_exit(&asy->asy_excl_hi);
if (async->async_xmitblk)
freeb(async->async_xmitblk);
async->async_xmitblk = NULL;
async_start(async);
mutex_enter(&asy->asy_excl_hi);
}
asysetsoft(asy);
}
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
mutex_exit(&asy->asy_soft_sr);
mutex_enter(&asy->asy_excl);
if (async->async_flags & ASYNC_RESUME_BUFCALL) {
async->async_wbufcid = bufcall(async->async_wbufcds,
BPRI_HI, (void (*)(void *)) async_reioctl,
(void *)(intptr_t)async->async_common->asy_unit);
async->async_flags &= ~ASYNC_RESUME_BUFCALL;
}
async_process_suspq(asy);
mutex_exit(&asy->asy_excl);
return (DDI_SUCCESS);
}
static int
asy_suspend(struct asycom *asy)
{
struct asyncline *async = asy->asy_priv;
unsigned i;
uchar_t lsr;
#ifdef DEBUG
if (asy_nosuspend)
return (DDI_SUCCESS);
#endif
mutex_enter(&asy->asy_excl);
ASSERT(async->async_ops >= 0);
while (async->async_ops > 0)
cv_wait(&async->async_ops_cv, &asy->asy_excl);
async->async_flags |= ASYNC_DDI_SUSPENDED;
while ((async->async_flags & (ASYNC_BREAK|ASYNC_DELAY))) {
if (cv_wait_sig(&async->async_flags_cv, &asy->asy_excl) == 0) {
async_process_suspq(asy);
mutex_exit(&asy->asy_excl);
return (DDI_FAILURE);
}
}
if (async->async_flags & ASYNC_OUT_SUSPEND)
async_resume_utbrk(async);
mutex_exit(&asy->asy_excl);
mutex_enter(&asy->asy_soft_sr);
mutex_enter(&asy->asy_excl);
if (async->async_wbufcid != 0) {
bufcall_id_t bcid = async->async_wbufcid;
async->async_wbufcid = 0;
async->async_flags |= ASYNC_RESUME_BUFCALL;
mutex_exit(&asy->asy_excl);
unbufcall(bcid);
mutex_enter(&asy->asy_excl);
}
mutex_enter(&asy->asy_excl_hi);
asy_disable_interrupts(asy, ASY_IER_ALL);
asy->asy_flags |= ASY_DDI_SUSPENDED;
mutex_exit(&asy->asy_excl_hi);
lsr = asy_get(asy, ASY_LSR);
async_rxint(asy, lsr);
for (i = 1000; i > 0; i--) {
lsr = asy_get(asy, ASY_LSR);
if ((lsr & (ASY_LSR_TEMT | ASY_LSR_THRE)) ==
(ASY_LSR_TEMT | ASY_LSR_THRE))
break;
delay(drv_usectohz(10000));
}
if (i == 0)
asyerror(asy, CE_WARN, "transmitter wasn't drained before "
"driver was suspended");
mutex_exit(&asy->asy_excl);
mutex_exit(&asy->asy_soft_sr);
return (DDI_SUCCESS);
}
static int
asydetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
{
int instance;
struct asycom *asy;
instance = ddi_get_instance(devi);
asy = ddi_get_soft_state(asy_soft_state, instance);
if (asy == NULL)
return (DDI_FAILURE);
switch (cmd) {
case DDI_DETACH:
break;
case DDI_SUSPEND:
return (asy_suspend(asy));
default:
return (DDI_FAILURE);
}
ASY_DPRINTF(asy, ASY_DEBUG_INIT, "%s shutdown", asy_hw_name(asy));
if ((asy->asy_progress & ASY_PROGRESS_INT) != 0)
asy_intr_free(asy);
if ((asy->asy_progress & ASY_PROGRESS_SOFTINT) != 0)
asy_softintr_free(asy);
if ((asy->asy_progress & ASY_PROGRESS_ASYNC) != 0) {
struct asyncline *async = asy->asy_priv;
asy->asy_priv = NULL;
if (async->async_dtrtid != 0) {
(void) untimeout(async->async_dtrtid);
async->async_dtrtid = 0;
}
cv_destroy(&async->async_flags_cv);
kmem_free(async, sizeof (struct asyncline));
}
if ((asy->asy_progress & ASY_PROGRESS_MINOR) != 0)
ddi_remove_minor_node(devi, NULL);
if ((asy->asy_progress & ASY_PROGRESS_MUTEX) != 0) {
mutex_destroy(&asy->asy_excl);
mutex_destroy(&asy->asy_excl_hi);
mutex_destroy(&asy->asy_soft_lock);
}
if ((asy->asy_progress & ASY_PROGRESS_REGS) != 0)
ddi_regs_map_free(&asy->asy_iohandle);
ASY_DPRINTF(asy, ASY_DEBUG_INIT, "shutdown complete");
ddi_set_driver_private(devi, NULL);
asy_soft_state_free(asy);
return (DDI_SUCCESS);
}
static int
asyprobe(dev_info_t *devi)
{
return ((asy_get_io_regnum(devi, NULL) < 0) ?
DDI_PROBE_FAILURE : DDI_PROBE_DONTCARE);
}
static int
asyattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
int instance;
int mcr;
int ret;
int regnum = 0;
int i;
struct asycom *asy;
char name[ASY_MINOR_LEN];
int status;
static ddi_device_acc_attr_t ioattr = {
.devacc_attr_version = DDI_DEVICE_ATTR_V1,
.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC,
.devacc_attr_dataorder = DDI_STRICTORDER_ACC,
.devacc_attr_access = DDI_DEFAULT_ACC
};
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
return (asy_resume(devi));
default:
return (DDI_FAILURE);
}
mutex_enter(&asy_glob_lock);
if (com_ports == NULL) {
if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, devi, 0,
"motherboard-serial-ports", &com_ports, &num_com_ports) !=
DDI_PROP_SUCCESS) {
com_ports = (int *)standard_com_ports;
num_com_ports = sizeof (standard_com_ports) /
sizeof (standard_com_ports[0]);
}
if (num_com_ports > 10) {
num_com_ports = 10;
cmn_err(CE_WARN,
"%s: more than %d motherboard-serial-ports",
asy_info.mi_idname, num_com_ports);
}
}
mutex_exit(&asy_glob_lock);
instance = ddi_get_instance(devi);
ret = ddi_soft_state_zalloc(asy_soft_state, instance);
if (ret != DDI_SUCCESS)
return (DDI_FAILURE);
asy = ddi_get_soft_state(asy_soft_state, instance);
asy->asy_dip = devi;
#ifdef DEBUG
asy->asy_debug = debug;
#endif
asy->asy_unit = instance;
regnum = asy_get_io_regnum(devi, asy);
if (regnum < 0 ||
ddi_regs_map_setup(devi, regnum, (caddr_t *)&asy->asy_ioaddr,
(offset_t)0, (offset_t)0, &ioattr, &asy->asy_iohandle)
!= DDI_SUCCESS) {
asyerror(asy, CE_WARN, "could not map UART registers @ %p",
(void *)asy->asy_ioaddr);
goto fail;
}
asy->asy_progress |= ASY_PROGRESS_REGS;
ASY_DPRINTF(asy, ASY_DEBUG_INIT, "UART @ %p", (void *)asy->asy_ioaddr);
for (i = 0; i < num_com_ports; i++) {
if (asy->asy_ioaddr == (uint8_t *)(uintptr_t)com_ports[i]) {
asy->asy_com_port = i + 1;
break;
}
}
ddi_put8(asy->asy_iohandle,
asy->asy_ioaddr + asy_reg_table[ASY_IER].asy_reg_off, 0);
asy->asy_mcr |= ASY_MCR_RTS | ASY_MCR_DTR;
asy->asy_lcr = ASY_LCR_STOP1 | ASY_LCR_BITS8;
asy->asy_bidx = B9600;
asy->asy_fifo_buf = 1;
asy->asy_use_fifo = ASY_FCR_FIFO_OFF;
#ifdef DEBUG
asy->asy_msint_cnt = 0;
#endif
mcr = 0;
if (asy->asy_com_port != 0) {
switch (asy_getproperty(devi, asy, "ignore-cd")) {
case 0:
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"clear ASY_IGNORE_CD");
asy->asy_flags &= ~ASY_IGNORE_CD;
break;
case 1:
default:
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"set ASY_IGNORE_CD, set RTS & DTR");
mcr = asy->asy_mcr;
asy->asy_flags |= ASY_IGNORE_CD;
break;
}
switch (asy_getproperty(devi, asy, "rts-dtr-off")) {
case 0:
asy->asy_flags |= ASY_RTS_DTR_OFF;
mcr = asy->asy_mcr;
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"ASY_RTS_DTR_OFF set and DTR & RTS set");
break;
case 1:
default:
break;
}
asy_parse_mode(devi, asy);
} else {
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"clear ASY_IGNORE_CD, clear RTS & DTR");
asy->asy_flags &= ~ASY_IGNORE_CD;
}
if (asy_softintr_setup(asy) != DDI_SUCCESS) {
asyerror(asy, CE_WARN, "Cannot set soft interrupt");
goto fail;
}
asy->asy_progress |= ASY_PROGRESS_SOFTINT;
if ((asy_intr_setup(asy, DDI_INTR_TYPE_MSIX) != DDI_SUCCESS) &&
(asy_intr_setup(asy, DDI_INTR_TYPE_MSI) != DDI_SUCCESS) &&
(asy_intr_setup(asy, DDI_INTR_TYPE_FIXED) != DDI_SUCCESS)) {
asyerror(asy, CE_WARN, "Cannot set device interrupt");
goto fail;
}
asy->asy_progress |= ASY_PROGRESS_INT;
mutex_init(&asy->asy_soft_lock, NULL, MUTEX_DRIVER,
DDI_INTR_PRI(asy->asy_soft_intr_pri));
mutex_init(&asy->asy_soft_sr, NULL, MUTEX_DRIVER,
DDI_INTR_PRI(asy->asy_soft_intr_pri));
mutex_init(&asy->asy_excl, NULL, MUTEX_DRIVER, NULL);
mutex_init(&asy->asy_excl_hi, NULL, MUTEX_DRIVER,
DDI_INTR_PRI(asy->asy_intr_pri));
asy->asy_progress |= ASY_PROGRESS_MUTEX;
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
if (asy_identify_chip(devi, asy) != DDI_SUCCESS) {
ASY_DPRINTF(asy, ASY_DEBUG_INIT,
"Cannot identify UART chip at %p",
(void *)asy->asy_ioaddr);
goto fail;
}
asy_disable_interrupts(asy, ASY_IER_ALL);
asy_put(asy, ASY_LCR, asy->asy_lcr);
asy_set_baudrate(asy, asy->asy_bidx);
asy_put(asy, ASY_MCR, mcr);
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
asyinit(asy);
asy->asy_progress |= ASY_PROGRESS_ASYNC;
if (asy->asy_com_port != 0) {
name[0] = asy->asy_com_port + 'a' - 1;
name[1] = '\0';
} else {
(void) snprintf(name, ASY_MINOR_LEN, "%d", instance);
}
status = ddi_create_minor_node(devi, name, S_IFCHR, instance,
asy->asy_com_port != 0 ? DDI_NT_SERIAL_MB : DDI_NT_SERIAL, 0);
if (status == DDI_SUCCESS) {
(void) strcat(name, ",cu");
status = ddi_create_minor_node(devi, name, S_IFCHR,
OUTLINE | instance,
asy->asy_com_port != 0 ? DDI_NT_SERIAL_MB_DO :
DDI_NT_SERIAL_DO, 0);
}
if (status != DDI_SUCCESS)
goto fail;
asy->asy_progress |= ASY_PROGRESS_MINOR;
asy->polledio.cons_polledio_version = CONSPOLLEDIO_V0;
asy->polledio.cons_polledio_argument = (cons_polledio_arg_t)asy;
asy->polledio.cons_polledio_putchar = asyputchar;
asy->polledio.cons_polledio_getchar = asygetchar;
asy->polledio.cons_polledio_ischar = asyischar;
asy->polledio.cons_polledio_enter = NULL;
asy->polledio.cons_polledio_exit = NULL;
ddi_set_driver_private(devi, asy);
ddi_report_dev(devi);
ASY_DPRINTF(asy, ASY_DEBUG_INIT, "done");
return (DDI_SUCCESS);
fail:
(void) asydetach(devi, DDI_DETACH);
return (DDI_FAILURE);
}
static int
asyinfo(dev_info_t *dip __unused, ddi_info_cmd_t infocmd, void *arg,
void **result)
{
dev_t dev = (dev_t)arg;
int instance, error;
struct asycom *asy;
instance = UNIT(dev);
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
asy = ddi_get_soft_state(asy_soft_state, instance);
if ((asy == NULL) || (asy->asy_dip == NULL))
error = DDI_FAILURE;
else {
*result = (void *) asy->asy_dip;
error = DDI_SUCCESS;
}
break;
case DDI_INFO_DEVT2INSTANCE:
*result = (void *)(intptr_t)instance;
error = DDI_SUCCESS;
break;
default:
error = DDI_FAILURE;
}
return (error);
}
static int
asy_getproperty(dev_info_t *devi, struct asycom *asy, const char *property)
{
int len;
int ret;
char letter = asy->asy_com_port + 'a' - 1;
char number = asy->asy_com_port + '0';
char val[40];
char name[40];
(void) sprintf(name, "tty%c-%s", letter, property);
len = sizeof (val);
ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
if (ret != DDI_PROP_SUCCESS) {
(void) sprintf(name, "com%c-%s", number, property);
len = sizeof (val);
ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
}
if (ret != DDI_PROP_SUCCESS) {
(void) sprintf(name, "tty0%c-%s", number, property);
len = sizeof (val);
ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
}
if (ret != DDI_PROP_SUCCESS) {
(void) sprintf(name, "port-%c-%s", letter, property);
len = sizeof (val);
ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
}
if (ret != DDI_PROP_SUCCESS)
return (-1);
if (val[0] == 'f' || val[0] == 'F' || val[0] == '0')
return (0);
return (1);
}
static void
asy_soft_state_free(struct asycom *asy)
{
if (asy->asy_priv != NULL) {
kmem_free(asy->asy_priv, sizeof (struct asyncline));
asy->asy_priv = NULL;
}
ddi_soft_state_free(asy_soft_state, asy->asy_unit);
}
static char *
asy_hw_name(struct asycom *asy)
{
switch (asy->asy_hwtype) {
case ASY_8250A:
return ("8250A/16450");
case ASY_16550:
return ("16550");
case ASY_16550A:
return ("16550A");
case ASY_16650:
return ("16650");
case ASY_16750:
return ("16750");
case ASY_16950:
return ("16950");
}
ASY_DPRINTF(asy, ASY_DEBUG_INIT, "unknown asy_hwtype: %d",
asy->asy_hwtype);
return ("?");
}
static boolean_t
asy_is_devid(struct asycom *asy, char *venprop, char *devprop,
int venid, int devid)
{
if (ddi_prop_get_int(DDI_DEV_T_ANY, asy->asy_dip, DDI_PROP_DONTPASS,
venprop, 0) != venid) {
return (B_FALSE);
}
if (ddi_prop_get_int(DDI_DEV_T_ANY, asy->asy_dip, DDI_PROP_DONTPASS,
devprop, 0) != devid) {
return (B_FALSE);
}
return (B_FALSE);
}
static void
asy_check_loopback(struct asycom *asy)
{
if (asy_get_bus_type(asy->asy_dip) != ASY_BUS_PCI)
return;
if (asy_is_devid(asy, "vendor-id", "device-id", 0x11c1, 0x0480) ||
asy_is_devid(asy, "subsystem-vendor-id", "subsystem-id", 0x11c1,
0x0480))
asy->asy_flags2 |= ASY2_NO_LOOPBACK;
}
static int
asy_identify_chip(dev_info_t *devi, struct asycom *asy)
{
int isr, lsr, mcr, spr;
dev_t dev;
uint_t hwtype;
asy->asy_hwtype = ASY_MAXCHIP;
asy_check_loopback(asy);
if (asy_scr_test) {
asy_put(asy, ASY_SPR, ASY_SPR_TEST);
asy_put(asy, ASY_FCR, 0x00);
spr = asy_get(asy, ASY_SPR);
if (spr != ASY_SPR_TEST) {
ASY_DPRINTF(asy, ASY_DEBUG_INIT, "UART @ %p "
"scratch register: expected 0x5a, got 0x%02x",
(void *)asy->asy_ioaddr, spr);
return (DDI_FAILURE);
}
}
asy_put(asy, ASY_FCR, 0x00);
asy_put(asy, ASY_FCR, ASY_FCR_FIFO_EN);
asy_put(asy, ASY_FCR, ASY_FCR_FIFO_EN | ASY_FCR_RHR_FL);
if (asymaxchip >= ASY_16650 && asy_scr_test) {
asy_put(asy, ASY_EFR, 0);
}
asy->asy_fifor = 0;
if (asymaxchip >= ASY_16550A)
asy->asy_fifor |=
ASY_FCR_FIFO_EN | ASY_FCR_DMA | (asy_trig_level & 0xff);
if (asymaxchip >= ASY_16750)
asy->asy_fifor |= ASY_FCR_FIFO64;
asy_reset_fifo(asy, ASY_FCR_THR_FL | ASY_FCR_RHR_FL);
mcr = asy_get(asy, ASY_MCR);
isr = asy_get(asy, ASY_ISR);
if (isr == 0xff) {
asyerror(asy, CE_WARN, "UART @ %p interrupt register: got 0xff",
(void *)asy->asy_ioaddr);
return (DDI_FAILURE);
}
ASY_DPRINTF(asy, ASY_DEBUG_CHIP,
"probe fifo FIFOR=0x%02x ISR=0x%02x MCR=0x%02x",
asy->asy_fifor | ASY_FCR_THR_FL | ASY_FCR_RHR_FL, isr, mcr);
switch (isr & (ASY_ISR_FIFOEN | ASY_ISR_FIFO64)) {
case 0x40:
hwtype = ASY_16550;
asy->asy_fifor = 0;
break;
case ASY_ISR_FIFOEN:
hwtype = ASY_16550A;
asy->asy_fifo_buf = 16;
asy->asy_use_fifo = ASY_FCR_FIFO_EN;
asy->asy_fifor &= ~ASY_FCR_FIFO64;
break;
case ASY_ISR_FIFOEN | ASY_ISR_FIFO64:
hwtype = ASY_16750;
asy->asy_fifo_buf = 64;
asy->asy_use_fifo = ASY_FCR_FIFO_EN;
break;
default:
hwtype = ASY_8250A;
asy->asy_fifor = 0;
}
if (hwtype > asymaxchip) {
asyerror(asy, CE_WARN, "UART @ %p "
"unexpected probe result: "
"FCR=0x%02x ISR=0x%02x MCR=0x%02x",
(void *)asy->asy_ioaddr,
asy->asy_fifor | ASY_FCR_THR_FL | ASY_FCR_RHR_FL, isr, mcr);
return (DDI_FAILURE);
}
asy_reset_fifo(asy, 0);
if (hwtype >= ASY_16550A && asymaxchip >= ASY_16650 &&
asy_scr_test) {
asy_put(asy, ASY_XOFF2, 0);
spr = asy_get(asy, ASY_SPR);
if (spr == ASY_SPR_TEST) {
hwtype = ASY_16650;
asy_put(asy, ASY_EFR, ASY_EFR_ENH_EN);
if (asy->asy_fifo_buf < 32)
asy->asy_fifo_buf = 32;
if (asy_max_tx_fifo >= asy->asy_fifo_buf)
asy->asy_fifor |= ASY_FCR_THR_TRIG_24;
asy_reset_fifo(asy, 0);
}
}
if (hwtype >= ASY_16650 && asymaxchip >= ASY_16950) {
uint8_t ier, asr;
asy_put(asy, ASY_IER, 0);
ier = asy_get(asy, ASY_IER);
if (ier != 0) {
dev_err(asy->asy_dip, CE_WARN, "!%s: UART @ %p "
"interrupt enable register: got 0x%02x", __func__,
(void *)asy->asy_ioaddr, ier);
return (DDI_FAILURE);
}
asr = asy_get(asy, ASY_ASR);
if (asr != ier) {
hwtype = ASY_16950;
if ((asr & ASY_ASR_FIFOSZ) != 0)
asy->asy_fifo_buf = 128;
else
asy->asy_fifo_buf = 16;
asy_reset_fifo(asy, 0);
asy->asy_acr = ASY_ACR_TRIG | ASY_ACR_DTR_NORM;
asy_put(asy, ASY_ACR, asy->asy_acr);
asy_put(asy, ASY_RTL, asy->asy_fifo_buf/2);
asy_put(asy, ASY_TTL, 1);
ASY_DPRINTF(asy, ASY_DEBUG_CHIP, "ASR 0x%02x", asr);
ASY_DPRINTF(asy, ASY_DEBUG_CHIP, "RFL 0x%02x",
asy_get(asy, ASY_RFL));
ASY_DPRINTF(asy, ASY_DEBUG_CHIP, "TFL 0x%02x",
asy_get(asy, ASY_TFL));
ASY_DPRINTF(asy, ASY_DEBUG_CHIP, "ACR 0x%02x",
asy_get(asy, ASY_ACR));
ASY_DPRINTF(asy, ASY_DEBUG_CHIP, "CPR 0x%02x",
asy_get(asy, ASY_CPR));
ASY_DPRINTF(asy, ASY_DEBUG_CHIP, "TCR 0x%02x",
asy_get(asy, ASY_TCR));
ASY_DPRINTF(asy, ASY_DEBUG_CHIP, "CKS 0x%02x",
asy_get(asy, ASY_CKS));
ASY_DPRINTF(asy, ASY_DEBUG_CHIP, "TTL 0x%02x",
asy_get(asy, ASY_TTL));
ASY_DPRINTF(asy, ASY_DEBUG_CHIP, "RTL 0x%02x",
asy_get(asy, ASY_RTL));
ASY_DPRINTF(asy, ASY_DEBUG_CHIP, "FCL 0x%02x",
asy_get(asy, ASY_FCL));
ASY_DPRINTF(asy, ASY_DEBUG_CHIP, "FCH 0x%02x",
asy_get(asy, ASY_FCH));
ASY_DPRINTF(asy, ASY_DEBUG_CHIP,
"Chip ID: %02x%02x%02x,%02x",
asy_get(asy, ASY_ID1), asy_get(asy, ASY_ID2),
asy_get(asy, ASY_ID3), asy_get(asy, ASY_REV));
}
}
asy->asy_hwtype = hwtype;
if (asy_fifo_test > 0 &&
!(asy->asy_flags2 & ASY2_NO_LOOPBACK) &&
(asy->asy_fifo_buf > 16 ||
(asy_fifo_test > 1 && asy->asy_use_fifo == ASY_FCR_FIFO_EN) ||
ASY_DEBUG(asy, ASY_DEBUG_CHIP))) {
int i;
asy_set_baudrate(asy, B57600);
asy_put(asy, ASY_LCR, ASY_LCR_STOP1 | ASY_LCR_BITS8);
asy_put(asy, ASY_MCR, ASY_MCR_LOOPBACK);
for (i = 0; i < asy->asy_fifo_buf * 2; i++) {
asy_put(asy, ASY_THR, i);
}
delay(drv_usectohz(400 * asy->asy_fifo_buf * 3));
for (i = 0; i < asy->asy_fifo_buf * 3; i++) {
lsr = asy_get(asy, ASY_LSR);
if (!(lsr & ASY_LSR_DR))
break;
(void) asy_get(asy, ASY_RHR);
}
ASY_DPRINTF(asy, ASY_DEBUG_CHIP,
"FIFO size: expected=%d, measured=%d",
asy->asy_fifo_buf, i);
hwtype = asy->asy_hwtype;
if (i < asy->asy_fifo_buf) {
if (i >= 16 && asy->asy_fifo_buf >= 16) {
hwtype = ASY_16550A;
asy->asy_fifo_buf = 16;
asy->asy_fifor &=
~(ASY_FCR_THR_TR0 | ASY_FCR_THR_TR1);
} else {
hwtype = ASY_16550;
asy->asy_fifo_buf = 1;
asy->asy_use_fifo = ASY_FCR_FIFO_OFF;
asy->asy_fifor = 0;
}
} else if (i > asy->asy_fifo_buf) {
if (ISP2(i))
asy->asy_fifo_buf = i;
}
if (asy->asy_hwtype >= ASY_16650 && hwtype < ASY_16650) {
asy_put(asy, ASY_EFR, 0);
}
asy_reset_fifo(asy, ASY_FCR_THR_FL | ASY_FCR_RHR_FL);
asy->asy_hwtype = hwtype;
asy_put(asy, ASY_MCR, mcr);
}
ASY_DPRINTF(asy, ASY_DEBUG_CHIP, "%s @ %p",
asy_hw_name(asy), (void *)asy->asy_ioaddr);
dev = makedevice(DDI_MAJOR_T_UNKNOWN, asy->asy_unit);
(void) ddi_prop_update_string(dev, devi, "uart", asy_hw_name(asy));
if (asy->asy_hwtype == ASY_16550)
asy->asy_hwtype = ASY_8250A;
return (DDI_SUCCESS);
}
static void
asyinit(struct asycom *asy)
{
struct asyncline *async;
asy->asy_priv = kmem_zalloc(sizeof (struct asyncline), KM_SLEEP);
async = asy->asy_priv;
mutex_enter(&asy->asy_excl);
async->async_common = asy;
cv_init(&async->async_flags_cv, NULL, CV_DRIVER, NULL);
mutex_exit(&asy->asy_excl);
}
static int
asyopen(queue_t *rq, dev_t *dev, int flag, int sflag __unused, cred_t *cr)
{
struct asycom *asy;
struct asyncline *async;
int unit;
int len;
struct termios *termiosp;
unit = UNIT(*dev);
asy = ddi_get_soft_state(asy_soft_state, unit);
if (asy == NULL)
return (ENXIO);
ASY_DPRINTF(asy, ASY_DEBUG_INIT, "enter");
async = asy->asy_priv;
mutex_enter(&asy->asy_excl);
again:
mutex_enter(&asy->asy_excl_hi);
if (!(async->async_flags & ASYNC_ISOPEN)) {
mutex_exit(&asy->asy_excl_hi);
if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_root_node(),
0, "ttymodes",
(caddr_t)&termiosp, &len) == DDI_PROP_SUCCESS &&
len == sizeof (struct termios)) {
async->async_ttycommon.t_cflag = termiosp->c_cflag;
kmem_free(termiosp, len);
} else {
asyerror(asy, CE_WARN,
"couldn't get ttymodes property");
}
mutex_enter(&asy->asy_excl_hi);
if (asy->asy_cflag)
async->async_ttycommon.t_cflag = asy->asy_cflag;
async->async_ttycommon.t_iflag = 0;
async->async_ttycommon.t_iocpending = NULL;
async->async_ttycommon.t_size.ws_row = 0;
async->async_ttycommon.t_size.ws_col = 0;
async->async_ttycommon.t_size.ws_xpixel = 0;
async->async_ttycommon.t_size.ws_ypixel = 0;
async->async_dev = *dev;
async->async_wbufcid = 0;
async->async_startc = CSTART;
async->async_stopc = CSTOP;
asy_program(asy, ASY_INIT);
} else if ((async->async_ttycommon.t_flags & TS_XCLUDE) &&
secpolicy_excl_open(cr) != 0) {
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
return (EBUSY);
} else if ((*dev & OUTLINE) && !(async->async_flags & ASYNC_OUT)) {
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
return (EBUSY);
}
if (*dev & OUTLINE)
async->async_flags |= ASYNC_OUT;
while (async->async_flags & ASYNC_DTR_DELAY) {
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"waiting for the ASYNC_DTR_DELAY to be clear");
mutex_exit(&asy->asy_excl_hi);
if (cv_wait_sig(&async->async_flags_cv,
&asy->asy_excl) == 0) {
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"interrupted by signal, exiting");
mutex_exit(&asy->asy_excl);
return (EINTR);
}
mutex_enter(&asy->asy_excl_hi);
}
asy_set(asy, ASY_MCR, asy->asy_mcr & ASY_MCR_DTR);
ASY_DPRINTF(asy, ASY_DEBUG_INIT, "\"Raise DTR on every open\": "
"make mcr = %x, make TS_SOFTCAR = %s", asy_get(asy, ASY_MCR),
(asy->asy_flags & ASY_IGNORE_CD) ? "ON" : "OFF");
if (asy->asy_flags & ASY_IGNORE_CD) {
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"ASY_IGNORE_CD set, set TS_SOFTCAR");
async->async_ttycommon.t_flags |= TS_SOFTCAR;
} else {
async->async_ttycommon.t_flags &= ~TS_SOFTCAR;
}
asy->asy_msr = asy_get(asy, ASY_MSR);
ASY_DPRINTF(asy, ASY_DEBUG_INIT, "TS_SOFTCAR is %s, MSR & DCD is %s",
(async->async_ttycommon.t_flags & TS_SOFTCAR) ? "set" : "clear",
(asy->asy_msr & ASY_MSR_DCD) ? "set" : "clear");
if (asy->asy_msr & ASY_MSR_DCD)
async->async_flags |= ASYNC_CARR_ON;
else
async->async_flags &= ~ASYNC_CARR_ON;
mutex_exit(&asy->asy_excl_hi);
if (!(flag & (FNDELAY|FNONBLOCK)) &&
!(async->async_ttycommon.t_cflag & CLOCAL)) {
if ((!(async->async_flags & (ASYNC_CARR_ON|ASYNC_OUT)) &&
!(async->async_ttycommon.t_flags & TS_SOFTCAR)) ||
((async->async_flags & ASYNC_OUT) &&
!(*dev & OUTLINE))) {
async->async_flags |= ASYNC_WOPEN;
if (cv_wait_sig(&async->async_flags_cv,
&asy->asy_excl) == B_FALSE) {
async->async_flags &= ~ASYNC_WOPEN;
mutex_exit(&asy->asy_excl);
return (EINTR);
}
async->async_flags &= ~ASYNC_WOPEN;
goto again;
}
} else if ((async->async_flags & ASYNC_OUT) && !(*dev & OUTLINE)) {
mutex_exit(&asy->asy_excl);
return (EBUSY);
}
async->async_ttycommon.t_readq = rq;
async->async_ttycommon.t_writeq = WR(rq);
rq->q_ptr = WR(rq)->q_ptr = (caddr_t)async;
mutex_exit(&asy->asy_excl);
qprocson(rq);
async->async_flags |= ASYNC_ISOPEN;
async->async_polltid = 0;
ASY_DPRINTF(asy, ASY_DEBUG_INIT, "done");
return (0);
}
static void
async_progress_check(void *arg)
{
struct asyncline *async = arg;
struct asycom *asy = async->async_common;
mblk_t *bp;
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
if (!(async->async_flags & (ASYNC_BREAK|ASYNC_DELAY|ASYNC_PROGRESS))) {
async->async_ocnt = 0;
async->async_flags &= ~ASYNC_BUSY;
async->async_timer = 0;
bp = async->async_xmitblk;
async->async_xmitblk = NULL;
mutex_exit(&asy->asy_excl_hi);
if (bp != NULL)
freeb(bp);
flushq(async->async_ttycommon.t_writeq, FLUSHALL);
cv_broadcast(&async->async_flags_cv);
} else {
async->async_flags &= ~ASYNC_PROGRESS;
async->async_timer = timeout(async_progress_check, async,
drv_usectohz(asy_drain_check));
mutex_exit(&asy->asy_excl_hi);
}
mutex_exit(&asy->asy_excl);
}
static void
async_dtr_free(struct asyncline *async)
{
struct asycom *asy = async->async_common;
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"async_dtr_free, clearing ASYNC_DTR_DELAY");
mutex_enter(&asy->asy_excl);
async->async_flags &= ~ASYNC_DTR_DELAY;
async->async_dtrtid = 0;
cv_broadcast(&async->async_flags_cv);
mutex_exit(&asy->asy_excl);
}
static int
asyclose(queue_t *q, int flag, cred_t *credp __unused)
{
struct asyncline *async;
struct asycom *asy;
async = (struct asyncline *)q->q_ptr;
ASSERT(async != NULL);
asy = async->async_common;
ASY_DPRINTF(asy, ASY_DEBUG_CLOSE, "enter");
mutex_enter(&asy->asy_excl);
async->async_flags |= ASYNC_CLOSING;
mutex_enter(&asy->asy_excl_hi);
asy->asy_flags &= ~(ASY_PPS | ASY_PPS_EDGE);
mutex_exit(&asy->asy_excl_hi);
if (async->async_flags & ASYNC_OUT_SUSPEND) {
if (async->async_utbrktid != 0) {
(void) untimeout(async->async_utbrktid);
async->async_utbrktid = 0;
}
mutex_enter(&asy->asy_excl_hi);
(void) asy_clr(asy, ASY_LCR, ASY_LCR_SETBRK);
mutex_exit(&asy->asy_excl_hi);
async->async_flags &= ~ASYNC_OUT_SUSPEND;
goto nodrain;
}
if ((flag & (FNDELAY|FNONBLOCK)) ||
(async->async_flags & ASYNC_STOPPED)) {
goto nodrain;
}
if (!ddi_can_receive_sig() && asy_drain_check != 0) {
async->async_flags &= ~ASYNC_PROGRESS;
async->async_timer = timeout(async_progress_check, async,
drv_usectohz(asy_drain_check));
}
while (async->async_ocnt > 0 ||
async->async_ttycommon.t_writeq->q_first != NULL ||
(async->async_flags & (ASYNC_BUSY|ASYNC_BREAK|ASYNC_DELAY))) {
if (cv_wait_sig(&async->async_flags_cv, &asy->asy_excl) == 0)
break;
}
if (async->async_timer != 0) {
(void) untimeout(async->async_timer);
async->async_timer = 0;
}
nodrain:
async->async_ocnt = 0;
if (async->async_xmitblk != NULL)
freeb(async->async_xmitblk);
async->async_xmitblk = NULL;
ASY_DPRINTF(asy, ASY_DEBUG_MODEM, "next check HUPCL flag");
mutex_enter(&asy->asy_excl_hi);
if ((async->async_ttycommon.t_cflag & HUPCL) ||
(async->async_flags & ASYNC_WOPEN)) {
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"HUPCL flag = %x, ASYNC_WOPEN flag = %x",
async->async_ttycommon.t_cflag & HUPCL,
async->async_ttycommon.t_cflag & ASYNC_WOPEN);
async->async_flags |= ASYNC_DTR_DELAY;
if (asy->asy_flags & (ASY_IGNORE_CD|ASY_RTS_DTR_OFF)) {
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"ASY_IGNORE_CD flag = %x, "
"ASY_RTS_DTR_OFF flag = %x",
asy->asy_flags & ASY_IGNORE_CD,
asy->asy_flags & ASY_RTS_DTR_OFF);
asy_put(asy, ASY_MCR, asy->asy_mcr | ASY_MCR_OUT2);
} else {
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"Dropping DTR and RTS");
asy_put(asy, ASY_MCR, ASY_MCR_OUT2);
}
async->async_dtrtid =
timeout((void (*)())async_dtr_free,
(caddr_t)async, drv_usectohz(asy_min_dtr_low));
}
if ((async->async_flags & (ASYNC_WOPEN|ASYNC_ISOPEN)) == 0)
asy_disable_interrupts(asy, ASY_IER_RIEN);
mutex_exit(&asy->asy_excl_hi);
ttycommon_close(&async->async_ttycommon);
if (async->async_wbufcid != 0) {
unbufcall(async->async_wbufcid);
async->async_wbufcid = 0;
}
qprocsoff(q);
q->q_ptr = WR(q)->q_ptr = NULL;
async->async_ttycommon.t_readq = NULL;
async->async_ttycommon.t_writeq = NULL;
async->async_flags &= (ASYNC_DTR_DELAY|ASY_RTS_DTR_OFF);
cv_broadcast(&async->async_flags_cv);
mutex_exit(&asy->asy_excl);
ASY_DPRINTF(asy, ASY_DEBUG_CLOSE, "done");
return (0);
}
static boolean_t
asy_isbusy(struct asycom *asy)
{
struct asyncline *async;
ASY_DPRINTF(asy, ASY_DEBUG_EOT, "enter");
async = asy->asy_priv;
ASSERT(mutex_owned(&asy->asy_excl));
ASSERT(mutex_owned(&asy->asy_excl_hi));
return ((async->async_ocnt > 0) ||
((asy_get(asy, ASY_LSR) & (ASY_LSR_TEMT | ASY_LSR_THRE)) == 0));
}
static void
asy_waiteot(struct asycom *asy)
{
ASY_DPRINTF(asy, ASY_DEBUG_EOT, "enter");
ASSERT(mutex_owned(&asy->asy_excl));
ASSERT(mutex_owned(&asy->asy_excl_hi));
while (asy_isbusy(asy)) {
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
drv_usecwait(10000);
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
}
}
static void
asy_reset_fifo(struct asycom *asy, uchar_t flush)
{
ASSERT(mutex_owned(&asy->asy_excl_hi));
if (asy->asy_hwtype >= ASY_16750)
asy_set(asy, ASY_LCR, ASY_LCR_DLAB);
asy_put(asy, ASY_FCR, asy->asy_fifor | flush);
if (asy->asy_hwtype >= ASY_16750)
asy_clr(asy, ASY_LCR, ASY_LCR_DLAB);
}
static void
asy_program(struct asycom *asy, int mode)
{
struct asyncline *async;
int baudrate, c_flag;
uint8_t ier;
int flush_reg;
int ocflags;
ASSERT(mutex_owned(&asy->asy_excl));
ASSERT(mutex_owned(&asy->asy_excl_hi));
async = asy->asy_priv;
ASY_DPRINTF(asy, ASY_DEBUG_PROCS, "mode = 0x%08X, enter", mode);
baudrate = BAUDINDEX(async->async_ttycommon.t_cflag);
async->async_ttycommon.t_cflag &= ~(CIBAUD);
if (baudrate > CBAUD) {
async->async_ttycommon.t_cflag |= CIBAUDEXT;
async->async_ttycommon.t_cflag |=
(((baudrate - CBAUD - 1) << IBSHIFT) & CIBAUD);
} else {
async->async_ttycommon.t_cflag &= ~CIBAUDEXT;
async->async_ttycommon.t_cflag |=
((baudrate << IBSHIFT) & CIBAUD);
}
c_flag = async->async_ttycommon.t_cflag &
(CLOCAL|CREAD|CSTOPB|CSIZE|PARENB|PARODD|CBAUD|CBAUDEXT);
asy_disable_interrupts(asy, ASY_IER_ALL);
ocflags = asy->asy_ocflag;
(void) asy_get(asy, ASY_ISR);
(void) asy_get(asy, ASY_LSR);
asy->asy_msr = flush_reg = asy_get(asy, ASY_MSR);
if ((CRTSCTS & async->async_ttycommon.t_cflag) &&
!(flush_reg & ASY_MSR_CTS)) {
async_flowcontrol_hw_output(asy, FLOW_STOP);
} else {
async->async_flags &= ~ASYNC_HW_OUT_FLW;
}
if (!(IXON & async->async_ttycommon.t_iflag))
async->async_flags &= ~ASYNC_SW_OUT_FLW;
if (mode == ASY_INIT) {
if (asy->asy_use_fifo == ASY_FCR_FIFO_EN) {
for (flush_reg = asy->asy_fifo_buf; flush_reg-- > 0; ) {
(void) asy_get(asy, ASY_RHR);
}
} else {
flush_reg = asy_get(asy, ASY_RHR);
}
}
if (ocflags != (c_flag & ~CLOCAL) || mode == ASY_INIT) {
uint8_t lcr = 0;
if (c_flag & CSTOPB)
lcr |= ASY_LCR_STOP2;
if (c_flag & PARENB)
lcr |= ASY_LCR_PEN;
if ((c_flag & PARODD) == 0)
lcr |= ASY_LCR_EPS;
switch (c_flag & CSIZE) {
case CS5:
lcr |= ASY_LCR_BITS5;
break;
case CS6:
lcr |= ASY_LCR_BITS6;
break;
case CS7:
lcr |= ASY_LCR_BITS7;
break;
case CS8:
lcr |= ASY_LCR_BITS8;
break;
}
asy_clr(asy, ASY_LCR, ASY_LCR_WLS0 | ASY_LCR_WLS1 |
ASY_LCR_STB | ASY_LCR_PEN | ASY_LCR_EPS);
asy_set(asy, ASY_LCR, lcr);
asy_set_baudrate(asy, baudrate);
if (((ocflags & CREAD) == 0 && (c_flag & CREAD)) ||
mode == ASY_INIT) {
if (asy->asy_use_fifo == ASY_FCR_FIFO_EN)
asy_reset_fifo(asy, ASY_FCR_RHR_FL);
}
asy->asy_ocflag = c_flag & ~CLOCAL;
}
if (baudrate == 0)
asy_put(asy, ASY_MCR,
(asy->asy_mcr & ASY_MCR_RTS) | ASY_MCR_OUT2);
else
asy_put(asy, ASY_MCR, asy->asy_mcr | ASY_MCR_OUT2);
async_msint(asy);
ASY_DPRINTF(asy, ASY_DEBUG_MODM2,
"c_flag & CLOCAL = %x t_cflag & CRTSCTS = %x",
c_flag & CLOCAL, async->async_ttycommon.t_cflag & CRTSCTS);
ier = ASY_IER_TIEN | ASY_IER_SIEN;
if (((c_flag & CLOCAL) == 0) ||
(async->async_ttycommon.t_cflag & CRTSCTS))
ier |= ASY_IER_MIEN;
if (c_flag & CREAD)
ier |= ASY_IER_RIEN;
asy_enable_interrupts(asy, ier);
ASY_DPRINTF(asy, ASY_DEBUG_PROCS, "done");
}
static boolean_t
asy_baudok(struct asycom *asy)
{
struct asyncline *async = asy->asy_priv;
int baudrate;
baudrate = BAUDINDEX(async->async_ttycommon.t_cflag);
if (baudrate >= ARRAY_SIZE(asy_baud_tab))
return (0);
return (baudrate == 0 ||
asy_baud_tab[baudrate].asy_dll != 0 ||
asy_baud_tab[baudrate].asy_dlh != 0);
}
uint_t
asyintr(caddr_t argasy, caddr_t argunused __unused)
{
struct asycom *asy = (struct asycom *)argasy;
struct asyncline *async;
int ret_status = DDI_INTR_UNCLAIMED;
mutex_enter(&asy->asy_excl_hi);
async = asy->asy_priv;
if (async == NULL ||
(async->async_flags & (ASYNC_ISOPEN|ASYNC_WOPEN)) == 0) {
const uint8_t intr_id = asy_get(asy, ASY_ISR);
ASY_DPRINTF(asy, ASY_DEBUG_INTR,
"not open async=%p flags=0x%x interrupt_id=0x%x",
async, async == NULL ? 0 : async->async_flags, intr_id);
if ((intr_id & ASY_ISR_NOINTR) == 0) {
(void) asy_get(asy, ASY_LSR);
(void) asy_get(asy, ASY_RHR);
asy->asy_msr = asy_get(asy, ASY_MSR);
ret_status = DDI_INTR_CLAIMED;
}
mutex_exit(&asy->asy_excl_hi);
return (ret_status);
}
ret_status = DDI_INTR_CLAIMED;
if (asy->asy_flags & ASY_DDI_SUSPENDED) {
mutex_exit(&asy->asy_excl_hi);
return (ret_status);
}
for (;;) {
const uint8_t intr_id = asy_get(asy, ASY_ISR);
const uint8_t lsr = asy_get(asy, ASY_LSR);
ASY_DPRINTF(asy, ASY_DEBUG_INTR,
"interrupt_id=0x%x LSR=0x%x",
intr_id, lsr);
if (intr_id & ASY_ISR_NOINTR)
break;
switch (intr_id & ASY_ISR_MASK) {
case ASY_ISR_ID_RLST:
case ASY_ISR_ID_RDA:
case ASY_ISR_ID_TMO:
async_rxint(asy, lsr);
break;
case ASY_ISR_ID_THRE:
if ((lsr & ASY_LSR_THRE) == 0) {
continue;
}
async_txint(asy);
continue;
case ASY_ISR_ID_MST:
async_msint(asy);
break;
}
if ((lsr & ASY_LSR_THRE) && (async->async_flags & ASYNC_BUSY) &&
async->async_ocnt > 0)
async_txint(asy);
}
mutex_exit(&asy->asy_excl_hi);
return (ret_status);
}
static void
async_txint(struct asycom *asy)
{
struct asyncline *async = asy->asy_priv;
int fifo_len;
ASSERT(MUTEX_HELD(&asy->asy_excl_hi));
if (async->async_flags & (ASYNC_BREAK|ASYNC_OUT_SUSPEND))
return;
fifo_len = asy->asy_fifo_buf;
if (fifo_len > asy_max_tx_fifo)
fifo_len = asy_max_tx_fifo;
if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
fifo_len--;
if (async->async_ocnt > 0 && fifo_len > 0 &&
!(async->async_flags &
(ASYNC_HW_OUT_FLW|ASYNC_SW_OUT_FLW|ASYNC_STOPPED))) {
while (fifo_len-- > 0 && async->async_ocnt-- > 0) {
asy_put(asy, ASY_THR, *async->async_optr++);
}
async->async_flags |= ASYNC_PROGRESS;
}
if (fifo_len <= 0)
return;
asysetsoft(asy);
}
static void
asy_ppsevent(struct asycom *asy, int msr)
{
ASSERT(MUTEX_HELD(&asy->asy_excl_hi));
if (asy->asy_flags & ASY_PPS_EDGE) {
if ((msr & ASY_MSR_DCD) == 0)
asy->asy_flags &= ~ASY_PPS_EDGE;
} else if (msr & ASY_MSR_DCD) {
struct timeval *tvp = &asy_ppsev.tv;
timestruc_t ts;
long nsec, usec;
asy->asy_flags |= ASY_PPS_EDGE;
LED_OFF;
gethrestime(&ts);
LED_ON;
nsec = ts.tv_nsec;
usec = nsec + (nsec >> 2);
usec = nsec + (usec >> 1);
usec = nsec + (usec >> 2);
usec = nsec + (usec >> 4);
usec = nsec - (usec >> 3);
usec = nsec + (usec >> 2);
usec = nsec + (usec >> 3);
usec = nsec + (usec >> 4);
usec = nsec + (usec >> 1);
usec = nsec + (usec >> 6);
tvp->tv_usec = usec >> 10;
tvp->tv_sec = ts.tv_sec;
++asy_ppsev.serial;
ddi_hardpps(tvp, 0);
}
}
static void
async_rxint(struct asycom *asy, uchar_t lsr)
{
struct asyncline *async = asy->asy_priv;
uchar_t c;
uint_t s, needsoft = 0;
tty_common_t *tp;
int looplim = asy->asy_fifo_buf * 2;
ASSERT(MUTEX_HELD(&asy->asy_excl_hi));
tp = &async->async_ttycommon;
if (!(tp->t_cflag & CREAD)) {
while ((lsr & (ASY_LSR_DR | ASY_LSR_ERRORS)) != 0) {
(void) asy_get(asy, ASY_RHR);
lsr = asy_get(asy, ASY_LSR);
if (looplim-- < 0)
break;
}
return;
}
while ((lsr & (ASY_LSR_DR | ASY_LSR_ERRORS)) != 0) {
c = 0;
s = 0;
if (lsr & ASY_LSR_DR) {
c = asy_get(asy, ASY_RHR);
if (tp->t_iflag & IXON) {
if ((c == async->async_stopc) &&
(c != _POSIX_VDISABLE)) {
async_flowcontrol_sw_output(asy,
FLOW_STOP);
goto check_looplim;
} else if ((c == async->async_startc) &&
(c != _POSIX_VDISABLE)) {
async_flowcontrol_sw_output(asy,
FLOW_START);
needsoft = 1;
goto check_looplim;
}
if ((tp->t_iflag & IXANY) &&
(async->async_flags & ASYNC_SW_OUT_FLW)) {
async_flowcontrol_sw_output(asy,
FLOW_START);
needsoft = 1;
}
}
}
if ((abort_enable == KIOCABORTALTERNATE) &&
(asy->asy_flags & ASY_CONSOLE)) {
if (abort_charseq_recognize(c))
abort_sequence_enter((char *)NULL);
}
if (lsr & ASY_LSR_ERRORS) {
if (lsr & ASY_LSR_PE) {
if (tp->t_iflag & INPCK)
s |= PERROR;
}
if (lsr & (ASY_LSR_FE | ASY_LSR_BI))
s |= FRERROR;
if (lsr & ASY_LSR_OE) {
async->async_hw_overrun = 1;
s |= OVERRUN;
}
}
if (s == 0)
if ((tp->t_iflag & PARMRK) &&
!(tp->t_iflag & (IGNPAR|ISTRIP)) &&
(c == 0377))
if (RING_POK(async, 2)) {
RING_PUT(async, 0377);
RING_PUT(async, c);
} else
async->async_sw_overrun = 1;
else
if (RING_POK(async, 1))
RING_PUT(async, c);
else
async->async_sw_overrun = 1;
else
if (s & FRERROR)
if (c == 0)
if ((asy->asy_flags & ASY_CONSOLE) &&
(abort_enable !=
KIOCABORTALTERNATE))
abort_sequence_enter((char *)0);
else
async->async_break++;
else
if (RING_POK(async, 1))
RING_MARK(async, c, s);
else
async->async_sw_overrun = 1;
else
if (RING_POK(async, 1))
RING_MARK(async, c, s);
else
async->async_sw_overrun = 1;
check_looplim:
lsr = asy_get(asy, ASY_LSR);
if (looplim-- < 0)
break;
}
if ((RING_CNT(async) > (RINGSIZE * 3)/4) &&
!(async->async_inflow_source & IN_FLOW_RINGBUFF)) {
async_flowcontrol_hw_input(asy, FLOW_STOP, IN_FLOW_RINGBUFF);
(void) async_flowcontrol_sw_input(asy, FLOW_STOP,
IN_FLOW_RINGBUFF);
}
if ((async->async_flags & ASYNC_SERVICEIMM) || needsoft ||
(RING_FRAC(async)) || (async->async_polltid == 0)) {
asysetsoft(asy);
}
}
static void
async_msint(struct asycom *asy)
{
struct asyncline *async = asy->asy_priv;
int msr, t_cflag = async->async_ttycommon.t_cflag;
ASSERT(MUTEX_HELD(&asy->asy_excl_hi));
async_msint_retry:
msr = asy_get(asy, ASY_MSR);
ASY_DPRINTF(asy, ASY_DEBUG_STATE, "call #%d:",
++(asy->asy_msint_cnt));
ASY_DPRINTF(asy, ASY_DEBUG_STATE, " transition: %3s %3s %3s %3s",
(msr & ASY_MSR_DCTS) ? "DCTS" : " ",
(msr & ASY_MSR_DDSR) ? "DDSR" : " ",
(msr & ASY_MSR_TERI) ? "TERI" : " ",
(msr & ASY_MSR_DDCD) ? "DDCD" : " ");
ASY_DPRINTF(asy, ASY_DEBUG_STATE, "current state: %3s %3s %3s %3s",
(msr & ASY_MSR_CTS) ? "CTS " : " ",
(msr & ASY_MSR_DSR) ? "DSR " : " ",
(msr & ASY_MSR_RI) ? "RI " : " ",
(msr & ASY_MSR_DCD) ? "DCD " : " ");
if ((t_cflag & CRTSCTS) && (((asy->asy_msr ^ msr) & ASY_MSR_CTS) != 0))
async_flowcontrol_hw_output(asy,
msr & ASY_MSR_CTS ? FLOW_START : FLOW_STOP);
asy->asy_msr = (uchar_t)msr;
if (asy->asy_flags & ASY_PPS)
asy_ppsevent(asy, msr);
async->async_ext++;
asysetsoft(asy);
msr = asy_get(asy, ASY_MSR);
if (ASY_MSR_STATES(msr) != ASY_MSR_STATES(asy->asy_msr))
goto async_msint_retry;
}
static void
asysetsoft(struct asycom *asy)
{
ASSERT(MUTEX_HELD(&asy->asy_excl_hi));
if (mutex_tryenter(&asy->asy_soft_lock) == 0)
return;
asy->asy_flags |= ASY_NEEDSOFT;
if (!asy->asysoftpend) {
asy->asysoftpend = 1;
mutex_exit(&asy->asy_soft_lock);
(void) ddi_intr_trigger_softint(asy->asy_soft_inth, NULL);
} else {
mutex_exit(&asy->asy_soft_lock);
}
}
static void
asy_carrier_check(struct asycom *asy)
{
struct asyncline *async = asy->asy_priv;
tty_common_t *tp = &async->async_ttycommon;
queue_t *q = tp->t_readq;
mblk_t *bp;
int flushflag;
ASY_DPRINTF(asy, ASY_DEBUG_MODM2,
"asy_msr & DCD = %x, tp->t_flags & TS_SOFTCAR = %x",
asy->asy_msr & ASY_MSR_DCD, tp->t_flags & TS_SOFTCAR);
if (asy->asy_msr & ASY_MSR_DCD) {
if ((async->async_flags & ASYNC_CARR_ON) != 0)
return;
ASY_DPRINTF(asy, ASY_DEBUG_MODM2, "set ASYNC_CARR_ON");
async->async_flags |= ASYNC_CARR_ON;
if (async->async_flags & ASYNC_ISOPEN) {
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
(void) putctl(q, M_UNHANGUP);
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
}
cv_broadcast(&async->async_flags_cv);
return;
}
if ((async->async_flags & ASYNC_CARR_ON) == 0)
return;
if ((tp->t_cflag & CLOCAL) != 0 || (tp->t_flags & TS_SOFTCAR) != 0)
goto out;
ASY_DPRINTF(asy, ASY_DEBUG_MODEM, "carrier dropped, so drop DTR");
asy_clr(asy, ASY_MCR, ASY_MCR_DTR);
if (async->async_flags & ASYNC_BUSY) {
ASY_DPRINTF(asy, ASY_DEBUG_BUSY,
"Carrier dropped. Clearing async_ocnt");
async->async_ocnt = 0;
}
async->async_flags &= ~ASYNC_STOPPED;
if ((async->async_flags & ASYNC_ISOPEN) == 0)
goto out;
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
(void) putctl(q, M_HANGUP);
mutex_enter(&asy->asy_excl);
ASY_DPRINTF(asy, ASY_DEBUG_MODEM, "putctl(q, M_HANGUP)");
if (asy->asy_use_fifo == ASY_FCR_FIFO_EN) {
mutex_enter(&asy->asy_excl_hi);
asy_reset_fifo(asy, ASY_FCR_THR_FL);
mutex_exit(&asy->asy_excl_hi);
}
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"Flushing to prevent HUPCL hanging");
flushflag = (async->async_flags & ASYNC_CLOSING) ? FLUSHALL : FLUSHDATA;
flushq(tp->t_writeq, flushflag);
bp = async->async_xmitblk;
if (bp != NULL) {
freeb(bp);
async->async_xmitblk = NULL;
}
mutex_enter(&asy->asy_excl_hi);
async->async_flags &= ~ASYNC_BUSY;
out:
async->async_flags &= ~ASYNC_CARR_ON;
cv_broadcast(&async->async_flags_cv);
}
uint_t
asysoftintr(caddr_t intarg, caddr_t unusedarg __unused)
{
struct asycom *asy = (struct asycom *)intarg;
struct asyncline *async;
int rv;
uint_t cc;
mutex_enter(&asy->asy_soft_lock);
ASY_DPRINTF(asy, ASY_DEBUG_PROCS, "enter");
rv = asy->asysoftpend;
if (rv != 0)
asy->asysoftpend = 0;
mutex_exit(&asy->asy_soft_lock);
if (rv) {
if (asy->asy_priv == NULL)
return (rv ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED);
async = (struct asyncline *)asy->asy_priv;
mutex_enter(&asy->asy_excl_hi);
if (asy->asy_flags & ASY_NEEDSOFT) {
asy->asy_flags &= ~ASY_NEEDSOFT;
mutex_exit(&asy->asy_excl_hi);
async_softint(asy);
mutex_enter(&asy->asy_excl_hi);
}
cc = RING_CNT(async);
mutex_exit(&asy->asy_excl_hi);
if (cc > 0)
(void) async_softint(asy);
}
return (rv ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED);
}
static void
async_softint(struct asycom *asy)
{
struct asyncline *async = asy->asy_priv;
uint_t cc;
mblk_t *bp;
queue_t *q;
uchar_t c;
tty_common_t *tp;
int nb;
ASY_DPRINTF(asy, ASY_DEBUG_PROCS, "enter");
mutex_enter(&asy->asy_excl_hi);
if (asy->asy_flags & ASY_DOINGSOFT) {
asy->asy_flags |= ASY_DOINGSOFT_RETRY;
mutex_exit(&asy->asy_excl_hi);
return;
}
asy->asy_flags |= ASY_DOINGSOFT;
begin:
asy->asy_flags &= ~ASY_DOINGSOFT_RETRY;
mutex_exit(&asy->asy_excl_hi);
mutex_enter(&asy->asy_excl);
tp = &async->async_ttycommon;
q = tp->t_readq;
if (async->async_flags & ASYNC_OUT_FLW_RESUME) {
if (async->async_ocnt > 0) {
mutex_enter(&asy->asy_excl_hi);
async_resume(async);
mutex_exit(&asy->asy_excl_hi);
} else {
if (async->async_xmitblk)
freeb(async->async_xmitblk);
async->async_xmitblk = NULL;
async_start(async);
}
async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
}
mutex_enter(&asy->asy_excl_hi);
if (async->async_ext) {
async->async_ext = 0;
asy_carrier_check(asy);
}
mutex_exit(&asy->asy_excl_hi);
mutex_enter(&asy->asy_excl_hi);
if (!(async->async_flags & ASYNC_ISOPEN)) {
RING_INIT(async);
goto rv;
}
if ((cc = RING_CNT(async)) == 0)
goto rv;
mutex_exit(&asy->asy_excl_hi);
if (!canput(q)) {
mutex_enter(&asy->asy_excl_hi);
if (!(async->async_inflow_source & IN_FLOW_STREAMS)) {
async_flowcontrol_hw_input(asy, FLOW_STOP,
IN_FLOW_STREAMS);
(void) async_flowcontrol_sw_input(asy, FLOW_STOP,
IN_FLOW_STREAMS);
}
goto rv;
}
if (async->async_inflow_source & IN_FLOW_STREAMS) {
mutex_enter(&asy->asy_excl_hi);
async_flowcontrol_hw_input(asy, FLOW_START,
IN_FLOW_STREAMS);
(void) async_flowcontrol_sw_input(asy, FLOW_START,
IN_FLOW_STREAMS);
mutex_exit(&asy->asy_excl_hi);
}
ASY_DPRINTF(asy, ASY_DEBUG_INPUT, "%d char(s) in queue", cc);
if (!(bp = allocb(cc, BPRI_MED))) {
mutex_exit(&asy->asy_excl);
ttycommon_qfull(&async->async_ttycommon, q);
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
goto rv;
}
mutex_enter(&asy->asy_excl_hi);
do {
if (RING_ERR(async, S_ERRORS)) {
RING_UNMARK(async);
c = RING_GET(async);
break;
} else {
*bp->b_wptr++ = RING_GET(async);
}
} while (--cc);
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
if (bp->b_wptr > bp->b_rptr) {
if (!canput(q)) {
asyerror(asy, CE_WARN, "local queue full");
freemsg(bp);
} else {
(void) putq(q, bp);
}
} else {
freemsg(bp);
}
if (cc)
(void) putctl1(q, M_BREAK, c);
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
if (cc) {
asysetsoft(asy);
}
rv:
if ((RING_CNT(async) < (RINGSIZE/4)) &&
(async->async_inflow_source & IN_FLOW_RINGBUFF)) {
async_flowcontrol_hw_input(asy, FLOW_START, IN_FLOW_RINGBUFF);
(void) async_flowcontrol_sw_input(asy, FLOW_START,
IN_FLOW_RINGBUFF);
}
if (async->async_break > 0) {
nb = async->async_break;
async->async_break = 0;
if (async->async_flags & ASYNC_ISOPEN) {
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
for (; nb > 0; nb--)
(void) putctl(q, M_BREAK);
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
}
}
if (async->async_ocnt <= 0 && (async->async_flags & ASYNC_BUSY)) {
ASY_DPRINTF(asy, ASY_DEBUG_BUSY,
"Clearing ASYNC_BUSY, async_ocnt=%d", async->async_ocnt);
async->async_flags &= ~ASYNC_BUSY;
mutex_exit(&asy->asy_excl_hi);
if (async->async_xmitblk)
freeb(async->async_xmitblk);
async->async_xmitblk = NULL;
async_start(async);
if (!(async->async_flags & ASYNC_BUSY))
cv_broadcast(&async->async_flags_cv);
mutex_enter(&asy->asy_excl_hi);
}
if (async->async_hw_overrun) {
if (async->async_flags & ASYNC_ISOPEN) {
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
asyerror(asy, CE_WARN, "silo overflow");
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
}
async->async_hw_overrun = 0;
}
if (async->async_sw_overrun) {
if (async->async_flags & ASYNC_ISOPEN) {
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
asyerror(asy, CE_WARN, "ring buffer overflow");
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
}
async->async_sw_overrun = 0;
}
if (asy->asy_flags & ASY_DOINGSOFT_RETRY) {
mutex_exit(&asy->asy_excl);
goto begin;
}
asy->asy_flags &= ~ASY_DOINGSOFT;
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
ASY_DPRINTF(asy, ASY_DEBUG_PROCS, "done");
}
static void
async_restart(void *arg)
{
struct asyncline *async = (struct asyncline *)arg;
struct asycom *asy = async->async_common;
ASY_DPRINTF(asy, ASY_DEBUG_PROCS, "enter");
mutex_enter(&asy->asy_excl);
if ((async->async_flags & ASYNC_BREAK) &&
!(async->async_flags & ASYNC_OUT_SUSPEND)) {
mutex_enter(&asy->asy_excl_hi);
asy_clr(asy, ASY_LCR, ASY_LCR_SETBRK);
mutex_exit(&asy->asy_excl_hi);
}
async->async_flags &= ~(ASYNC_DELAY|ASYNC_BREAK);
cv_broadcast(&async->async_flags_cv);
async_start(async);
mutex_exit(&asy->asy_excl);
}
static void
async_start(struct asyncline *async)
{
struct asycom *asy = async->async_common;
int cc;
queue_t *q;
mblk_t *bp;
uchar_t *xmit_addr;
int fifo_len = 1;
boolean_t didsome;
mblk_t *nbp;
ASY_DPRINTF(asy, ASY_DEBUG_PROCS, "enter");
if (asy->asy_use_fifo == ASY_FCR_FIFO_EN) {
fifo_len = asy->asy_fifo_buf;
if (fifo_len > asy_max_tx_fifo)
fifo_len = asy_max_tx_fifo;
}
ASSERT(mutex_owned(&asy->asy_excl));
if (async->async_flags & (ASYNC_BREAK|ASYNC_BUSY)) {
ASY_DPRINTF(asy, ASY_DEBUG_OUT, "%s",
async->async_flags & ASYNC_BREAK ? "break" : "busy");
return;
}
mutex_enter(&asy->asy_excl_hi);
if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
fifo_len--;
mutex_exit(&asy->asy_excl_hi);
if (async->async_flags & ASYNC_DELAY) {
ASY_DPRINTF(asy, ASY_DEBUG_OUT, "start ASYNC_DELAY");
return;
}
if ((q = async->async_ttycommon.t_writeq) == NULL) {
ASY_DPRINTF(asy, ASY_DEBUG_OUT, "start writeq is null");
return;
}
for (;;) {
if ((bp = getq(q)) == NULL)
return;
switch (bp->b_datap->db_type) {
case M_BREAK:
mutex_enter(&asy->asy_excl_hi);
asy_set(asy, ASY_LCR, ASY_LCR_SETBRK);
mutex_exit(&asy->asy_excl_hi);
async->async_flags |= ASYNC_BREAK;
(void) timeout(async_restart, (caddr_t)async,
drv_usectohz(1000000)/4);
freemsg(bp);
return;
case M_DELAY:
(void) timeout(async_restart, (caddr_t)async,
(int)(*(unsigned char *)bp->b_rptr + 6));
async->async_flags |= ASYNC_DELAY;
freemsg(bp);
return;
case M_IOCTL:
mutex_exit(&asy->asy_excl);
async_ioctl(async, q, bp);
mutex_enter(&asy->asy_excl);
continue;
}
while (bp != NULL && ((cc = MBLKL(bp)) == 0)) {
nbp = bp->b_cont;
freeb(bp);
bp = nbp;
}
if (bp != NULL)
break;
}
if (async->async_flags & (ASYNC_HW_OUT_FLW | ASYNC_SW_OUT_FLW |
ASYNC_STOPPED | ASYNC_OUT_SUSPEND)) {
(void) putbq(q, bp);
return;
}
async->async_xmitblk = bp;
xmit_addr = bp->b_rptr;
bp = bp->b_cont;
if (bp != NULL)
(void) putbq(q, bp);
if ((async->async_ttycommon.t_cflag & CSIZE) == CS5) {
unsigned char *p = xmit_addr;
int cnt = cc;
while (cnt--)
*p++ &= (unsigned char) 0x1f;
}
mutex_enter(&asy->asy_excl_hi);
didsome = B_FALSE;
while (--fifo_len >= 0 && cc > 0) {
if (!(asy_get(asy, ASY_LSR) & ASY_LSR_THRE))
break;
asy_put(asy, ASY_THR, *xmit_addr++);
cc--;
didsome = B_TRUE;
}
async->async_optr = xmit_addr;
async->async_ocnt = cc;
if (didsome)
async->async_flags |= ASYNC_PROGRESS;
ASY_DPRINTF(asy, ASY_DEBUG_BUSY, "Set ASYNC_BUSY, async_ocnt=%d",
async->async_ocnt);
async->async_flags |= ASYNC_BUSY;
mutex_exit(&asy->asy_excl_hi);
}
static void
async_resume(struct asyncline *async)
{
struct asycom *asy = async->async_common;
ASY_DPRINTF(asy, ASY_DEBUG_PROCS, "enter");
ASSERT(mutex_owned(&asy->asy_excl_hi));
if (asy_get(asy, ASY_LSR) & ASY_LSR_THRE) {
if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
return;
if (async->async_ocnt > 0 &&
!(async->async_flags &
(ASYNC_HW_OUT_FLW|ASYNC_SW_OUT_FLW|ASYNC_OUT_SUSPEND))) {
asy_put(asy, ASY_THR, *async->async_optr++);
async->async_ocnt--;
async->async_flags |= ASYNC_PROGRESS;
}
}
}
static void
async_hold_utbrk(void *arg)
{
struct asyncline *async = arg;
struct asycom *asy = async->async_common;
mutex_enter(&asy->asy_excl);
async->async_flags &= ~ASYNC_HOLD_UTBRK;
cv_broadcast(&async->async_flags_cv);
async->async_utbrktid = 0;
mutex_exit(&asy->asy_excl);
}
static void
async_resume_utbrk(struct asyncline *async)
{
struct asycom *asy = async->async_common;
ASSERT(mutex_owned(&asy->asy_excl));
while (async->async_flags & ASYNC_HOLD_UTBRK) {
cv_wait(&async->async_flags_cv, &asy->asy_excl);
}
mutex_enter(&asy->asy_excl_hi);
if (!(async->async_flags & ASYNC_BREAK))
asy_clr(asy, ASY_LCR, ASY_LCR_SETBRK);
async->async_flags &= ~ASYNC_OUT_SUSPEND;
cv_broadcast(&async->async_flags_cv);
if (async->async_ocnt > 0) {
async_resume(async);
mutex_exit(&asy->asy_excl_hi);
} else {
async->async_flags &= ~ASYNC_BUSY;
mutex_exit(&asy->asy_excl_hi);
if (async->async_xmitblk != NULL) {
freeb(async->async_xmitblk);
async->async_xmitblk = NULL;
}
async_start(async);
}
}
int asydelay = 10000;
static void
async_ioctl(struct asyncline *async, queue_t *wq, mblk_t *mp)
{
struct asycom *asy = async->async_common;
tty_common_t *tp = &async->async_ttycommon;
struct iocblk *iocp;
unsigned datasize;
int error = 0;
mblk_t *datamp;
ASY_DPRINTF(asy, ASY_DEBUG_PROCS, "enter");
if (tp->t_iocpending != NULL) {
freemsg(async->async_ttycommon.t_iocpending);
async->async_ttycommon.t_iocpending = NULL;
}
iocp = (struct iocblk *)mp->b_rptr;
ASY_DPRINTF(asy, ASY_DEBUG_IOCTL, "%s",
iocp->ioc_cmd == TIOCMGET ? "TIOCMGET" :
iocp->ioc_cmd == TIOCMSET ? "TIOCMSET" :
iocp->ioc_cmd == TIOCMBIS ? "TIOCMBIS" :
iocp->ioc_cmd == TIOCMBIC ? "TIOCMBIC" :
"other");
switch (iocp->ioc_cmd) {
case TIOCMGET:
case TIOCGPPS:
case TIOCSPPS:
case TIOCGPPSEV:
case CONSOPENPOLLEDIO:
case CONSCLOSEPOLLEDIO:
case CONSSETABORTENABLE:
case CONSGETABORTENABLE:
error = -1;
break;
default:
if ((datasize = ttycommon_ioctl(tp, wq, mp, &error)) != 0) {
if (async->async_wbufcid)
unbufcall(async->async_wbufcid);
async->async_wbufcid = bufcall(datasize, BPRI_HI,
(void (*)(void *)) async_reioctl,
(void *)(intptr_t)async->async_common->asy_unit);
return;
}
}
mutex_enter(&asy->asy_excl);
if (error == 0) {
switch (iocp->ioc_cmd) {
case TCSETS:
mutex_enter(&asy->asy_excl_hi);
if (asy_baudok(asy))
asy_program(asy, ASY_NOINIT);
else
error = EINVAL;
mutex_exit(&asy->asy_excl_hi);
break;
case TCSETSF:
case TCSETSW:
case TCSETA:
case TCSETAW:
case TCSETAF:
mutex_enter(&asy->asy_excl_hi);
if (!asy_baudok(asy))
error = EINVAL;
else {
if (asy_isbusy(asy))
asy_waiteot(asy);
asy_program(asy, ASY_NOINIT);
}
mutex_exit(&asy->asy_excl_hi);
break;
}
} else if (error < 0) {
error = 0;
switch (iocp->ioc_cmd) {
case TIOCGPPS:
if (mp->b_cont != NULL)
freemsg(mp->b_cont);
mp->b_cont = allocb(sizeof (int), BPRI_HI);
if (mp->b_cont == NULL) {
error = ENOMEM;
break;
}
if (asy->asy_flags & ASY_PPS)
*(int *)mp->b_cont->b_wptr = 1;
else
*(int *)mp->b_cont->b_wptr = 0;
mp->b_cont->b_wptr += sizeof (int);
mp->b_datap->db_type = M_IOCACK;
iocp->ioc_count = sizeof (int);
break;
case TIOCSPPS:
error = miocpullup(mp, sizeof (int));
if (error != 0)
break;
mutex_enter(&asy->asy_excl_hi);
if (*(int *)mp->b_cont->b_rptr)
asy->asy_flags |= ASY_PPS;
else
asy->asy_flags &= ~ASY_PPS;
asy->asy_flags &= ~ASY_PPS_EDGE;
mutex_exit(&asy->asy_excl_hi);
mp->b_datap->db_type = M_IOCACK;
break;
case TIOCGPPSEV:
{
mblk_t *bp;
void *buf;
#ifdef _SYSCALL32_IMPL
struct ppsclockev32 p32;
#endif
struct ppsclockev ppsclockev;
if (mp->b_cont != NULL) {
freemsg(mp->b_cont);
mp->b_cont = NULL;
}
if ((asy->asy_flags & ASY_PPS) == 0) {
error = ENXIO;
break;
}
mutex_enter(&asy->asy_excl_hi);
ppsclockev = asy_ppsev;
mutex_exit(&asy->asy_excl_hi);
#ifdef _SYSCALL32_IMPL
if ((iocp->ioc_flag & IOC_MODELS) != IOC_NATIVE) {
TIMEVAL_TO_TIMEVAL32(&p32.tv, &ppsclockev.tv);
p32.serial = ppsclockev.serial;
buf = &p32;
iocp->ioc_count = sizeof (struct ppsclockev32);
} else
#endif
{
buf = &ppsclockev;
iocp->ioc_count = sizeof (struct ppsclockev);
}
if ((bp = allocb(iocp->ioc_count, BPRI_HI)) == NULL) {
error = ENOMEM;
break;
}
mp->b_cont = bp;
bcopy(buf, bp->b_wptr, iocp->ioc_count);
bp->b_wptr += iocp->ioc_count;
mp->b_datap->db_type = M_IOCACK;
break;
}
case TCSBRK:
error = miocpullup(mp, sizeof (int));
if (error != 0)
break;
if (*(int *)mp->b_cont->b_rptr == 0) {
if (asydelay)
delay(drv_usectohz(asydelay));
while (async->async_flags & ASYNC_BREAK) {
cv_wait(&async->async_flags_cv,
&asy->asy_excl);
}
mutex_enter(&asy->asy_excl_hi);
async->async_flags |= ASYNC_BREAK;
asy_wait_baudrate(asy);
asy_set(asy, ASY_LCR, ASY_LCR_SETBRK);
mutex_exit(&asy->asy_excl_hi);
(void) timeout(async_restart, (caddr_t)async,
drv_usectohz(1000000)/4);
} else {
ASY_DPRINTF(asy, ASY_DEBUG_OUT,
"wait for flush");
mutex_enter(&asy->asy_excl_hi);
asy_waiteot(asy);
mutex_exit(&asy->asy_excl_hi);
ASY_DPRINTF(asy, ASY_DEBUG_OUT,
"ldterm satisfied");
}
break;
case TIOCSBRK:
if (!(async->async_flags & ASYNC_OUT_SUSPEND)) {
mutex_enter(&asy->asy_excl_hi);
async->async_flags |= ASYNC_OUT_SUSPEND;
async->async_flags |= ASYNC_HOLD_UTBRK;
asy_wait_baudrate(asy);
mutex_exit(&asy->asy_excl_hi);
async->async_utbrktid =
timeout((void (*)())async_hold_utbrk,
(caddr_t)async,
drv_usectohz(asy_min_utbrk));
}
mioc2ack(mp, NULL, 0, 0);
break;
case TIOCCBRK:
if (async->async_flags & ASYNC_OUT_SUSPEND)
async_resume_utbrk(async);
mioc2ack(mp, NULL, 0, 0);
break;
case TIOCMSET:
case TIOCMBIS:
case TIOCMBIC:
if (iocp->ioc_count != TRANSPARENT) {
ASY_DPRINTF(asy, ASY_DEBUG_IOCTL,
"non-transparent");
error = miocpullup(mp, sizeof (int));
if (error != 0)
break;
mutex_enter(&asy->asy_excl_hi);
(void) asymctl(asy,
dmtoasy(asy, *(int *)mp->b_cont->b_rptr),
iocp->ioc_cmd);
mutex_exit(&asy->asy_excl_hi);
iocp->ioc_error = 0;
mp->b_datap->db_type = M_IOCACK;
} else {
ASY_DPRINTF(asy, ASY_DEBUG_IOCTL,
"transparent");
mcopyin(mp, NULL, sizeof (int), NULL);
}
break;
case TIOCMGET:
datamp = allocb(sizeof (int), BPRI_MED);
if (datamp == NULL) {
error = EAGAIN;
break;
}
mutex_enter(&asy->asy_excl_hi);
*(int *)datamp->b_rptr = asymctl(asy, 0, TIOCMGET);
mutex_exit(&asy->asy_excl_hi);
if (iocp->ioc_count == TRANSPARENT) {
ASY_DPRINTF(asy, ASY_DEBUG_IOCTL,
"transparent");
mcopyout(mp, NULL, sizeof (int), NULL, datamp);
} else {
ASY_DPRINTF(asy, ASY_DEBUG_IOCTL,
"non-transparent");
mioc2ack(mp, datamp, sizeof (int), 0);
}
break;
case CONSOPENPOLLEDIO:
error = miocpullup(mp, sizeof (struct cons_polledio *));
if (error != 0)
break;
*(struct cons_polledio **)mp->b_cont->b_rptr =
&asy->polledio;
mp->b_datap->db_type = M_IOCACK;
break;
case CONSCLOSEPOLLEDIO:
mp->b_datap->db_type = M_IOCACK;
iocp->ioc_error = 0;
iocp->ioc_rval = 0;
break;
case CONSSETABORTENABLE:
error = secpolicy_console(iocp->ioc_cr);
if (error != 0)
break;
if (iocp->ioc_count != TRANSPARENT) {
error = EINVAL;
break;
}
mutex_enter(&asy->asy_excl_hi);
if (*(intptr_t *)mp->b_cont->b_rptr)
asy->asy_flags |= ASY_CONSOLE;
else
asy->asy_flags &= ~ASY_CONSOLE;
mutex_exit(&asy->asy_excl_hi);
mp->b_datap->db_type = M_IOCACK;
iocp->ioc_error = 0;
iocp->ioc_rval = 0;
break;
case CONSGETABORTENABLE:
ASSERT(sizeof (boolean_t) <= sizeof (boolean_t *));
mcopyout(mp, NULL, sizeof (boolean_t), NULL, NULL);
*(boolean_t *)mp->b_cont->b_rptr =
(asy->asy_flags & ASY_CONSOLE) != 0;
break;
default:
error = EINVAL;
break;
}
}
if (error != 0) {
iocp->ioc_error = error;
mp->b_datap->db_type = M_IOCNAK;
}
mutex_exit(&asy->asy_excl);
qreply(wq, mp);
ASY_DPRINTF(asy, ASY_DEBUG_PROCS, "done");
}
static int
asyrsrv(queue_t *q)
{
mblk_t *bp;
struct asyncline *async;
struct asycom *asy;
async = (struct asyncline *)q->q_ptr;
asy = (struct asycom *)async->async_common;
while (canputnext(q) && (bp = getq(q)))
putnext(q, bp);
mutex_enter(&asy->asy_excl_hi);
asysetsoft(asy);
mutex_exit(&asy->asy_excl_hi);
async->async_polltid = 0;
return (0);
}
#define ASYWPUTDO_NOT_SUSP(async, wput) \
!((wput) && ((async)->async_flags & ASYNC_DDI_SUSPENDED))
static int
asywputdo(queue_t *q, mblk_t *mp, boolean_t wput)
{
struct asyncline *async;
struct asycom *asy;
int error;
async = (struct asyncline *)q->q_ptr;
asy = async->async_common;
switch (mp->b_datap->db_type) {
case M_STOP:
mutex_enter(&asy->asy_excl);
async->async_flags |= ASYNC_STOPPED;
mutex_exit(&asy->asy_excl);
freemsg(mp);
break;
case M_START:
mutex_enter(&asy->asy_excl);
if (async->async_flags & ASYNC_STOPPED) {
async->async_flags &= ~ASYNC_STOPPED;
if (ASYWPUTDO_NOT_SUSP(async, wput)) {
if (async->async_ocnt > 0) {
mutex_enter(&asy->asy_excl_hi);
async_resume(async);
mutex_exit(&asy->asy_excl_hi);
} else {
async_start(async);
}
}
}
mutex_exit(&asy->asy_excl);
freemsg(mp);
break;
case M_IOCTL:
switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
case TCSBRK:
error = miocpullup(mp, sizeof (int));
if (error != 0) {
miocnak(q, mp, 0, error);
return (0);
}
if (*(int *)mp->b_cont->b_rptr != 0) {
ASY_DPRINTF(asy, ASY_DEBUG_OUT,
"flush request");
(void) putq(q, mp);
mutex_enter(&asy->asy_excl);
if (ASYWPUTDO_NOT_SUSP(async, wput)) {
async_resume_utbrk(async);
}
mutex_exit(&asy->asy_excl);
break;
}
case TCSETSW:
case TCSETSF:
case TCSETAW:
case TCSETAF:
(void) putq(q, mp);
mutex_enter(&asy->asy_excl);
if (ASYWPUTDO_NOT_SUSP(async, wput)) {
async_resume_utbrk(async);
}
mutex_exit(&asy->asy_excl);
break;
default:
mutex_enter(&asy->asy_excl);
if (ASYWPUTDO_NOT_SUSP(async, wput)) {
mutex_exit(&asy->asy_excl);
async_ioctl(async, q, mp);
break;
}
async_put_suspq(asy, mp);
mutex_exit(&asy->asy_excl);
break;
}
break;
case M_FLUSH:
if (*mp->b_rptr & FLUSHW) {
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
if (async->async_flags & ASYNC_BUSY) {
ASY_DPRINTF(asy, ASY_DEBUG_BUSY,
"Clearing async_ocnt, "
"leaving ASYNC_BUSY set");
async->async_ocnt = 0;
async->async_flags &= ~ASYNC_BUSY;
}
if (ASYWPUTDO_NOT_SUSP(async, wput)) {
if (asy->asy_use_fifo == ASY_FCR_FIFO_EN) {
asy_reset_fifo(asy, ASY_FCR_THR_FL);
}
}
mutex_exit(&asy->asy_excl_hi);
flushq(q, FLUSHDATA);
if (async->async_xmitblk != NULL) {
freeb(async->async_xmitblk);
async->async_xmitblk = NULL;
}
mutex_exit(&asy->asy_excl);
*mp->b_rptr &= ~FLUSHW;
}
if (*mp->b_rptr & FLUSHR) {
if (ASYWPUTDO_NOT_SUSP(async, wput)) {
mutex_enter(&asy->asy_excl);
mutex_enter(&asy->asy_excl_hi);
if (asy->asy_use_fifo == ASY_FCR_FIFO_EN) {
asy_reset_fifo(asy, ASY_FCR_RHR_FL);
}
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
}
flushq(RD(q), FLUSHDATA);
qreply(q, mp);
} else {
freemsg(mp);
}
if (ASYWPUTDO_NOT_SUSP(async, wput)) {
mutex_enter(&asy->asy_excl);
async_start(async);
mutex_exit(&asy->asy_excl);
}
break;
case M_BREAK:
case M_DELAY:
case M_DATA:
(void) putq(q, mp);
if (ASYWPUTDO_NOT_SUSP(async, wput)) {
mutex_enter(&asy->asy_excl);
async_start(async);
mutex_exit(&asy->asy_excl);
}
break;
case M_STOPI:
mutex_enter(&asy->asy_excl);
if (ASYWPUTDO_NOT_SUSP(async, wput)) {
mutex_enter(&asy->asy_excl_hi);
if (!(async->async_inflow_source & IN_FLOW_USER)) {
async_flowcontrol_hw_input(asy, FLOW_STOP,
IN_FLOW_USER);
(void) async_flowcontrol_sw_input(asy,
FLOW_STOP, IN_FLOW_USER);
}
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
freemsg(mp);
break;
}
async_put_suspq(asy, mp);
mutex_exit(&asy->asy_excl);
break;
case M_STARTI:
mutex_enter(&asy->asy_excl);
if (ASYWPUTDO_NOT_SUSP(async, wput)) {
mutex_enter(&asy->asy_excl_hi);
if (async->async_inflow_source & IN_FLOW_USER) {
async_flowcontrol_hw_input(asy, FLOW_START,
IN_FLOW_USER);
(void) async_flowcontrol_sw_input(asy,
FLOW_START, IN_FLOW_USER);
}
mutex_exit(&asy->asy_excl_hi);
mutex_exit(&asy->asy_excl);
freemsg(mp);
break;
}
async_put_suspq(asy, mp);
mutex_exit(&asy->asy_excl);
break;
case M_CTL:
if (MBLKL(mp) >= sizeof (struct iocblk) &&
((struct iocblk *)mp->b_rptr)->ioc_cmd == MC_POSIXQUERY) {
mutex_enter(&asy->asy_excl);
if (ASYWPUTDO_NOT_SUSP(async, wput)) {
((struct iocblk *)mp->b_rptr)->ioc_cmd =
MC_HAS_POSIX;
mutex_exit(&asy->asy_excl);
qreply(q, mp);
break;
} else {
async_put_suspq(asy, mp);
}
} else {
mutex_enter(&asy->asy_excl);
switch (*mp->b_rptr) {
case MC_SERVICEIMM:
async->async_flags |= ASYNC_SERVICEIMM;
break;
case MC_SERVICEDEF:
async->async_flags &= ~ASYNC_SERVICEIMM;
break;
}
mutex_exit(&asy->asy_excl);
freemsg(mp);
}
break;
case M_IOCDATA:
mutex_enter(&asy->asy_excl);
if (ASYWPUTDO_NOT_SUSP(async, wput)) {
mutex_exit(&asy->asy_excl);
async_iocdata(q, mp);
break;
}
async_put_suspq(asy, mp);
mutex_exit(&asy->asy_excl);
break;
default:
freemsg(mp);
break;
}
return (0);
}
static int
asywput(queue_t *q, mblk_t *mp)
{
return (asywputdo(q, mp, B_TRUE));
}
static void
async_reioctl(void *unit)
{
int instance = (uintptr_t)unit;
struct asyncline *async;
struct asycom *asy;
queue_t *q;
mblk_t *mp;
asy = ddi_get_soft_state(asy_soft_state, instance);
ASSERT(asy != NULL);
async = asy->asy_priv;
mutex_enter(&asy->asy_excl);
async->async_wbufcid = 0;
if ((q = async->async_ttycommon.t_writeq) == NULL) {
mutex_exit(&asy->asy_excl);
return;
}
if ((mp = async->async_ttycommon.t_iocpending) != NULL) {
async->async_ttycommon.t_iocpending = NULL;
mutex_exit(&asy->asy_excl);
async_ioctl(async, q, mp);
} else
mutex_exit(&asy->asy_excl);
}
static void
async_iocdata(queue_t *q, mblk_t *mp)
{
struct asyncline *async = (struct asyncline *)q->q_ptr;
struct asycom *asy;
struct iocblk *ip;
struct copyresp *csp;
asy = async->async_common;
ip = (struct iocblk *)mp->b_rptr;
csp = (struct copyresp *)mp->b_rptr;
if (csp->cp_rval != 0) {
if (csp->cp_private)
freemsg(csp->cp_private);
freemsg(mp);
return;
}
mutex_enter(&asy->asy_excl);
ASY_DPRINTF(asy, ASY_DEBUG_MODEM, "case %s",
csp->cp_cmd == TIOCMGET ? "TIOCMGET" :
csp->cp_cmd == TIOCMSET ? "TIOCMSET" :
csp->cp_cmd == TIOCMBIS ? "TIOCMBIS" :
"TIOCMBIC");
switch (csp->cp_cmd) {
case TIOCMGET:
if (mp->b_cont) {
freemsg(mp->b_cont);
mp->b_cont = NULL;
}
mp->b_datap->db_type = M_IOCACK;
ip->ioc_error = 0;
ip->ioc_count = 0;
ip->ioc_rval = 0;
mp->b_wptr = mp->b_rptr + sizeof (struct iocblk);
break;
case TIOCMSET:
case TIOCMBIS:
case TIOCMBIC:
mutex_enter(&asy->asy_excl_hi);
(void) asymctl(asy, dmtoasy(asy, *(int *)mp->b_cont->b_rptr),
csp->cp_cmd);
mutex_exit(&asy->asy_excl_hi);
mioc2ack(mp, NULL, 0, 0);
break;
default:
mp->b_datap->db_type = M_IOCNAK;
ip->ioc_error = EINVAL;
break;
}
qreply(q, mp);
mutex_exit(&asy->asy_excl);
}
static void
asyputchar(cons_polledio_arg_t arg, uchar_t c)
{
struct asycom *asy = (struct asycom *)arg;
if (c == '\n')
asyputchar(arg, '\r');
while ((asy_get_reg(asy, ASY_LSR) & ASY_LSR_THRE) == 0) {
drv_usecwait(10);
}
asy_put_reg(asy, ASY_THR, c);
}
static boolean_t
asyischar(cons_polledio_arg_t arg)
{
struct asycom *asy = (struct asycom *)arg;
return ((asy_get_reg(asy, ASY_LSR) & ASY_LSR_DR) != 0);
}
static int
asygetchar(cons_polledio_arg_t arg)
{
struct asycom *asy = (struct asycom *)arg;
while (!asyischar(arg))
drv_usecwait(10);
return (asy_get_reg(asy, ASY_RHR));
}
static int
asymctl(struct asycom *asy, int bits, int how)
{
int mcr_r, msr_r;
ASSERT(mutex_owned(&asy->asy_excl_hi));
ASSERT(mutex_owned(&asy->asy_excl));
mcr_r = asy_get(asy, ASY_MCR);
switch (how) {
case TIOCMSET:
ASY_DPRINTF(asy, ASY_DEBUG_MODEM, "TIOCMSET, bits = %x", bits);
mcr_r = bits;
break;
case TIOCMBIS:
ASY_DPRINTF(asy, ASY_DEBUG_MODEM, "TIOCMBIS, bits = %x", bits);
mcr_r |= bits;
break;
case TIOCMBIC:
ASY_DPRINTF(asy, ASY_DEBUG_MODEM, "TIOCMBIC, bits = %x", bits);
mcr_r &= ~bits;
break;
case TIOCMGET:
if (asy_get(asy, ASY_IER) & ASY_IER_MIEN) {
msr_r = asy->asy_msr;
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"TIOCMGET, read msr_r = %x", msr_r);
} else {
msr_r = asy_get(asy, ASY_MSR);
ASY_DPRINTF(asy, ASY_DEBUG_MODEM,
"TIOCMGET, read MSR = %x", msr_r);
}
ASY_DPRINTF(asy, ASY_DEBUG_MODEM, "modem_lines = %x",
asytodm(mcr_r, msr_r));
return (asytodm(mcr_r, msr_r));
}
asy_put(asy, ASY_MCR, mcr_r);
return (mcr_r);
}
static int
asytodm(int mcr_r, int msr_r)
{
int b = 0;
if (mcr_r & ASY_MCR_RTS)
b |= TIOCM_RTS;
if (mcr_r & ASY_MCR_DTR)
b |= TIOCM_DTR;
if (msr_r & ASY_MSR_DCD)
b |= TIOCM_CAR;
if (msr_r & ASY_MSR_CTS)
b |= TIOCM_CTS;
if (msr_r & ASY_MSR_DSR)
b |= TIOCM_DSR;
if (msr_r & ASY_MSR_RI)
b |= TIOCM_RNG;
return (b);
}
static int
dmtoasy(struct asycom *asy, int bits)
{
int b = 0;
ASY_DPRINTF(asy, ASY_DEBUG_MODEM, "bits = %x", bits);
#ifdef CAN_NOT_SET
if (bits & TIOCM_CAR)
b |= ASY_MSR_DCD;
if (bits & TIOCM_CTS)
b |= ASY_MSR_CTS;
if (bits & TIOCM_DSR)
b |= ASY_MSR_DSR;
if (bits & TIOCM_RNG)
b |= ASY_MSR_RI;
#endif
if (bits & TIOCM_RTS) {
ASY_DPRINTF(asy, ASY_DEBUG_MODEM, "set b & RTS");
b |= ASY_MCR_RTS;
}
if (bits & TIOCM_DTR) {
ASY_DPRINTF(asy, ASY_DEBUG_MODEM, "set b & DTR");
b |= ASY_MCR_DTR;
}
return (b);
}
static void
asyerror(const struct asycom *asy, int level, const char *fmt, ...)
{
va_list adx;
static time_t last;
static const char *lastfmt;
time_t now;
now = gethrestime_sec();
if (last == now && lastfmt == fmt)
return;
last = now;
lastfmt = fmt;
va_start(adx, fmt);
vdev_err(asy->asy_dip, level, fmt, adx);
va_end(adx);
}
static void
asy_parse_mode(dev_info_t *devi, struct asycom *asy)
{
char name[40];
char val[40];
int len;
int ret;
char *p;
char *p1;
ASSERT(asy->asy_com_port != 0);
(void) sprintf(name, "tty%c-mode", asy->asy_com_port + 'a' - 1);
len = sizeof (val);
ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
if (ret != DDI_PROP_SUCCESS) {
(void) sprintf(name, "com%c-mode", asy->asy_com_port + '0');
len = sizeof (val);
ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
}
asy->asy_cflag = 0;
if (ret != DDI_PROP_SUCCESS)
return;
p = val;
asy->asy_cflag = CREAD|B9600;
if (p && (p1 = strchr(p, ',')) != 0) {
*p1++ = '\0';
} else {
asy->asy_cflag |= ASY_LCR_BITS8;
return;
}
if (strcmp(p, "110") == 0)
asy->asy_bidx = B110;
else if (strcmp(p, "150") == 0)
asy->asy_bidx = B150;
else if (strcmp(p, "300") == 0)
asy->asy_bidx = B300;
else if (strcmp(p, "600") == 0)
asy->asy_bidx = B600;
else if (strcmp(p, "1200") == 0)
asy->asy_bidx = B1200;
else if (strcmp(p, "2400") == 0)
asy->asy_bidx = B2400;
else if (strcmp(p, "4800") == 0)
asy->asy_bidx = B4800;
else if (strcmp(p, "9600") == 0)
asy->asy_bidx = B9600;
else if (strcmp(p, "19200") == 0)
asy->asy_bidx = B19200;
else if (strcmp(p, "38400") == 0)
asy->asy_bidx = B38400;
else if (strcmp(p, "57600") == 0)
asy->asy_bidx = B57600;
else if (strcmp(p, "115200") == 0)
asy->asy_bidx = B115200;
else
asy->asy_bidx = B9600;
asy->asy_cflag &= ~CBAUD;
if (asy->asy_bidx > CBAUD) {
asy->asy_cflag |= CBAUDEXT;
asy->asy_cflag |= asy->asy_bidx - CBAUD - 1;
} else {
asy->asy_cflag |= asy->asy_bidx;
}
ASSERT(asy->asy_bidx == BAUDINDEX(asy->asy_cflag));
p = p1;
if (p && (p1 = strchr(p, ',')) != 0) {
*p1++ = '\0';
} else {
asy->asy_cflag |= ASY_LCR_BITS8;
return;
}
switch (*p) {
default:
case '8':
asy->asy_cflag |= CS8;
asy->asy_lcr = ASY_LCR_BITS8;
break;
case '7':
asy->asy_cflag |= CS7;
asy->asy_lcr = ASY_LCR_BITS7;
break;
case '6':
asy->asy_cflag |= CS6;
asy->asy_lcr = ASY_LCR_BITS6;
break;
case '5':
asy->asy_cflag |= CS5;
asy->asy_lcr = ASY_LCR_BITS5;
break;
}
p = p1;
if (p && (p1 = strchr(p, ',')) != 0) {
*p1++ = '\0';
} else {
return;
}
switch (*p) {
default:
case 'n':
break;
case 'e':
asy->asy_cflag |= PARENB;
asy->asy_lcr |= ASY_LCR_PEN;
break;
case 'o':
asy->asy_cflag |= PARENB|PARODD;
asy->asy_lcr |= ASY_LCR_PEN | ASY_LCR_EPS;
break;
}
p = p1;
if (p && (p1 = strchr(p, ',')) != 0) {
*p1++ = '\0';
} else {
return;
}
if (*p == '2') {
asy->asy_cflag |= CSTOPB;
asy->asy_lcr |= ASY_LCR_STB;
}
p = p1;
if (p) {
if ((p1 = strchr(p, ',')) != 0)
*p1++ = '\0';
if (*p == 'h')
asy->asy_cflag |= CRTSCTS;
else if (*p == 's')
asy->asy_cflag |= CRTSXOFF;
}
}
static boolean_t
abort_charseq_recognize(uchar_t ch)
{
static int state = 0;
#define CNTRL(c) ((c)&037)
static char sequence[] = { '\r', '~', CNTRL('b') };
if (ch == sequence[state]) {
if (++state >= sizeof (sequence)) {
state = 0;
return (B_TRUE);
}
} else {
state = (ch == sequence[0]) ? 1 : 0;
}
return (B_FALSE);
}
static boolean_t
async_flowcontrol_sw_input(struct asycom *asy, async_flowc_action onoff,
int type)
{
struct asyncline *async = asy->asy_priv;
int rval = B_FALSE;
ASSERT(mutex_owned(&asy->asy_excl_hi));
if (!(async->async_ttycommon.t_iflag & IXOFF))
return (rval);
switch (onoff) {
case FLOW_STOP:
async->async_inflow_source |= type;
if (async->async_inflow_source & (IN_FLOW_RINGBUFF |
IN_FLOW_STREAMS | IN_FLOW_USER))
async->async_flags |= ASYNC_SW_IN_FLOW |
ASYNC_SW_IN_NEEDED;
ASY_DPRINTF(asy, ASY_DEBUG_SFLOW, "input sflow stop, type = %x",
async->async_inflow_source);
break;
case FLOW_START:
async->async_inflow_source &= ~type;
if (async->async_inflow_source == 0) {
async->async_flags = (async->async_flags &
~ASYNC_SW_IN_FLOW) | ASYNC_SW_IN_NEEDED;
ASY_DPRINTF(asy, ASY_DEBUG_SFLOW, "input sflow start");
}
break;
default:
break;
}
if (((async->async_flags & (ASYNC_SW_IN_NEEDED | ASYNC_BREAK |
ASYNC_OUT_SUSPEND)) == ASYNC_SW_IN_NEEDED) &&
(asy_get(asy, ASY_LSR) & ASY_LSR_THRE)) {
async->async_flags = (async->async_flags &
~ASYNC_SW_IN_NEEDED) | ASYNC_BUSY;
asy_put(asy, ASY_THR,
async->async_flags & ASYNC_SW_IN_FLOW ?
async->async_stopc : async->async_startc);
rval = B_TRUE;
}
return (rval);
}
static void
async_flowcontrol_sw_output(struct asycom *asy, async_flowc_action onoff)
{
struct asyncline *async = asy->asy_priv;
ASSERT(mutex_owned(&asy->asy_excl_hi));
if (!(async->async_ttycommon.t_iflag & IXON))
return;
switch (onoff) {
case FLOW_STOP:
async->async_flags |= ASYNC_SW_OUT_FLW;
async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
ASY_DPRINTF(asy, ASY_DEBUG_SFLOW, "output sflow stop");
break;
case FLOW_START:
async->async_flags &= ~ASYNC_SW_OUT_FLW;
if (!(async->async_flags & ASYNC_HW_OUT_FLW))
async->async_flags |= ASYNC_OUT_FLW_RESUME;
ASY_DPRINTF(asy, ASY_DEBUG_SFLOW, "output sflow start");
break;
default:
break;
}
}
static void
async_flowcontrol_hw_input(struct asycom *asy, async_flowc_action onoff,
int type)
{
uchar_t mcr;
uchar_t flag;
struct asyncline *async = asy->asy_priv;
ASSERT(mutex_owned(&asy->asy_excl_hi));
if (!(async->async_ttycommon.t_cflag & CRTSXOFF))
return;
switch (onoff) {
case FLOW_STOP:
async->async_inflow_source |= type;
if (async->async_inflow_source & (IN_FLOW_RINGBUFF |
IN_FLOW_STREAMS | IN_FLOW_USER))
async->async_flags |= ASYNC_HW_IN_FLOW;
ASY_DPRINTF(asy, ASY_DEBUG_HFLOW, "input hflow stop, type = %x",
async->async_inflow_source);
break;
case FLOW_START:
async->async_inflow_source &= ~type;
if (async->async_inflow_source == 0) {
async->async_flags &= ~ASYNC_HW_IN_FLOW;
ASY_DPRINTF(asy, ASY_DEBUG_HFLOW, "input hflow start");
}
break;
default:
break;
}
mcr = asy_get(asy, ASY_MCR);
flag = (async->async_flags & ASYNC_HW_IN_FLOW) ? 0 : ASY_MCR_RTS;
if (((mcr ^ flag) & ASY_MCR_RTS) != 0) {
asy_put(asy, ASY_MCR, (mcr ^ ASY_MCR_RTS));
}
}
static void
async_flowcontrol_hw_output(struct asycom *asy, async_flowc_action onoff)
{
struct asyncline *async = asy->asy_priv;
ASSERT(mutex_owned(&asy->asy_excl_hi));
if (!(async->async_ttycommon.t_cflag & CRTSCTS))
return;
switch (onoff) {
case FLOW_STOP:
async->async_flags |= ASYNC_HW_OUT_FLW;
async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
ASY_DPRINTF(asy, ASY_DEBUG_HFLOW, "output hflow stop");
break;
case FLOW_START:
async->async_flags &= ~ASYNC_HW_OUT_FLW;
if (!(async->async_flags & ASYNC_SW_OUT_FLW))
async->async_flags |= ASYNC_OUT_FLW_RESUME;
ASY_DPRINTF(asy, ASY_DEBUG_HFLOW, "output hflow start");
break;
default:
break;
}
}
static int
asyquiesce(dev_info_t *devi)
{
int instance;
struct asycom *asy;
instance = ddi_get_instance(devi);
asy = ddi_get_soft_state(asy_soft_state, instance);
if (asy == NULL)
return (DDI_FAILURE);
asy_disable_interrupts(asy, ASY_IER_ALL);
if (asy->asy_use_fifo == ASY_FCR_FIFO_EN) {
asy_reset_fifo(asy, ASY_FCR_THR_FL | ASY_FCR_RHR_FL);
}
return (DDI_SUCCESS);
}