#define SU_REGISTER_FILE_NO 0
#define SU_REGOFFSET 0
#define SU_REGISTER_LEN 8
#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/strsun.h>
#include <sys/strtty.h>
#include <sys/debug.h>
#include <sys/kbio.h>
#include <sys/cred.h>
#include <sys/modctl.h>
#include <sys/stat.h>
#include <sys/consdev.h>
#include <sys/mkdev.h>
#include <sys/kmem.h>
#include <sys/cred.h>
#ifdef DEBUG
#include <sys/promif.h>
#endif
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sudev.h>
#include <sys/note.h>
#include <sys/timex.h>
#include <sys/policy.h>
#define async_stopc async_ttycommon.t_stopc
#define async_startc async_ttycommon.t_startc
#define ASY_INIT 1
#define ASY_NOINIT 0
#ifdef DEBUG
#define ASY_DEBUG_INIT 0x001
#define ASY_DEBUG_INPUT 0x002
#define ASY_DEBUG_EOT 0x004
#define ASY_DEBUG_CLOSE 0x008
#define ASY_DEBUG_HFLOW 0x010
#define ASY_DEBUG_PROCS 0x020
#define ASY_DEBUG_STATE 0x040
#define ASY_DEBUG_INTR 0x080
static int asydebug = 0;
#endif
static int su_log = 0;
int su_drain_check = 15000000;
static struct ppsclockev asy_ppsev;
static int max_asy_instance = -1;
static void *su_asycom;
static void *su_asyncline;
static boolean_t abort_charseq_recognize(uchar_t ch);
static uint_t asysoftintr(caddr_t intarg);
static uint_t asyintr(caddr_t argasy);
static void async_txint(struct asycom *asy, uchar_t lsr);
static void async_rxint(struct asycom *asy, uchar_t lsr);
static void async_msint(struct asycom *asy);
static int async_softint(struct asycom *asy);
static void async_ioctl(struct asyncline *async, queue_t *q, mblk_t *mp,
boolean_t iswput);
static void async_reioctl(void *);
static void async_iocdata(queue_t *q, mblk_t *mp);
static void async_restart(void *);
static void async_start(struct asyncline *async);
static void async_nstart(struct asyncline *async, int mode);
static void async_resume(struct asyncline *async);
static int asy_program(struct asycom *asy, int mode);
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 void asy_polled_enter(cons_polledio_arg_t);
static void asy_polled_exit(cons_polledio_arg_t);
static int asymctl(struct asycom *, int, int);
static int asytodm(int, int);
static int dmtoasy(int);
static void asycheckflowcontrol_hw(struct asycom *asy);
static boolean_t asycheckflowcontrol_sw(struct asycom *asy);
static void asy_ppsevent(struct asycom *asy, int msr);
extern kcondvar_t lbolt_cv;
extern int ddi_create_internal_pathname(dev_info_t *dip, char *name,
int spec_type, minor_t minor_num);
ushort_t asyspdtab[] = {
0,
0x900,
0x600,
0x417,
0x359,
0x300,
0x240,
0x180,
0x0c0,
0x060,
0x040,
0x030,
0x018,
0x00c,
0x006,
0x003,
0x002,
0,
0x001,
0,
0x8002,
0,
0x8001
};
#define N_SU_SPEEDS (sizeof (asyspdtab)/sizeof (ushort_t))
int baudtable[] = {
0,
50,
75,
110,
134,
150,
200,
300,
600,
1200,
1800,
2400,
4800,
9600,
19200,
38400,
57600,
76800,
115200,
153600,
230400,
307200,
460800
};
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 *cr);
static int asywput(queue_t *q, mblk_t *mp);
static int asyrsrv(queue_t *q);
struct module_info asy_info = {
0,
"su",
0,
INFPSZ,
32*4096,
4096
};
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 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 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,
ddi_quiesce_not_supported,
};
static struct modldrv modldrv = {
&mod_driverops,
"su driver",
&asy_ops,
};
static struct modlinkage modlinkage = {
MODREV_1,
&modldrv,
NULL
};
int
_init(void)
{
int status;
status = ddi_soft_state_init(&su_asycom, sizeof (struct asycom),
SU_INITIAL_SOFT_ITEMS);
if (status != 0)
return (status);
status = ddi_soft_state_init(&su_asyncline, sizeof (struct asyncline),
SU_INITIAL_SOFT_ITEMS);
if (status != 0) {
ddi_soft_state_fini(&su_asycom);
return (status);
}
if ((status = mod_install(&modlinkage)) != 0) {
ddi_soft_state_fini(&su_asycom);
ddi_soft_state_fini(&su_asyncline);
}
return (status);
}
int
_fini(void)
{
int i;
i = mod_remove(&modlinkage);
if (i == 0) {
ddi_soft_state_fini(&su_asycom);
ddi_soft_state_fini(&su_asyncline);
}
return (i);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static int
asyprobe(dev_info_t *devi)
{
int instance;
ddi_acc_handle_t handle;
uchar_t *addr;
ddi_device_acc_attr_t attr;
attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
if (ddi_regs_map_setup(devi, SU_REGISTER_FILE_NO, (caddr_t *)&addr,
SU_REGOFFSET, SU_REGISTER_LEN, &attr, &handle) != DDI_SUCCESS) {
cmn_err(CE_WARN, "asyprobe regs map setup failed");
return (DDI_PROBE_FAILURE);
}
#ifdef DEBUG
if (asydebug)
printf("Probe address mapped %p\n", (void *)addr);
#endif
if (ddi_get8(handle, addr+ISR) & 0x30) {
ddi_regs_map_free(&handle);
return (DDI_PROBE_FAILURE);
}
instance = ddi_get_instance(devi);
if (max_asy_instance < instance)
max_asy_instance = instance;
ddi_regs_map_free(&handle);
return (DDI_PROBE_SUCCESS);
}
static int
asydetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
{
int instance;
struct asycom *asy;
struct asyncline *async;
char name[16];
instance = ddi_get_instance(devi);
asy = (struct asycom *)ddi_get_soft_state(su_asycom, instance);
async = (struct asyncline *)ddi_get_soft_state(su_asyncline, instance);
switch (cmd) {
case DDI_DETACH:
break;
case DDI_SUSPEND:
mutex_enter(asy->asy_excl);
mutex_enter(asy->asy_excl_hi);
if (asy->suspended) {
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
return (DDI_SUCCESS);
}
asy->suspended = B_TRUE;
if (asy->asy_hwtype == ASY16C554D)
OUTB(SPR, 0);
OUTB(ICR, 0);
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
#ifdef DEBUG
if (asydebug & ASY_DEBUG_INIT)
cmn_err(CE_NOTE, "su%d: ASY%s shutdown.", instance,
asy->asy_hwtype == ASY82510 ? "82510" :
asy->asy_hwtype == ASY16550AF ? "16550AF" :
asy->asy_hwtype == ASY16C554D ? "16C554D" :
"8250");
#endif
mutex_enter(asy->asy_excl);
mutex_enter(asy->asy_excl_hi);
if (asy->asy_hwtype == ASY16C554D)
OUTB(SPR, 0);
OUTB(ICR, 0);
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
(void) sprintf(name, "%c", (instance+'a'));
ddi_remove_minor_node(devi, name);
(void) sprintf(name, "%c,cu", (instance+'a'));
ddi_remove_minor_node(devi, name);
mutex_destroy(asy->asy_excl);
mutex_destroy(asy->asy_excl_hi);
kmem_free(asy->asy_excl, sizeof (kmutex_t));
kmem_free(asy->asy_excl_hi, sizeof (kmutex_t));
cv_destroy(&async->async_flags_cv);
kstat_delete(asy->sukstat);
ddi_remove_intr(devi, 0, asy->asy_iblock);
ddi_regs_map_free(&asy->asy_handle);
ddi_remove_softintr(asy->asy_softintr_id);
mutex_destroy(asy->asy_soft_lock);
kmem_free(asy->asy_soft_lock, sizeof (kmutex_t));
ddi_soft_state_free(su_asycom, instance);
ddi_soft_state_free(su_asyncline, instance);
return (DDI_SUCCESS);
}
static int
asyattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
int instance;
struct asycom *asy;
struct asyncline *async;
char name[40];
ddi_device_acc_attr_t attr;
enum states { EMPTY, SOFTSTATE, REGSMAP, MUTEXES, ADDINTR,
SOFTINTR, ASYINIT, KSTAT, MINORNODE };
enum states state = EMPTY;
char *hwtype;
instance = ddi_get_instance(devi);
if (instance > max_asy_instance)
return (DDI_FAILURE);
if (cmd != DDI_RESUME) {
if (ddi_soft_state_zalloc(su_asycom, instance) != DDI_SUCCESS) {
cmn_err(CE_WARN, "su%d: cannot allocate soft state",
instance);
goto error;
}
}
state = SOFTSTATE;
asy = (struct asycom *)ddi_get_soft_state(su_asycom, instance);
if (asy == NULL) {
cmn_err(CE_WARN, "su%d: cannot get soft state", instance);
goto error;
}
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME: {
struct asyncline *async;
mutex_enter(asy->asy_excl);
mutex_enter(asy->asy_excl_hi);
if (!asy->suspended) {
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
return (DDI_SUCCESS);
}
async = (struct asyncline *)asy->asy_priv;
if ((async) && (async->async_flags & ASYNC_ISOPEN))
(void) asy_program(asy, ASY_INIT);
asy->suspended = B_FALSE;
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
return (DDI_SUCCESS);
}
default:
goto error;
}
attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
if (ddi_regs_map_setup(devi, SU_REGISTER_FILE_NO,
(caddr_t *)&asy->asy_ioaddr, SU_REGOFFSET, SU_REGISTER_LEN,
&attr, &asy->asy_handle) != DDI_SUCCESS) {
cmn_err(CE_WARN, "asyprobe regs map setup failed");
goto error;
}
state = REGSMAP;
#ifdef DEBUG
if (asydebug)
printf("su attach mapped %p\n", (void *)asy->asy_ioaddr);
#endif
asy->asy_fifo_buf = 1;
asy->asy_use_fifo = FIFO_OFF;
asy->asy_baud_divisor_factor = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
DDI_PROP_DONTPASS, "baud-divisor-factor", 1);
asy->asy_speed_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
DDI_PROP_DONTPASS, "serial-speed-cap", 115200);
OUTB(ISR, 0x20);
if (INB(ISR) & 0x20) {
asy->asy_hwtype = ASY82510;
OUTB(DAT+7, 0x04);
OUTB(ISR, 0x40);
OUTB(MCR, 0x08);
OUTB(DAT, 0x21);
OUTB(ISR, 0x00);
asy->asy_trig_level = 0;
} else {
asy->asy_hwtype = ASY16550AF;
OUTB(FIFOR, 0x00);
asy->asy_trig_level = 0x00;
OUTB(FIFOR, FIFO_ON | FIFODMA | FIFOTXFLSH | FIFORXFLSH |
(asy->asy_trig_level & 0xff));
if ((INB(ISR) & 0xc0) == 0xc0)
asy->asy_use_fifo = FIFO_ON;
else {
asy->asy_hwtype = ASY8250;
OUTB(FIFOR, 0x00);
asy->asy_trig_level = 0;
}
}
if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, DDI_PROP_NOTPROM |
DDI_PROP_DONTPASS, "hwtype", &hwtype)) == DDI_PROP_SUCCESS) {
if (strcmp(hwtype, "ST16C554D") == 0)
asy->asy_hwtype = ASY16C554D;
ddi_prop_free(hwtype);
}
if (asy->asy_hwtype == ASY16C554D)
OUTB(SPR, 0);
OUTB(ICR, 0);
OUTB(LCR, DLAB);
OUTB(DAT+DLL, (ASY9600*asy->asy_baud_divisor_factor) & 0xff);
OUTB(DAT+DLH, ((ASY9600*asy->asy_baud_divisor_factor) >> 8) & 0xff);
OUTB(LCR, STOP1|BITS8);
OUTB(MCR, (DTR | RTS| OUT2));
asy->asy_excl = (kmutex_t *)
kmem_zalloc(sizeof (kmutex_t), KM_SLEEP);
asy->asy_excl_hi = (kmutex_t *)
kmem_zalloc(sizeof (kmutex_t), KM_SLEEP);
asy->asy_soft_lock = (kmutex_t *)
kmem_zalloc(sizeof (kmutex_t), KM_SLEEP);
asy->asy_unit = instance;
asy->asy_dip = devi;
if (ddi_get_iblock_cookie(devi, 0, &asy->asy_iblock) != DDI_SUCCESS) {
cmn_err(CE_NOTE,
"Get iblock_cookie failed-Device interrupt%x\n", instance);
goto error;
}
if (ddi_get_soft_iblock_cookie(devi, DDI_SOFTINT_HIGH,
&asy->asy_soft_iblock) != DDI_SUCCESS) {
cmn_err(CE_NOTE, "Get iblock_cookie failed -soft interrupt%x\n",
instance);
goto error;
}
mutex_init(asy->asy_soft_lock, NULL, MUTEX_DRIVER,
(void *)asy->asy_soft_iblock);
mutex_init(asy->asy_excl, NULL, MUTEX_DRIVER, NULL);
mutex_init(asy->asy_excl_hi, NULL, MUTEX_DRIVER,
(void *)asy->asy_iblock);
state = MUTEXES;
if (ddi_add_intr(devi, 0, &(asy->asy_iblock), 0, asyintr,
(caddr_t)asy) != DDI_SUCCESS) {
cmn_err(CE_CONT,
"Cannot set device interrupt for su driver\n");
goto error;
}
state = ADDINTR;
if (ddi_add_softintr(devi, DDI_SOFTINT_HIGH, &(asy->asy_softintr_id),
&asy->asy_soft_iblock, 0, asysoftintr, (caddr_t)asy)
!= DDI_SUCCESS) {
cmn_err(CE_CONT, "Cannot set soft interrupt for su driver\n");
goto error;
}
state = SOFTINTR;
if (ddi_soft_state_zalloc(su_asyncline, instance) != DDI_SUCCESS) {
cmn_err(CE_CONT, "su%d: cannot allocate soft state", instance);
goto error;
}
state = ASYINIT;
async = (struct asyncline *)ddi_get_soft_state(su_asyncline, instance);
mutex_enter(asy->asy_excl);
async->async_common = asy;
cv_init(&async->async_flags_cv, NULL, CV_DEFAULT, NULL);
mutex_exit(asy->asy_excl);
if ((asy->sukstat = kstat_create("su", instance, "serialstat",
"misc", KSTAT_TYPE_NAMED, 2, KSTAT_FLAG_VIRTUAL)) != NULL) {
asy->sukstat->ks_data = &asy->kstats;
kstat_named_init(&asy->kstats.ringover, "ring buffer overflow",
KSTAT_DATA_UINT64);
kstat_named_init(&asy->kstats.siloover, "silo overflow",
KSTAT_DATA_UINT64);
kstat_install(asy->sukstat);
}
state = KSTAT;
if (strcmp(ddi_node_name(devi), "rsc-console") == 0) {
if (ddi_create_minor_node(devi, "ssp", S_IFCHR,
asy->asy_unit | RSC_DEVICE, DDI_PSEUDO, 0) == DDI_FAILURE) {
cmn_err(CE_WARN,
"%s%d: Failed to create node rsc-console",
ddi_get_name(devi), ddi_get_instance(devi));
goto error;
}
asy->asy_lom_console = 0;
asy->asy_rsc_console = 1;
asy->asy_rsc_control = 0;
asy->asy_device_type = ASY_SERIAL;
asy->asy_flags |= ASY_IGNORE_CD;
} else if (strcmp(ddi_node_name(devi), "lom-console") == 0) {
if (ddi_create_minor_node(devi, "lom-console", S_IFCHR,
instance, DDI_NT_SERIAL_LOMCON, 0) == DDI_FAILURE) {
cmn_err(CE_WARN,
"%s%d: Failed to create node lom-console",
ddi_get_name(devi), ddi_get_instance(devi));
goto error;
}
asy->asy_lom_console = 1;
asy->asy_rsc_console = 0;
asy->asy_rsc_control = 0;
asy->asy_device_type = ASY_SERIAL;
asy->asy_flags |= ASY_IGNORE_CD;
} else if (strcmp(ddi_node_name(devi), "rsc-control") == 0) {
if (ddi_create_minor_node(devi, "sspctl", S_IFCHR,
asy->asy_unit | RSC_DEVICE, DDI_PSEUDO, 0) == DDI_FAILURE) {
cmn_err(CE_WARN, "%s%d: Failed to create rsc-control",
ddi_get_name(devi), ddi_get_instance(devi));
goto error;
}
asy->asy_lom_console = 0;
asy->asy_rsc_console = 0;
asy->asy_rsc_control = 1;
asy->asy_device_type = ASY_SERIAL;
asy->asy_flags |= ASY_IGNORE_CD;
} else if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
"keyboard", 0)) {
if (ddi_create_internal_pathname(devi, "keyboard",
S_IFCHR, instance) == DDI_FAILURE) {
goto error;
}
asy->asy_flags |= ASY_IGNORE_CD;
asy->asy_device_type = ASY_KEYBOARD;
} else if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
"mouse", 0)) {
if (ddi_create_internal_pathname(devi, "mouse", S_IFCHR,
instance) == DDI_FAILURE) {
goto error;
}
asy->asy_flags |= ASY_IGNORE_CD;
asy->asy_device_type = ASY_MOUSE;
} else {
(void) sprintf(name, "%c", (instance+'a'));
if (ddi_create_minor_node(devi, name, S_IFCHR, instance,
DDI_NT_SERIAL_MB, 0) == DDI_FAILURE) {
goto error;
}
state = MINORNODE;
(void) sprintf(name, "%c,cu", (instance+'a'));
if (ddi_create_minor_node(devi, name, S_IFCHR, instance|OUTLINE,
DDI_NT_SERIAL_MB_DO, 0) == DDI_FAILURE) {
goto error;
}
if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
"ignore-cd", 0)) {
asy->asy_flags |= ASY_IGNORE_CD;
} else {
asy->asy_flags &= ~ASY_IGNORE_CD;
(void) sprintf(name,
"port-%c-ignore-cd", (instance+ 'a'));
if (ddi_getprop(DDI_DEV_T_ANY, devi,
DDI_PROP_DONTPASS, name, 0))
asy->asy_flags |= ASY_IGNORE_CD;
}
asy->asy_device_type = ASY_SERIAL;
}
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 = asy_polled_enter;
asy->polledio.cons_polledio_exit = asy_polled_exit;
asy->polled_icr = 0;
asy->polled_enter = B_FALSE;
ddi_report_dev(devi);
return (DDI_SUCCESS);
error:
if (state == MINORNODE) {
(void) sprintf(name, "%c", (instance+'a'));
ddi_remove_minor_node(devi, name);
}
if (state >= KSTAT)
kstat_delete(asy->sukstat);
if (state >= ASYINIT) {
cv_destroy(&async->async_flags_cv);
ddi_soft_state_free(su_asyncline, instance);
}
if (state >= SOFTINTR)
ddi_remove_softintr(asy->asy_softintr_id);
if (state >= ADDINTR)
ddi_remove_intr(devi, 0, asy->asy_iblock);
if (state >= MUTEXES) {
mutex_destroy(asy->asy_excl_hi);
mutex_destroy(asy->asy_excl);
mutex_destroy(asy->asy_soft_lock);
kmem_free(asy->asy_excl_hi, sizeof (kmutex_t));
kmem_free(asy->asy_excl, sizeof (kmutex_t));
kmem_free(asy->asy_soft_lock, sizeof (kmutex_t));
}
if (state >= REGSMAP)
ddi_regs_map_free(&asy->asy_handle);
if (state >= SOFTSTATE)
ddi_soft_state_free(su_asycom, instance);
return (DDI_FAILURE);
}
static int
asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
void **result)
{
_NOTE(ARGUNUSED(dip))
dev_t dev = (dev_t)arg;
int instance, error;
struct asycom *asy;
if ((instance = UNIT(dev)) > max_asy_instance)
return (DDI_FAILURE);
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
asy = (struct asycom *)ddi_get_soft_state(su_asycom,
instance);
if (asy->asy_dip == NULL)
error = DDI_FAILURE;
else {
*result = (void *) asy->asy_dip;
error = DDI_SUCCESS;
}
break;
case DDI_INFO_DEVT2INSTANCE:
*result = (void *)(uintptr_t)instance;
error = DDI_SUCCESS;
break;
default:
error = DDI_FAILURE;
}
return (error);
}
static int
asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr)
{
_NOTE(ARGUNUSED(sflag))
struct asycom *asy;
struct asyncline *async;
int mcr;
int unit;
int len;
struct termios *termiosp;
#ifdef DEBUG
if (asydebug & ASY_DEBUG_CLOSE)
printf("open\n");
#endif
unit = UNIT(*dev);
if (unit > max_asy_instance)
return (ENXIO);
async = (struct asyncline *)ddi_get_soft_state(su_asyncline, unit);
if (async == NULL)
return (ENXIO);
asy = async->async_common;
if (asy == NULL)
return (ENXIO);
mutex_enter(asy->asy_excl);
asy->asy_priv = (caddr_t)async;
again:
mutex_enter(asy->asy_excl_hi);
if (!(async->async_flags & ASYNC_ISOPEN)) {
if (asy->asy_rsc_console || asy->asy_rsc_control) {
async->async_ttycommon.t_cflag = CIBAUDEXT | CBAUDEXT |
(B115200 & CBAUD);
async->async_ttycommon.t_cflag |= ((B115200 << IBSHIFT)
& CIBAUD);
async->async_ttycommon.t_cflag |= CS8 | CREAD | CLOCAL;
} else if (asy->asy_lom_console) {
async->async_ttycommon.t_cflag = B9600 & CBAUD;
async->async_ttycommon.t_cflag |= ((B9600 << IBSHIFT)
& CIBAUD);
async->async_ttycommon.t_cflag |= CS8 | CREAD | CLOCAL;
} else {
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 {
cmn_err(CE_WARN,
"su: couldn't get ttymodes property!");
}
mutex_enter(asy->asy_excl_hi);
}
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;
(void) 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;
mcr = INB(MCR);
OUTB(MCR, mcr|DTR);
if (asy->asy_flags & ASY_IGNORE_CD)
async->async_ttycommon.t_flags |= TS_SOFTCAR;
if ((async->async_ttycommon.t_flags & TS_SOFTCAR) ||
(INB(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_flags & ASYNC_OUT) &&
!(*dev & OUTLINE))) {
async->async_flags |= ASYNC_WOPEN;
if (cv_wait_sig(&async->async_flags_cv,
asy->asy_excl) == 0) {
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);
}
if (asy->suspended) {
mutex_exit(asy->asy_excl);
(void) ddi_dev_is_needed(asy->asy_dip, 0, 1);
mutex_enter(asy->asy_excl);
}
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;
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(su_drain_check));
mutex_exit(asy->asy_excl_hi);
}
mutex_exit(asy->asy_excl);
}
static int
asyclose(queue_t *q, int flag, cred_t *cr __unused)
{
struct asyncline *async;
struct asycom *asy;
int icr, lcr;
int nohupcl;
#ifdef DEBUG
if (asydebug & ASY_DEBUG_CLOSE)
printf("close\n");
#endif
async = q->q_ptr;
ASSERT(async != NULL);
asy = async->async_common;
nohupcl = ddi_getprop(DDI_DEV_T_ANY, asy->asy_dip, DDI_PROP_DONTPASS,
"nohupcl", 0);
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_BREAK)) {
mutex_enter(asy->asy_excl_hi);
lcr = INB(LCR);
if (lcr & SETBREAK) {
OUTB(LCR, (lcr & ~SETBREAK));
}
mutex_exit(asy->asy_excl_hi);
if (lcr & SETBREAK)
goto nodrain;
}
if ((flag & (FNDELAY|FNONBLOCK)) ||
(async->async_flags & ASYNC_STOPPED)) {
goto nodrain;
}
if (!ddi_can_receive_sig() && su_drain_check != 0) {
async->async_flags &= ~ASYNC_PROGRESS;
async->async_timer = timeout(async_progress_check, async,
drv_usectohz(su_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:
mutex_enter(asy->asy_excl_hi);
if ((async->async_dev != rconsdev) &&
(async->async_dev != kbddev) &&
(async->async_dev != stdindev)) {
OUTB(MCR, INB(MCR) & ~ ASY_LOOP);
}
async->async_ocnt = 0;
if (async->async_xmitblk != NULL)
freeb(async->async_xmitblk);
async->async_xmitblk = NULL;
if (!nohupcl && ((async->async_ttycommon.t_cflag & HUPCL) ||
(async->async_flags & ASYNC_WOPEN))) {
OUTB(MCR, OUT2);
mutex_exit(asy->asy_excl_hi);
if (cv_wait_sig(&lbolt_cv, asy->asy_excl) == 0)
goto out;
mutex_enter(asy->asy_excl_hi);
}
if ((async->async_flags & (ASYNC_WOPEN|ASYNC_ISOPEN)) == 0) {
icr = INB(ICR);
OUTB(ICR, (icr & ~RIEN));
}
mutex_exit(asy->asy_excl_hi);
out:
async->async_flags = 0;
ttycommon_close(&async->async_ttycommon);
cv_broadcast(&async->async_flags_cv);
asy->asy_flags &= ~ASY_DOINGSOFT & ~ASY_NEEDSOFT;
if (async->async_wbufcid) {
unbufcall(async->async_wbufcid);
async->async_wbufcid = 0;
}
if (asy->inperim == B_TRUE) {
asy->inperim = B_FALSE;
mutex_exit(asy->asy_excl);
leaveq(q);
} else {
mutex_exit(asy->asy_excl);
}
qprocsoff(q);
q->q_ptr = WR(q)->q_ptr = NULL;
async->async_ttycommon.t_readq = NULL;
async->async_ttycommon.t_writeq = NULL;
return (0);
}
static boolean_t
asy_isbusy(struct asycom *asy)
{
struct asyncline *async;
#ifdef DEBUG
if (asydebug & ASY_DEBUG_EOT)
printf("isbusy\n");
#endif
async = (struct asyncline *)asy->asy_priv;
ASSERT(mutex_owned(asy->asy_excl));
ASSERT(mutex_owned(asy->asy_excl_hi));
return ((async->async_ocnt > 0) ||
((INB(LSR) & XSRE) == 0));
}
static int
asy_program(struct asycom *asy, int mode)
{
struct asyncline *async;
int baudrate, c_flag;
int icr, lcr;
int ocflags;
int error = 0;
ASSERT(mutex_owned(asy->asy_excl));
ASSERT(mutex_owned(asy->asy_excl_hi));
#ifdef DEBUG
if (asydebug & ASY_DEBUG_PROCS)
printf("program\n");
#endif
async = (struct asyncline *)asy->asy_priv;
baudrate = async->async_ttycommon.t_cflag & CBAUD;
if (async->async_ttycommon.t_cflag & CBAUDEXT)
baudrate += 16;
if (baudrate >= N_SU_SPEEDS) baudrate = B9600;
if (((baudrate > 0) && (asyspdtab[baudrate] == 0)) ||
(baudtable[baudrate] > asy->asy_speed_cap)) {
async->async_ttycommon.t_cflag &= ~CBAUD & ~CBAUDEXT &
~CIBAUD & ~CIBAUDEXT;
if (mode == ASY_INIT) {
async->async_ttycommon.t_cflag |= B9600;
async->async_ttycommon.t_cflag |= B9600 << IBSHIFT;
baudrate = B9600;
} else {
async->async_ttycommon.t_cflag |=
(asy->asy_ocflags & (CBAUD | CBAUDEXT |
CIBAUD | CIBAUDEXT));
error = EINVAL;
goto end;
}
}
if ((async->async_ttycommon.t_cflag & (CIBAUD|CIBAUDEXT)) == 0) {
async->async_ttycommon.t_cflag |=
(async->async_ttycommon.t_cflag & CBAUD) << IBSHIFT;
if (async->async_ttycommon.t_cflag & CBAUDEXT)
async->async_ttycommon.t_cflag |= CIBAUDEXT;
} else {
if ((((async->async_ttycommon.t_cflag & CBAUD) << IBSHIFT) !=
(async->async_ttycommon.t_cflag & CIBAUD)) ||
!(((async->async_ttycommon.t_cflag & (CBAUDEXT |
CIBAUDEXT)) == (CBAUDEXT | CIBAUDEXT)) ||
((async->async_ttycommon.t_cflag & (CBAUDEXT |
CIBAUDEXT)) == 0))) {
async->async_ttycommon.t_cflag &= ~CBAUD & ~CBAUDEXT &
~CIBAUD & ~CIBAUDEXT;
async->async_ttycommon.t_cflag |=
(asy->asy_ocflags & (CBAUD | CBAUDEXT |
CIBAUD | CIBAUDEXT));
error = EINVAL;
goto end;
}
}
c_flag = async->async_ttycommon.t_cflag &
(CLOCAL | CREAD | CSTOPB | CSIZE | PARENB | PARODD | CBAUD |
CBAUDEXT | CIBAUD | CIBAUDEXT);
if (asy->asy_hwtype == ASY16C554D)
OUTB(SPR, 0);
OUTB(ICR, 0);
ocflags = asy->asy_ocflags;
if (mode == ASY_INIT) {
(void) INB(DAT);
(void) INB(ISR);
(void) INB(LSR);
(void) INB(MSR);
}
if (ocflags != (c_flag & ~CLOCAL) || mode == ASY_INIT) {
lcr = INB(LCR);
lcr &= ~(WLS0|WLS1|STB|PEN|EPS);
if (c_flag & CSTOPB)
lcr |= STB;
if (c_flag & PARENB)
lcr |= PEN;
if ((c_flag & PARODD) == 0)
lcr |= EPS;
switch (c_flag & CSIZE) {
case CS5:
lcr |= BITS5;
break;
case CS6:
lcr |= BITS6;
break;
case CS7:
lcr |= BITS7;
break;
case CS8:
lcr |= BITS8;
break;
}
if (baudrate != 0) {
OUTB(LCR, DLAB);
OUTB(DAT, (asyspdtab[baudrate] *
asy->asy_baud_divisor_factor) & 0xff);
OUTB(ICR, ((asyspdtab[baudrate] *
asy->asy_baud_divisor_factor) >> 8) & 0xff);
}
OUTB(LCR, lcr);
if ((ocflags & CREAD) == 0 && (c_flag & CREAD)) {
if (asy->asy_use_fifo == FIFO_ON) {
OUTB(FIFOR, FIFO_ON | FIFODMA | FIFORXFLSH |
(asy->asy_trig_level & 0xff));
}
}
asy->asy_ocflags = c_flag & ~CLOCAL;
}
if (baudrate == 0)
OUTB(MCR, RTS|OUT2);
else
OUTB(MCR, DTR|RTS|OUT2);
async_msint(asy);
if ((c_flag & CLOCAL) && !(async->async_ttycommon.t_cflag & CRTSCTS))
icr = (TIEN | SIEN);
else
icr = (TIEN | SIEN | MIEN);
if (c_flag & CREAD)
icr |= RIEN;
OUTB(ICR, icr);
end:
return (error);
}
static void
asyputchar(cons_polledio_arg_t arg, uchar_t c)
{
struct asycom *asy = (struct asycom *)arg;
if (c == '\n')
asyputchar(arg, '\r');
while ((INB(LSR) & XHRE) == 0) {
drv_usecwait(10);
}
OUTB(DAT, c);
}
static boolean_t
asyischar(cons_polledio_arg_t arg)
{
struct asycom *asy = (struct asycom *)arg;
return ((INB(LSR) & RCA) != 0);
}
static int
asygetchar(cons_polledio_arg_t arg)
{
struct asycom *asy = (struct asycom *)arg;
while (!asyischar(arg))
drv_usecwait(10);
return (INB(DAT));
}
static void
asy_polled_enter(cons_polledio_arg_t arg)
{
struct asycom *asy = (struct asycom *)arg;
mutex_enter(asy->asy_excl);
mutex_enter(asy->asy_excl_hi);
if (!asy->polled_enter) {
asy->polled_enter = B_TRUE;
asy->polled_icr = INB(ICR);
if (asy->asy_hwtype == ASY16C554D)
OUTB(SPR, 0);
OUTB(ICR, 0);
asy->asy_flags &= ~ASY_DOINGSOFT & ~ASY_NEEDSOFT;
}
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
}
static void
asy_polled_exit(cons_polledio_arg_t arg)
{
struct asycom *asy = (struct asycom *)arg;
mutex_enter(asy->asy_excl);
mutex_enter(asy->asy_excl_hi);
OUTB(ICR, asy->polled_icr);
asy->polled_enter = B_FALSE;
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
}
uint_t
asyintr(caddr_t argasy)
{
struct asycom *asy = (struct asycom *)argasy;
struct asyncline *async;
int ret_status = DDI_INTR_UNCLAIMED;
uchar_t interrupt_id, lsr;
interrupt_id = INB(ISR) & 0x0F;
async = (struct asyncline *)asy->asy_priv;
if ((async == NULL) ||
!(async->async_flags & (ASYNC_ISOPEN|ASYNC_WOPEN))) {
if (interrupt_id & NOINTERRUPT) {
return (DDI_INTR_UNCLAIMED);
} else {
lsr = INB(LSR);
if ((lsr & BRKDET) &&
((abort_enable == KIOCABORTENABLE) &&
(async->async_dev == rconsdev)))
abort_sequence_enter((char *)NULL);
else {
(void) INB(LSR);
(void) INB(DAT);
(void) INB(MSR);
return (DDI_INTR_CLAIMED);
}
}
}
if (interrupt_id & NOINTERRUPT) {
mutex_enter(asy->asy_excl_hi);
if ((asy->asy_xmit_count > 1) ||
(asy->asy_out_of_band_xmit > 0) ||
(asy->asy_rx_count > 1)) {
asy->asy_xmit_count = 0;
asy->asy_out_of_band_xmit = 0;
asy->asy_rx_count = 0;
mutex_exit(asy->asy_excl_hi);
return (DDI_INTR_CLAIMED);
} else {
mutex_exit(asy->asy_excl_hi);
return (DDI_INTR_UNCLAIMED);
}
}
ret_status = DDI_INTR_CLAIMED;
mutex_enter(asy->asy_excl_hi);
if (asy->asy_hwtype == ASY82510)
OUTB(ISR, 0x00);
#ifdef DEBUG
if (asydebug & ASY_DEBUG_INTR)
prom_printf("l");
#endif
lsr = INB(LSR);
switch (interrupt_id) {
case RxRDY:
case RSTATUS:
case FFTMOUT:
async_rxint(asy, lsr);
break;
case TxRDY:
async_txint(asy, lsr);
break;
case MSTATUS:
async_msint(asy);
break;
}
mutex_exit(asy->asy_excl_hi);
return (ret_status);
}
static void
async_txint(struct asycom *asy, uchar_t lsr)
{
struct asyncline *async = (struct asyncline *)asy->asy_priv;
int fifo_len;
int xmit_progress;
asycheckflowcontrol_hw(asy);
if (async->async_flags & ASYNC_BREAK)
return;
fifo_len = asy->asy_fifo_buf;
if (asycheckflowcontrol_sw(asy)) {
return;
}
if (async->async_ocnt > 0 &&
!(async->async_flags & (ASYNC_HW_OUT_FLW|ASYNC_STOPPED))) {
xmit_progress = 0;
while (fifo_len > 0 && async->async_ocnt > 0) {
if (lsr & XHRE) {
OUTB(DAT, *async->async_optr++);
fifo_len--;
async->async_ocnt--;
xmit_progress++;
}
lsr = INB(LSR);
}
asy->asy_xmit_count = xmit_progress;
if (xmit_progress > 0)
async->async_flags |= ASYNC_PROGRESS;
}
if (fifo_len == 0) {
return;
}
ASYSETSOFT(asy);
}
static void
async_rxint(struct asycom *asy, uchar_t lsr)
{
struct asyncline *async = (struct asyncline *)asy->asy_priv;
uchar_t c = 0;
uint_t s = 0, needsoft = 0;
register tty_common_t *tp;
tp = &async->async_ttycommon;
if (!(tp->t_cflag & CREAD)) {
if (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
(void) (INB(DAT) & 0xff);
}
return;
}
asy->asy_rx_count = 0;
while (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
c = 0;
s = 0;
asy->asy_rx_count++;
if (lsr & RCA) {
c = INB(DAT) & 0xff;
needsoft = 1;
}
if ((abort_enable == KIOCABORTALTERNATE) &&
(async->async_dev == rconsdev)) {
if (abort_charseq_recognize(c))
abort_sequence_enter((char *)NULL);
}
if (lsr & (PARERR|FRMERR|BRKDET|OVRRUN)) {
if (lsr & PARERR) {
if (tp->t_iflag & INPCK)
s |= PERROR;
}
if (lsr & (FRMERR|BRKDET))
s |= FRERROR;
if (lsr & OVRRUN) {
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 ((async->async_dev == kbddev) ||
((async->async_dev == rconsdev) ||
(async->async_dev == stdindev)) &&
(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;
}
lsr = INB(LSR);
if (asy->asy_rx_count > 16) break;
}
if ((async->async_ttycommon.t_cflag & CRTSXOFF) ||
(async->async_ttycommon.t_iflag & IXOFF))
if ((int)(RING_CNT(async)) > (RINGSIZE * 3)/4) {
#ifdef DEBUG
if (asydebug & ASY_DEBUG_HFLOW)
printf("asy%d: hardware flow stop input.\n",
UNIT(async->async_dev));
#endif
async->async_flags |= ASYNC_HW_IN_FLOW;
async->async_flowc = async->async_stopc;
async->async_ringbuf_overflow = 1;
}
if ((async->async_flags & ASYNC_SERVICEIMM) || needsoft ||
(RING_FRAC(async)) || (async->async_polltid == 0))
ASYSETSOFT(asy);
}
static void
asy_ppsevent(struct asycom *asy, int msr)
{
if (asy->asy_flags & ASY_PPS_EDGE) {
if ((msr & DCD) == 0)
asy->asy_flags &= ~ASY_PPS_EDGE;
} else if (msr & DCD) {
struct timeval *tvp = &asy_ppsev.tv;
timestruc_t ts;
long nsec, usec;
asy->asy_flags |= ASY_PPS_EDGE;
gethrestime(&ts);
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_msint(struct asycom *asy)
{
struct asyncline *async = (struct asyncline *)asy->asy_priv;
int msr;
msr = INB(MSR);
asy->asy_cached_msr = msr;
#ifdef DEBUG
if (asydebug & ASY_DEBUG_STATE) {
printf(" transition: %3s %3s %3s %3s\n"
"current state: %3s %3s %3s %3s\n",
(msr & DCTS) ? "CTS" : " ",
(msr & DDSR) ? "DSR" : " ",
(msr & DRI) ? "RI " : " ",
(msr & DDCD) ? "DCD" : " ",
(msr & CTS) ? "CTS" : " ",
(msr & DSR) ? "DSR" : " ",
(msr & RI) ? "RI " : " ",
(msr & DCD) ? "DCD" : " ");
}
#endif
if (async->async_ttycommon.t_cflag & CRTSCTS && !(msr & CTS)) {
#ifdef DEBUG
if (asydebug & ASY_DEBUG_HFLOW)
printf("asy%d: hflow start\n",
UNIT(async->async_dev));
#endif
async->async_flags |= ASYNC_HW_OUT_FLW;
}
if (asy->asy_hwtype == ASY82510)
OUTB(MSR, (msr & 0xF0));
if (asy->asy_flags & ASY_PPS)
asy_ppsevent(asy, msr);
async->async_ext++;
ASYSETSOFT(asy);
}
uint_t
asysoftintr(caddr_t intarg)
{
struct asycom *asy = (struct asycom *)intarg;
struct asyncline *async;
int rv;
int cc;
mutex_enter(asy->asy_soft_lock);
#ifdef DEBUG
if (asydebug & ASY_DEBUG_PROCS)
printf("softintr\n");
#endif
rv = asy->asysoftpend;
if (rv != 0)
asy->asysoftpend = 0;
mutex_exit(asy->asy_soft_lock);
if (rv) {
if (asy->asy_priv == NULL)
return (rv);
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);
(void) 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);
}
static int
async_softint(struct asycom *asy)
{
struct asyncline *async = (struct asyncline *)asy->asy_priv;
uint_t cc;
mblk_t *bp;
queue_t *q;
uchar_t val;
uchar_t c;
tty_common_t *tp;
#ifdef DEBUG
if (asydebug & ASY_DEBUG_PROCS)
printf("process\n");
#endif
mutex_enter(asy->asy_excl);
if (asy->asy_flags & ASY_DOINGSOFT) {
mutex_exit(asy->asy_excl);
return (0);
}
tp = &async->async_ttycommon;
q = tp->t_readq;
if (q != NULL) {
mutex_exit(asy->asy_excl);
enterq(q);
mutex_enter(asy->asy_excl);
}
mutex_enter(asy->asy_excl_hi);
asy->asy_flags |= ASY_DOINGSOFT;
if (INB(ICR) & MIEN)
val = asy->asy_cached_msr & 0xFF;
else
val = INB(MSR) & 0xFF;
if (async->async_ttycommon.t_cflag & CRTSCTS) {
if ((val & CTS) && (async->async_flags & ASYNC_HW_OUT_FLW)) {
#ifdef DEBUG
if (asydebug & ASY_DEBUG_HFLOW)
printf("asy%d: hflow start\n",
UNIT(async->async_dev));
#endif
async->async_flags &= ~ASYNC_HW_OUT_FLW;
mutex_exit(asy->asy_excl_hi);
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_enter(asy->asy_excl_hi);
}
}
if (async->async_ext) {
async->async_ext = 0;
if ((val & DCD) || (tp->t_flags & TS_SOFTCAR)) {
if ((async->async_flags & ASYNC_CARR_ON) == 0) {
async->async_flags |= ASYNC_CARR_ON;
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
if (async->async_flags & ASYNC_ISOPEN)
(void) putctl(q, M_UNHANGUP);
cv_broadcast(&async->async_flags_cv);
mutex_enter(asy->asy_excl);
mutex_enter(asy->asy_excl_hi);
}
} else {
if ((async->async_flags & ASYNC_CARR_ON) &&
!(tp->t_cflag & CLOCAL)) {
int flushflag;
val = INB(MCR);
OUTB(MCR, (val & ~DTR));
flushflag = (async->async_flags &
ASYNC_CLOSING) ? FLUSHALL : FLUSHDATA;
if (tp->t_writeq != NULL) {
flushq(tp->t_writeq, flushflag);
}
if (async->async_xmitblk != NULL) {
freeb(async->async_xmitblk);
async->async_xmitblk = NULL;
}
if (async->async_flags & ASYNC_BUSY) {
async->async_ocnt = 0;
async->async_flags &= ~ASYNC_BUSY;
}
async->async_flags &= ~ASYNC_STOPPED;
if (async->async_flags & ASYNC_ISOPEN) {
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
(void) putctl(q, M_HANGUP);
mutex_enter(asy->asy_excl);
mutex_enter(asy->asy_excl_hi);
}
async->async_flags &= ~ASYNC_CARR_ON;
mutex_exit(asy->asy_excl_hi);
cv_broadcast(&async->async_flags_cv);
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)) {
if ((async->async_flags & ASYNC_HW_IN_FLOW) == 0) {
#ifdef DEBUG
if (!(asydebug & ASY_DEBUG_HFLOW)) {
printf("asy%d: hflow stop input.\n",
UNIT(async->async_dev));
if (canputnext(q))
printf("asy%d: next queue is "
"ready\n",
UNIT(async->async_dev));
}
#endif
mutex_enter(asy->asy_excl_hi);
async->async_flags |= ASYNC_HW_IN_FLOW;
async->async_flowc = async->async_stopc;
} else mutex_enter(asy->asy_excl_hi);
goto rv;
}
if (async->async_ringbuf_overflow) {
if ((async->async_flags & ASYNC_HW_IN_FLOW) &&
((int)(RING_CNT(async)) < (RINGSIZE/4))) {
#ifdef DEBUG
if (asydebug & ASY_DEBUG_HFLOW)
printf("asy%d: hflow start input.\n",
UNIT(async->async_dev));
#endif
mutex_enter(asy->asy_excl_hi);
async->async_flags &= ~ASYNC_HW_IN_FLOW;
async->async_flowc = async->async_startc;
async->async_ringbuf_overflow = 0;
goto rv;
}
}
#ifdef DEBUG
if (asydebug & ASY_DEBUG_INPUT)
printf("asy%d: %d char(s) in queue.\n",
UNIT(async->async_dev), cc);
#endif
if ((!canputnext(q)) || (!canput(q))) {
mutex_enter(asy->asy_excl_hi);
if ((async->async_flags & ASYNC_HW_IN_FLOW) == 0) {
async->async_flags |= ASYNC_HW_IN_FLOW;
async->async_flowc = async->async_stopc;
async->async_queue_full = 1;
}
goto rv;
}
mutex_enter(asy->asy_excl_hi);
if (async->async_queue_full) {
if (async->async_flags & ASYNC_HW_IN_FLOW) {
async->async_flags &= ~ASYNC_HW_IN_FLOW;
async->async_queue_full = 0;
async->async_flowc = async->async_startc;
goto rv;
} else
async->async_queue_full = 0;
}
mutex_exit(asy->asy_excl_hi);
if (!(bp = allocb(cc, BPRI_MED))) {
ttycommon_qfull(&async->async_ttycommon, q);
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 (!canputnext(q)) {
if (!canput(q)) {
cmn_err(CE_NOTE,
"su%d: local queue full\n",
UNIT(async->async_dev));
freemsg(bp);
mutex_enter(asy->asy_excl_hi);
if ((async->async_flags &
ASYNC_HW_IN_FLOW) == 0) {
async->async_flags |=
ASYNC_HW_IN_FLOW;
async->async_flowc =
async->async_stopc;
async->async_queue_full = 1;
}
mutex_exit(asy->asy_excl_hi);
} else {
(void) putq(q, bp);
}
} else {
putnext(q, bp);
}
} else {
freemsg(bp);
}
if (cc)
(void) putctl1(q, M_BREAK, c);
mutex_enter(asy->asy_excl);
mutex_enter(asy->asy_excl_hi);
rv:
if (async->async_break) {
async->async_break = 0;
if (async->async_flags & ASYNC_ISOPEN) {
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
(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)) ||
(async->async_flowc != '\0')) {
async->async_flags &= ~ASYNC_BUSY;
mutex_exit(asy->asy_excl_hi);
if (async->async_xmitblk)
freeb(async->async_xmitblk);
async->async_xmitblk = NULL;
if (async->async_flags & ASYNC_ISOPEN) {
asy->inperim = B_TRUE;
mutex_exit(asy->asy_excl);
enterq(async->async_ttycommon.t_writeq);
mutex_enter(asy->asy_excl);
}
async_start(async);
if ((asy->inperim) && (async->async_flags & ASYNC_ISOPEN)) {
mutex_exit(asy->asy_excl);
leaveq(async->async_ttycommon.t_writeq);
mutex_enter(asy->asy_excl);
asy->inperim = B_FALSE;
}
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) {
if (su_log > 0) {
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
cmn_err(CE_NOTE, "su%d: silo overflow\n",
UNIT(async->async_dev));
mutex_enter(asy->asy_excl);
mutex_enter(asy->asy_excl_hi);
}
INC64_KSTAT(asy, siloover);
}
async->async_hw_overrun = 0;
}
if (async->async_sw_overrun) {
if (async->async_flags & ASYNC_ISOPEN) {
if (su_log > 0) {
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
cmn_err(CE_NOTE, "su%d: ring buffer overflow\n",
UNIT(async->async_dev));
mutex_enter(asy->asy_excl);
mutex_enter(asy->asy_excl_hi);
}
INC64_KSTAT(asy, ringover);
}
async->async_sw_overrun = 0;
}
asy->asy_flags &= ~ASY_DOINGSOFT;
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
if (q != NULL)
leaveq(q);
return (0);
}
static void
async_restart(void *arg)
{
struct asyncline *async = arg;
struct asycom *asy = async->async_common;
queue_t *q;
uchar_t lcr;
#ifdef DEBUG
if (asydebug & ASY_DEBUG_PROCS)
printf("restart\n");
#endif
mutex_enter(asy->asy_excl);
if (async->async_flags & ASYNC_BREAK) {
unsigned int rate;
mutex_enter(asy->asy_excl_hi);
lcr = INB(LCR);
OUTB(LCR, (lcr & ~SETBREAK));
rate = async->async_ttycommon.t_cflag & CBAUD;
if (async->async_ttycommon.t_cflag & CBAUDEXT)
rate += 16;
if (rate >= N_SU_SPEEDS || rate == B0) {
rate = B9600;
}
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
drv_usecwait(2 * MICROSEC / baudtable[rate]);
mutex_enter(asy->asy_excl);
}
async->async_flags &= ~(ASYNC_DELAY|ASYNC_BREAK|ASYNC_DRAINING);
if ((q = async->async_ttycommon.t_writeq) != NULL) {
mutex_exit(asy->asy_excl);
enterq(q);
mutex_enter(asy->asy_excl);
}
async_start(async);
mutex_exit(asy->asy_excl);
if (q != NULL)
leaveq(q);
cv_broadcast(&async->async_flags_cv);
}
static void
async_start(struct asyncline *async)
{
async_nstart(async, 0);
}
static void
async_nstart(struct asyncline *async, int mode)
{
register struct asycom *asy = async->async_common;
register int cc;
register queue_t *q;
mblk_t *bp, *nbp;
uchar_t *xmit_addr;
uchar_t val;
int fifo_len = 1;
int xmit_progress;
#ifdef DEBUG
if (asydebug & ASY_DEBUG_PROCS)
printf("start\n");
#endif
if (asy->asy_use_fifo == FIFO_ON)
fifo_len = asy->asy_fifo_buf;
ASSERT(mutex_owned(asy->asy_excl));
mutex_enter(asy->asy_excl_hi);
asycheckflowcontrol_hw(asy);
if (async->async_flags & (ASYNC_BREAK|ASYNC_BUSY|ASYNC_DRAINING)) {
mutex_exit(asy->asy_excl_hi);
#ifdef DEBUG
if (mode && asydebug & ASY_DEBUG_CLOSE)
printf("asy%d: start %s.\n",
UNIT(async->async_dev),
async->async_flags & ASYNC_BREAK
? "break" : "busy");
#endif
return;
}
if (asycheckflowcontrol_sw(asy)) {
mutex_exit(asy->asy_excl_hi);
return;
}
mutex_exit(asy->asy_excl_hi);
if (async->async_flags & ASYNC_DELAY) {
#ifdef DEBUG
if (mode && asydebug & ASY_DEBUG_CLOSE)
printf("asy%d: start ASYNC_DELAY.\n",
UNIT(async->async_dev));
#endif
return;
}
if ((q = async->async_ttycommon.t_writeq) == NULL) {
#ifdef DEBUG
if (mode && asydebug & ASY_DEBUG_CLOSE)
printf("asy%d: start writeq is null.\n",
UNIT(async->async_dev));
#endif
return;
}
for (;;) {
if ((bp = getq(q)) == NULL)
return;
switch (bp->b_datap->db_type) {
case M_BREAK:
mutex_enter(asy->asy_excl_hi);
val = INB(LCR);
OUTB(LCR, (val | SETBREAK));
mutex_exit(asy->asy_excl_hi);
async->async_flags |= ASYNC_BREAK;
(void) timeout(async_restart, async, hz / 4);
freemsg(bp);
return;
case M_DELAY:
(void) timeout(async_restart, async,
(clock_t)(*(unsigned char *)bp->b_rptr + 6));
async->async_flags |= ASYNC_DELAY;
freemsg(bp);
return;
case M_IOCTL:
mutex_enter(asy->asy_excl_hi);
if (asy_isbusy(asy)) {
unsigned int rate;
mutex_exit(asy->asy_excl_hi);
rate = async->async_ttycommon.t_cflag & CBAUD;
if (async->async_ttycommon.t_cflag & CBAUDEXT)
rate += 16;
if (rate >= N_SU_SPEEDS || rate == B0) {
rate = B9600;
}
async->async_flags |= ASYNC_DRAINING;
if (putq(q, bp) == 0)
freemsg(bp);
(void) timeout(async_restart, async,
drv_usectohz(16 * MICROSEC /
baudtable[rate]));
return;
}
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
async_ioctl(async, q, bp, B_FALSE);
mutex_enter(asy->asy_excl);
continue;
}
while (bp != NULL && (cc = bp->b_wptr - bp->b_rptr) == 0) {
nbp = bp->b_cont;
freeb(bp);
bp = nbp;
}
if (bp != NULL)
break;
}
if (async->async_flags & (ASYNC_HW_OUT_FLW|ASYNC_STOPPED)) {
#ifdef DEBUG
if (asydebug & ASY_DEBUG_HFLOW &&
async->async_flags & ASYNC_HW_OUT_FLW)
printf("asy%d: output hflow in effect.\n",
UNIT(async->async_dev));
#endif
mutex_exit(asy->asy_excl);
(void) putbq(q, bp);
mutex_enter(asy->asy_excl);
return;
}
async->async_xmitblk = bp;
xmit_addr = bp->b_rptr;
bp = bp->b_cont;
if (bp != NULL) {
mutex_exit(asy->asy_excl);
(void) putbq(q, bp);
mutex_enter(asy->asy_excl);
}
if ((async->async_ttycommon.t_cflag & CSIZE) == CS5) {
register unsigned char *p = xmit_addr;
register int cnt = cc;
while (cnt--)
*p++ &= (unsigned char) 0x1f;
}
mutex_enter(asy->asy_excl_hi);
async->async_optr = xmit_addr;
async->async_ocnt = cc;
xmit_progress = 0;
while (fifo_len-- && async->async_ocnt) {
if (INB(LSR) & XHRE) {
OUTB(DAT, *async->async_optr++);
async->async_ocnt--;
xmit_progress++;
}
}
asy->asy_out_of_band_xmit = xmit_progress;
if (xmit_progress > 0)
async->async_flags |= ASYNC_PROGRESS;
async->async_flags |= ASYNC_BUSY;
mutex_exit(asy->asy_excl_hi);
}
static void
async_resume(struct asyncline *async)
{
register struct asycom *asy = async->async_common;
ASSERT(mutex_owned(asy->asy_excl_hi));
#ifdef DEBUG
if (asydebug & ASY_DEBUG_PROCS)
printf("resume\n");
#endif
asycheckflowcontrol_hw(asy);
if (INB(LSR) & XHRE) {
if (asycheckflowcontrol_sw(asy)) {
return;
} else if (async->async_ocnt > 0) {
OUTB(DAT, *async->async_optr++);
async->async_ocnt--;
async->async_flags |= ASYNC_PROGRESS;
}
}
}
static void
async_ioctl(struct asyncline *async, queue_t *wq, mblk_t *mp, boolean_t iswput)
{
register struct asycom *asy = async->async_common;
register tty_common_t *tp = &async->async_ttycommon;
register struct iocblk *iocp;
register unsigned datasize;
size_t ioc_count;
mblk_t *datamp;
int error = 0;
uchar_t val, icr;
#ifdef DEBUG
if (asydebug & ASY_DEBUG_PROCS)
printf("ioctl\n");
#endif
if (tp->t_iocpending != NULL) {
freemsg(async->async_ttycommon.t_iocpending);
async->async_ttycommon.t_iocpending = NULL;
}
iocp = (struct iocblk *)mp->b_rptr;
ioc_count = iocp->ioc_count;
if (iocp->ioc_cmd == TIOCMGET ||
iocp->ioc_cmd == TIOCMBIC ||
iocp->ioc_cmd == TIOCMBIS ||
iocp->ioc_cmd == TIOCMSET ||
iocp->ioc_cmd == TIOCGPPS ||
iocp->ioc_cmd == TIOCSPPS ||
iocp->ioc_cmd == TIOCGPPSEV ||
iocp->ioc_cmd == CONSOPENPOLLEDIO ||
iocp->ioc_cmd == CONSCLOSEPOLLEDIO)
error = -1;
else
if ((datasize = ttycommon_ioctl(tp, wq, mp, &error)) != 0) {
if (async->async_wbufcid)
unbufcall(async->async_wbufcid);
async->async_wbufcid = bufcall(datasize, BPRI_HI, async_reioctl,
async);
return;
}
mutex_enter(asy->asy_excl);
if (error == 0) {
switch (iocp->ioc_cmd) {
case TCSETS:
if (!(asy->asy_rsc_console || asy->asy_rsc_control ||
asy->asy_lom_console)) {
mutex_enter(asy->asy_excl_hi);
error = asy_program(asy, ASY_NOINIT);
mutex_exit(asy->asy_excl_hi);
}
break;
case TCSETSF:
case TCSETSW:
case TCSETA:
case TCSETAW:
case TCSETAF:
if (!(asy->asy_rsc_console || asy->asy_rsc_control ||
asy->asy_lom_console)) {
mutex_enter(asy->asy_excl_hi);
if (iswput && asy_isbusy(asy)) {
mp->b_datap->db_type = M_IOCTL;
iocp->ioc_count = ioc_count;
if (putq(wq, mp) == 0)
freemsg(mp);
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
return;
}
if (iocp->ioc_cmd == TCSETA ||
iocp->ioc_cmd == TCSETAW ||
iocp->ioc_cmd == TCSETAF)
tp->t_cflag &= ~(CIBAUD |
CIBAUDEXT | CBAUDEXT);
error = asy_program(asy, ASY_NOINIT);
mutex_exit(asy->asy_excl_hi);
}
break;
case TIOCSSOFTCAR:
mutex_enter(asy->asy_excl_hi);
if (tp->t_flags & TS_SOFTCAR)
asy->asy_flags |= ASY_IGNORE_CD;
else
asy->asy_flags &= ~ASY_IGNORE_CD;
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;
mutex_enter(asy->asy_excl_hi);
if (*(int *)mp->b_cont->b_rptr == 0) {
unsigned int rate, divisor;
rate = async->async_ttycommon.t_cflag & CBAUD;
if (async->async_ttycommon.t_cflag & CBAUDEXT)
rate += 16;
if (rate >= N_SU_SPEEDS) rate = B9600;
divisor = asyspdtab[rate] & 0xfff;
async->async_flags |= ASYNC_BREAK;
while ((INB(LSR) & XSRE) == 0) {
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
drv_usecwait(32*divisor);
mutex_enter(asy->asy_excl);
mutex_enter(asy->asy_excl_hi);
}
val = INB(LCR);
OUTB(LCR, (val | SETBREAK));
mutex_exit(asy->asy_excl_hi);
(void) timeout(async_restart, async, hz / 4);
} else {
#ifdef DEBUG
if (asydebug & ASY_DEBUG_CLOSE)
printf("asy%d: wait for flush.\n",
UNIT(async->async_dev));
#endif
if (iswput && asy_isbusy(asy)) {
if (putq(wq, mp) == 0)
freemsg(mp);
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
return;
}
mutex_exit(asy->asy_excl_hi);
#ifdef DEBUG
if (asydebug & ASY_DEBUG_CLOSE)
printf("asy%d: ldterm satisfied.\n",
UNIT(async->async_dev));
#endif
}
break;
case TIOCSBRK:
mutex_enter(asy->asy_excl_hi);
val = INB(LCR);
OUTB(LCR, (val | SETBREAK));
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
miocack(wq, mp, 0, 0);
return;
case TIOCCBRK:
mutex_enter(asy->asy_excl_hi);
val = INB(LCR);
OUTB(LCR, (val & ~SETBREAK));
mutex_exit(asy->asy_excl_hi);
mutex_exit(asy->asy_excl);
miocack(wq, mp, 0, 0);
return;
case TIOCMSET:
case TIOCMBIS:
case TIOCMBIC:
if (iocp->ioc_count == TRANSPARENT)
mcopyin(mp, NULL, sizeof (int), NULL);
else {
error = miocpullup(mp, sizeof (int));
if (error != 0)
break;
mutex_enter(asy->asy_excl_hi);
(void) asymctl(asy,
dmtoasy(*(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;
}
break;
case TIOCSILOOP:
mutex_enter(asy->asy_excl_hi);
if ((asy->asy_device_type == ASY_KEYBOARD) ||
(asy->asy_device_type == ASY_MOUSE)) {
mutex_exit(asy->asy_excl_hi);
error = ENOTTY;
break;
}
if ((async->async_dev == kbddev) ||
(async->async_dev == rconsdev) ||
(async->async_dev == stdindev)) {
mutex_exit(asy->asy_excl_hi);
error = EINVAL;
break;
}
val = INB(MCR);
icr = INB(ICR);
OUTB(ICR, icr & ~ MIEN);
OUTB(MCR, val | ASY_LOOP);
mutex_exit(asy->asy_excl_hi);
iocp->ioc_error = 0;
mp->b_datap->db_type = M_IOCACK;
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) {
mcopyout(mp, NULL, sizeof (int), NULL, datamp);
} else {
if (mp->b_cont != NULL)
freemsg(mp->b_cont);
mp->b_cont = datamp;
mp->b_cont->b_wptr += sizeof (int);
mp->b_datap->db_type = M_IOCACK;
iocp->ioc_count = sizeof (int);
}
break;
case CONSOPENPOLLEDIO:
if (asy->asy_device_type == ASY_KEYBOARD) {
error = ENOTSUP;
break;
}
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:
if (asy->asy_device_type == ASY_KEYBOARD) {
error = ENOTSUP;
break;
}
mp->b_datap->db_type = M_IOCACK;
iocp->ioc_error = 0;
iocp->ioc_rval = 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);
}
static int
asyrsrv(queue_t *q)
{
mblk_t *bp;
struct asyncline *async;
async = (struct asyncline *)q->q_ptr;
while (canputnext(q) && (bp = getq(q)))
putnext(q, bp);
ASYSETSOFT(async->async_common);
async->async_polltid = 0;
return (0);
}
static int
asywput(queue_t *q, mblk_t *mp)
{
register struct asyncline *async;
register 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 (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) {
#ifdef DEBUG
if (asydebug & ASY_DEBUG_CLOSE)
printf("asy%d: flush request.\n",
UNIT(async->async_dev));
#endif
(void) putq(q, mp);
mutex_enter(asy->asy_excl);
async_nstart(async, 1);
mutex_exit(asy->asy_excl);
break;
}
case TCSETSW:
case TCSETSF:
case TCSETAW:
case TCSETAF:
(void) putq(q, mp);
mutex_enter(asy->asy_excl);
async_start(async);
mutex_exit(asy->asy_excl);
break;
default:
async_ioctl(async, q, mp, B_TRUE);
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) {
async->async_ocnt = 0;
async->async_flags &= ~ASYNC_BUSY;
}
mutex_exit(asy->asy_excl_hi);
if (asy->asy_use_fifo == FIFO_ON) {
OUTB(FIFOR, FIFO_ON | FIFODMA | FIFOTXFLSH |
(asy->asy_trig_level & 0xff));
}
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 (asy->asy_use_fifo == FIFO_ON) {
OUTB(FIFOR, FIFO_ON | FIFODMA | FIFORXFLSH |
(asy->asy_trig_level & 0xff));
}
flushq(RD(q), FLUSHDATA);
qreply(q, mp);
} else {
freemsg(mp);
}
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);
mutex_enter(asy->asy_excl);
async_start(async);
mutex_exit(asy->asy_excl);
break;
case M_STOPI:
mutex_enter(asy->asy_excl);
async->async_flowc = async->async_stopc;
async_start(async);
mutex_exit(asy->asy_excl);
freemsg(mp);
break;
case M_STARTI:
mutex_enter(asy->asy_excl);
async->async_flowc = async->async_startc;
async_start(async);
mutex_exit(asy->asy_excl);
freemsg(mp);
break;
case M_CTL:
if (MBLKL(mp) >= sizeof (struct iocblk) &&
((struct iocblk *)mp->b_rptr)->ioc_cmd == MC_POSIXQUERY) {
((struct iocblk *)mp->b_rptr)->ioc_cmd = MC_HAS_POSIX;
qreply(q, 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:
async_iocdata(q, mp);
break;
default:
freemsg(mp);
break;
}
return (0);
}
static void
async_reioctl(void *arg)
{
struct asyncline *async = arg;
struct asycom *asy = async->async_common;
queue_t *q;
mblk_t *mp;
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, B_TRUE);
} 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 copyresp *csp;
asy = async->async_common;
csp = (struct copyresp *)mp->b_rptr;
if (csp->cp_rval != 0) {
freemsg(mp);
return;
}
mutex_enter(asy->asy_excl);
switch (csp->cp_cmd) {
case TIOCMSET:
case TIOCMBIS:
case TIOCMBIC:
if (mp->b_cont == NULL) {
mutex_exit(asy->asy_excl);
miocnak(q, mp, 0, EINVAL);
break;
}
mutex_enter(asy->asy_excl_hi);
(void) asymctl(asy, dmtoasy(*(int *)mp->b_cont->b_rptr),
csp->cp_cmd);
mutex_exit(asy->asy_excl_hi);
freemsg(mp->b_cont);
mp->b_cont = NULL;
mutex_exit(asy->asy_excl);
miocack(q, mp, 0, 0);
break;
case TIOCMGET:
if (mp->b_cont != NULL) {
freemsg(mp->b_cont);
mp->b_cont = NULL;
}
mutex_exit(asy->asy_excl);
miocack(q, mp, 0, 0);
break;
default:
mutex_exit(asy->asy_excl);
miocnak(q, mp, 0, EINVAL);
break;
}
}
static int
asymctl(struct asycom *asy, int bits, int how)
{
register int mcr_r, msr_r;
ASSERT(mutex_owned(asy->asy_excl_hi));
ASSERT(mutex_owned(asy->asy_excl));
mcr_r = INB(MCR);
switch (how) {
case TIOCMSET:
mcr_r = bits;
break;
case TIOCMBIS:
mcr_r |= bits;
break;
case TIOCMBIC:
mcr_r &= ~bits;
break;
case TIOCMGET:
if (INB(ICR) & MIEN)
msr_r = asy->asy_cached_msr;
else
msr_r = INB(MSR);
return (asytodm(mcr_r, msr_r));
}
OUTB(MCR, mcr_r);
return (mcr_r);
}
static int
asytodm(int mcr_r, int msr_r)
{
register int b = 0;
if (mcr_r & RTS)
b |= TIOCM_RTS;
if (mcr_r & DTR)
b |= TIOCM_DTR;
if (msr_r & DCD)
b |= TIOCM_CAR;
if (msr_r & CTS)
b |= TIOCM_CTS;
if (msr_r & DSR)
b |= TIOCM_DSR;
if (msr_r & RI)
b |= TIOCM_RNG;
return (b);
}
static int
dmtoasy(int bits)
{
register int b = 0;
#ifdef CAN_NOT_SET
if (bits & TIOCM_CAR)
b |= DCD;
if (bits & TIOCM_CTS)
b |= CTS;
if (bits & TIOCM_DSR)
b |= DSR;
if (bits & TIOCM_RNG)
b |= RI;
#endif
if (bits & TIOCM_RTS)
b |= RTS;
if (bits & TIOCM_DTR)
b |= DTR;
return (b);
}
static void
asycheckflowcontrol_hw(struct asycom *asy)
{
struct asyncline *async;
uchar_t mcr, flag;
ASSERT(mutex_owned(asy->asy_excl_hi));
async = (struct asyncline *)asy->asy_priv;
ASSERT(async != NULL);
if (async->async_ttycommon.t_cflag & CRTSXOFF) {
mcr = INB(MCR);
flag = (async->async_flags & ASYNC_HW_IN_FLOW) ? 0 : RTS;
if (((mcr ^ flag) & RTS) != 0) {
OUTB(MCR, (mcr ^ RTS));
}
}
}
static boolean_t
asycheckflowcontrol_sw(struct asycom *asy)
{
uchar_t ss;
struct asyncline *async;
int rval = B_FALSE;
ASSERT(mutex_owned(asy->asy_excl_hi));
async = (struct asyncline *)asy->asy_priv;
ASSERT(async != NULL);
if ((ss = async->async_flowc) != '\0' && (INB(LSR) & XHRE)) {
async->async_flowc = '\0';
if (async->async_ttycommon.t_iflag & IXOFF) {
async->async_flags |= ASYNC_BUSY;
OUTB(DAT, ss);
rval = B_TRUE;
}
}
return (rval);
}
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);
}