xref: /onnv-gate/usr/src/uts/sun4/io/su_driver.c (revision 9957:8769ec0debbf)
152Sdf157793 /*
252Sdf157793  * CDDL HEADER START
352Sdf157793  *
452Sdf157793  * The contents of this file are subject to the terms of the
51815Srameshc  * Common Development and Distribution License (the "License").
61815Srameshc  * You may not use this file except in compliance with the License.
752Sdf157793  *
852Sdf157793  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
952Sdf157793  * or http://www.opensolaris.org/os/licensing.
1052Sdf157793  * See the License for the specific language governing permissions
1152Sdf157793  * and limitations under the License.
1252Sdf157793  *
1352Sdf157793  * When distributing Covered Code, include this CDDL HEADER in each
1452Sdf157793  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1552Sdf157793  * If applicable, add the following below this CDDL HEADER, with the
1652Sdf157793  * fields enclosed by brackets "[]" replaced with your own identifying
1752Sdf157793  * information: Portions Copyright [yyyy] [name of copyright owner]
1852Sdf157793  *
1952Sdf157793  * CDDL HEADER END
2052Sdf157793  */
2152Sdf157793 /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
2252Sdf157793 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
2352Sdf157793 /*	  All Rights Reserved					*/
2452Sdf157793 
2552Sdf157793 /*
26*9957SAn.Bui@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2752Sdf157793  * Use is subject to license terms.
2852Sdf157793  */
2952Sdf157793 
3052Sdf157793 
3152Sdf157793 /*
322877Sanovick  *	Serial I/O driver for 82510/8250/16450/16550AF/16C554D chips.
3352Sdf157793  *	Modified as sparc keyboard/mouse driver.
3452Sdf157793  */
3552Sdf157793 #define	SU_REGISTER_FILE_NO 0
3652Sdf157793 #define	SU_REGOFFSET 0
3752Sdf157793 #define	SU_REGISTER_LEN 8
3852Sdf157793 
3952Sdf157793 #include <sys/param.h>
4052Sdf157793 #include <sys/types.h>
4152Sdf157793 #include <sys/signal.h>
4252Sdf157793 #include <sys/stream.h>
4352Sdf157793 #include <sys/termio.h>
4452Sdf157793 #include <sys/errno.h>
4552Sdf157793 #include <sys/file.h>
4652Sdf157793 #include <sys/cmn_err.h>
4752Sdf157793 #include <sys/stropts.h>
4852Sdf157793 #include <sys/strsubr.h>
4952Sdf157793 #include <sys/strsun.h>
5052Sdf157793 #include <sys/strtty.h>
5152Sdf157793 #include <sys/debug.h>
5252Sdf157793 #include <sys/kbio.h>
5352Sdf157793 #include <sys/cred.h>
5452Sdf157793 #include <sys/modctl.h>
5552Sdf157793 #include <sys/stat.h>
5652Sdf157793 #include <sys/consdev.h>
5752Sdf157793 #include <sys/mkdev.h>
5852Sdf157793 #include <sys/kmem.h>
5952Sdf157793 #include <sys/cred.h>
6052Sdf157793 #ifdef DEBUG
6152Sdf157793 #include <sys/promif.h>
6252Sdf157793 #endif
6352Sdf157793 #include <sys/ddi.h>
6452Sdf157793 #include <sys/sunddi.h>
6552Sdf157793 #include <sys/sudev.h>
6652Sdf157793 #include <sys/note.h>
6752Sdf157793 #include <sys/timex.h>
6852Sdf157793 #include <sys/policy.h>
6952Sdf157793 
7052Sdf157793 #define	async_stopc	async_ttycommon.t_stopc
7152Sdf157793 #define	async_startc	async_ttycommon.t_startc
7252Sdf157793 
7352Sdf157793 #define	ASY_INIT	1
7452Sdf157793 #define	ASY_NOINIT	0
7552Sdf157793 
7652Sdf157793 #ifdef DEBUG
7752Sdf157793 #define	ASY_DEBUG_INIT	0x001
7852Sdf157793 #define	ASY_DEBUG_INPUT	0x002
7952Sdf157793 #define	ASY_DEBUG_EOT	0x004
8052Sdf157793 #define	ASY_DEBUG_CLOSE	0x008
8152Sdf157793 #define	ASY_DEBUG_HFLOW	0x010
8252Sdf157793 #define	ASY_DEBUG_PROCS	0x020
8352Sdf157793 #define	ASY_DEBUG_STATE	0x040
8452Sdf157793 #define	ASY_DEBUG_INTR	0x080
8552Sdf157793 static	int asydebug = 0;
8652Sdf157793 #endif
8752Sdf157793 static	int su_log = 0;
8852Sdf157793 
8952Sdf157793 int su_drain_check = 15000000;		/* tunable: exit drain check time */
9052Sdf157793 
9152Sdf157793 static	struct ppsclockev asy_ppsev;
9252Sdf157793 
9352Sdf157793 static	int max_asy_instance = -1;
9452Sdf157793 static	void	*su_asycom;	/* soft state asycom pointer */
9552Sdf157793 static	void	*su_asyncline;	/* soft state asyncline pointer */
9652Sdf157793 static	boolean_t abort_charseq_recognize(uchar_t ch);
9752Sdf157793 
9852Sdf157793 static	uint_t	asysoftintr(caddr_t intarg);
9952Sdf157793 static	uint_t	asyintr(caddr_t argasy);
10052Sdf157793 
10152Sdf157793 /* The async interrupt entry points */
10252Sdf157793 static void	async_txint(struct asycom *asy, uchar_t lsr);
10352Sdf157793 static void	async_rxint(struct asycom *asy, uchar_t lsr);
10452Sdf157793 static void	async_msint(struct asycom *asy);
10552Sdf157793 static int	async_softint(struct asycom *asy);
10652Sdf157793 
10752Sdf157793 static void	async_ioctl(struct asyncline *async, queue_t *q, mblk_t *mp,
10852Sdf157793     boolean_t iswput);
10952Sdf157793 static void	async_reioctl(void *);
11052Sdf157793 static void	async_iocdata(queue_t *q, mblk_t *mp);
11152Sdf157793 static void	async_restart(void *);
11252Sdf157793 static void	async_start(struct asyncline *async);
11352Sdf157793 static void	async_nstart(struct asyncline *async, int mode);
11452Sdf157793 static void	async_resume(struct asyncline *async);
11552Sdf157793 static int	asy_program(struct asycom *asy, int mode);
11652Sdf157793 
1175973Szk194757 /* Polled mode functions */
1185973Szk194757 static void	asyputchar(cons_polledio_arg_t, uchar_t c);
1195973Szk194757 static int	asygetchar(cons_polledio_arg_t);
1205973Szk194757 static boolean_t	asyischar(cons_polledio_arg_t);
1215973Szk194757 static void	asy_polled_enter(cons_polledio_arg_t);
1225973Szk194757 static void	asy_polled_exit(cons_polledio_arg_t);
1235973Szk194757 
12452Sdf157793 static int	asymctl(struct asycom *, int, int);
12552Sdf157793 static int	asytodm(int, int);
12652Sdf157793 static int	dmtoasy(int);
12752Sdf157793 static void	asycheckflowcontrol_hw(struct asycom *asy);
12852Sdf157793 static boolean_t asycheckflowcontrol_sw(struct asycom *asy);
12952Sdf157793 static void	asy_ppsevent(struct asycom *asy, int msr);
13052Sdf157793 
13152Sdf157793 extern kcondvar_t lbolt_cv;
13252Sdf157793 extern int ddi_create_internal_pathname(dev_info_t *dip, char *name,
13352Sdf157793 		int spec_type, minor_t minor_num);
13452Sdf157793 
13552Sdf157793 
13652Sdf157793 /*
13752Sdf157793  * Baud rate table. Indexed by #defines found in sys/termios.h
13852Sdf157793  */
13952Sdf157793 ushort_t asyspdtab[] = {
14052Sdf157793 	0,	/* 0 baud rate */
14152Sdf157793 	0x900,	/* 50 baud rate */
14252Sdf157793 	0x600,	/* 75 baud rate */
14352Sdf157793 	0x417,	/* 110 baud rate (%0.026) */
14452Sdf157793 	0x359,	/* 134 baud rate (%0.058) */
14552Sdf157793 	0x300,	/* 150 baud rate */
14652Sdf157793 	0x240,	/* 200 baud rate */
14752Sdf157793 	0x180,	/* 300 baud rate */
14852Sdf157793 	0x0c0,	/* 600 baud rate */
14952Sdf157793 	0x060,	/* 1200 baud rate */
15052Sdf157793 	0x040,	/* 1800 baud rate */
15152Sdf157793 	0x030,	/* 2400 baud rate */
15252Sdf157793 	0x018,	/* 4800 baud rate */
15352Sdf157793 	0x00c,	/* 9600 baud rate */
15452Sdf157793 	0x006,	/* 19200 baud rate */
15552Sdf157793 	0x003,	/* 38400 baud rate */
15652Sdf157793 	0x002,	/* 57600 baud rate */
15752Sdf157793 	0,	/* 76800 baud rate - not supported */
15852Sdf157793 	0x001,	/* 115200 baud rate */
15952Sdf157793 	0,	/* 153600 baud rate - not supported */
16052Sdf157793 	0x8002,	/* 230400 baud rate - supported on specific platforms */
16152Sdf157793 	0,	/* 307200 baud rate - not supported */
16252Sdf157793 	0x8001	/* 460800 baud rate - supported on specific platforms */
16352Sdf157793 };
16452Sdf157793 
16552Sdf157793 /*
16652Sdf157793  * Number of speeds supported is the number of entries in
16752Sdf157793  * the above table.
16852Sdf157793  */
16952Sdf157793 #define	N_SU_SPEEDS	(sizeof (asyspdtab)/sizeof (ushort_t))
17052Sdf157793 
17152Sdf157793 /*
17252Sdf157793  * Human-readable baud rate table.
17352Sdf157793  * Indexed by #defines found in sys/termios.h
17452Sdf157793  */
17552Sdf157793 int baudtable[] = {
17652Sdf157793 	0,	/* 0 baud rate */
17752Sdf157793 	50,	/* 50 baud rate */
17852Sdf157793 	75,	/* 75 baud rate */
17952Sdf157793 	110,	/* 110 baud rate */
18052Sdf157793 	134,	/* 134 baud rate */
18152Sdf157793 	150,	/* 150 baud rate */
18252Sdf157793 	200,	/* 200 baud rate */
18352Sdf157793 	300,	/* 300 baud rate */
18452Sdf157793 	600,	/* 600 baud rate */
18552Sdf157793 	1200,	/* 1200 baud rate */
18652Sdf157793 	1800,	/* 1800 baud rate */
18752Sdf157793 	2400,	/* 2400 baud rate */
18852Sdf157793 	4800,	/* 4800 baud rate */
18952Sdf157793 	9600,	/* 9600 baud rate */
19052Sdf157793 	19200,	/* 19200 baud rate */
19152Sdf157793 	38400,	/* 38400 baud rate */
19252Sdf157793 	57600,	/* 57600 baud rate */
19352Sdf157793 	76800,	/* 76800 baud rate */
19452Sdf157793 	115200,	/* 115200 baud rate */
19552Sdf157793 	153600,	/* 153600 baud rate */
19652Sdf157793 	230400,	/* 230400 baud rate */
19752Sdf157793 	307200,	/* 307200 baud rate */
19852Sdf157793 	460800	/* 460800 baud rate */
19952Sdf157793 };
20052Sdf157793 
20152Sdf157793 static int asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr);
20252Sdf157793 static int asyclose(queue_t *q, int flag);
20352Sdf157793 static void asywput(queue_t *q, mblk_t *mp);
20452Sdf157793 static void asyrsrv(queue_t *q);
20552Sdf157793 
20652Sdf157793 struct module_info asy_info = {
20752Sdf157793 	0,
20852Sdf157793 	"su",
20952Sdf157793 	0,
21052Sdf157793 	INFPSZ,
21152Sdf157793 	32*4096,
21252Sdf157793 	4096
21352Sdf157793 };
21452Sdf157793 
21552Sdf157793 static struct qinit asy_rint = {
21652Sdf157793 	putq,
21752Sdf157793 	(int (*)())asyrsrv,
21852Sdf157793 	asyopen,
21952Sdf157793 	asyclose,
22052Sdf157793 	NULL,
22152Sdf157793 	&asy_info,
22252Sdf157793 	NULL
22352Sdf157793 };
22452Sdf157793 
22552Sdf157793 static struct qinit asy_wint = {
22652Sdf157793 	(int (*)())asywput,
22752Sdf157793 	NULL,
22852Sdf157793 	NULL,
22952Sdf157793 	NULL,
23052Sdf157793 	NULL,
23152Sdf157793 	&asy_info,
23252Sdf157793 	NULL
23352Sdf157793 };
23452Sdf157793 
23552Sdf157793 struct streamtab asy_str_info = {
23652Sdf157793 	&asy_rint,
23752Sdf157793 	&asy_wint,
23852Sdf157793 	NULL,
23952Sdf157793 	NULL
24052Sdf157793 };
24152Sdf157793 
24252Sdf157793 static int asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
24352Sdf157793 		void **result);
24452Sdf157793 static int asyprobe(dev_info_t *);
24552Sdf157793 static int asyattach(dev_info_t *, ddi_attach_cmd_t);
24652Sdf157793 static int asydetach(dev_info_t *, ddi_detach_cmd_t);
24752Sdf157793 
24852Sdf157793 static 	struct cb_ops cb_asy_ops = {
24952Sdf157793 	nodev,			/* cb_open */
25052Sdf157793 	nodev,			/* cb_close */
25152Sdf157793 	nodev,			/* cb_strategy */
25252Sdf157793 	nodev,			/* cb_print */
25352Sdf157793 	nodev,			/* cb_dump */
25452Sdf157793 	nodev,			/* cb_read */
25552Sdf157793 	nodev,			/* cb_write */
25652Sdf157793 	nodev,			/* cb_ioctl */
25752Sdf157793 	nodev,			/* cb_devmap */
25852Sdf157793 	nodev,			/* cb_mmap */
25952Sdf157793 	nodev,			/* cb_segmap */
26052Sdf157793 	nochpoll,		/* cb_chpoll */
26152Sdf157793 	ddi_prop_op,		/* cb_prop_op */
26252Sdf157793 	&asy_str_info,		/* cb_stream */
26352Sdf157793 	D_MP			/* cb_flag */
26452Sdf157793 };
26552Sdf157793 
26652Sdf157793 struct dev_ops asy_ops = {
26752Sdf157793 	DEVO_REV,		/* devo_rev */
26852Sdf157793 	0,			/* devo_refcnt */
26952Sdf157793 	asyinfo,		/* devo_getinfo */
27052Sdf157793 	nulldev,		/* devo_identify */
27152Sdf157793 	asyprobe,		/* devo_probe */
27252Sdf157793 	asyattach,		/* devo_attach */
27352Sdf157793 	asydetach,		/* devo_detach */
27452Sdf157793 	nodev,			/* devo_reset */
27552Sdf157793 	&cb_asy_ops,		/* devo_cb_ops */
2767656SSherry.Moore@Sun.COM 	NULL,			/* devo_bus_ops */
2777656SSherry.Moore@Sun.COM 	NULL,			/* devo_power */
2787656SSherry.Moore@Sun.COM 	ddi_quiesce_not_supported,	/* devo_quiesce */
27952Sdf157793 };
28052Sdf157793 
28152Sdf157793 /*
28252Sdf157793  * Module linkage information for the kernel.
28352Sdf157793  */
28452Sdf157793 
28552Sdf157793 static struct modldrv modldrv = {
28652Sdf157793 	&mod_driverops, /* Type of module.  This one is a driver */
2877656SSherry.Moore@Sun.COM 	"su driver",
28852Sdf157793 	&asy_ops,	/* driver ops */
28952Sdf157793 };
29052Sdf157793 
29152Sdf157793 static struct modlinkage modlinkage = {
29252Sdf157793 	MODREV_1,
29352Sdf157793 	&modldrv,
29452Sdf157793 	NULL
29552Sdf157793 };
29652Sdf157793 
29752Sdf157793 int
_init(void)29852Sdf157793 _init(void)
29952Sdf157793 {
30052Sdf157793 	int status;
30152Sdf157793 
30252Sdf157793 	status = ddi_soft_state_init(&su_asycom, sizeof (struct asycom),
30352Sdf157793 	    SU_INITIAL_SOFT_ITEMS);
30452Sdf157793 	if (status != 0)
3055973Szk194757 		return (status);
30652Sdf157793 	status = ddi_soft_state_init(&su_asyncline, sizeof (struct asyncline),
30752Sdf157793 	    SU_INITIAL_SOFT_ITEMS);
30852Sdf157793 	if (status != 0) {
30952Sdf157793 		ddi_soft_state_fini(&su_asycom);
31052Sdf157793 		return (status);
31152Sdf157793 	}
31252Sdf157793 
31352Sdf157793 	if ((status = mod_install(&modlinkage)) != 0) {
31452Sdf157793 		ddi_soft_state_fini(&su_asycom);
31552Sdf157793 		ddi_soft_state_fini(&su_asyncline);
31652Sdf157793 	}
31752Sdf157793 
31852Sdf157793 	return (status);
31952Sdf157793 }
32052Sdf157793 
32152Sdf157793 int
_fini(void)32252Sdf157793 _fini(void)
32352Sdf157793 {
32452Sdf157793 	int i;
32552Sdf157793 
32652Sdf157793 	i = mod_remove(&modlinkage);
32752Sdf157793 	if (i == 0) {
32852Sdf157793 		ddi_soft_state_fini(&su_asycom);
32952Sdf157793 		ddi_soft_state_fini(&su_asyncline);
33052Sdf157793 	}
33152Sdf157793 
33252Sdf157793 	return (i);
33352Sdf157793 }
33452Sdf157793 
33552Sdf157793 int
_info(struct modinfo * modinfop)33652Sdf157793 _info(struct modinfo *modinfop)
33752Sdf157793 {
33852Sdf157793 	return (mod_info(&modlinkage, modinfop));
33952Sdf157793 }
34052Sdf157793 
34152Sdf157793 static int
asyprobe(dev_info_t * devi)34252Sdf157793 asyprobe(dev_info_t *devi)
34352Sdf157793 {
34452Sdf157793 	int		instance;
34552Sdf157793 	ddi_acc_handle_t handle;
34652Sdf157793 	uchar_t *addr;
34752Sdf157793 	ddi_device_acc_attr_t attr;
34852Sdf157793 
34952Sdf157793 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
35052Sdf157793 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
35152Sdf157793 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
35252Sdf157793 	if (ddi_regs_map_setup(devi, SU_REGISTER_FILE_NO, (caddr_t *)&addr,
35352Sdf157793 	    SU_REGOFFSET, SU_REGISTER_LEN, &attr, &handle) != DDI_SUCCESS) {
35452Sdf157793 		cmn_err(CE_WARN, "asyprobe regs map setup failed");
35552Sdf157793 		return (DDI_PROBE_FAILURE);
35652Sdf157793 	}
35752Sdf157793 #ifdef DEBUG
35852Sdf157793 	if (asydebug)
3595973Szk194757 		printf("Probe address mapped %p\n", (void *)addr);
36052Sdf157793 #endif
36152Sdf157793 
36252Sdf157793 	/*
36352Sdf157793 	 * Probe for the device:
36452Sdf157793 	 * 	Ser. int. uses bits 0,1,2; FIFO uses 3,6,7; 4,5 wired low.
36552Sdf157793 	 * 	If bit 4 or 5 appears on inb() ISR, board is not there.
36652Sdf157793 	 */
3674125Sjesusm 	if (ddi_get8(handle, addr+ISR) & 0x30) {
3685973Szk194757 		ddi_regs_map_free(&handle);
3695973Szk194757 		return (DDI_PROBE_FAILURE);
3704125Sjesusm 	}
3714125Sjesusm 
37252Sdf157793 	instance = ddi_get_instance(devi);
37352Sdf157793 	if (max_asy_instance < instance)
3745973Szk194757 		max_asy_instance = instance;
37552Sdf157793 	ddi_regs_map_free(&handle);
37652Sdf157793 
37752Sdf157793 	return (DDI_PROBE_SUCCESS); /* hw is present */
37852Sdf157793 }
37952Sdf157793 
38052Sdf157793 static int
asydetach(dev_info_t * devi,ddi_detach_cmd_t cmd)38152Sdf157793 asydetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
38252Sdf157793 {
38352Sdf157793 	register int	instance;
38452Sdf157793 	struct asycom	*asy;
38552Sdf157793 	struct asyncline *async;
38652Sdf157793 	char		name[16];
38752Sdf157793 
38852Sdf157793 	instance = ddi_get_instance(devi);	/* find out which unit */
38952Sdf157793 
39052Sdf157793 	asy = (struct asycom *)ddi_get_soft_state(su_asycom, instance);
39152Sdf157793 	async = (struct asyncline *)ddi_get_soft_state(su_asyncline, instance);
39252Sdf157793 
39352Sdf157793 	switch (cmd) {
3945973Szk194757 		case DDI_DETACH:
3955973Szk194757 			break;
3965973Szk194757 		case DDI_SUSPEND:
3975973Szk194757 			/* grab both mutex locks */
3985973Szk194757 			mutex_enter(asy->asy_excl);
3995973Szk194757 			mutex_enter(asy->asy_excl_hi);
4005973Szk194757 			if (asy->suspended) {
4015973Szk194757 				mutex_exit(asy->asy_excl_hi);
4025973Szk194757 				mutex_exit(asy->asy_excl);
4035973Szk194757 				return (DDI_SUCCESS);
4045973Szk194757 			}
4055973Szk194757 			asy->suspended = B_TRUE;
4065973Szk194757 
4075973Szk194757 			/*
4085973Szk194757 			 * The quad UART ST16C554D, version D2 (made by EXAR)
4095973Szk194757 			 * has an anomaly of generating spurious interrupts
4105973Szk194757 			 * when the ICR is loaded with zero. The workaround
4115973Szk194757 			 * would be to read/write any register with DATA1 bit
4125973Szk194757 			 * set to 0 before such write.
4135973Szk194757 			 */
4145973Szk194757 			if (asy->asy_hwtype == ASY16C554D)
4155973Szk194757 				OUTB(SPR, 0);
4165973Szk194757 
4175973Szk194757 			/* Disable further interrupts */
4185973Szk194757 			OUTB(ICR, 0);
41952Sdf157793 			mutex_exit(asy->asy_excl_hi);
42052Sdf157793 			mutex_exit(asy->asy_excl);
42152Sdf157793 			return (DDI_SUCCESS);
4225973Szk194757 
4235973Szk194757 		default:
4245973Szk194757 			return (DDI_FAILURE);
42552Sdf157793 	}
42652Sdf157793 
42752Sdf157793 #ifdef DEBUG
42852Sdf157793 	if (asydebug & ASY_DEBUG_INIT)
4295973Szk194757 		cmn_err(CE_NOTE, "su%d: ASY%s shutdown.", instance,
4305973Szk194757 		    asy->asy_hwtype == ASY82510 ? "82510" :
4315973Szk194757 		    asy->asy_hwtype == ASY16550AF ? "16550AF" :
4325973Szk194757 		    asy->asy_hwtype == ASY16C554D ? "16C554D" :
4335973Szk194757 		    "8250");
43452Sdf157793 #endif
43552Sdf157793 	/*
43652Sdf157793 	 * Before removing interrupts it is always better to disable
43752Sdf157793 	 * interrupts if the chip gives a provision to disable the
43852Sdf157793 	 * serial port interrupts.
43952Sdf157793 	 */
44052Sdf157793 	mutex_enter(asy->asy_excl);
44152Sdf157793 	mutex_enter(asy->asy_excl_hi);
4422119Sanovick 	/* disable interrupts, see EXAR bug */
4432877Sanovick 	if (asy->asy_hwtype == ASY16C554D)
4442877Sanovick 		OUTB(SPR, 0);
4452119Sanovick 	OUTB(ICR, 0);
44652Sdf157793 	mutex_exit(asy->asy_excl_hi);
44752Sdf157793 	mutex_exit(asy->asy_excl);
44852Sdf157793 
44952Sdf157793 	/* remove minor device node(s) for this device */
45052Sdf157793 	(void) sprintf(name, "%c", (instance+'a'));	/* serial-port */
45152Sdf157793 	ddi_remove_minor_node(devi, name);
45252Sdf157793 	(void) sprintf(name, "%c,cu", (instance+'a')); /* serial-port:dailout */
45352Sdf157793 	ddi_remove_minor_node(devi, name);
45452Sdf157793 
45552Sdf157793 	mutex_destroy(asy->asy_excl);
45652Sdf157793 	mutex_destroy(asy->asy_excl_hi);
45752Sdf157793 	kmem_free(asy->asy_excl, sizeof (kmutex_t));
45852Sdf157793 	kmem_free(asy->asy_excl_hi, sizeof (kmutex_t));
45952Sdf157793 	cv_destroy(&async->async_flags_cv);
46052Sdf157793 	kstat_delete(asy->sukstat);
46152Sdf157793 	ddi_remove_intr(devi, 0, asy->asy_iblock);
46252Sdf157793 	ddi_regs_map_free(&asy->asy_handle);
46352Sdf157793 	ddi_remove_softintr(asy->asy_softintr_id);
46452Sdf157793 	mutex_destroy(asy->asy_soft_lock);
46552Sdf157793 	kmem_free(asy->asy_soft_lock, sizeof (kmutex_t));
46652Sdf157793 	ddi_soft_state_free(su_asycom, instance);
46752Sdf157793 	ddi_soft_state_free(su_asyncline, instance);
46852Sdf157793 	return (DDI_SUCCESS);
46952Sdf157793 }
47052Sdf157793 
47152Sdf157793 static int
asyattach(dev_info_t * devi,ddi_attach_cmd_t cmd)47252Sdf157793 asyattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
47352Sdf157793 {
47452Sdf157793 	register int	instance;
47552Sdf157793 	struct asycom	*asy;
47652Sdf157793 	struct asyncline *async;
47752Sdf157793 	char		name[40];
47852Sdf157793 	ddi_device_acc_attr_t attr;
47952Sdf157793 	enum states { EMPTY, SOFTSTATE, REGSMAP, MUTEXES, ADDINTR,
48052Sdf157793 	    SOFTINTR, ASYINIT, KSTAT, MINORNODE };
48152Sdf157793 	enum states state = EMPTY;
4822877Sanovick 	char *hwtype;
48352Sdf157793 
48452Sdf157793 	instance = ddi_get_instance(devi);	/* find out which unit */
48552Sdf157793 
48652Sdf157793 	/* cannot attach a device that has not been probed first */
48752Sdf157793 	if (instance > max_asy_instance)
4885973Szk194757 		return (DDI_FAILURE);
48952Sdf157793 
49052Sdf157793 	if (cmd != DDI_RESUME) {
49152Sdf157793 		/* Allocate soft state space */
49252Sdf157793 		if (ddi_soft_state_zalloc(su_asycom, instance) != DDI_SUCCESS) {
49352Sdf157793 			cmn_err(CE_WARN, "su%d: cannot allocate soft state",
49452Sdf157793 			    instance);
49552Sdf157793 			goto error;
49652Sdf157793 		}
49752Sdf157793 	}
49852Sdf157793 	state = SOFTSTATE;
49952Sdf157793 
50052Sdf157793 	asy = (struct asycom *)ddi_get_soft_state(su_asycom, instance);
50152Sdf157793 
50252Sdf157793 	if (asy == NULL) {
50352Sdf157793 		cmn_err(CE_WARN, "su%d: cannot get soft state", instance);
50452Sdf157793 		goto error;
50552Sdf157793 	}
50652Sdf157793 
50752Sdf157793 	switch (cmd) {
5085973Szk194757 		case DDI_ATTACH:
5095973Szk194757 			break;
5105973Szk194757 		case DDI_RESUME: {
5115973Szk194757 			struct asyncline *async;
5125973Szk194757 
5135973Szk194757 			/* grab both mutex locks */
5145973Szk194757 			mutex_enter(asy->asy_excl);
5155973Szk194757 			mutex_enter(asy->asy_excl_hi);
5165973Szk194757 			if (!asy->suspended) {
5175973Szk194757 				mutex_exit(asy->asy_excl_hi);
5185973Szk194757 				mutex_exit(asy->asy_excl);
5195973Szk194757 				return (DDI_SUCCESS);
5205973Szk194757 			}
5215973Szk194757 			/*
5225973Szk194757 			 * re-setup all the registers and enable interrupts if
5235973Szk194757 			 * needed
5245973Szk194757 			 */
5255973Szk194757 			async = (struct asyncline *)asy->asy_priv;
5265973Szk194757 			if ((async) && (async->async_flags & ASYNC_ISOPEN))
5275973Szk194757 				(void) asy_program(asy, ASY_INIT);
5285973Szk194757 			asy->suspended = B_FALSE;
52952Sdf157793 			mutex_exit(asy->asy_excl_hi);
53052Sdf157793 			mutex_exit(asy->asy_excl);
53152Sdf157793 			return (DDI_SUCCESS);
53252Sdf157793 		}
5335973Szk194757 		default:
5345973Szk194757 			goto error;
53552Sdf157793 	}
53652Sdf157793 
53752Sdf157793 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
53852Sdf157793 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
53952Sdf157793 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
54052Sdf157793 
54152Sdf157793 	if (ddi_regs_map_setup(devi, SU_REGISTER_FILE_NO,
54252Sdf157793 	    (caddr_t *)&asy->asy_ioaddr, SU_REGOFFSET, SU_REGISTER_LEN,
54352Sdf157793 	    &attr, &asy->asy_handle) != DDI_SUCCESS) {
54452Sdf157793 		cmn_err(CE_WARN, "asyprobe regs map setup failed");
54552Sdf157793 		goto error;
54652Sdf157793 	}
54752Sdf157793 	state = REGSMAP;
54852Sdf157793 
54952Sdf157793 #ifdef DEBUG
55052Sdf157793 	if (asydebug)
5515973Szk194757 		printf("su attach mapped %p\n", (void *)asy->asy_ioaddr);
55252Sdf157793 #endif
55352Sdf157793 
55452Sdf157793 	/*
55552Sdf157793 	 * Initialize the port with default settings.
55652Sdf157793 	 */
55752Sdf157793 	asy->asy_fifo_buf = 1;
55852Sdf157793 	asy->asy_use_fifo = FIFO_OFF;
55952Sdf157793 
56052Sdf157793 	/*
56152Sdf157793 	 * Check for baudrate generator's "baud-divisor-factor" property setup
56252Sdf157793 	 * by OBP, since different UART chips might have different baudrate
56352Sdf157793 	 * generator divisor. e.g., in case of NSPG's Sputnik platform, the
56452Sdf157793 	 * baud-divisor-factor is 13, it uses dedicated 16552 "DUART" chip
56552Sdf157793 	 * instead of SuperIO. Since the baud-divisor-factor must be a positive
56652Sdf157793 	 * integer, the divisors will always be at least as large as the values
56752Sdf157793 	 * in asyspdtab[].  Make the default factor 1.
56852Sdf157793 	 */
56952Sdf157793 	asy->asy_baud_divisor_factor = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
57052Sdf157793 	    DDI_PROP_DONTPASS, "baud-divisor-factor", 1);
57152Sdf157793 
57252Sdf157793 	/* set speed cap */
57352Sdf157793 	asy->asy_speed_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
57452Sdf157793 	    DDI_PROP_DONTPASS, "serial-speed-cap", 115200);
57552Sdf157793 
57652Sdf157793 	/* check for ASY82510 chip */
57752Sdf157793 	OUTB(ISR, 0x20);
57852Sdf157793 	if (INB(ISR) & 0x20) { /* 82510 chip is present */
57952Sdf157793 		/*
58052Sdf157793 		 * Since most of the general operation of the 82510 chip
58152Sdf157793 		 * can be done from BANK 0 (8250A/16450 compatable mode)
58252Sdf157793 		 * we will default to BANK 0.
58352Sdf157793 		 */
58452Sdf157793 		asy->asy_hwtype = ASY82510;
58552Sdf157793 		OUTB(DAT+7, 0x04); /* clear status */
58652Sdf157793 		OUTB(ISR, 0x40); /* set to bank 2 */
58752Sdf157793 		OUTB(MCR, 0x08); /* IMD */
58852Sdf157793 		OUTB(DAT, 0x21); /* FMD */
58952Sdf157793 		OUTB(ISR, 0x00); /* set to bank 0 */
59052Sdf157793 		asy->asy_trig_level = 0;
59152Sdf157793 	} else { /* Set the UART in FIFO mode if it has FIFO buffers */
59252Sdf157793 		asy->asy_hwtype = ASY16550AF;
59352Sdf157793 		OUTB(FIFOR, 0x00); /* clear fifo register */
59452Sdf157793 		asy->asy_trig_level = 0x00; /* sets the fifo Threshold to 1 */
59552Sdf157793 
59652Sdf157793 		/* set/Enable FIFO */
59752Sdf157793 		OUTB(FIFOR, FIFO_ON | FIFODMA | FIFOTXFLSH | FIFORXFLSH |
59852Sdf157793 		    (asy->asy_trig_level & 0xff));
59952Sdf157793 
60052Sdf157793 		if ((INB(ISR) & 0xc0) == 0xc0)
6015973Szk194757 			asy->asy_use_fifo = FIFO_ON;
60252Sdf157793 		else {
60352Sdf157793 			asy->asy_hwtype = ASY8250;
60452Sdf157793 			OUTB(FIFOR, 0x00); /* NO FIFOs */
60552Sdf157793 			asy->asy_trig_level = 0;
60652Sdf157793 		}
60752Sdf157793 	}
60852Sdf157793 
6092877Sanovick 	/* check for ST16C554D chip */
6102877Sanovick 	if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, DDI_PROP_NOTPROM |
6112877Sanovick 	    DDI_PROP_DONTPASS, "hwtype", &hwtype)) == DDI_PROP_SUCCESS) {
6122877Sanovick 		if (strcmp(hwtype, "ST16C554D") == 0)
6132877Sanovick 			asy->asy_hwtype = ASY16C554D;
6142877Sanovick 		ddi_prop_free(hwtype);
6152877Sanovick 	}
6162877Sanovick 
6172119Sanovick 	/* disable interrupts, see EXAR bug */
6182877Sanovick 	if (asy->asy_hwtype == ASY16C554D)
6192877Sanovick 		OUTB(SPR, 0);
6202119Sanovick 	OUTB(ICR, 0);
62152Sdf157793 	OUTB(LCR, DLAB); /* select baud rate generator */
62252Sdf157793 	/* Set the baud rate to 9600 */
62352Sdf157793 	OUTB(DAT+DLL, (ASY9600*asy->asy_baud_divisor_factor) & 0xff);
62452Sdf157793 	OUTB(DAT+DLH, ((ASY9600*asy->asy_baud_divisor_factor) >> 8) & 0xff);
62552Sdf157793 	OUTB(LCR, STOP1|BITS8);
62652Sdf157793 	OUTB(MCR, (DTR | RTS| OUT2));
62752Sdf157793 
62852Sdf157793 	/*
62952Sdf157793 	 * Set up the other components of the asycom structure for this port.
63052Sdf157793 	 */
63152Sdf157793 	asy->asy_excl = (kmutex_t *)
63252Sdf157793 	    kmem_zalloc(sizeof (kmutex_t), KM_SLEEP);
63352Sdf157793 	asy->asy_excl_hi = (kmutex_t *)
63452Sdf157793 	    kmem_zalloc(sizeof (kmutex_t), KM_SLEEP);
63552Sdf157793 	asy->asy_soft_lock = (kmutex_t *)
63652Sdf157793 	    kmem_zalloc(sizeof (kmutex_t), KM_SLEEP);
63752Sdf157793 	asy->asy_unit = instance;
63852Sdf157793 	asy->asy_dip = devi;
63952Sdf157793 
64052Sdf157793 	if (ddi_get_iblock_cookie(devi, 0, &asy->asy_iblock) != DDI_SUCCESS) {
64152Sdf157793 		cmn_err(CE_NOTE,
64252Sdf157793 		    "Get iblock_cookie failed-Device interrupt%x\n", instance);
64352Sdf157793 		goto error;
64452Sdf157793 	}
64552Sdf157793 
64652Sdf157793 	if (ddi_get_soft_iblock_cookie(devi, DDI_SOFTINT_HIGH,
64752Sdf157793 	    &asy->asy_soft_iblock) != DDI_SUCCESS) {
64852Sdf157793 		cmn_err(CE_NOTE, "Get iblock_cookie failed -soft interrupt%x\n",
64952Sdf157793 		    instance);
65052Sdf157793 		goto error;
65152Sdf157793 	}
65252Sdf157793 
65352Sdf157793 	mutex_init(asy->asy_soft_lock, NULL, MUTEX_DRIVER,
65452Sdf157793 	    (void *)asy->asy_soft_iblock);
65552Sdf157793 	mutex_init(asy->asy_excl, NULL, MUTEX_DRIVER, NULL);
65652Sdf157793 	mutex_init(asy->asy_excl_hi, NULL, MUTEX_DRIVER,
65752Sdf157793 	    (void *)asy->asy_iblock);
65852Sdf157793 	state = MUTEXES;
65952Sdf157793 
66052Sdf157793 	/*
66152Sdf157793 	 * Install interrupt handlers for this device.
66252Sdf157793 	 */
66352Sdf157793 	if (ddi_add_intr(devi, 0, &(asy->asy_iblock), 0, asyintr,
66452Sdf157793 	    (caddr_t)asy) != DDI_SUCCESS) {
66552Sdf157793 		cmn_err(CE_CONT,
66652Sdf157793 		    "Cannot set device interrupt for su driver\n");
66752Sdf157793 		goto error;
66852Sdf157793 	}
66952Sdf157793 	state = ADDINTR;
67052Sdf157793 
67152Sdf157793 	if (ddi_add_softintr(devi, DDI_SOFTINT_HIGH, &(asy->asy_softintr_id),
67252Sdf157793 	    &asy->asy_soft_iblock, 0, asysoftintr, (caddr_t)asy)
67352Sdf157793 	    != DDI_SUCCESS) {
67452Sdf157793 		cmn_err(CE_CONT, "Cannot set soft interrupt for su driver\n");
67552Sdf157793 		goto error;
67652Sdf157793 	}
67752Sdf157793 	state = SOFTINTR;
67852Sdf157793 
67952Sdf157793 	/* initialize the asyncline structure */
68052Sdf157793 	if (ddi_soft_state_zalloc(su_asyncline, instance) != DDI_SUCCESS) {
68152Sdf157793 		cmn_err(CE_CONT, "su%d: cannot allocate soft state", instance);
68252Sdf157793 		goto error;
68352Sdf157793 	}
68452Sdf157793 	state = ASYINIT;
68552Sdf157793 
68652Sdf157793 	async = (struct asyncline *)ddi_get_soft_state(su_asyncline, instance);
68752Sdf157793 
68852Sdf157793 	mutex_enter(asy->asy_excl);
68952Sdf157793 	async->async_common = asy;
69052Sdf157793 	cv_init(&async->async_flags_cv, NULL, CV_DEFAULT, NULL);
69152Sdf157793 	mutex_exit(asy->asy_excl);
69252Sdf157793 
69352Sdf157793 	if ((asy->sukstat = kstat_create("su", instance, "serialstat",
69452Sdf157793 	    "misc", KSTAT_TYPE_NAMED, 2, KSTAT_FLAG_VIRTUAL)) != NULL) {
69552Sdf157793 		asy->sukstat->ks_data = &asy->kstats;
69652Sdf157793 		kstat_named_init(&asy->kstats.ringover, "ring buffer overflow",
69752Sdf157793 		    KSTAT_DATA_UINT64);
69852Sdf157793 		kstat_named_init(&asy->kstats.siloover, "silo overflow",
69952Sdf157793 		    KSTAT_DATA_UINT64);
70052Sdf157793 		kstat_install(asy->sukstat);
70152Sdf157793 	}
70252Sdf157793 	state = KSTAT;
70352Sdf157793 
70452Sdf157793 	if (strcmp(ddi_node_name(devi), "rsc-console") == 0) {
70552Sdf157793 		/*
70652Sdf157793 		 * If the device is configured as the 'rsc-console'
70752Sdf157793 		 * create the minor device for this node.
70852Sdf157793 		 */
70952Sdf157793 		if (ddi_create_minor_node(devi, "ssp", S_IFCHR,
71052Sdf157793 		    asy->asy_unit | RSC_DEVICE, DDI_PSEUDO, NULL)
71152Sdf157793 		    == DDI_FAILURE) {
71252Sdf157793 			cmn_err(CE_WARN,
71352Sdf157793 			    "%s%d: Failed to create node rsc-console",
71452Sdf157793 			    ddi_get_name(devi), ddi_get_instance(devi));
71552Sdf157793 			goto error;
71652Sdf157793 		}
71752Sdf157793 
71852Sdf157793 		asy->asy_lom_console = 0;
71952Sdf157793 		asy->asy_rsc_console = 1;
72052Sdf157793 		asy->asy_rsc_control = 0;
72152Sdf157793 		asy->asy_device_type = ASY_SERIAL;
72252Sdf157793 		asy->asy_flags |= ASY_IGNORE_CD;
72352Sdf157793 
72452Sdf157793 	} else if (strcmp(ddi_node_name(devi), "lom-console") == 0) {
72552Sdf157793 		/*
72652Sdf157793 		 * If the device is configured as the 'lom-console'
72752Sdf157793 		 * create the minor device for this node.
72852Sdf157793 		 * Do not create a dialout device.
72952Sdf157793 		 * Use the same minor numbers as would be used for standard
73052Sdf157793 		 * serial instances.
73152Sdf157793 		 */
73252Sdf157793 		if (ddi_create_minor_node(devi, "lom-console", S_IFCHR,
73352Sdf157793 		    instance, DDI_NT_SERIAL_LOMCON, NULL) == DDI_FAILURE) {
73452Sdf157793 			cmn_err(CE_WARN,
73552Sdf157793 			    "%s%d: Failed to create node lom-console",
73652Sdf157793 			    ddi_get_name(devi), ddi_get_instance(devi));
73752Sdf157793 			goto error;
73852Sdf157793 		}
73952Sdf157793 		asy->asy_lom_console = 1;
74052Sdf157793 		asy->asy_rsc_console = 0;
74152Sdf157793 		asy->asy_rsc_control = 0;
74252Sdf157793 		asy->asy_device_type = ASY_SERIAL;
74352Sdf157793 		asy->asy_flags |= ASY_IGNORE_CD;
74452Sdf157793 
74552Sdf157793 	} else if (strcmp(ddi_node_name(devi), "rsc-control") == 0) {
74652Sdf157793 		/*
74752Sdf157793 		 * If the device is configured as the 'rsc-control'
74852Sdf157793 		 * create the minor device for this node.
74952Sdf157793 		 */
75052Sdf157793 		if (ddi_create_minor_node(devi, "sspctl", S_IFCHR,
75152Sdf157793 		    asy->asy_unit | RSC_DEVICE, DDI_PSEUDO, NULL)
75252Sdf157793 		    == DDI_FAILURE) {
75352Sdf157793 			cmn_err(CE_WARN, "%s%d: Failed to create rsc-control",
75452Sdf157793 			    ddi_get_name(devi), ddi_get_instance(devi));
75552Sdf157793 			goto error;
75652Sdf157793 		}
75752Sdf157793 
75852Sdf157793 		asy->asy_lom_console = 0;
75952Sdf157793 		asy->asy_rsc_console = 0;
76052Sdf157793 		asy->asy_rsc_control = 1;
76152Sdf157793 		asy->asy_device_type = ASY_SERIAL;
76252Sdf157793 		asy->asy_flags |= ASY_IGNORE_CD;
76352Sdf157793 
76452Sdf157793 	} else if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7655973Szk194757 	    "keyboard", 0)) {
76652Sdf157793 		/*
76752Sdf157793 		 * If the device is a keyboard, then create an internal
76852Sdf157793 		 * pathname so that the dacf code will link the node into
76952Sdf157793 		 * the keyboard console stream.  See dacf.conf.
77052Sdf157793 		 */
77152Sdf157793 		if (ddi_create_internal_pathname(devi, "keyboard",
77252Sdf157793 		    S_IFCHR, instance) == DDI_FAILURE) {
77352Sdf157793 			goto error;
77452Sdf157793 		}
77552Sdf157793 		asy->asy_flags |= ASY_IGNORE_CD;	/* ignore cd */
77652Sdf157793 		asy->asy_device_type = ASY_KEYBOARD; 	/* Device type */
77752Sdf157793 	} else if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7785973Szk194757 	    "mouse", 0)) {
77952Sdf157793 		/*
78052Sdf157793 		 * If the device is a mouse, then create an internal
78152Sdf157793 		 * pathname so that the dacf code will link the node into
78252Sdf157793 		 * the mouse stream.  See dacf.conf.
78352Sdf157793 		 */
78452Sdf157793 		if (ddi_create_internal_pathname(devi, "mouse", S_IFCHR,
78552Sdf157793 		    instance) == DDI_FAILURE) {
78652Sdf157793 			goto error;
78752Sdf157793 		}
78852Sdf157793 		asy->asy_flags |= ASY_IGNORE_CD;	/* ignore cd */
78952Sdf157793 		asy->asy_device_type = ASY_MOUSE;
79052Sdf157793 	} else {
79152Sdf157793 		/*
79252Sdf157793 		 * If not used for keyboard/mouse, create minor devices nodes
79352Sdf157793 		 * for this device
79452Sdf157793 		 */
79552Sdf157793 		/* serial-port */
79652Sdf157793 		(void) sprintf(name, "%c", (instance+'a'));
79752Sdf157793 		if (ddi_create_minor_node(devi, name, S_IFCHR, instance,
79852Sdf157793 		    DDI_NT_SERIAL_MB, NULL) == DDI_FAILURE) {
79952Sdf157793 			goto error;
80052Sdf157793 		}
80152Sdf157793 		state = MINORNODE;
80252Sdf157793 		/* serial-port:dailout */
80352Sdf157793 		(void) sprintf(name, "%c,cu", (instance+'a'));
80452Sdf157793 		if (ddi_create_minor_node(devi, name, S_IFCHR, instance|OUTLINE,
80552Sdf157793 		    DDI_NT_SERIAL_MB_DO, NULL) == DDI_FAILURE) {
80652Sdf157793 			goto error;
80752Sdf157793 		}
80852Sdf157793 		/* Property for ignoring DCD */
80952Sdf157793 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
81052Sdf157793 		    "ignore-cd", 0)) {
81152Sdf157793 			asy->asy_flags |= ASY_IGNORE_CD;  /* ignore cd */
81252Sdf157793 		} else {
81352Sdf157793 			asy->asy_flags &= ~ASY_IGNORE_CD;
81452Sdf157793 			/*
81552Sdf157793 			 * if ignore-cd is not available it could be
81652Sdf157793 			 * some old legacy platform, try to see
81752Sdf157793 			 * whether the old legacy property exists
81852Sdf157793 			 */
81952Sdf157793 			(void) sprintf(name,
82052Sdf157793 			    "port-%c-ignore-cd", (instance+ 'a'));
82152Sdf157793 			if (ddi_getprop(DDI_DEV_T_ANY, devi,
82252Sdf157793 			    DDI_PROP_DONTPASS, name, 0))
82352Sdf157793 				asy->asy_flags |= ASY_IGNORE_CD;
82452Sdf157793 		}
82552Sdf157793 		asy->asy_device_type = ASY_SERIAL;
82652Sdf157793 	}
8275973Szk194757 
8285973Szk194757 	/*
8295973Szk194757 	 * Fill in the polled I/O structure
8305973Szk194757 	 */
8315973Szk194757 	asy->polledio.cons_polledio_version = CONSPOLLEDIO_V0;
8325973Szk194757 	asy->polledio.cons_polledio_argument = (cons_polledio_arg_t)asy;
8335973Szk194757 	asy->polledio.cons_polledio_putchar =  asyputchar;
8345973Szk194757 	asy->polledio.cons_polledio_getchar = asygetchar;
8355973Szk194757 	asy->polledio.cons_polledio_ischar = asyischar;
8365973Szk194757 	asy->polledio.cons_polledio_enter = asy_polled_enter;
8375973Szk194757 	asy->polledio.cons_polledio_exit = asy_polled_exit;
8385973Szk194757 
8395973Szk194757 	/* Initialize saved ICR and polled_enter */
8405973Szk194757 	asy->polled_icr = 0;
8415973Szk194757 	asy->polled_enter = B_FALSE;
8425973Szk194757 
84352Sdf157793 	ddi_report_dev(devi);
84452Sdf157793 	return (DDI_SUCCESS);
84552Sdf157793 
84652Sdf157793 error:
84752Sdf157793 	if (state == MINORNODE) {
84852Sdf157793 		(void) sprintf(name, "%c", (instance+'a'));
84952Sdf157793 		ddi_remove_minor_node(devi, name);
85052Sdf157793 	}
85152Sdf157793 	if (state >= KSTAT)
85252Sdf157793 		kstat_delete(asy->sukstat);
85352Sdf157793 	if (state >= ASYINIT) {
85452Sdf157793 		cv_destroy(&async->async_flags_cv);
85552Sdf157793 		ddi_soft_state_free(su_asyncline, instance);
85652Sdf157793 	}
85752Sdf157793 	if (state >= SOFTINTR)
85852Sdf157793 		ddi_remove_softintr(asy->asy_softintr_id);
85952Sdf157793 	if (state >= ADDINTR)
86052Sdf157793 		ddi_remove_intr(devi, 0, asy->asy_iblock);
86152Sdf157793 	if (state >= MUTEXES) {
86252Sdf157793 		mutex_destroy(asy->asy_excl_hi);
86352Sdf157793 		mutex_destroy(asy->asy_excl);
86452Sdf157793 		mutex_destroy(asy->asy_soft_lock);
86552Sdf157793 		kmem_free(asy->asy_excl_hi, sizeof (kmutex_t));
86652Sdf157793 		kmem_free(asy->asy_excl, sizeof (kmutex_t));
86752Sdf157793 		kmem_free(asy->asy_soft_lock, sizeof (kmutex_t));
86852Sdf157793 	}
86952Sdf157793 	if (state >= REGSMAP)
87052Sdf157793 		ddi_regs_map_free(&asy->asy_handle);
87152Sdf157793 	if (state >= SOFTSTATE)
87252Sdf157793 		ddi_soft_state_free(su_asycom, instance);
87352Sdf157793 	/* no action for EMPTY state */
87452Sdf157793 	return (DDI_FAILURE);
87552Sdf157793 }
87652Sdf157793 
87752Sdf157793 static int
asyinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)87852Sdf157793 asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
87952Sdf157793 	void **result)
88052Sdf157793 {
88152Sdf157793 	_NOTE(ARGUNUSED(dip))
88252Sdf157793 	register dev_t dev = (dev_t)arg;
88352Sdf157793 	register int instance, error;
88452Sdf157793 	struct asycom *asy;
88552Sdf157793 
88652Sdf157793 	if ((instance = UNIT(dev)) > max_asy_instance)
88752Sdf157793 		return (DDI_FAILURE);
88852Sdf157793 
88952Sdf157793 	switch (infocmd) {
8905973Szk194757 		case DDI_INFO_DEVT2DEVINFO:
8915973Szk194757 			asy = (struct asycom *)ddi_get_soft_state(su_asycom,
8925973Szk194757 			    instance);
8935973Szk194757 			if (asy->asy_dip == NULL)
8945973Szk194757 				error = DDI_FAILURE;
8955973Szk194757 			else {
8965973Szk194757 				*result = (void *) asy->asy_dip;
8975973Szk194757 				error = DDI_SUCCESS;
8985973Szk194757 			}
8995973Szk194757 			break;
9005973Szk194757 		case DDI_INFO_DEVT2INSTANCE:
9015973Szk194757 			*result = (void *)(uintptr_t)instance;
90252Sdf157793 			error = DDI_SUCCESS;
9035973Szk194757 			break;
9045973Szk194757 		default:
9055973Szk194757 			error = DDI_FAILURE;
90652Sdf157793 	}
90752Sdf157793 	return (error);
90852Sdf157793 }
90952Sdf157793 
91052Sdf157793 static int
asyopen(queue_t * rq,dev_t * dev,int flag,int sflag,cred_t * cr)91152Sdf157793 asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr)
91252Sdf157793 {
91352Sdf157793 	_NOTE(ARGUNUSED(sflag))
91452Sdf157793 	struct asycom	*asy;
91552Sdf157793 	struct asyncline *async;
91652Sdf157793 	int		mcr;
91752Sdf157793 	int		unit;
91852Sdf157793 	int 		len;
91952Sdf157793 	struct termios 	*termiosp;
92052Sdf157793 
92152Sdf157793 #ifdef DEBUG
92252Sdf157793 	if (asydebug & ASY_DEBUG_CLOSE)
92352Sdf157793 		printf("open\n");
92452Sdf157793 #endif
92552Sdf157793 	unit = UNIT(*dev);
92652Sdf157793 	if (unit > max_asy_instance)
92752Sdf157793 		return (ENXIO);		/* unit not configured */
92852Sdf157793 
92952Sdf157793 	async = (struct asyncline *)ddi_get_soft_state(su_asyncline, unit);
93052Sdf157793 	if (async == NULL)
93152Sdf157793 		return (ENXIO);
93252Sdf157793 
93352Sdf157793 	asy = async->async_common;
93452Sdf157793 	if (asy == NULL)
93552Sdf157793 		return (ENXIO);		/* device not found by autoconfig */
93652Sdf157793 
93752Sdf157793 	mutex_enter(asy->asy_excl);
93852Sdf157793 	asy->asy_priv = (caddr_t)async;
93952Sdf157793 
94052Sdf157793 again:
94152Sdf157793 	mutex_enter(asy->asy_excl_hi);
94252Sdf157793 	/*
94352Sdf157793 	 * Block waiting for carrier to come up, unless this is a no-delay open.
94452Sdf157793 	 */
94552Sdf157793 	if (!(async->async_flags & ASYNC_ISOPEN)) {
94652Sdf157793 		/*
94752Sdf157793 		 * If this port is for a RSC console or control
94852Sdf157793 		 * use the following termio info
94952Sdf157793 		 */
95052Sdf157793 		if (asy->asy_rsc_console || asy->asy_rsc_control) {
95152Sdf157793 			async->async_ttycommon.t_cflag = CIBAUDEXT | CBAUDEXT |
95252Sdf157793 			    (B115200 & CBAUD);
95352Sdf157793 			async->async_ttycommon.t_cflag |= ((B115200 << IBSHIFT)
95452Sdf157793 			    & CIBAUD);
95552Sdf157793 			async->async_ttycommon.t_cflag |= CS8 | CREAD | CLOCAL;
95652Sdf157793 		} else if (asy->asy_lom_console) {
95752Sdf157793 			async->async_ttycommon.t_cflag = B9600 & CBAUD;
95852Sdf157793 			async->async_ttycommon.t_cflag |= ((B9600 << IBSHIFT)
95952Sdf157793 			    & CIBAUD);
96052Sdf157793 			async->async_ttycommon.t_cflag |= CS8 | CREAD | CLOCAL;
96152Sdf157793 		} else {
96252Sdf157793 
96352Sdf157793 			/*
96452Sdf157793 			 * Set the default termios settings (cflag).
96552Sdf157793 			 * Others are set in ldterm.  Release the spin
96652Sdf157793 			 * mutex as we can block here, reaquire before
96752Sdf157793 			 * calling asy_program.
96852Sdf157793 			 */
96952Sdf157793 			mutex_exit(asy->asy_excl_hi);
97052Sdf157793 			if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_root_node(),
97152Sdf157793 			    0, "ttymodes", (caddr_t)&termiosp, &len)
97252Sdf157793 			    == DDI_PROP_SUCCESS &&
97352Sdf157793 			    len == sizeof (struct termios)) {
97452Sdf157793 				async->async_ttycommon.t_cflag =
97552Sdf157793 				    termiosp->c_cflag;
97652Sdf157793 				kmem_free(termiosp, len);
97752Sdf157793 			} else {
97852Sdf157793 				cmn_err(CE_WARN,
97952Sdf157793 					"su: couldn't get ttymodes property!");
98052Sdf157793 			}
98152Sdf157793 			mutex_enter(asy->asy_excl_hi);
98252Sdf157793 		}
98352Sdf157793 		async->async_ttycommon.t_iflag = 0;
98452Sdf157793 		async->async_ttycommon.t_iocpending = NULL;
98552Sdf157793 		async->async_ttycommon.t_size.ws_row = 0;
98652Sdf157793 		async->async_ttycommon.t_size.ws_col = 0;
98752Sdf157793 		async->async_ttycommon.t_size.ws_xpixel = 0;
98852Sdf157793 		async->async_ttycommon.t_size.ws_ypixel = 0;
98952Sdf157793 		async->async_dev = *dev;
99052Sdf157793 		async->async_wbufcid = 0;
99152Sdf157793 
99252Sdf157793 		async->async_startc = CSTART;
99352Sdf157793 		async->async_stopc = CSTOP;
99452Sdf157793 		(void) asy_program(asy, ASY_INIT);
99552Sdf157793 	} else if ((async->async_ttycommon.t_flags & TS_XCLUDE) &&
9965973Szk194757 	    secpolicy_excl_open(cr) != 0) {
99752Sdf157793 		mutex_exit(asy->asy_excl_hi);
99852Sdf157793 		mutex_exit(asy->asy_excl);
99952Sdf157793 		return (EBUSY);
100052Sdf157793 	} else if ((*dev & OUTLINE) && !(async->async_flags & ASYNC_OUT)) {
100152Sdf157793 		mutex_exit(asy->asy_excl_hi);
100252Sdf157793 		mutex_exit(asy->asy_excl);
100352Sdf157793 		return (EBUSY);
100452Sdf157793 	}
100552Sdf157793 
100652Sdf157793 	if (*dev & OUTLINE)
100752Sdf157793 		async->async_flags |= ASYNC_OUT;
100852Sdf157793 
100952Sdf157793 	/* Raise DTR on every open */
101052Sdf157793 	mcr = INB(MCR);
101152Sdf157793 	OUTB(MCR, mcr|DTR);
101252Sdf157793 
101352Sdf157793 	/*
101452Sdf157793 	 * Check carrier.
101552Sdf157793 	 */
101652Sdf157793 	if (asy->asy_flags & ASY_IGNORE_CD)
101752Sdf157793 		async->async_ttycommon.t_flags |= TS_SOFTCAR;
101852Sdf157793 	if ((async->async_ttycommon.t_flags & TS_SOFTCAR) ||
10195973Szk194757 	    (INB(MSR) & DCD))
102052Sdf157793 		async->async_flags |= ASYNC_CARR_ON;
102152Sdf157793 	else
102252Sdf157793 		async->async_flags &= ~ASYNC_CARR_ON;
102352Sdf157793 	mutex_exit(asy->asy_excl_hi);
102452Sdf157793 
102552Sdf157793 	/*
102652Sdf157793 	 * If FNDELAY and FNONBLOCK are clear, block until carrier up.
102752Sdf157793 	 * Quit on interrupt.
102852Sdf157793 	 */
102952Sdf157793 	if (!(flag & (FNDELAY|FNONBLOCK)) &&
10305973Szk194757 	    !(async->async_ttycommon.t_cflag & CLOCAL)) {
103152Sdf157793 		if (!(async->async_flags & (ASYNC_CARR_ON|ASYNC_OUT)) ||
10325973Szk194757 		    ((async->async_flags & ASYNC_OUT) &&
10335973Szk194757 		    !(*dev & OUTLINE))) {
10345973Szk194757 				async->async_flags |= ASYNC_WOPEN;
10355973Szk194757 				if (cv_wait_sig(&async->async_flags_cv,
10365973Szk194757 				    asy->asy_excl) == 0) {
10375973Szk194757 					async->async_flags &= ~ASYNC_WOPEN;
10385973Szk194757 					mutex_exit(asy->asy_excl);
10395973Szk194757 					return (EINTR);
10405973Szk194757 				}
104152Sdf157793 				async->async_flags &= ~ASYNC_WOPEN;
10425973Szk194757 				goto again;
104352Sdf157793 		}
104452Sdf157793 	} else if ((async->async_flags & ASYNC_OUT) && !(*dev & OUTLINE)) {
10455973Szk194757 		mutex_exit(asy->asy_excl);
10465973Szk194757 		return (EBUSY);
104752Sdf157793 	}
104852Sdf157793 
104952Sdf157793 	if (asy->suspended) {
105052Sdf157793 		mutex_exit(asy->asy_excl);
105152Sdf157793 		(void) ddi_dev_is_needed(asy->asy_dip, 0, 1);
105252Sdf157793 		mutex_enter(asy->asy_excl);
105352Sdf157793 	}
105452Sdf157793 
105552Sdf157793 	async->async_ttycommon.t_readq = rq;
105652Sdf157793 	async->async_ttycommon.t_writeq = WR(rq);
105752Sdf157793 	rq->q_ptr = WR(rq)->q_ptr = (caddr_t)async;
105852Sdf157793 	mutex_exit(asy->asy_excl);
105952Sdf157793 	qprocson(rq);
106052Sdf157793 	async->async_flags |= ASYNC_ISOPEN;
106152Sdf157793 	async->async_polltid = 0;
106252Sdf157793 	return (0);
106352Sdf157793 }
106452Sdf157793 
106552Sdf157793 static void
async_progress_check(void * arg)106652Sdf157793 async_progress_check(void *arg)
106752Sdf157793 {
106852Sdf157793 	struct asyncline *async = arg;
106952Sdf157793 	struct asycom	 *asy = async->async_common;
107052Sdf157793 	mblk_t *bp;
107152Sdf157793 
107252Sdf157793 	/*
107352Sdf157793 	 * We define "progress" as either waiting on a timed break or delay, or
107452Sdf157793 	 * having had at least one transmitter interrupt.  If none of these are
107552Sdf157793 	 * true, then just terminate the output and wake up that close thread.
107652Sdf157793 	 */
107752Sdf157793 	mutex_enter(asy->asy_excl);
107852Sdf157793 	mutex_enter(asy->asy_excl_hi);
107952Sdf157793 	if (!(async->async_flags & (ASYNC_BREAK|ASYNC_DELAY|ASYNC_PROGRESS))) {
108052Sdf157793 		async->async_ocnt = 0;
108152Sdf157793 		async->async_flags &= ~ASYNC_BUSY;
108252Sdf157793 		async->async_timer = 0;
108352Sdf157793 		bp = async->async_xmitblk;
108452Sdf157793 		async->async_xmitblk = NULL;
108552Sdf157793 		mutex_exit(asy->asy_excl_hi);
108652Sdf157793 		if (bp != NULL)
108752Sdf157793 			freeb(bp);
108852Sdf157793 		/*
108952Sdf157793 		 * Since this timer is running, we know that we're in exit(2).
109052Sdf157793 		 * That means that the user can't possibly be waiting on any
109152Sdf157793 		 * valid ioctl(2) completion anymore, and we should just flush
109252Sdf157793 		 * everything.
109352Sdf157793 		 */
109452Sdf157793 		flushq(async->async_ttycommon.t_writeq, FLUSHALL);
109552Sdf157793 		cv_broadcast(&async->async_flags_cv);
109652Sdf157793 	} else {
109752Sdf157793 		async->async_flags &= ~ASYNC_PROGRESS;
109852Sdf157793 		async->async_timer = timeout(async_progress_check, async,
109952Sdf157793 		    drv_usectohz(su_drain_check));
110052Sdf157793 		mutex_exit(asy->asy_excl_hi);
110152Sdf157793 	}
110252Sdf157793 	mutex_exit(asy->asy_excl);
110352Sdf157793 }
110452Sdf157793 
110552Sdf157793 /*
110652Sdf157793  * Close routine.
110752Sdf157793  */
110852Sdf157793 static int
asyclose(queue_t * q,int flag)110952Sdf157793 asyclose(queue_t *q, int flag)
111052Sdf157793 {
111152Sdf157793 	struct asyncline *async;
111252Sdf157793 	struct asycom	 *asy;
111352Sdf157793 	int icr, lcr;
111452Sdf157793 	int		nohupcl;
111552Sdf157793 
111652Sdf157793 
111752Sdf157793 #ifdef DEBUG
111852Sdf157793 	if (asydebug & ASY_DEBUG_CLOSE)
111952Sdf157793 		printf("close\n");
112052Sdf157793 #endif
112152Sdf157793 	async = q->q_ptr;
112252Sdf157793 	ASSERT(async != NULL);
112352Sdf157793 	asy = async->async_common;
112452Sdf157793 
112552Sdf157793 	/* get the nohupcl OBP property of this device */
112652Sdf157793 	nohupcl = ddi_getprop(DDI_DEV_T_ANY, asy->asy_dip, DDI_PROP_DONTPASS,
112752Sdf157793 	    "nohupcl", 0);
112852Sdf157793 
112952Sdf157793 	mutex_enter(asy->asy_excl);
113052Sdf157793 	async->async_flags |= ASYNC_CLOSING;
113152Sdf157793 
113252Sdf157793 	/*
113352Sdf157793 	 * Turn off PPS handling early to avoid events occuring during
113452Sdf157793 	 * close.  Also reset the DCD edge monitoring bit.
113552Sdf157793 	 */
113652Sdf157793 	mutex_enter(asy->asy_excl_hi);
113752Sdf157793 	asy->asy_flags &= ~(ASY_PPS | ASY_PPS_EDGE);
113852Sdf157793 	mutex_exit(asy->asy_excl_hi);
113952Sdf157793 
114052Sdf157793 	/*
114152Sdf157793 	 * There are two flavors of break -- timed (M_BREAK or TCSBRK) and
114252Sdf157793 	 * untimed (TIOCSBRK).  For the timed case, these are enqueued on our
114352Sdf157793 	 * write queue and there's a timer running, so we don't have to worry
114452Sdf157793 	 * about them.  For the untimed case, though, the user obviously made a
114552Sdf157793 	 * mistake, because these are handled immediately.  We'll terminate the
114652Sdf157793 	 * break now and honor his implicit request by discarding the rest of
114752Sdf157793 	 * the data.
114852Sdf157793 	 */
114952Sdf157793 	if (!(async->async_flags & ASYNC_BREAK)) {
115052Sdf157793 		mutex_enter(asy->asy_excl_hi);
115152Sdf157793 		lcr = INB(LCR);
115252Sdf157793 		if (lcr & SETBREAK) {
115352Sdf157793 			OUTB(LCR, (lcr & ~SETBREAK));
115452Sdf157793 		}
115552Sdf157793 		mutex_exit(asy->asy_excl_hi);
115652Sdf157793 		if (lcr & SETBREAK)
115752Sdf157793 			goto nodrain;
115852Sdf157793 	}
115952Sdf157793 
116052Sdf157793 	/*
116152Sdf157793 	 * If the user told us not to delay the close ("non-blocking"), then
116252Sdf157793 	 * don't bother trying to drain.
116352Sdf157793 	 *
116452Sdf157793 	 * If the user did M_STOP (ASYNC_STOPPED), there's no hope of ever
116552Sdf157793 	 * getting an M_START (since these messages aren't enqueued), and the
116652Sdf157793 	 * only other way to clear the stop condition is by loss of DCD, which
116752Sdf157793 	 * would discard the queue data.  Thus, we drop the output data if
116852Sdf157793 	 * ASYNC_STOPPED is set.
116952Sdf157793 	 */
117052Sdf157793 	if ((flag & (FNDELAY|FNONBLOCK)) ||
117152Sdf157793 	    (async->async_flags & ASYNC_STOPPED)) {
117252Sdf157793 		goto nodrain;
117352Sdf157793 	}
117452Sdf157793 
117552Sdf157793 	/*
117652Sdf157793 	 * If there's any pending output, then we have to try to drain it.
117752Sdf157793 	 * There are two main cases to be handled:
117852Sdf157793 	 *	- called by close(2): need to drain until done or until
117952Sdf157793 	 *	  a signal is received.  No timeout.
118052Sdf157793 	 *	- called by exit(2): need to drain while making progress
118152Sdf157793 	 *	  or until a timeout occurs.  No signals.
118252Sdf157793 	 *
118352Sdf157793 	 * If we can't rely on receiving a signal to get us out of a hung
118452Sdf157793 	 * session, then we have to use a timer.  In this case, we set a timer
118552Sdf157793 	 * to check for progress in sending the output data -- all that we ask
118652Sdf157793 	 * (at each interval) is that there's been some progress made.  Since
118752Sdf157793 	 * the interrupt routine grabs buffers from the write queue, we can't
118852Sdf157793 	 * trust async_ocnt.  Instead, we use a flag.
118952Sdf157793 	 *
119052Sdf157793 	 * Note that loss of carrier will cause the output queue to be flushed,
119152Sdf157793 	 * and we'll wake up again and finish normally.
119252Sdf157793 	 */
119352Sdf157793 	if (!ddi_can_receive_sig() && su_drain_check != 0) {
119452Sdf157793 		async->async_flags &= ~ASYNC_PROGRESS;
119552Sdf157793 		async->async_timer = timeout(async_progress_check, async,
119652Sdf157793 		    drv_usectohz(su_drain_check));
119752Sdf157793 	}
119852Sdf157793 
119952Sdf157793 	while (async->async_ocnt > 0 ||
120052Sdf157793 	    async->async_ttycommon.t_writeq->q_first != NULL ||
120152Sdf157793 	    (async->async_flags & (ASYNC_BUSY|ASYNC_BREAK|ASYNC_DELAY))) {
120252Sdf157793 		if (cv_wait_sig(&async->async_flags_cv, asy->asy_excl) == 0)
120352Sdf157793 			break;
120452Sdf157793 	}
120552Sdf157793 	if (async->async_timer != 0) {
120652Sdf157793 		(void) untimeout(async->async_timer);
120752Sdf157793 		async->async_timer = 0;
120852Sdf157793 	}
120952Sdf157793 
121052Sdf157793 nodrain:
121152Sdf157793 	mutex_enter(asy->asy_excl_hi);
121252Sdf157793 
121352Sdf157793 	/* turn off the loopback mode */
121452Sdf157793 	if ((async->async_dev != rconsdev) &&
121552Sdf157793 	    (async->async_dev != kbddev) &&
121652Sdf157793 	    (async->async_dev != stdindev)) {
121752Sdf157793 		OUTB(MCR, INB(MCR) & ~ ASY_LOOP);
121852Sdf157793 	}
121952Sdf157793 
122052Sdf157793 	async->async_ocnt = 0;
122152Sdf157793 	if (async->async_xmitblk != NULL)
122252Sdf157793 		freeb(async->async_xmitblk);
122352Sdf157793 	async->async_xmitblk = NULL;
122452Sdf157793 
122552Sdf157793 	/*
122652Sdf157793 	 * If the "nohupcl" OBP property is set for this device, do
122752Sdf157793 	 * not turn off DTR and RTS no matter what.  Otherwise, if the
122852Sdf157793 	 * line has HUPCL set or is incompletely opened, turn off DTR
122952Sdf157793 	 * and RTS to fix the modem line.
123052Sdf157793 	 */
123152Sdf157793 	if (!nohupcl && ((async->async_ttycommon.t_cflag & HUPCL) ||
123252Sdf157793 	    (async->async_flags & ASYNC_WOPEN))) {
123352Sdf157793 		/* turn off DTR, RTS but NOT interrupt to 386 */
123452Sdf157793 		OUTB(MCR, OUT2);
123552Sdf157793 		mutex_exit(asy->asy_excl_hi);
123652Sdf157793 		/*
123752Sdf157793 		 * Don't let an interrupt in the middle of close
123852Sdf157793 		 * bounce us back to the top; just continue closing
123952Sdf157793 		 * as if nothing had happened.
124052Sdf157793 		 */
124152Sdf157793 		if (cv_wait_sig(&lbolt_cv, asy->asy_excl) == 0)
124252Sdf157793 			goto out;
124352Sdf157793 		mutex_enter(asy->asy_excl_hi);
124452Sdf157793 	}
124552Sdf157793 
124652Sdf157793 	/*
124752Sdf157793 	 * If nobody's using it now, turn off receiver interrupts.
124852Sdf157793 	 */
124952Sdf157793 	if ((async->async_flags & (ASYNC_WOPEN|ASYNC_ISOPEN)) == 0) {
125052Sdf157793 		icr = INB(ICR);
125152Sdf157793 		OUTB(ICR, (icr & ~RIEN));
125252Sdf157793 	}
125352Sdf157793 	mutex_exit(asy->asy_excl_hi);
125452Sdf157793 out:
125552Sdf157793 	/*
125652Sdf157793 	 * Clear out device state.
125752Sdf157793 	 */
125852Sdf157793 	async->async_flags = 0;
125952Sdf157793 	ttycommon_close(&async->async_ttycommon);
126052Sdf157793 	cv_broadcast(&async->async_flags_cv);
126152Sdf157793 
126252Sdf157793 	/*
126352Sdf157793 	 * Clear ASY_DOINGSOFT and ASY_NEEDSOFT in case we were in
126452Sdf157793 	 * async_softint or an interrupt was pending when the process
126552Sdf157793 	 * using the port exited.
126652Sdf157793 	 */
126752Sdf157793 	asy->asy_flags &= ~ASY_DOINGSOFT & ~ASY_NEEDSOFT;
126852Sdf157793 
126952Sdf157793 	/*
127052Sdf157793 	 * Cancel outstanding "bufcall" request.
127152Sdf157793 	 */
127252Sdf157793 	if (async->async_wbufcid) {
127352Sdf157793 		unbufcall(async->async_wbufcid);
127452Sdf157793 		async->async_wbufcid = 0;
127552Sdf157793 	}
127652Sdf157793 
127752Sdf157793 	/*
127852Sdf157793 	 * If inperim is true, it means the port is closing while there's
127952Sdf157793 	 * a pending software interrupt.  async_flags has been zeroed out,
128052Sdf157793 	 * so this instance of leaveq() needs to be called before we call
128152Sdf157793 	 * qprocsoff() to disable services on the q.  If inperim is false,
128252Sdf157793 	 * leaveq() has already been called or we're not in a perimeter.
128352Sdf157793 	 */
128452Sdf157793 	if (asy->inperim == B_TRUE) {
128552Sdf157793 		asy->inperim = B_FALSE;
128652Sdf157793 		mutex_exit(asy->asy_excl);
128752Sdf157793 		leaveq(q);
128852Sdf157793 	} else {
128952Sdf157793 		mutex_exit(asy->asy_excl);
129052Sdf157793 	}
129152Sdf157793 
129252Sdf157793 	/* Note that qprocsoff can't be done until after interrupts are off */
129352Sdf157793 	qprocsoff(q);
129452Sdf157793 	q->q_ptr = WR(q)->q_ptr = NULL;
129552Sdf157793 	async->async_ttycommon.t_readq = NULL;
129652Sdf157793 	async->async_ttycommon.t_writeq = NULL;
129752Sdf157793 
129852Sdf157793 	return (0);
129952Sdf157793 }
130052Sdf157793 
130152Sdf157793 /*
130252Sdf157793  * Checks to see if the serial port is still transmitting
130352Sdf157793  * characters.  It returns true when there are characters
130452Sdf157793  * queued to transmit,  when the holding register contains
130552Sdf157793  * a byte, or when the shifting register still contains
130652Sdf157793  * data to send.
130752Sdf157793  *
130852Sdf157793  */
130952Sdf157793 static boolean_t
asy_isbusy(struct asycom * asy)131052Sdf157793 asy_isbusy(struct asycom *asy)
131152Sdf157793 {
131252Sdf157793 	struct asyncline *async;
131352Sdf157793 
131452Sdf157793 #ifdef DEBUG
131552Sdf157793 	if (asydebug & ASY_DEBUG_EOT)
131652Sdf157793 		printf("isbusy\n");
131752Sdf157793 #endif
131852Sdf157793 	async = (struct asyncline *)asy->asy_priv;
131952Sdf157793 	ASSERT(mutex_owned(asy->asy_excl));
132052Sdf157793 	ASSERT(mutex_owned(asy->asy_excl_hi));
132152Sdf157793 	return ((async->async_ocnt > 0) ||
13225973Szk194757 	    ((INB(LSR) & XSRE) == 0));
132352Sdf157793 }
132452Sdf157793 
132552Sdf157793 /*
132652Sdf157793  * Program the ASY port. Most of the async operation is based on the values
132752Sdf157793  * of 'c_iflag' and 'c_cflag'.
132852Sdf157793  */
132952Sdf157793 static int
asy_program(struct asycom * asy,int mode)133052Sdf157793 asy_program(struct asycom *asy, int mode)
133152Sdf157793 {
133252Sdf157793 	struct asyncline *async;
133352Sdf157793 	int baudrate, c_flag;
133452Sdf157793 	int icr, lcr;
133552Sdf157793 	int ocflags;
133652Sdf157793 	int error = 0;
133752Sdf157793 
133852Sdf157793 	ASSERT(mutex_owned(asy->asy_excl));
133952Sdf157793 	ASSERT(mutex_owned(asy->asy_excl_hi));
134052Sdf157793 
134152Sdf157793 #ifdef DEBUG
134252Sdf157793 	if (asydebug & ASY_DEBUG_PROCS)
134352Sdf157793 		printf("program\n");
134452Sdf157793 #endif
134552Sdf157793 	async = (struct asyncline *)asy->asy_priv;
134652Sdf157793 
134752Sdf157793 	baudrate = async->async_ttycommon.t_cflag & CBAUD;
134852Sdf157793 	if (async->async_ttycommon.t_cflag & CBAUDEXT)
134952Sdf157793 		baudrate += 16;
135052Sdf157793 
135152Sdf157793 	/* Limit baudrate so it can't index out of baudtable */
135252Sdf157793 	if (baudrate >= N_SU_SPEEDS) baudrate = B9600;
135352Sdf157793 
135452Sdf157793 	/*
135552Sdf157793 	 * If baud rate requested is greater than the speed cap
135652Sdf157793 	 * or is an unsupported baud rate then reset t_cflag baud
135752Sdf157793 	 * to the last valid baud rate.  If this is the initial
135852Sdf157793 	 * pass through asy_program then set it to 9600.
135952Sdf157793 	 */
136052Sdf157793 	if (((baudrate > 0) && (asyspdtab[baudrate] == 0)) ||
136152Sdf157793 	    (baudtable[baudrate] > asy->asy_speed_cap)) {
136252Sdf157793 		async->async_ttycommon.t_cflag &= ~CBAUD & ~CBAUDEXT &
136352Sdf157793 		    ~CIBAUD & ~CIBAUDEXT;
136452Sdf157793 		if (mode == ASY_INIT) {
136552Sdf157793 			async->async_ttycommon.t_cflag |= B9600;
13662422Skc28005 			async->async_ttycommon.t_cflag |= B9600 << IBSHIFT;
136752Sdf157793 			baudrate = B9600;
136852Sdf157793 		} else {
136952Sdf157793 			async->async_ttycommon.t_cflag |=
137052Sdf157793 			    (asy->asy_ocflags & (CBAUD | CBAUDEXT |
137152Sdf157793 			    CIBAUD | CIBAUDEXT));
13722422Skc28005 			error = EINVAL;
13732422Skc28005 			goto end;
137452Sdf157793 		}
137552Sdf157793 	}
137652Sdf157793 
13772422Skc28005 	/*
13782422Skc28005 	 * If CIBAUD and CIBAUDEXT are zero then we should set them to
13792422Skc28005 	 * the equivelant output baud bits.  Else, if CIBAUD and CIBAUDEXT
13802422Skc28005 	 * don't match CBAUD and CBAUDEXT respectively then we should
13812422Skc28005 	 * notify the requestor that we do not support split speeds.
13822422Skc28005 	 */
13832422Skc28005 	if ((async->async_ttycommon.t_cflag  & (CIBAUD|CIBAUDEXT)) == 0) {
13842422Skc28005 		async->async_ttycommon.t_cflag |=
13852422Skc28005 		    (async->async_ttycommon.t_cflag & CBAUD) << IBSHIFT;
13862422Skc28005 		if (async->async_ttycommon.t_cflag & CBAUDEXT)
13875973Szk194757 			async->async_ttycommon.t_cflag |= CIBAUDEXT;
13882422Skc28005 	} else {
13892422Skc28005 		if ((((async->async_ttycommon.t_cflag & CBAUD) << IBSHIFT) !=
13902422Skc28005 		    (async->async_ttycommon.t_cflag & CIBAUD)) ||
13912422Skc28005 		    !(((async->async_ttycommon.t_cflag & (CBAUDEXT |
13922422Skc28005 		    CIBAUDEXT)) == (CBAUDEXT | CIBAUDEXT)) ||
13932422Skc28005 		    ((async->async_ttycommon.t_cflag & (CBAUDEXT |
13942422Skc28005 		    CIBAUDEXT)) == 0))) {
13952422Skc28005 			async->async_ttycommon.t_cflag &= ~CBAUD & ~CBAUDEXT &
13962422Skc28005 			    ~CIBAUD & ~CIBAUDEXT;
139752Sdf157793 			async->async_ttycommon.t_cflag |=
13982422Skc28005 			    (asy->asy_ocflags & (CBAUD | CBAUDEXT |
13992422Skc28005 			    CIBAUD | CIBAUDEXT));
14002422Skc28005 			error = EINVAL;
14012422Skc28005 			goto end;
140252Sdf157793 		}
140352Sdf157793 	}
140452Sdf157793 
140552Sdf157793 	c_flag = async->async_ttycommon.t_cflag &
140652Sdf157793 	    (CLOCAL | CREAD | CSTOPB | CSIZE | PARENB | PARODD | CBAUD |
140752Sdf157793 	    CBAUDEXT | CIBAUD | CIBAUDEXT);
14082119Sanovick 
14092119Sanovick 	/* disable interrupts, see EXAR bug */
14102877Sanovick 	if (asy->asy_hwtype == ASY16C554D)
14112877Sanovick 		OUTB(SPR, 0);
14122119Sanovick 	OUTB(ICR, 0);
141352Sdf157793 
141452Sdf157793 	ocflags = asy->asy_ocflags;
141552Sdf157793 
141652Sdf157793 	/* flush/reset the status registers */
141752Sdf157793 	if (mode == ASY_INIT) {
141852Sdf157793 		(void) INB(DAT);
141952Sdf157793 		(void) INB(ISR);
142052Sdf157793 		(void) INB(LSR);
142152Sdf157793 		(void) INB(MSR);
142252Sdf157793 	}
142352Sdf157793 
142452Sdf157793 	if (ocflags != (c_flag & ~CLOCAL) || mode == ASY_INIT) {
142552Sdf157793 		/* Set line control */
142652Sdf157793 		lcr = INB(LCR);
142752Sdf157793 		lcr &= ~(WLS0|WLS1|STB|PEN|EPS);
142852Sdf157793 
142952Sdf157793 		if (c_flag & CSTOPB)
143052Sdf157793 			lcr |= STB;	/* 2 stop bits */
143152Sdf157793 
143252Sdf157793 		if (c_flag & PARENB)
143352Sdf157793 			lcr |= PEN;
143452Sdf157793 
143552Sdf157793 		if ((c_flag & PARODD) == 0)
143652Sdf157793 			lcr |= EPS;
143752Sdf157793 
143852Sdf157793 		switch (c_flag & CSIZE) {
143952Sdf157793 		case CS5:
144052Sdf157793 			lcr |= BITS5;
144152Sdf157793 			break;
144252Sdf157793 		case CS6:
144352Sdf157793 			lcr |= BITS6;
144452Sdf157793 			break;
144552Sdf157793 		case CS7:
144652Sdf157793 			lcr |= BITS7;
144752Sdf157793 			break;
144852Sdf157793 		case CS8:
144952Sdf157793 			lcr |= BITS8;
145052Sdf157793 			break;
145152Sdf157793 		}
145252Sdf157793 
145352Sdf157793 		/* set the baud rate when the rate is NOT B0 */
145452Sdf157793 		if (baudrate != 0) {
145552Sdf157793 			OUTB(LCR, DLAB);
145652Sdf157793 			OUTB(DAT, (asyspdtab[baudrate] *
14575973Szk194757 			    asy->asy_baud_divisor_factor) & 0xff);
145852Sdf157793 			OUTB(ICR, ((asyspdtab[baudrate] *
14595973Szk194757 			    asy->asy_baud_divisor_factor) >> 8) & 0xff);
146052Sdf157793 		}
146152Sdf157793 		/* set the line control modes */
146252Sdf157793 		OUTB(LCR, lcr);
146352Sdf157793 
146452Sdf157793 		/*
146552Sdf157793 		 * if transitioning from CREAD off to CREAD on,
146652Sdf157793 		 * flush the FIFO buffer if we have one.
146752Sdf157793 		 */
146852Sdf157793 		if ((ocflags & CREAD) == 0 && (c_flag & CREAD)) {
146952Sdf157793 			if (asy->asy_use_fifo == FIFO_ON) {
147052Sdf157793 				OUTB(FIFOR, FIFO_ON | FIFODMA | FIFORXFLSH |
147152Sdf157793 				    (asy->asy_trig_level & 0xff));
147252Sdf157793 			}
147352Sdf157793 		}
147452Sdf157793 
147552Sdf157793 		/* remember the new cflags */
147652Sdf157793 		asy->asy_ocflags = c_flag & ~CLOCAL;
147752Sdf157793 	}
147852Sdf157793 
147952Sdf157793 	/* whether or not CLOCAL is set, modify the modem control lines */
148052Sdf157793 	if (baudrate == 0)
148152Sdf157793 		/* B0 has been issued, lower DTR */
148252Sdf157793 		OUTB(MCR, RTS|OUT2);
148352Sdf157793 	else
148452Sdf157793 		/* raise DTR */
148552Sdf157793 		OUTB(MCR, DTR|RTS|OUT2);
148652Sdf157793 
148752Sdf157793 	/*
148852Sdf157793 	 * Call the modem status interrupt handler to check for the carrier
148952Sdf157793 	 * in case CLOCAL was turned off after the carrier came on.
149052Sdf157793 	 * (Note: Modem status interrupt is not enabled if CLOCAL is ON.)
149152Sdf157793 	 */
149252Sdf157793 	async_msint(asy);
149352Sdf157793 
149452Sdf157793 	/* Set interrupt control */
149552Sdf157793 	if ((c_flag & CLOCAL) && !(async->async_ttycommon.t_cflag & CRTSCTS))
149652Sdf157793 		/*
149752Sdf157793 		 * direct-wired line ignores DCD, so we don't enable modem
149852Sdf157793 		 * status interrupts.
149952Sdf157793 		 */
150052Sdf157793 		icr = (TIEN | SIEN);
150152Sdf157793 	else
150252Sdf157793 		icr = (TIEN | SIEN | MIEN);
150352Sdf157793 
150452Sdf157793 	if (c_flag & CREAD)
150552Sdf157793 		icr |= RIEN;
150652Sdf157793 
150752Sdf157793 	OUTB(ICR, icr);
150852Sdf157793 end:
150952Sdf157793 	return (error);
151052Sdf157793 }
151152Sdf157793 
151252Sdf157793 /*
15135973Szk194757  * Polled mode support -- all functions called with interrupts
15145973Szk194757  * disabled.
15155973Szk194757  */
15165973Szk194757 
15175973Szk194757 static void
asyputchar(cons_polledio_arg_t arg,uchar_t c)15185973Szk194757 asyputchar(cons_polledio_arg_t arg, uchar_t c)
15195973Szk194757 {
15205973Szk194757 	struct asycom *asy = (struct asycom *)arg;
15215973Szk194757 
15225973Szk194757 	/*
15235973Szk194757 	 * If we see a line feed make sure to also
15245973Szk194757 	 * put out a carriage return.
15255973Szk194757 	 */
15265973Szk194757 	if (c == '\n')
15275973Szk194757 		asyputchar(arg, '\r');
15285973Szk194757 
15295973Szk194757 	while ((INB(LSR) & XHRE) == 0) {
15305973Szk194757 		/* wait for the transmission to complete */
15315973Szk194757 		drv_usecwait(10);
15325973Szk194757 	}
15335973Szk194757 
15345973Szk194757 	/* ouput the character */
15355973Szk194757 	OUTB(DAT, c);
15365973Szk194757 }
15375973Szk194757 
15385973Szk194757 /*
15395973Szk194757  * Determines if there is a character avaialable for
15405973Szk194757  * reading.
15415973Szk194757  */
15425973Szk194757 static boolean_t
asyischar(cons_polledio_arg_t arg)15435973Szk194757 asyischar(cons_polledio_arg_t arg)
15445973Szk194757 {
15455973Szk194757 	struct asycom *asy = (struct asycom *)arg;
15465973Szk194757 	return ((INB(LSR) & RCA) != 0);
15475973Szk194757 }
15485973Szk194757 
15495973Szk194757 static int
asygetchar(cons_polledio_arg_t arg)15505973Szk194757 asygetchar(cons_polledio_arg_t arg)
15515973Szk194757 {
15525973Szk194757 	struct asycom *asy = (struct asycom *)arg;
15535973Szk194757 
15545973Szk194757 	/*
15555973Szk194757 	 * Spin waiting for a character to be
15565973Szk194757 	 * available to read.
15575973Szk194757 	 */
15585973Szk194757 	while (!asyischar(arg))
15595973Szk194757 		drv_usecwait(10);
15605973Szk194757 
15615973Szk194757 	return (INB(DAT));
15625973Szk194757 }
15635973Szk194757 
15645973Szk194757 /*
15655973Szk194757  * Called when machine is transitioning to polled mode
15665973Szk194757  */
15675973Szk194757 static void
asy_polled_enter(cons_polledio_arg_t arg)15685973Szk194757 asy_polled_enter(cons_polledio_arg_t arg)
15695973Szk194757 {
15705973Szk194757 	struct asycom *asy = (struct asycom *)arg;
15715973Szk194757 
15725973Szk194757 	mutex_enter(asy->asy_excl);
15735973Szk194757 	mutex_enter(asy->asy_excl_hi);
15745973Szk194757 
15755973Szk194757 	/*
15765973Szk194757 	 * If this is the first time that asy_polled_enter()
15775973Szk194757 	 * has been called, during this transition request,
15785973Szk194757 	 * save the ICR. Clear the software interrupt
15795973Szk194757 	 * flags since we won't be able to handle these when
15805973Szk194757 	 * we are in polled mode.
15815973Szk194757 	 */
15825973Szk194757 	if (!asy->polled_enter) {
15835973Szk194757 		asy->polled_enter = B_TRUE;
15845973Szk194757 		asy->polled_icr = INB(ICR);
15855973Szk194757 
15865973Szk194757 		/* Disable HW interrupts */
15875973Szk194757 		if (asy->asy_hwtype == ASY16C554D)
15885973Szk194757 			OUTB(SPR, 0);
15895973Szk194757 		OUTB(ICR, 0);
15905973Szk194757 
15915973Szk194757 		asy->asy_flags &= ~ASY_DOINGSOFT & ~ASY_NEEDSOFT;
15925973Szk194757 	}
15935973Szk194757 	mutex_exit(asy->asy_excl_hi);
15945973Szk194757 	mutex_exit(asy->asy_excl);
15955973Szk194757 }
15965973Szk194757 
15975973Szk194757 /*
15985973Szk194757  * Called when machine is transitioning from polled mode.
15995973Szk194757  */
16005973Szk194757 static void
asy_polled_exit(cons_polledio_arg_t arg)16015973Szk194757 asy_polled_exit(cons_polledio_arg_t arg)
16025973Szk194757 {
16035973Szk194757 	struct asycom *asy = (struct asycom *)arg;
16045973Szk194757 
16055973Szk194757 	mutex_enter(asy->asy_excl);
16065973Szk194757 	mutex_enter(asy->asy_excl_hi);
16075973Szk194757 
16085973Szk194757 	/* Restore the ICR */
16095973Szk194757 	OUTB(ICR, asy->polled_icr);
16105973Szk194757 
16115973Szk194757 	/*
16125973Szk194757 	 * We have finished this polled IO transition.
16135973Szk194757 	 * Set polled_enter to B_FALSE to note this.
16145973Szk194757 	 */
16155973Szk194757 	asy->polled_enter = B_FALSE;
16165973Szk194757 	mutex_exit(asy->asy_excl_hi);
16175973Szk194757 	mutex_exit(asy->asy_excl);
16185973Szk194757 }
16195973Szk194757 
16205973Szk194757 /*
162152Sdf157793  * asyintr() is the High Level Interrupt Handler.
162252Sdf157793  *
162352Sdf157793  * There are four different interrupt types indexed by ISR register values:
162452Sdf157793  *		0: modem
162552Sdf157793  *		1: Tx holding register is empty, ready for next char
162652Sdf157793  *		2: Rx register now holds a char to be picked up
162752Sdf157793  *		3: error or break on line
162852Sdf157793  * This routine checks the Bit 0 (interrupt-not-pending) to determine if
162952Sdf157793  * the interrupt is from this port.
163052Sdf157793  */
163152Sdf157793 uint_t
asyintr(caddr_t argasy)163252Sdf157793 asyintr(caddr_t argasy)
163352Sdf157793 {
163452Sdf157793 	struct asycom		*asy = (struct asycom *)argasy;
163552Sdf157793 	struct asyncline	*async;
163652Sdf157793 	int			ret_status = DDI_INTR_UNCLAIMED;
163752Sdf157793 	uchar_t			interrupt_id, lsr;
163852Sdf157793 
163952Sdf157793 	interrupt_id = INB(ISR) & 0x0F;
164052Sdf157793 	async = (struct asyncline *)asy->asy_priv;
164152Sdf157793 	if ((async == NULL) ||
16425973Szk194757 	    !(async->async_flags & (ASYNC_ISOPEN|ASYNC_WOPEN))) {
164352Sdf157793 		if (interrupt_id & NOINTERRUPT)  {
164452Sdf157793 			return (DDI_INTR_UNCLAIMED);
164552Sdf157793 		} else {
164652Sdf157793 			lsr = INB(LSR);
164752Sdf157793 			if ((lsr & BRKDET) &&
164852Sdf157793 			    ((abort_enable == KIOCABORTENABLE) &&
164952Sdf157793 			    (async->async_dev == rconsdev)))
165052Sdf157793 				abort_sequence_enter((char *)NULL);
165152Sdf157793 			else {
165252Sdf157793 				/* reset line status */
165352Sdf157793 				(void) INB(LSR);
165452Sdf157793 				/* discard any data */
165552Sdf157793 				(void) INB(DAT);
165652Sdf157793 				/* reset modem status */
165752Sdf157793 				(void) INB(MSR);
165852Sdf157793 				return (DDI_INTR_CLAIMED);
165952Sdf157793 			}
166052Sdf157793 		}
166152Sdf157793 	}
166252Sdf157793 	/*
166352Sdf157793 	 * Spurious interrupts happen in this driver
166452Sdf157793 	 * because of the transmission on serial port not handled
166552Sdf157793 	 * properly.
166652Sdf157793 	 *
166752Sdf157793 	 * The reasons for Spurious interrupts are:
166852Sdf157793 	 *    1. There is a path in async_nstart which transmits
166952Sdf157793 	 *	 characters without going through interrupt services routine
167052Sdf157793 	 *	 which causes spurious interrupts to happen.
167152Sdf157793 	 *    2. In the async_txint more than one character is sent
167252Sdf157793 	 *	 in one interrupt service.
167352Sdf157793 	 *    3. In async_rxint more than one characters are received in
167452Sdf157793 	 *	 in one interrupt service.
167552Sdf157793 	 *
167652Sdf157793 	 * Hence we have flags to indicate that such scenerio has happened.
167752Sdf157793 	 * and claim only such interrupts and others we donot claim it
167852Sdf157793 	 * as it could be a indicator of some hardware problem.
167952Sdf157793 	 *
168052Sdf157793 	 */
168152Sdf157793 	if (interrupt_id & NOINTERRUPT) {
168252Sdf157793 		mutex_enter(asy->asy_excl_hi);
168352Sdf157793 		if ((asy->asy_xmit_count > 1) ||
16845973Szk194757 		    (asy->asy_out_of_band_xmit > 0) ||
16855973Szk194757 		    (asy->asy_rx_count > 1)) {
168652Sdf157793 			asy->asy_xmit_count = 0;
168752Sdf157793 			asy->asy_out_of_band_xmit = 0;
168852Sdf157793 			asy->asy_rx_count = 0;
168952Sdf157793 			mutex_exit(asy->asy_excl_hi);
169052Sdf157793 			return (DDI_INTR_CLAIMED);
169152Sdf157793 		} else {
169252Sdf157793 			mutex_exit(asy->asy_excl_hi);
169352Sdf157793 			return (DDI_INTR_UNCLAIMED);
169452Sdf157793 		}
169552Sdf157793 	}
169652Sdf157793 	ret_status = DDI_INTR_CLAIMED;
169752Sdf157793 	mutex_enter(asy->asy_excl_hi);
169852Sdf157793 	if (asy->asy_hwtype == ASY82510)
169952Sdf157793 		OUTB(ISR, 0x00); /* set bank 0 */
170052Sdf157793 
170152Sdf157793 #ifdef DEBUG
170252Sdf157793 	if (asydebug & ASY_DEBUG_INTR)
170352Sdf157793 		prom_printf("l");
170452Sdf157793 #endif
170552Sdf157793 	lsr = INB(LSR);
170652Sdf157793 	switch (interrupt_id) {
170752Sdf157793 	case RxRDY:
170852Sdf157793 	case RSTATUS:
170952Sdf157793 	case FFTMOUT:
171052Sdf157793 		/* receiver interrupt or receiver errors */
171152Sdf157793 		async_rxint(asy, lsr);
171252Sdf157793 		break;
171352Sdf157793 	case TxRDY:
171452Sdf157793 		/* transmit interrupt */
171552Sdf157793 		async_txint(asy, lsr);
171652Sdf157793 		break;
171752Sdf157793 	case MSTATUS:
171852Sdf157793 		/* modem status interrupt */
171952Sdf157793 		async_msint(asy);
172052Sdf157793 		break;
172152Sdf157793 	}
172252Sdf157793 	mutex_exit(asy->asy_excl_hi);
172352Sdf157793 	return (ret_status);
172452Sdf157793 }
172552Sdf157793 
172652Sdf157793 /*
172752Sdf157793  * Transmitter interrupt service routine.
172852Sdf157793  * If there is more data to transmit in the current pseudo-DMA block,
172952Sdf157793  * send the next character if output is not stopped or draining.
173052Sdf157793  * Otherwise, queue up a soft interrupt.
173152Sdf157793  *
173252Sdf157793  * XXX -  Needs review for HW FIFOs.
173352Sdf157793  */
173452Sdf157793 static void
async_txint(struct asycom * asy,uchar_t lsr)173552Sdf157793 async_txint(struct asycom *asy, uchar_t lsr)
173652Sdf157793 {
173752Sdf157793 	struct asyncline *async = (struct asyncline *)asy->asy_priv;
173852Sdf157793 	int		fifo_len;
173952Sdf157793 	int		xmit_progress;
174052Sdf157793 
174152Sdf157793 	asycheckflowcontrol_hw(asy);
174252Sdf157793 
174352Sdf157793 	/*
174452Sdf157793 	 * If ASYNC_BREAK has been set, return to asyintr()'s context to
174552Sdf157793 	 * claim the interrupt without performing any action.
174652Sdf157793 	 */
174752Sdf157793 	if (async->async_flags & ASYNC_BREAK)
174852Sdf157793 		return;
174952Sdf157793 
175052Sdf157793 	fifo_len = asy->asy_fifo_buf; /* with FIFO buffers */
175152Sdf157793 
175252Sdf157793 	/*
175352Sdf157793 	 * Check for flow control and do the needed action.
175452Sdf157793 	 */
175552Sdf157793 	if (asycheckflowcontrol_sw(asy)) {
175652Sdf157793 		return;
175752Sdf157793 	}
175852Sdf157793 
175952Sdf157793 	if (async->async_ocnt > 0 &&
176052Sdf157793 	    !(async->async_flags & (ASYNC_HW_OUT_FLW|ASYNC_STOPPED))) {
176152Sdf157793 		xmit_progress = 0;
176252Sdf157793 		while (fifo_len > 0 && async->async_ocnt > 0) {
176352Sdf157793 			if (lsr & XHRE) {
176452Sdf157793 				OUTB(DAT, *async->async_optr++);
176552Sdf157793 				fifo_len--;
176652Sdf157793 				async->async_ocnt--;
176752Sdf157793 				xmit_progress++;
176852Sdf157793 			}
176952Sdf157793 			/*
177052Sdf157793 			 * Reading the lsr, (moved reading at the end of
177152Sdf157793 			 * while loop) as already we have read once at
177252Sdf157793 			 * the beginning of interrupt service
177352Sdf157793 			 */
177452Sdf157793 			lsr = INB(LSR);
177552Sdf157793 		}
177652Sdf157793 		asy->asy_xmit_count = xmit_progress;
177752Sdf157793 		if (xmit_progress > 0)
177852Sdf157793 			async->async_flags |= ASYNC_PROGRESS;
177952Sdf157793 	}
178052Sdf157793 
178152Sdf157793 	if (fifo_len == 0) {
178252Sdf157793 		return;
178352Sdf157793 	}
178452Sdf157793 
178552Sdf157793 
178652Sdf157793 	ASYSETSOFT(asy);
178752Sdf157793 }
178852Sdf157793 
178952Sdf157793 /*
179052Sdf157793  * Receiver interrupt: RxRDY interrupt, FIFO timeout interrupt or receive
179152Sdf157793  * error interrupt.
179252Sdf157793  * Try to put the character into the circular buffer for this line; if it
179352Sdf157793  * overflows, indicate a circular buffer overrun. If this port is always
179452Sdf157793  * to be serviced immediately, or the character is a STOP character, or
179552Sdf157793  * more than 15 characters have arrived, queue up a soft interrupt to
179652Sdf157793  * drain the circular buffer.
179752Sdf157793  * XXX - needs review for hw FIFOs support.
179852Sdf157793  */
179952Sdf157793 
180052Sdf157793 static void
async_rxint(struct asycom * asy,uchar_t lsr)180152Sdf157793 async_rxint(struct asycom *asy, uchar_t lsr)
180252Sdf157793 {
180352Sdf157793 	struct asyncline *async = (struct asyncline *)asy->asy_priv;
180452Sdf157793 	uchar_t c = 0;
180552Sdf157793 	uint_t s = 0, needsoft = 0;
180652Sdf157793 	register tty_common_t *tp;
180752Sdf157793 
180852Sdf157793 	tp = &async->async_ttycommon;
180952Sdf157793 	if (!(tp->t_cflag & CREAD)) {
181052Sdf157793 		if (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
181152Sdf157793 			(void) (INB(DAT) & 0xff);
181252Sdf157793 		}
181352Sdf157793 		return; /* line is not open for read? */
181452Sdf157793 	}
181552Sdf157793 	asy->asy_rx_count = 0;
181652Sdf157793 	while (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
181752Sdf157793 		c = 0;
181852Sdf157793 		s = 0;
181952Sdf157793 		asy->asy_rx_count++;
182052Sdf157793 		if (lsr & RCA) {
182152Sdf157793 			c = INB(DAT) & 0xff;
182252Sdf157793 			/*
182352Sdf157793 			 * Even a single character is received
182452Sdf157793 			 * we need Soft interrupt to pass it to
182552Sdf157793 			 * higher layers.
182652Sdf157793 			 */
182752Sdf157793 			needsoft = 1;
182852Sdf157793 		}
182952Sdf157793 
183052Sdf157793 		/* Check for character break sequence */
183152Sdf157793 		if ((abort_enable == KIOCABORTALTERNATE) &&
183252Sdf157793 		    (async->async_dev == rconsdev)) {
183352Sdf157793 			if (abort_charseq_recognize(c))
183452Sdf157793 				abort_sequence_enter((char *)NULL);
183552Sdf157793 			}
183652Sdf157793 
183752Sdf157793 		/* Handle framing errors */
183852Sdf157793 		if (lsr & (PARERR|FRMERR|BRKDET|OVRRUN)) {
183952Sdf157793 			if (lsr & PARERR) {
184052Sdf157793 				if (tp->t_iflag & INPCK) /* parity enabled */
184152Sdf157793 					s |= PERROR;
184252Sdf157793 			}
184352Sdf157793 			if (lsr & (FRMERR|BRKDET))
184452Sdf157793 				s |= FRERROR;
184552Sdf157793 			if (lsr & OVRRUN) {
184652Sdf157793 				async->async_hw_overrun = 1;
184752Sdf157793 				s |= OVERRUN;
184852Sdf157793 			}
184952Sdf157793 		}
185052Sdf157793 
185152Sdf157793 		if (s == 0)
185252Sdf157793 			if ((tp->t_iflag & PARMRK) &&
18535973Szk194757 			    !(tp->t_iflag & (IGNPAR|ISTRIP)) &&
18545973Szk194757 			    (c == 0377))
185552Sdf157793 				if (RING_POK(async, 2)) {
185652Sdf157793 					RING_PUT(async, 0377);
185752Sdf157793 					RING_PUT(async, c);
185852Sdf157793 				} else
185952Sdf157793 					async->async_sw_overrun = 1;
186052Sdf157793 			else
186152Sdf157793 				if (RING_POK(async, 1))
186252Sdf157793 					RING_PUT(async, c);
186352Sdf157793 				else
186452Sdf157793 					async->async_sw_overrun = 1;
186552Sdf157793 		else
186652Sdf157793 			if (s & FRERROR) { /* Handle framing errors */
186752Sdf157793 				if (c == 0)  {
186852Sdf157793 		/* Look for break on kbd, stdin, or rconsdev */
186952Sdf157793 					if ((async->async_dev == kbddev) ||
187052Sdf157793 					    ((async->async_dev == rconsdev) ||
187152Sdf157793 					    (async->async_dev == stdindev)) &&
187252Sdf157793 					    (abort_enable !=
187352Sdf157793 					    KIOCABORTALTERNATE))
187452Sdf157793 						abort_sequence_enter((char *)0);
187552Sdf157793 					else
187652Sdf157793 						async->async_break++;
187752Sdf157793 				} else {
187852Sdf157793 					if (RING_POK(async, 1))
187952Sdf157793 						RING_MARK(async, c, s);
188052Sdf157793 					else
188152Sdf157793 						async->async_sw_overrun = 1;
188252Sdf157793 				}
188352Sdf157793 			} else  { /* Parity errors  handled by ldterm */
188452Sdf157793 				if (RING_POK(async, 1))
188552Sdf157793 					RING_MARK(async, c, s);
188652Sdf157793 				else
188752Sdf157793 					async->async_sw_overrun = 1;
188852Sdf157793 			}
188952Sdf157793 		lsr = INB(LSR);
189052Sdf157793 		if (asy->asy_rx_count > 16) break;
189152Sdf157793 	}
189252Sdf157793 	/* Check whether there is a request for hw/sw inbound/input flow ctrl */
189352Sdf157793 	if ((async->async_ttycommon.t_cflag & CRTSXOFF) ||
18945973Szk194757 	    (async->async_ttycommon.t_iflag & IXOFF))
189552Sdf157793 		if ((int)(RING_CNT(async)) > (RINGSIZE * 3)/4) {
189652Sdf157793 #ifdef DEBUG
189752Sdf157793 			if (asydebug & ASY_DEBUG_HFLOW)
189852Sdf157793 				printf("asy%d: hardware flow stop input.\n",
18995973Szk194757 				    UNIT(async->async_dev));
190052Sdf157793 #endif
190152Sdf157793 			async->async_flags |= ASYNC_HW_IN_FLOW;
190252Sdf157793 			async->async_flowc = async->async_stopc;
190352Sdf157793 			async->async_ringbuf_overflow = 1;
190452Sdf157793 		}
190552Sdf157793 
190652Sdf157793 	if ((async->async_flags & ASYNC_SERVICEIMM) || needsoft ||
19075973Szk194757 	    (RING_FRAC(async)) || (async->async_polltid == 0))
190852Sdf157793 		ASYSETSOFT(asy);	/* need a soft interrupt */
190952Sdf157793 }
191052Sdf157793 
191152Sdf157793 /*
191252Sdf157793  * Interrupt on port: handle PPS event.  This function is only called
191352Sdf157793  * for a port on which PPS event handling has been enabled.
191452Sdf157793  */
191552Sdf157793 static void
asy_ppsevent(struct asycom * asy,int msr)191652Sdf157793 asy_ppsevent(struct asycom *asy, int msr)
191752Sdf157793 {
191852Sdf157793 	if (asy->asy_flags & ASY_PPS_EDGE) {
191952Sdf157793 		/* Have seen leading edge, now look for and record drop */
192052Sdf157793 		if ((msr & DCD) == 0)
192152Sdf157793 			asy->asy_flags &= ~ASY_PPS_EDGE;
192252Sdf157793 		/*
192352Sdf157793 		 * Waiting for leading edge, look for rise; stamp event and
192452Sdf157793 		 * calibrate kernel clock.
192552Sdf157793 		 */
192652Sdf157793 	} else if (msr & DCD) {
192752Sdf157793 		/*
192852Sdf157793 		 * This code captures a timestamp at the designated
192952Sdf157793 		 * transition of the PPS signal (DCD asserted).  The
193052Sdf157793 		 * code provides a pointer to the timestamp, as well
193152Sdf157793 		 * as the hardware counter value at the capture.
193252Sdf157793 		 *
193352Sdf157793 		 * Note: the kernel has nano based time values while
193452Sdf157793 		 * NTP requires micro based, an in-line fast algorithm
193552Sdf157793 		 * to convert nsec to usec is used here -- see hrt2ts()
193652Sdf157793 		 * in common/os/timers.c for a full description.
193752Sdf157793 		 */
193852Sdf157793 		struct timeval *tvp = &asy_ppsev.tv;
193952Sdf157793 		timestruc_t ts;
194052Sdf157793 		long nsec, usec;
194152Sdf157793 
194252Sdf157793 		asy->asy_flags |= ASY_PPS_EDGE;
194352Sdf157793 		gethrestime(&ts);
194452Sdf157793 		nsec = ts.tv_nsec;
194552Sdf157793 		usec = nsec + (nsec >> 2);
194652Sdf157793 		usec = nsec + (usec >> 1);
194752Sdf157793 		usec = nsec + (usec >> 2);
194852Sdf157793 		usec = nsec + (usec >> 4);
194952Sdf157793 		usec = nsec - (usec >> 3);
195052Sdf157793 		usec = nsec + (usec >> 2);
195152Sdf157793 		usec = nsec + (usec >> 3);
195252Sdf157793 		usec = nsec + (usec >> 4);
195352Sdf157793 		usec = nsec + (usec >> 1);
195452Sdf157793 		usec = nsec + (usec >> 6);
195552Sdf157793 		tvp->tv_usec = usec >> 10;
195652Sdf157793 		tvp->tv_sec = ts.tv_sec;
195752Sdf157793 
195852Sdf157793 		++asy_ppsev.serial;
195952Sdf157793 
196052Sdf157793 		/*
196152Sdf157793 		 * Because the kernel keeps a high-resolution time,
196252Sdf157793 		 * pass the current highres timestamp in tvp and zero
196352Sdf157793 		 * in usec.
196452Sdf157793 		 */
196552Sdf157793 		ddi_hardpps(tvp, 0);
196652Sdf157793 	}
196752Sdf157793 }
196852Sdf157793 
196952Sdf157793 /*
197052Sdf157793  * Modem status interrupt.
197152Sdf157793  *
197252Sdf157793  * (Note: It is assumed that the MSR hasn't been read by asyintr().)
197352Sdf157793  */
197452Sdf157793 
197552Sdf157793 static void
async_msint(struct asycom * asy)197652Sdf157793 async_msint(struct asycom *asy)
197752Sdf157793 {
197852Sdf157793 	struct asyncline *async = (struct asyncline *)asy->asy_priv;
197952Sdf157793 	int msr;
198052Sdf157793 
198152Sdf157793 	msr = INB(MSR);	/* this resets the interrupt */
198252Sdf157793 	asy->asy_cached_msr = msr;
198352Sdf157793 #ifdef DEBUG
198452Sdf157793 	if (asydebug & ASY_DEBUG_STATE) {
198552Sdf157793 		printf("   transition: %3s %3s %3s %3s\n"
19865973Szk194757 		    "current state: %3s %3s %3s %3s\n",
19875973Szk194757 		    (msr & DCTS) ? "CTS" : "   ",
19885973Szk194757 		    (msr & DDSR) ? "DSR" : "   ",
19895973Szk194757 		    (msr & DRI) ?  "RI " : "   ",
19905973Szk194757 		    (msr & DDCD) ? "DCD" : "   ",
19915973Szk194757 		    (msr & CTS) ?  "CTS" : "   ",
19925973Szk194757 		    (msr & DSR) ?  "DSR" : "   ",
19935973Szk194757 		    (msr & RI) ?   "RI " : "   ",
19945973Szk194757 		    (msr & DCD) ?  "DCD" : "   ");
199552Sdf157793 	}
199652Sdf157793 #endif
199752Sdf157793 	if (async->async_ttycommon.t_cflag & CRTSCTS && !(msr & CTS)) {
199852Sdf157793 #ifdef DEBUG
199952Sdf157793 		if (asydebug & ASY_DEBUG_HFLOW)
200052Sdf157793 			printf("asy%d: hflow start\n",
20015973Szk194757 			    UNIT(async->async_dev));
200252Sdf157793 #endif
200352Sdf157793 		async->async_flags |= ASYNC_HW_OUT_FLW;
200452Sdf157793 	}
200552Sdf157793 	if (asy->asy_hwtype == ASY82510)
200652Sdf157793 		OUTB(MSR, (msr & 0xF0));
200752Sdf157793 
200852Sdf157793 	/* Handle PPS event */
200952Sdf157793 	if (asy->asy_flags & ASY_PPS)
201052Sdf157793 		asy_ppsevent(asy, msr);
201152Sdf157793 
201252Sdf157793 	async->async_ext++;
201352Sdf157793 	ASYSETSOFT(asy);
201452Sdf157793 }
201552Sdf157793 
201652Sdf157793 /*
201752Sdf157793  * Handle a second-stage interrupt.
201852Sdf157793  */
201952Sdf157793 uint_t
asysoftintr(caddr_t intarg)202052Sdf157793 asysoftintr(caddr_t intarg)
202152Sdf157793 {
202252Sdf157793 	struct asycom *asy = (struct asycom *)intarg;
202352Sdf157793 	struct asyncline *async;
202452Sdf157793 	int rv;
202552Sdf157793 	int cc;
202652Sdf157793 	/*
202752Sdf157793 	 * Test and clear soft interrupt.
202852Sdf157793 	 */
202952Sdf157793 	mutex_enter(asy->asy_soft_lock);
203052Sdf157793 #ifdef DEBUG
203152Sdf157793 	if (asydebug & ASY_DEBUG_PROCS)
203252Sdf157793 		printf("softintr\n");
203352Sdf157793 #endif
203452Sdf157793 	rv = asy->asysoftpend;
203552Sdf157793 	if (rv != 0)
203652Sdf157793 		asy->asysoftpend = 0;
203752Sdf157793 	mutex_exit(asy->asy_soft_lock);
203852Sdf157793 
203952Sdf157793 	if (rv) {
204052Sdf157793 		if (asy->asy_priv == NULL)
204152Sdf157793 			return (rv);
204252Sdf157793 		async = (struct asyncline *)asy->asy_priv;
204352Sdf157793 		mutex_enter(asy->asy_excl_hi);
204452Sdf157793 		if (asy->asy_flags & ASY_NEEDSOFT) {
204552Sdf157793 			asy->asy_flags &= ~ASY_NEEDSOFT;
204652Sdf157793 			mutex_exit(asy->asy_excl_hi);
204752Sdf157793 			(void) async_softint(asy);
204852Sdf157793 			mutex_enter(asy->asy_excl_hi);
204952Sdf157793 		}
205052Sdf157793 		/*
205152Sdf157793 		 * There are some instances where the softintr is not
205252Sdf157793 		 * scheduled and hence not called. It so happened that makes
205352Sdf157793 		 * the last few characters to be stuck in ringbuffer.
205452Sdf157793 		 * Hence, call once again the  handler so that the last few
205552Sdf157793 		 * characters are cleared.
205652Sdf157793 		 */
205752Sdf157793 		cc = RING_CNT(async);
205852Sdf157793 		mutex_exit(asy->asy_excl_hi);
205952Sdf157793 		if (cc > 0) {
206052Sdf157793 			(void) async_softint(asy);
206152Sdf157793 		}
206252Sdf157793 	}
206352Sdf157793 	return (rv);
206452Sdf157793 }
206552Sdf157793 
206652Sdf157793 /*
206752Sdf157793  * Handle a software interrupt.
206852Sdf157793  */
206952Sdf157793 static int
async_softint(struct asycom * asy)207052Sdf157793 async_softint(struct asycom *asy)
207152Sdf157793 {
207252Sdf157793 	struct asyncline *async = (struct asyncline *)asy->asy_priv;
207352Sdf157793 	uint_t	cc;
207452Sdf157793 	mblk_t	*bp;
207552Sdf157793 	queue_t	*q;
207652Sdf157793 	uchar_t	val;
207752Sdf157793 	uchar_t	c;
207852Sdf157793 	tty_common_t	*tp;
207952Sdf157793 
208052Sdf157793 #ifdef DEBUG
208152Sdf157793 	if (asydebug & ASY_DEBUG_PROCS)
208252Sdf157793 		printf("process\n");
208352Sdf157793 #endif
208452Sdf157793 	mutex_enter(asy->asy_excl);
208552Sdf157793 	if (asy->asy_flags & ASY_DOINGSOFT) {
208652Sdf157793 		mutex_exit(asy->asy_excl);
208752Sdf157793 		return (0);
208852Sdf157793 	}
208952Sdf157793 	tp = &async->async_ttycommon;
209052Sdf157793 	q = tp->t_readq;
209152Sdf157793 	if (q != NULL) {
209252Sdf157793 		mutex_exit(asy->asy_excl);
209352Sdf157793 		enterq(q);
209452Sdf157793 		mutex_enter(asy->asy_excl);
209552Sdf157793 	}
209652Sdf157793 	mutex_enter(asy->asy_excl_hi);
209752Sdf157793 	asy->asy_flags |= ASY_DOINGSOFT;
209852Sdf157793 
209952Sdf157793 	if (INB(ICR) & MIEN)
210052Sdf157793 		val = asy->asy_cached_msr & 0xFF;
210152Sdf157793 	else
210252Sdf157793 		val = INB(MSR) & 0xFF;
210352Sdf157793 
210452Sdf157793 	if (async->async_ttycommon.t_cflag & CRTSCTS) {
210552Sdf157793 		if ((val & CTS) && (async->async_flags & ASYNC_HW_OUT_FLW)) {
210652Sdf157793 #ifdef DEBUG
210752Sdf157793 			if (asydebug & ASY_DEBUG_HFLOW)
210852Sdf157793 				printf("asy%d: hflow start\n",
21095973Szk194757 				    UNIT(async->async_dev));
211052Sdf157793 #endif
211152Sdf157793 			async->async_flags &= ~ASYNC_HW_OUT_FLW;
211252Sdf157793 			mutex_exit(asy->asy_excl_hi);
211352Sdf157793 			if (async->async_ocnt > 0) {
211452Sdf157793 				mutex_enter(asy->asy_excl_hi);
211552Sdf157793 				async_resume(async);
211652Sdf157793 				mutex_exit(asy->asy_excl_hi);
211752Sdf157793 			} else {
211852Sdf157793 				async_start(async);
211952Sdf157793 			}
212052Sdf157793 			mutex_enter(asy->asy_excl_hi);
212152Sdf157793 		}
212252Sdf157793 	}
212352Sdf157793 	if (async->async_ext) {
212452Sdf157793 		async->async_ext = 0;
212552Sdf157793 		/* check for carrier up */
212652Sdf157793 		if ((val & DCD) || (tp->t_flags & TS_SOFTCAR)) {
212752Sdf157793 			/* carrier present */
212852Sdf157793 			if ((async->async_flags & ASYNC_CARR_ON) == 0) {
212952Sdf157793 				async->async_flags |= ASYNC_CARR_ON;
213052Sdf157793 				mutex_exit(asy->asy_excl_hi);
213152Sdf157793 				mutex_exit(asy->asy_excl);
213252Sdf157793 				if (async->async_flags & ASYNC_ISOPEN)
213352Sdf157793 					(void) putctl(q, M_UNHANGUP);
213452Sdf157793 				cv_broadcast(&async->async_flags_cv);
213552Sdf157793 				mutex_enter(asy->asy_excl);
213652Sdf157793 				mutex_enter(asy->asy_excl_hi);
213752Sdf157793 			}
213852Sdf157793 		} else {
213952Sdf157793 			if ((async->async_flags & ASYNC_CARR_ON) &&
214052Sdf157793 			    !(tp->t_cflag & CLOCAL)) {
214152Sdf157793 				int flushflag;
214252Sdf157793 
214352Sdf157793 				/*
214452Sdf157793 				 * Carrier went away.
214552Sdf157793 				 * Drop DTR, abort any output in
214652Sdf157793 				 * progress, indicate that output is
214752Sdf157793 				 * not stopped, and send a hangup
214852Sdf157793 				 * notification upstream.
214952Sdf157793 				 *
215052Sdf157793 				 * If we're in the midst of close, then flush
215152Sdf157793 				 * everything.  Don't leave stale ioctls lying
215252Sdf157793 				 * about.
215352Sdf157793 				 */
215452Sdf157793 				val = INB(MCR);
215552Sdf157793 				OUTB(MCR, (val & ~DTR));
215652Sdf157793 				flushflag = (async->async_flags &
215752Sdf157793 				    ASYNC_CLOSING) ? FLUSHALL : FLUSHDATA;
2158*9957SAn.Bui@Sun.COM 				if (tp->t_writeq != NULL) {
2159*9957SAn.Bui@Sun.COM 					flushq(tp->t_writeq, flushflag);
2160*9957SAn.Bui@Sun.COM 				}
216152Sdf157793 				if (async->async_xmitblk != NULL) {
216252Sdf157793 					freeb(async->async_xmitblk);
216352Sdf157793 					async->async_xmitblk = NULL;
216452Sdf157793 				}
216552Sdf157793 				if (async->async_flags & ASYNC_BUSY) {
216652Sdf157793 					async->async_ocnt = 0;
216752Sdf157793 					async->async_flags &= ~ASYNC_BUSY;
216852Sdf157793 				}
216952Sdf157793 				async->async_flags &= ~ASYNC_STOPPED;
217052Sdf157793 				if (async->async_flags & ASYNC_ISOPEN) {
217152Sdf157793 					mutex_exit(asy->asy_excl_hi);
217252Sdf157793 					mutex_exit(asy->asy_excl);
217352Sdf157793 					(void) putctl(q, M_HANGUP);
217452Sdf157793 					mutex_enter(asy->asy_excl);
217552Sdf157793 					mutex_enter(asy->asy_excl_hi);
217652Sdf157793 				}
21771815Srameshc 				async->async_flags &= ~ASYNC_CARR_ON;
21781815Srameshc 				mutex_exit(asy->asy_excl_hi);
21791815Srameshc 				cv_broadcast(&async->async_flags_cv);
21801815Srameshc 				mutex_enter(asy->asy_excl_hi);
218152Sdf157793 			}
218252Sdf157793 		}
218352Sdf157793 	}
218452Sdf157793 
218552Sdf157793 	/*
218652Sdf157793 	 * If data has been added to the circular buffer, remove
218752Sdf157793 	 * it from the buffer, and send it up the stream if there's
218852Sdf157793 	 * somebody listening. Try to do it 16 bytes at a time. If we
218952Sdf157793 	 * have more than 16 bytes to move, move 16 byte chunks and
219052Sdf157793 	 * leave the rest for next time around (maybe it will grow).
219152Sdf157793 	 */
219252Sdf157793 	if (!(async->async_flags & ASYNC_ISOPEN)) {
219352Sdf157793 		RING_INIT(async);
219452Sdf157793 		goto rv;
219552Sdf157793 	}
219652Sdf157793 	if ((cc = RING_CNT(async)) == 0) {
219752Sdf157793 		goto rv;
219852Sdf157793 	}
219952Sdf157793 	mutex_exit(asy->asy_excl_hi);
220052Sdf157793 
220152Sdf157793 	if (!canput(q)) {
220252Sdf157793 		if ((async->async_flags & ASYNC_HW_IN_FLOW) == 0) {
220352Sdf157793 #ifdef DEBUG
220452Sdf157793 			if (!(asydebug & ASY_DEBUG_HFLOW)) {
220552Sdf157793 				printf("asy%d: hflow stop input.\n",
22065973Szk194757 				    UNIT(async->async_dev));
220752Sdf157793 				if (canputnext(q))
220852Sdf157793 					printf("asy%d: next queue is "
22095973Szk194757 					    "ready\n",
22105973Szk194757 					    UNIT(async->async_dev));
221152Sdf157793 			}
221252Sdf157793 #endif
221352Sdf157793 			mutex_enter(asy->asy_excl_hi);
221452Sdf157793 			async->async_flags |= ASYNC_HW_IN_FLOW;
221552Sdf157793 			async->async_flowc = async->async_stopc;
221652Sdf157793 		} else mutex_enter(asy->asy_excl_hi);
221752Sdf157793 		goto rv;
221852Sdf157793 	}
221952Sdf157793 
222052Sdf157793 	if (async->async_ringbuf_overflow) {
222152Sdf157793 		if ((async->async_flags & ASYNC_HW_IN_FLOW) &&
22225973Szk194757 		    ((int)(RING_CNT(async)) < (RINGSIZE/4))) {
222352Sdf157793 #ifdef DEBUG
222452Sdf157793 			if (asydebug & ASY_DEBUG_HFLOW)
222552Sdf157793 				printf("asy%d: hflow start input.\n",
22265973Szk194757 				    UNIT(async->async_dev));
222752Sdf157793 #endif
222852Sdf157793 			mutex_enter(asy->asy_excl_hi);
222952Sdf157793 			async->async_flags &= ~ASYNC_HW_IN_FLOW;
223052Sdf157793 			async->async_flowc = async->async_startc;
223152Sdf157793 			async->async_ringbuf_overflow = 0;
223252Sdf157793 			goto rv;
223352Sdf157793 		}
223452Sdf157793 	}
223552Sdf157793 #ifdef DEBUG
223652Sdf157793 	if (asydebug & ASY_DEBUG_INPUT)
223752Sdf157793 		printf("asy%d: %d char(s) in queue.\n",
22385973Szk194757 		    UNIT(async->async_dev), cc);
223952Sdf157793 #endif
224052Sdf157793 	/*
224152Sdf157793 	 * Before you pull the characters from the RING BUF
224252Sdf157793 	 * Check whether you can put into the queue again
224352Sdf157793 	 */
224452Sdf157793 	if ((!canputnext(q)) || (!canput(q))) {
224552Sdf157793 		mutex_enter(asy->asy_excl_hi);
224652Sdf157793 		if ((async->async_flags & ASYNC_HW_IN_FLOW) == 0) {
224752Sdf157793 			async->async_flags |= ASYNC_HW_IN_FLOW;
224852Sdf157793 			async->async_flowc = async->async_stopc;
224952Sdf157793 			async->async_queue_full = 1;
225052Sdf157793 		}
225152Sdf157793 		goto rv;
225252Sdf157793 	}
225352Sdf157793 	mutex_enter(asy->asy_excl_hi);
225452Sdf157793 	if (async->async_queue_full) {
225552Sdf157793 		/*
225652Sdf157793 		 * Last time the Stream queue didnot allow
225752Sdf157793 		 * now it allows so, relax, the flow control
225852Sdf157793 		 */
225952Sdf157793 		if (async->async_flags & ASYNC_HW_IN_FLOW) {
226052Sdf157793 			async->async_flags &= ~ASYNC_HW_IN_FLOW;
226152Sdf157793 			async->async_queue_full = 0;
226252Sdf157793 			async->async_flowc = async->async_startc;
226352Sdf157793 			goto rv;
226452Sdf157793 		} else
226552Sdf157793 			async->async_queue_full = 0;
226652Sdf157793 	}
226752Sdf157793 	mutex_exit(asy->asy_excl_hi);
226852Sdf157793 	if (!(bp = allocb(cc, BPRI_MED))) {
226952Sdf157793 		ttycommon_qfull(&async->async_ttycommon, q);
227052Sdf157793 		mutex_enter(asy->asy_excl_hi);
227152Sdf157793 		goto rv;
227252Sdf157793 	}
227352Sdf157793 	mutex_enter(asy->asy_excl_hi);
227452Sdf157793 	do {
227552Sdf157793 		if (RING_ERR(async, S_ERRORS)) {
227652Sdf157793 			RING_UNMARK(async);
227752Sdf157793 			c = RING_GET(async);
227852Sdf157793 			break;
227952Sdf157793 		} else {
228052Sdf157793 			*bp->b_wptr++ = RING_GET(async);
228152Sdf157793 		}
228252Sdf157793 	} while (--cc);
228352Sdf157793 
228452Sdf157793 	mutex_exit(asy->asy_excl_hi);
228552Sdf157793 	mutex_exit(asy->asy_excl);
228652Sdf157793 	if (bp->b_wptr > bp->b_rptr) {
228752Sdf157793 		if (!canputnext(q)) {
228852Sdf157793 			if (!canput(q)) {
228952Sdf157793 				/*
229052Sdf157793 				 * Even after taking all precautions that
229152Sdf157793 				 * Still we are unable to queue, then we
229252Sdf157793 				 * cannot do anything, just drop the block
229352Sdf157793 				 */
229452Sdf157793 				cmn_err(CE_NOTE,
22955973Szk194757 				    "su%d: local queue full\n",
22965973Szk194757 				    UNIT(async->async_dev));
229752Sdf157793 				freemsg(bp);
229852Sdf157793 				mutex_enter(asy->asy_excl_hi);
229952Sdf157793 				if ((async->async_flags &
23005973Szk194757 				    ASYNC_HW_IN_FLOW) == 0) {
230152Sdf157793 					async->async_flags |=
23025973Szk194757 					    ASYNC_HW_IN_FLOW;
230352Sdf157793 					async->async_flowc =
23045973Szk194757 					    async->async_stopc;
230552Sdf157793 					async->async_queue_full = 1;
230652Sdf157793 				}
230752Sdf157793 				mutex_exit(asy->asy_excl_hi);
230852Sdf157793 			} else {
230952Sdf157793 				(void) putq(q, bp);
231052Sdf157793 			}
231152Sdf157793 		} else {
231252Sdf157793 			putnext(q, bp);
231352Sdf157793 		}
231452Sdf157793 	} else {
231552Sdf157793 		freemsg(bp);
231652Sdf157793 	}
231752Sdf157793 	/*
231852Sdf157793 	 * If we have a parity error, then send
231952Sdf157793 	 * up an M_BREAK with the "bad"
232052Sdf157793 	 * character as an argument. Let ldterm
232152Sdf157793 	 * figure out what to do with the error.
232252Sdf157793 	 */
232352Sdf157793 	if (cc)
232452Sdf157793 		(void) putctl1(q, M_BREAK, c);
232552Sdf157793 	mutex_enter(asy->asy_excl);
232652Sdf157793 	mutex_enter(asy->asy_excl_hi);
232752Sdf157793 rv:
232852Sdf157793 	/*
232952Sdf157793 	 * If a transmission has finished, indicate that it's finished,
233052Sdf157793 	 * and start that line up again.
233152Sdf157793 	 */
233252Sdf157793 	if (async->async_break) {
233352Sdf157793 		async->async_break = 0;
233452Sdf157793 		if (async->async_flags & ASYNC_ISOPEN) {
233552Sdf157793 			mutex_exit(asy->asy_excl_hi);
233652Sdf157793 			mutex_exit(asy->asy_excl);
233752Sdf157793 			(void) putctl(q, M_BREAK);
233852Sdf157793 			mutex_enter(asy->asy_excl);
233952Sdf157793 			mutex_enter(asy->asy_excl_hi);
234052Sdf157793 		}
234152Sdf157793 	}
234252Sdf157793 	if ((async->async_ocnt <= 0 && (async->async_flags & ASYNC_BUSY)) ||
2343859Skc28005 	    (async->async_flowc != '\0')) {
234452Sdf157793 		async->async_flags &= ~ASYNC_BUSY;
234552Sdf157793 		mutex_exit(asy->asy_excl_hi);
234652Sdf157793 		if (async->async_xmitblk)
234752Sdf157793 			freeb(async->async_xmitblk);
234852Sdf157793 		async->async_xmitblk = NULL;
234952Sdf157793 		if (async->async_flags & ASYNC_ISOPEN) {
235052Sdf157793 			asy->inperim = B_TRUE;
235152Sdf157793 			mutex_exit(asy->asy_excl);
235252Sdf157793 			enterq(async->async_ttycommon.t_writeq);
235352Sdf157793 			mutex_enter(asy->asy_excl);
235452Sdf157793 		}
235552Sdf157793 		async_start(async);
235652Sdf157793 		/*
235752Sdf157793 		 * We need to check for inperim and ISOPEN due to
235852Sdf157793 		 * multi-threading implications; it's possible to close the
235952Sdf157793 		 * port and nullify async_flags while completing the software
236052Sdf157793 		 * interrupt.  If the port is closed, leaveq() will have already
236152Sdf157793 		 * been called.  We don't want to call it twice.
236252Sdf157793 		 */
236352Sdf157793 		if ((asy->inperim) && (async->async_flags & ASYNC_ISOPEN)) {
236452Sdf157793 			mutex_exit(asy->asy_excl);
236552Sdf157793 			leaveq(async->async_ttycommon.t_writeq);
236652Sdf157793 			mutex_enter(asy->asy_excl);
236752Sdf157793 			asy->inperim = B_FALSE;
236852Sdf157793 		}
236952Sdf157793 		if (!(async->async_flags & ASYNC_BUSY))
237052Sdf157793 			cv_broadcast(&async->async_flags_cv);
237152Sdf157793 		mutex_enter(asy->asy_excl_hi);
237252Sdf157793 	}
237352Sdf157793 	/*
237452Sdf157793 	 * A note about these overrun bits: all they do is *tell* someone
237552Sdf157793 	 * about an error- They do not track multiple errors. In fact,
237652Sdf157793 	 * you could consider them latched register bits if you like.
237752Sdf157793 	 * We are only interested in printing the error message once for
237852Sdf157793 	 * any cluster of overrun errrors.
237952Sdf157793 	 */
238052Sdf157793 	if (async->async_hw_overrun) {
238152Sdf157793 		if (async->async_flags & ASYNC_ISOPEN) {
238252Sdf157793 			if (su_log > 0) {
238352Sdf157793 				mutex_exit(asy->asy_excl_hi);
238452Sdf157793 				mutex_exit(asy->asy_excl);
238552Sdf157793 				cmn_err(CE_NOTE, "su%d: silo overflow\n",
238652Sdf157793 				    UNIT(async->async_dev));
238752Sdf157793 				mutex_enter(asy->asy_excl);
238852Sdf157793 				mutex_enter(asy->asy_excl_hi);
238952Sdf157793 			}
239052Sdf157793 			INC64_KSTAT(asy, siloover);
239152Sdf157793 		}
239252Sdf157793 		async->async_hw_overrun = 0;
239352Sdf157793 	}
239452Sdf157793 	if (async->async_sw_overrun) {
239552Sdf157793 		if (async->async_flags & ASYNC_ISOPEN) {
239652Sdf157793 			if (su_log > 0) {
239752Sdf157793 				mutex_exit(asy->asy_excl_hi);
239852Sdf157793 				mutex_exit(asy->asy_excl);
239952Sdf157793 				cmn_err(CE_NOTE, "su%d: ring buffer overflow\n",
240052Sdf157793 				    UNIT(async->async_dev));
240152Sdf157793 				mutex_enter(asy->asy_excl);
240252Sdf157793 				mutex_enter(asy->asy_excl_hi);
240352Sdf157793 			}
240452Sdf157793 			INC64_KSTAT(asy, ringover);
240552Sdf157793 		}
240652Sdf157793 		async->async_sw_overrun = 0;
240752Sdf157793 	}
240852Sdf157793 	asy->asy_flags &= ~ASY_DOINGSOFT;
240952Sdf157793 	mutex_exit(asy->asy_excl_hi);
241052Sdf157793 	mutex_exit(asy->asy_excl);
241152Sdf157793 	if (q != NULL)
241252Sdf157793 		leaveq(q);
241352Sdf157793 	return (0);
241452Sdf157793 }
241552Sdf157793 
241652Sdf157793 /*
241752Sdf157793  * Restart output on a line after a delay or break timer expired.
241852Sdf157793  */
241952Sdf157793 static void
async_restart(void * arg)242052Sdf157793 async_restart(void *arg)
242152Sdf157793 {
242252Sdf157793 	struct asyncline *async = arg;
242352Sdf157793 	struct asycom *asy = async->async_common;
242452Sdf157793 	queue_t *q;
242552Sdf157793 	uchar_t lcr;
242652Sdf157793 
242752Sdf157793 	/*
242852Sdf157793 	 * If break timer expired, turn off the break bit.
242952Sdf157793 	 */
243052Sdf157793 #ifdef DEBUG
243152Sdf157793 	if (asydebug & ASY_DEBUG_PROCS)
243252Sdf157793 		printf("restart\n");
243352Sdf157793 #endif
243452Sdf157793 	mutex_enter(asy->asy_excl);
243552Sdf157793 	if (async->async_flags & ASYNC_BREAK) {
24361434Skc28005 		unsigned int rate;
24371434Skc28005 
243852Sdf157793 		mutex_enter(asy->asy_excl_hi);
243952Sdf157793 		lcr = INB(LCR);
244052Sdf157793 		OUTB(LCR, (lcr & ~SETBREAK));
24411434Skc28005 
24421434Skc28005 		/*
24431434Skc28005 		 * Go to sleep for the time it takes for at least one
24441434Skc28005 		 * stop bit to be received by the device at the other
24451434Skc28005 		 * end of the line as stated in the RS-232 specification.
24461434Skc28005 		 * The wait period is equal to:
24471434Skc28005 		 * 2 clock cycles * (1 MICROSEC / baud rate)
24481434Skc28005 		 */
24491434Skc28005 		rate = async->async_ttycommon.t_cflag & CBAUD;
24501434Skc28005 		if (async->async_ttycommon.t_cflag & CBAUDEXT)
24511434Skc28005 			rate += 16;
24521434Skc28005 		if (rate >= N_SU_SPEEDS || rate == B0) {
24531434Skc28005 			rate = B9600;
24541434Skc28005 		}
24551434Skc28005 
245652Sdf157793 		mutex_exit(asy->asy_excl_hi);
24571434Skc28005 		mutex_exit(asy->asy_excl);
24581434Skc28005 		drv_usecwait(2 * MICROSEC / baudtable[rate]);
24591434Skc28005 		mutex_enter(asy->asy_excl);
246052Sdf157793 	}
246152Sdf157793 	async->async_flags &= ~(ASYNC_DELAY|ASYNC_BREAK|ASYNC_DRAINING);
246252Sdf157793 	if ((q = async->async_ttycommon.t_writeq) != NULL) {
246352Sdf157793 		mutex_exit(asy->asy_excl);
246452Sdf157793 		enterq(q);
246552Sdf157793 		mutex_enter(asy->asy_excl);
246652Sdf157793 	}
246752Sdf157793 	async_start(async);
246852Sdf157793 	mutex_exit(asy->asy_excl);
246952Sdf157793 	if (q != NULL)
247052Sdf157793 		leaveq(q);
247152Sdf157793 
247252Sdf157793 	/* cleared break or delay flag; may have made some output progress */
247352Sdf157793 	cv_broadcast(&async->async_flags_cv);
247452Sdf157793 }
247552Sdf157793 
247652Sdf157793 static void
async_start(struct asyncline * async)247752Sdf157793 async_start(struct asyncline *async)
247852Sdf157793 {
247952Sdf157793 	async_nstart(async, 0);
248052Sdf157793 }
248152Sdf157793 
248252Sdf157793 /*
248352Sdf157793  * Start output on a line, unless it's busy, frozen, or otherwise.
248452Sdf157793  */
248552Sdf157793 static void
async_nstart(struct asyncline * async,int mode)248652Sdf157793 async_nstart(struct asyncline *async, int mode)
248752Sdf157793 {
248852Sdf157793 	register struct asycom *asy = async->async_common;
248952Sdf157793 	register int cc;
249052Sdf157793 	register queue_t *q;
249152Sdf157793 	mblk_t *bp, *nbp;
249252Sdf157793 	uchar_t *xmit_addr;
249352Sdf157793 	uchar_t	val;
249452Sdf157793 	int	fifo_len = 1;
249552Sdf157793 	int	xmit_progress;
249652Sdf157793 
249752Sdf157793 #ifdef DEBUG
249852Sdf157793 	if (asydebug & ASY_DEBUG_PROCS)
249952Sdf157793 		printf("start\n");
250052Sdf157793 #endif
250152Sdf157793 	if (asy->asy_use_fifo == FIFO_ON)
250252Sdf157793 		fifo_len = asy->asy_fifo_buf; /* with FIFO buffers */
250352Sdf157793 
250452Sdf157793 	ASSERT(mutex_owned(asy->asy_excl));
250552Sdf157793 	mutex_enter(asy->asy_excl_hi);
250652Sdf157793 	asycheckflowcontrol_hw(asy);
250752Sdf157793 
250852Sdf157793 	/*
250952Sdf157793 	 * If the chip is busy (i.e., we're waiting for a break timeout
251052Sdf157793 	 * to expire, or for the current transmission to finish, or for
251152Sdf157793 	 * output to finish draining from chip), don't grab anything new.
251252Sdf157793 	 */
251352Sdf157793 	if (async->async_flags & (ASYNC_BREAK|ASYNC_BUSY|ASYNC_DRAINING)) {
251452Sdf157793 		mutex_exit(asy->asy_excl_hi);
251552Sdf157793 #ifdef DEBUG
251652Sdf157793 		if (mode && asydebug & ASY_DEBUG_CLOSE)
251752Sdf157793 			printf("asy%d: start %s.\n",
25185973Szk194757 			    UNIT(async->async_dev),
25195973Szk194757 			    async->async_flags & ASYNC_BREAK
25205973Szk194757 			    ? "break" : "busy");
252152Sdf157793 #endif
252252Sdf157793 		return;
252352Sdf157793 	}
252452Sdf157793 
252552Sdf157793 	/*
252652Sdf157793 	 * If we have a flow-control character to transmit, do it now.
252752Sdf157793 	 */
252852Sdf157793 	if (asycheckflowcontrol_sw(asy)) {
252952Sdf157793 		mutex_exit(asy->asy_excl_hi);
253052Sdf157793 		return;
253152Sdf157793 	}
253252Sdf157793 	mutex_exit(asy->asy_excl_hi);
253352Sdf157793 	/*
253452Sdf157793 	 * If we're waiting for a delay timeout to expire, don't grab
253552Sdf157793 	 * anything new.
253652Sdf157793 	 */
253752Sdf157793 	if (async->async_flags & ASYNC_DELAY) {
253852Sdf157793 #ifdef DEBUG
253952Sdf157793 		if (mode && asydebug & ASY_DEBUG_CLOSE)
254052Sdf157793 			printf("asy%d: start ASYNC_DELAY.\n",
25415973Szk194757 			    UNIT(async->async_dev));
254252Sdf157793 #endif
254352Sdf157793 		return;
254452Sdf157793 	}
254552Sdf157793 
254652Sdf157793 	if ((q = async->async_ttycommon.t_writeq) == NULL) {
254752Sdf157793 #ifdef DEBUG
254852Sdf157793 		if (mode && asydebug & ASY_DEBUG_CLOSE)
254952Sdf157793 			printf("asy%d: start writeq is null.\n",
25505973Szk194757 			    UNIT(async->async_dev));
255152Sdf157793 #endif
255252Sdf157793 		return;	/* not attached to a stream */
255352Sdf157793 	}
255452Sdf157793 
255552Sdf157793 	for (;;) {
255652Sdf157793 		if ((bp = getq(q)) == NULL)
255752Sdf157793 			return;	/* no data to transmit */
255852Sdf157793 
255952Sdf157793 		/*
256052Sdf157793 		 * We have a message block to work on.
256152Sdf157793 		 * Check whether it's a break, a delay, or an ioctl (the latter
256252Sdf157793 		 * occurs if the ioctl in question was waiting for the output
256352Sdf157793 		 * to drain).  If it's one of those, process it immediately.
256452Sdf157793 		 */
256552Sdf157793 		switch (bp->b_datap->db_type) {
256652Sdf157793 
256752Sdf157793 		case M_BREAK:
256852Sdf157793 			/*
256952Sdf157793 			 * Set the break bit, and arrange for "async_restart"
257052Sdf157793 			 * to be called in 1/4 second; it will turn the
257152Sdf157793 			 * break bit off, and call "async_start" to grab
257252Sdf157793 			 * the next message.
257352Sdf157793 			 */
257452Sdf157793 			mutex_enter(asy->asy_excl_hi);
257552Sdf157793 			val = INB(LCR);
257652Sdf157793 			OUTB(LCR, (val | SETBREAK));
257752Sdf157793 			mutex_exit(asy->asy_excl_hi);
257852Sdf157793 			async->async_flags |= ASYNC_BREAK;
257952Sdf157793 			(void) timeout(async_restart, async, hz / 4);
258052Sdf157793 			freemsg(bp);
258152Sdf157793 			return;	/* wait for this to finish */
258252Sdf157793 
258352Sdf157793 		case M_DELAY:
258452Sdf157793 			/*
258552Sdf157793 			 * Arrange for "async_restart" to be called when the
258652Sdf157793 			 * delay expires; it will turn ASYNC_DELAY off,
258752Sdf157793 			 * and call "async_start" to grab the next message.
258852Sdf157793 			 */
258952Sdf157793 			(void) timeout(async_restart, async,
25905973Szk194757 			    (clock_t)(*(unsigned char *)bp->b_rptr + 6));
259152Sdf157793 			async->async_flags |= ASYNC_DELAY;
259252Sdf157793 			freemsg(bp);
259352Sdf157793 			return;	/* wait for this to finish */
259452Sdf157793 
259552Sdf157793 		case M_IOCTL:
259652Sdf157793 			/*
259752Sdf157793 			 * This ioctl needs to wait for the output ahead of
259852Sdf157793 			 * it to drain.  Try to do it, and then either
259952Sdf157793 			 * redo the ioctl at a later time or grab the next
260052Sdf157793 			 * message after it.
260152Sdf157793 			 */
260252Sdf157793 
260352Sdf157793 			mutex_enter(asy->asy_excl_hi);
260452Sdf157793 			if (asy_isbusy(asy)) {
260552Sdf157793 				/*
260652Sdf157793 				 * Get the divisor by calculating the rate
260752Sdf157793 				 */
260852Sdf157793 				unsigned int rate;
260952Sdf157793 
261052Sdf157793 				mutex_exit(asy->asy_excl_hi);
261152Sdf157793 				rate = async->async_ttycommon.t_cflag & CBAUD;
261252Sdf157793 				if (async->async_ttycommon.t_cflag & CBAUDEXT)
261352Sdf157793 					rate += 16;
261452Sdf157793 				if (rate >= N_SU_SPEEDS || rate == B0) {
261552Sdf157793 					rate = B9600;
261652Sdf157793 				}
261752Sdf157793 
261852Sdf157793 				/*
261952Sdf157793 				 * We need to do a callback as the port will
262052Sdf157793 				 * be set to drain
262152Sdf157793 				 */
262252Sdf157793 				async->async_flags |= ASYNC_DRAINING;
262352Sdf157793 
262452Sdf157793 				/*
262552Sdf157793 				 * Put the message we just processed back onto
262652Sdf157793 				 * the end of the queue
262752Sdf157793 				 */
262852Sdf157793 				if (putq(q, bp) == 0)
262952Sdf157793 					freemsg(bp);
263052Sdf157793 
263152Sdf157793 				/*
263252Sdf157793 				 * We need to delay until the TSR and THR
263352Sdf157793 				 * have been exhausted.  We base the delay on
263452Sdf157793 				 * the amount of time it takes to transmit
263552Sdf157793 				 * 2 chars at the current baud rate in
263652Sdf157793 				 * microseconds.
263752Sdf157793 				 *
263852Sdf157793 				 * Therefore, the wait period is:
263952Sdf157793 				 *
264052Sdf157793 				 * (#TSR bits + #THR bits) *
264152Sdf157793 				 * 	1 MICROSEC / baud rate
264252Sdf157793 				 */
264352Sdf157793 				(void) timeout(async_restart, async,
26445973Szk194757 				    drv_usectohz(16 * MICROSEC /
26455973Szk194757 				    baudtable[rate]));
264652Sdf157793 				return;
264752Sdf157793 			}
264852Sdf157793 			mutex_exit(asy->asy_excl_hi);
264952Sdf157793 			mutex_exit(asy->asy_excl);
265052Sdf157793 			async_ioctl(async, q, bp, B_FALSE);
265152Sdf157793 			mutex_enter(asy->asy_excl);
265252Sdf157793 			continue;
265352Sdf157793 		}
265452Sdf157793 
265552Sdf157793 		while (bp != NULL && (cc = bp->b_wptr - bp->b_rptr) == 0) {
265652Sdf157793 			nbp = bp->b_cont;
265752Sdf157793 			freeb(bp);
265852Sdf157793 			bp = nbp;
265952Sdf157793 		}
266052Sdf157793 		if (bp != NULL)
266152Sdf157793 			break;
266252Sdf157793 	}
266352Sdf157793 
266452Sdf157793 	/*
266552Sdf157793 	 * We have data to transmit.  If output is stopped, put
266652Sdf157793 	 * it back and try again later.
266752Sdf157793 	 */
266852Sdf157793 	if (async->async_flags & (ASYNC_HW_OUT_FLW|ASYNC_STOPPED)) {
266952Sdf157793 #ifdef DEBUG
267052Sdf157793 		if (asydebug & ASY_DEBUG_HFLOW &&
26715973Szk194757 		    async->async_flags & ASYNC_HW_OUT_FLW)
267252Sdf157793 			printf("asy%d: output hflow in effect.\n",
26735973Szk194757 			    UNIT(async->async_dev));
267452Sdf157793 #endif
267552Sdf157793 		mutex_exit(asy->asy_excl);
267652Sdf157793 		(void) putbq(q, bp);
267752Sdf157793 		/*
267852Sdf157793 		 * We entered the routine owning the lock, we need to
267952Sdf157793 		 * exit the routine owning the lock.
268052Sdf157793 		 */
268152Sdf157793 		mutex_enter(asy->asy_excl);
268252Sdf157793 		return;
268352Sdf157793 	}
268452Sdf157793 
268552Sdf157793 	async->async_xmitblk = bp;
268652Sdf157793 	xmit_addr = bp->b_rptr;
268752Sdf157793 	bp = bp->b_cont;
268852Sdf157793 	if (bp != NULL) {
268952Sdf157793 		mutex_exit(asy->asy_excl);
269052Sdf157793 		(void) putbq(q, bp);	/* not done with this message yet */
269152Sdf157793 		mutex_enter(asy->asy_excl);
269252Sdf157793 	}
269352Sdf157793 
269452Sdf157793 	/*
269552Sdf157793 	 * In 5-bit mode, the high order bits are used
269652Sdf157793 	 * to indicate character sizes less than five,
269752Sdf157793 	 * so we need to explicitly mask before transmitting
269852Sdf157793 	 */
269952Sdf157793 	if ((async->async_ttycommon.t_cflag & CSIZE) == CS5) {
270052Sdf157793 		register unsigned char *p = xmit_addr;
270152Sdf157793 		register int cnt = cc;
270252Sdf157793 
270352Sdf157793 		while (cnt--)
270452Sdf157793 			*p++ &= (unsigned char) 0x1f;
270552Sdf157793 	}
270652Sdf157793 
270752Sdf157793 	/*
270852Sdf157793 	 * Set up this block for pseudo-DMA.
270952Sdf157793 	 */
271052Sdf157793 	mutex_enter(asy->asy_excl_hi);
271152Sdf157793 	async->async_optr = xmit_addr;
271252Sdf157793 	async->async_ocnt = cc;
271352Sdf157793 	/*
271452Sdf157793 	 * If the transmitter is ready, shove some
271552Sdf157793 	 * characters out.
271652Sdf157793 	 */
271752Sdf157793 	xmit_progress = 0;
271852Sdf157793 	while (fifo_len-- && async->async_ocnt) {
271952Sdf157793 		if (INB(LSR) & XHRE) {
272052Sdf157793 			OUTB(DAT, *async->async_optr++);
272152Sdf157793 			async->async_ocnt--;
272252Sdf157793 			xmit_progress++;
272352Sdf157793 		}
272452Sdf157793 	}
272552Sdf157793 	asy->asy_out_of_band_xmit = xmit_progress;
272652Sdf157793 	if (xmit_progress > 0)
272752Sdf157793 		async->async_flags |= ASYNC_PROGRESS;
272852Sdf157793 	async->async_flags |= ASYNC_BUSY;
272952Sdf157793 	mutex_exit(asy->asy_excl_hi);
273052Sdf157793 }
273152Sdf157793 
273252Sdf157793 /*
273352Sdf157793  * Resume output by poking the transmitter.
273452Sdf157793  */
273552Sdf157793 static void
async_resume(struct asyncline * async)273652Sdf157793 async_resume(struct asyncline *async)
273752Sdf157793 {
273852Sdf157793 	register struct asycom *asy = async->async_common;
273952Sdf157793 
274052Sdf157793 	ASSERT(mutex_owned(asy->asy_excl_hi));
274152Sdf157793 #ifdef DEBUG
274252Sdf157793 	if (asydebug & ASY_DEBUG_PROCS)
274352Sdf157793 		printf("resume\n");
274452Sdf157793 #endif
274552Sdf157793 
274652Sdf157793 	asycheckflowcontrol_hw(asy);
274752Sdf157793 
274852Sdf157793 	if (INB(LSR) & XHRE) {
274952Sdf157793 		if (asycheckflowcontrol_sw(asy)) {
275052Sdf157793 			return;
275152Sdf157793 		} else if (async->async_ocnt > 0) {
275252Sdf157793 			OUTB(DAT, *async->async_optr++);
275352Sdf157793 			async->async_ocnt--;
275452Sdf157793 			async->async_flags |= ASYNC_PROGRESS;
275552Sdf157793 		}
275652Sdf157793 	}
275752Sdf157793 }
275852Sdf157793 
275952Sdf157793 /*
276052Sdf157793  * Process an "ioctl" message sent down to us.
276152Sdf157793  * Note that we don't need to get any locks until we are ready to access
276252Sdf157793  * the hardware.  Nothing we access until then is going to be altered
276352Sdf157793  * outside of the STREAMS framework, so we should be safe.
276452Sdf157793  */
276552Sdf157793 static void
async_ioctl(struct asyncline * async,queue_t * wq,mblk_t * mp,boolean_t iswput)276652Sdf157793 async_ioctl(struct asyncline *async, queue_t *wq, mblk_t *mp, boolean_t iswput)
276752Sdf157793 {
276852Sdf157793 	register struct asycom *asy = async->async_common;
276952Sdf157793 	register tty_common_t  *tp = &async->async_ttycommon;
277052Sdf157793 	register struct iocblk *iocp;
277152Sdf157793 	register unsigned datasize;
27722211Szk194757 	size_t ioc_count;
277352Sdf157793 	mblk_t *datamp;
277452Sdf157793 	int error = 0;
277552Sdf157793 	uchar_t val, icr;
277652Sdf157793 #ifdef DEBUG
277752Sdf157793 	if (asydebug & ASY_DEBUG_PROCS)
277852Sdf157793 		printf("ioctl\n");
277952Sdf157793 #endif
278052Sdf157793 
278152Sdf157793 	if (tp->t_iocpending != NULL) {
278252Sdf157793 		/*
278352Sdf157793 		 * We were holding an "ioctl" response pending the
278452Sdf157793 		 * availability of an "mblk" to hold data to be passed up;
278552Sdf157793 		 * another "ioctl" came through, which means that "ioctl"
278652Sdf157793 		 * must have timed out or been aborted.
278752Sdf157793 		 */
278852Sdf157793 		freemsg(async->async_ttycommon.t_iocpending);
278952Sdf157793 		async->async_ttycommon.t_iocpending = NULL;
279052Sdf157793 	}
279152Sdf157793 
279252Sdf157793 	iocp = (struct iocblk *)mp->b_rptr;
279352Sdf157793 
279452Sdf157793 	/*
27952211Szk194757 	 * Save off the ioc count in case we need to restore it
27962211Szk194757 	 * because we are queuing a message block.
27972211Szk194757 	 */
27982211Szk194757 	ioc_count = iocp->ioc_count;
27992211Szk194757 
28002211Szk194757 	/*
280152Sdf157793 	 * For TIOCMGET, TIOCMBIC, TIOCMBIS, TIOCMSET, and PPS, do NOT call
280252Sdf157793 	 * ttycommon_ioctl() because this function frees up the message block
280352Sdf157793 	 * (mp->b_cont) that contains the address of the user variable where
280452Sdf157793 	 * we need to pass back the bit array.
28055973Szk194757 	 *
28065973Szk194757 	 * Similarly, ttycommon_ioctl() does not know about CONSOPENPOLLEDIO
28075973Szk194757 	 * and CONSCLOSEPOLLEDIO, so don't let ttycommon_ioctl() touch them.
280852Sdf157793 	 */
280952Sdf157793 	if (iocp->ioc_cmd == TIOCMGET ||
28105973Szk194757 	    iocp->ioc_cmd == TIOCMBIC ||
28115973Szk194757 	    iocp->ioc_cmd == TIOCMBIS ||
28125973Szk194757 	    iocp->ioc_cmd == TIOCMSET ||
28135973Szk194757 	    iocp->ioc_cmd == TIOCGPPS ||
28145973Szk194757 	    iocp->ioc_cmd == TIOCSPPS ||
28155973Szk194757 	    iocp->ioc_cmd == TIOCGPPSEV ||
28165973Szk194757 	    iocp->ioc_cmd == CONSOPENPOLLEDIO ||
28175973Szk194757 	    iocp->ioc_cmd == CONSCLOSEPOLLEDIO)
281852Sdf157793 		error = -1; /* Do Nothing */
281952Sdf157793 	else
282052Sdf157793 
282152Sdf157793 	/*
282252Sdf157793 	 * The only way in which "ttycommon_ioctl" can fail is if the "ioctl"
282352Sdf157793 	 * requires a response containing data to be returned to the user,
282452Sdf157793 	 * and no mblk could be allocated for the data.
282552Sdf157793 	 * No such "ioctl" alters our state.  Thus, we always go ahead and
282652Sdf157793 	 * do any state-changes the "ioctl" calls for.  If we couldn't allocate
282752Sdf157793 	 * the data, "ttycommon_ioctl" has stashed the "ioctl" away safely, so
282852Sdf157793 	 * we just call "bufcall" to request that we be called back when we
282952Sdf157793 	 * stand a better chance of allocating the data.
283052Sdf157793 	 */
283152Sdf157793 	if ((datasize = ttycommon_ioctl(tp, wq, mp, &error)) != 0) {
283252Sdf157793 		if (async->async_wbufcid)
283352Sdf157793 			unbufcall(async->async_wbufcid);
283452Sdf157793 		async->async_wbufcid = bufcall(datasize, BPRI_HI, async_reioctl,
283552Sdf157793 		    async);
283652Sdf157793 		return;
283752Sdf157793 	}
283852Sdf157793 
283952Sdf157793 	mutex_enter(asy->asy_excl);
284052Sdf157793 
284152Sdf157793 	if (error == 0) {
284252Sdf157793 		/*
284352Sdf157793 		 * "ttycommon_ioctl" did most of the work; we just use the
284452Sdf157793 		 * data it set up.
284552Sdf157793 		 */
284652Sdf157793 		switch (iocp->ioc_cmd) {
284752Sdf157793 
284852Sdf157793 		case TCSETS:
284952Sdf157793 			if (!(asy->asy_rsc_console || asy->asy_rsc_control ||
285052Sdf157793 			    asy->asy_lom_console)) {
285152Sdf157793 				mutex_enter(asy->asy_excl_hi);
285252Sdf157793 				error = asy_program(asy, ASY_NOINIT);
285352Sdf157793 				mutex_exit(asy->asy_excl_hi);
285452Sdf157793 			}
285552Sdf157793 			break;
285652Sdf157793 		case TCSETSF:
285752Sdf157793 		case TCSETSW:
285852Sdf157793 		case TCSETA:
285952Sdf157793 		case TCSETAW:
286052Sdf157793 		case TCSETAF:
286152Sdf157793 			if (!(asy->asy_rsc_console || asy->asy_rsc_control ||
286252Sdf157793 			    asy->asy_lom_console)) {
286352Sdf157793 				mutex_enter(asy->asy_excl_hi);
286452Sdf157793 				if (iswput && asy_isbusy(asy)) {
28652211Szk194757 					/*
28662211Szk194757 					 * ttycommon_ioctl sets the db_type to
28672211Szk194757 					 * M_IOCACK and ioc_count to zero
28682211Szk194757 					 * we need to undo this when we
28692211Szk194757 					 * queue a control message. This will
28702211Szk194757 					 * allow the control messages to be
28712211Szk194757 					 * processed again when the chip
28722211Szk194757 					 * becomes available.
28732211Szk194757 					 */
28742211Szk194757 					mp->b_datap->db_type = M_IOCTL;
28752211Szk194757 					iocp->ioc_count = ioc_count;
28762211Szk194757 
287752Sdf157793 					if (putq(wq, mp) == 0)
287852Sdf157793 						freemsg(mp);
287952Sdf157793 					mutex_exit(asy->asy_excl_hi);
288052Sdf157793 					mutex_exit(asy->asy_excl);
288152Sdf157793 					return;
288252Sdf157793 				}
28833880Szk194757 
28843880Szk194757 				/*
28853880Szk194757 				 * TCSETA, TCSETAW, and TCSETAF make use of
28863880Szk194757 				 * the termio structure and therefore have
28873880Szk194757 				 * no concept of any speed except what can
28883880Szk194757 				 * be represented by CBAUD. This is because
28893880Szk194757 				 * of legacy SVR4 code. Therefore, if we see
28903880Szk194757 				 * one of the aforementioned IOCTL commands
28913880Szk194757 				 * we should zero out CBAUDEXT, CIBAUD, and
28923880Szk194757 				 * CIBAUDEXT as to not break legacy
28933880Szk194757 				 * functionality. This is because CBAUDEXT,
28943880Szk194757 				 * CIBAUD, and CIBAUDEXT can't be stored in
28953880Szk194757 				 * an unsigned short. By zeroing out CBAUDEXT,
28963880Szk194757 				 * CIBAUD, and CIBAUDEXT in the t_cflag of the
28973880Szk194757 				 * termios structure asy_program() will set the
28983880Szk194757 				 * input baud rate to the output baud rate.
28993880Szk194757 				 */
29003880Szk194757 				if (iocp->ioc_cmd == TCSETA ||
29013880Szk194757 				    iocp->ioc_cmd == TCSETAW ||
29023880Szk194757 				    iocp->ioc_cmd == TCSETAF)
29033880Szk194757 					tp->t_cflag &= ~(CIBAUD |
29043880Szk194757 					    CIBAUDEXT | CBAUDEXT);
29053880Szk194757 
290652Sdf157793 				error = asy_program(asy, ASY_NOINIT);
290752Sdf157793 				mutex_exit(asy->asy_excl_hi);
290852Sdf157793 			}
290952Sdf157793 			break;
291052Sdf157793 		case TIOCSSOFTCAR:
291152Sdf157793 			/* Set the driver state appropriately */
291252Sdf157793 			mutex_enter(asy->asy_excl_hi);
291352Sdf157793 			if (tp->t_flags & TS_SOFTCAR)
291452Sdf157793 				asy->asy_flags |= ASY_IGNORE_CD;
291552Sdf157793 			else
291652Sdf157793 				asy->asy_flags &= ~ASY_IGNORE_CD;
291752Sdf157793 			mutex_exit(asy->asy_excl_hi);
291852Sdf157793 			break;
291952Sdf157793 		}
292052Sdf157793 	} else if (error < 0) {
292152Sdf157793 		/*
292252Sdf157793 		 * "ttycommon_ioctl" didn't do anything; we process it here.
292352Sdf157793 		 */
292452Sdf157793 		error = 0;
292552Sdf157793 		switch (iocp->ioc_cmd) {
292652Sdf157793 
292752Sdf157793 		case TIOCGPPS:
292852Sdf157793 			/*
292952Sdf157793 			 * Get PPS on/off.
293052Sdf157793 			 */
293152Sdf157793 			if (mp->b_cont != NULL)
293252Sdf157793 				freemsg(mp->b_cont);
293352Sdf157793 
293452Sdf157793 			mp->b_cont = allocb(sizeof (int), BPRI_HI);
293552Sdf157793 			if (mp->b_cont == NULL) {
293652Sdf157793 				error = ENOMEM;
293752Sdf157793 				break;
293852Sdf157793 			}
293952Sdf157793 			if (asy->asy_flags & ASY_PPS)
294052Sdf157793 				*(int *)mp->b_cont->b_wptr = 1;
294152Sdf157793 			else
294252Sdf157793 				*(int *)mp->b_cont->b_wptr = 0;
294352Sdf157793 			mp->b_cont->b_wptr += sizeof (int);
294452Sdf157793 			mp->b_datap->db_type = M_IOCACK;
294552Sdf157793 			iocp->ioc_count = sizeof (int);
294652Sdf157793 			break;
294752Sdf157793 
294852Sdf157793 		case TIOCSPPS:
294952Sdf157793 			/*
295052Sdf157793 			 * Set PPS on/off.
295152Sdf157793 			 */
295252Sdf157793 			error = miocpullup(mp, sizeof (int));
295352Sdf157793 			if (error != 0)
295452Sdf157793 				break;
295552Sdf157793 
295652Sdf157793 			mutex_enter(asy->asy_excl_hi);
295752Sdf157793 			if (*(int *)mp->b_cont->b_rptr)
295852Sdf157793 				asy->asy_flags |= ASY_PPS;
295952Sdf157793 			else
296052Sdf157793 				asy->asy_flags &= ~ASY_PPS;
296152Sdf157793 			/* Reset edge sense */
296252Sdf157793 			asy->asy_flags &= ~ASY_PPS_EDGE;
296352Sdf157793 			mutex_exit(asy->asy_excl_hi);
296452Sdf157793 			mp->b_datap->db_type = M_IOCACK;
296552Sdf157793 			break;
296652Sdf157793 
296752Sdf157793 		case TIOCGPPSEV: {
296852Sdf157793 			/*
296952Sdf157793 			 * Get PPS event data.
297052Sdf157793 			 */
297152Sdf157793 			mblk_t *bp;
297252Sdf157793 			void *buf;
297352Sdf157793 #ifdef _SYSCALL32_IMPL
297452Sdf157793 			struct ppsclockev32 p32;
297552Sdf157793 #endif
297652Sdf157793 			struct ppsclockev ppsclockev;
297752Sdf157793 
297852Sdf157793 			if (mp->b_cont != NULL) {
297952Sdf157793 				freemsg(mp->b_cont);
298052Sdf157793 				mp->b_cont = NULL;
298152Sdf157793 			}
298252Sdf157793 
298352Sdf157793 			if ((asy->asy_flags & ASY_PPS) == 0) {
298452Sdf157793 				error = ENXIO;
298552Sdf157793 				break;
298652Sdf157793 			}
298752Sdf157793 
298852Sdf157793 			/* Protect from incomplete asy_ppsev */
298952Sdf157793 			mutex_enter(asy->asy_excl_hi);
299052Sdf157793 			ppsclockev = asy_ppsev;
299152Sdf157793 			mutex_exit(asy->asy_excl_hi);
299252Sdf157793 
299352Sdf157793 #ifdef _SYSCALL32_IMPL
299452Sdf157793 			if ((iocp->ioc_flag & IOC_MODELS) != IOC_NATIVE) {
299552Sdf157793 				TIMEVAL_TO_TIMEVAL32(&p32.tv, &ppsclockev.tv);
299652Sdf157793 				p32.serial = ppsclockev.serial;
299752Sdf157793 				buf = &p32;
299852Sdf157793 				iocp->ioc_count = sizeof (struct ppsclockev32);
299952Sdf157793 			} else
300052Sdf157793 #endif
300152Sdf157793 			{
300252Sdf157793 				buf = &ppsclockev;
300352Sdf157793 				iocp->ioc_count = sizeof (struct ppsclockev);
300452Sdf157793 			}
300552Sdf157793 
300652Sdf157793 			if ((bp = allocb(iocp->ioc_count, BPRI_HI)) == NULL) {
300752Sdf157793 				error = ENOMEM;
300852Sdf157793 				break;
300952Sdf157793 			}
301052Sdf157793 			mp->b_cont = bp;
301152Sdf157793 
301252Sdf157793 			bcopy(buf, bp->b_wptr, iocp->ioc_count);
301352Sdf157793 			bp->b_wptr += iocp->ioc_count;
301452Sdf157793 			mp->b_datap->db_type = M_IOCACK;
301552Sdf157793 			break;
301652Sdf157793 		}
301752Sdf157793 
301852Sdf157793 		case TCSBRK:
301952Sdf157793 			error = miocpullup(mp, sizeof (int));
302052Sdf157793 			if (error != 0)
302152Sdf157793 				break;
302252Sdf157793 
302352Sdf157793 			mutex_enter(asy->asy_excl_hi);
302452Sdf157793 			if (*(int *)mp->b_cont->b_rptr == 0) {
302552Sdf157793 				/*
302652Sdf157793 				 * Get the divisor by calculating the rate
302752Sdf157793 				 */
302852Sdf157793 				unsigned int rate, divisor;
302952Sdf157793 				rate = async->async_ttycommon.t_cflag & CBAUD;
303052Sdf157793 				if (async->async_ttycommon.t_cflag & CBAUDEXT)
303152Sdf157793 					rate += 16;
303252Sdf157793 				if (rate >= N_SU_SPEEDS) rate = B9600;
303352Sdf157793 				divisor = asyspdtab[rate] & 0xfff;
303452Sdf157793 
303552Sdf157793 				/*
303652Sdf157793 				 * To ensure that erroneous characters are
303752Sdf157793 				 * not sent out when the break is set, SB
303852Sdf157793 				 * recommends three steps:
303952Sdf157793 				 *
304052Sdf157793 				 * 1) pad the TSR with 0 bits
304152Sdf157793 				 * 2) When the TSR is full, set break
304252Sdf157793 				 * 3) When the TSR has been flushed, unset
304352Sdf157793 				 *    the break when transmission must be
304452Sdf157793 				 *    restored.
304552Sdf157793 				 *
304652Sdf157793 				 * We loop until the TSR is empty and then
304752Sdf157793 				 * set the break.  ASYNC_BREAK has been set
304852Sdf157793 				 * to ensure that no characters are
304952Sdf157793 				 * transmitted while the TSR is being
305052Sdf157793 				 * flushed and SOUT is being used for the
305152Sdf157793 				 * break signal.
305252Sdf157793 				 *
305352Sdf157793 				 * The wait period is equal to
305452Sdf157793 				 * clock / (baud * 16) * 16 * 2.
305552Sdf157793 				 */
305652Sdf157793 				async->async_flags |= ASYNC_BREAK;
305752Sdf157793 				while ((INB(LSR) & XSRE) == 0) {
305852Sdf157793 					mutex_exit(asy->asy_excl_hi);
305952Sdf157793 					mutex_exit(asy->asy_excl);
306052Sdf157793 					drv_usecwait(32*divisor);
306152Sdf157793 					mutex_enter(asy->asy_excl);
306252Sdf157793 					mutex_enter(asy->asy_excl_hi);
306352Sdf157793 				}
306452Sdf157793 
306552Sdf157793 				/*
306652Sdf157793 				 * Set the break bit, and arrange for
306752Sdf157793 				 * "async_restart" to be called in 1/4 second;
306852Sdf157793 				 * it will turn the break bit off, and call
306952Sdf157793 				 * "async_start" to grab the next message.
307052Sdf157793 				 */
307152Sdf157793 				val = INB(LCR);
307252Sdf157793 				OUTB(LCR, (val | SETBREAK));
307352Sdf157793 				mutex_exit(asy->asy_excl_hi);
307452Sdf157793 				(void) timeout(async_restart, async, hz / 4);
307552Sdf157793 			} else {
307652Sdf157793 #ifdef DEBUG
307752Sdf157793 				if (asydebug & ASY_DEBUG_CLOSE)
307852Sdf157793 					printf("asy%d: wait for flush.\n",
30795973Szk194757 					    UNIT(async->async_dev));
308052Sdf157793 #endif
308152Sdf157793 				if (iswput && asy_isbusy(asy)) {
308252Sdf157793 					if (putq(wq, mp) == 0)
308352Sdf157793 						freemsg(mp);
308452Sdf157793 					mutex_exit(asy->asy_excl_hi);
308552Sdf157793 					mutex_exit(asy->asy_excl);
308652Sdf157793 					return;
308752Sdf157793 				}
308852Sdf157793 				mutex_exit(asy->asy_excl_hi);
308952Sdf157793 #ifdef DEBUG
309052Sdf157793 				if (asydebug & ASY_DEBUG_CLOSE)
309152Sdf157793 					printf("asy%d: ldterm satisfied.\n",
30925973Szk194757 					    UNIT(async->async_dev));
309352Sdf157793 #endif
309452Sdf157793 			}
309552Sdf157793 			break;
309652Sdf157793 
309752Sdf157793 		case TIOCSBRK:
309852Sdf157793 			mutex_enter(asy->asy_excl_hi);
309952Sdf157793 			val = INB(LCR);
310052Sdf157793 			OUTB(LCR, (val | SETBREAK));
310152Sdf157793 			mutex_exit(asy->asy_excl_hi);
310252Sdf157793 			mutex_exit(asy->asy_excl);
310352Sdf157793 			miocack(wq, mp, 0, 0);
310452Sdf157793 			return;
310552Sdf157793 
310652Sdf157793 		case TIOCCBRK:
310752Sdf157793 			mutex_enter(asy->asy_excl_hi);
310852Sdf157793 			val = INB(LCR);
310952Sdf157793 			OUTB(LCR, (val & ~SETBREAK));
311052Sdf157793 			mutex_exit(asy->asy_excl_hi);
311152Sdf157793 			mutex_exit(asy->asy_excl);
311252Sdf157793 			miocack(wq, mp, 0, 0);
311352Sdf157793 			return;
311452Sdf157793 
311552Sdf157793 		case TIOCMSET:
311652Sdf157793 		case TIOCMBIS:
311752Sdf157793 		case TIOCMBIC:
311852Sdf157793 			if (iocp->ioc_count == TRANSPARENT)
311952Sdf157793 				mcopyin(mp, NULL, sizeof (int), NULL);
312052Sdf157793 			else {
312152Sdf157793 				error = miocpullup(mp, sizeof (int));
312252Sdf157793 				if (error != 0)
312352Sdf157793 					break;
312452Sdf157793 
312552Sdf157793 				mutex_enter(asy->asy_excl_hi);
312652Sdf157793 
312752Sdf157793 				(void) asymctl(asy,
31285973Szk194757 				    dmtoasy(*(int *)mp->b_cont->b_rptr),
31295973Szk194757 				    iocp->ioc_cmd);
313052Sdf157793 
313152Sdf157793 				mutex_exit(asy->asy_excl_hi);
313252Sdf157793 				iocp->ioc_error = 0;
313352Sdf157793 				mp->b_datap->db_type = M_IOCACK;
313452Sdf157793 			}
313552Sdf157793 			break;
313652Sdf157793 
313752Sdf157793 		case TIOCSILOOP:
313852Sdf157793 			mutex_enter(asy->asy_excl_hi);
313952Sdf157793 			/*
314052Sdf157793 			 * If somebody misues this Ioctl when used for
314152Sdf157793 			 * driving keyboard and mouse indicate not supported
314252Sdf157793 			 */
314352Sdf157793 			if ((asy->asy_device_type == ASY_KEYBOARD) ||
31445973Szk194757 			    (asy->asy_device_type == ASY_MOUSE)) {
314552Sdf157793 				mutex_exit(asy->asy_excl_hi);
314652Sdf157793 				error = ENOTTY;
314752Sdf157793 				break;
314852Sdf157793 			}
314952Sdf157793 
315052Sdf157793 			/* should not use when we're the console */
315152Sdf157793 			if ((async->async_dev == kbddev) ||
315252Sdf157793 			    (async->async_dev == rconsdev) ||
315352Sdf157793 			    (async->async_dev == stdindev)) {
315452Sdf157793 				mutex_exit(asy->asy_excl_hi);
315552Sdf157793 				error = EINVAL;
315652Sdf157793 				break;
315752Sdf157793 			}
315852Sdf157793 
315952Sdf157793 			val = INB(MCR);
316052Sdf157793 			icr = INB(ICR);
316152Sdf157793 			/*
316252Sdf157793 			 * Disable the Modem Status Interrupt
316352Sdf157793 			 * The reason for disabling is  the status of
316452Sdf157793 			 * modem signal are in the higher 4 bits instead of
316552Sdf157793 			 * lower four bits when in loopback mode,
316652Sdf157793 			 * so, donot worry about Modem interrupt when
316752Sdf157793 			 * you are planning to set
316852Sdf157793 			 * this in loopback mode until it is cleared by
316952Sdf157793 			 * another ioctl to get out of the loopback mode
317052Sdf157793 			 */
317152Sdf157793 			OUTB(ICR, icr & ~ MIEN);
317252Sdf157793 			OUTB(MCR, val | ASY_LOOP);
317352Sdf157793 			mutex_exit(asy->asy_excl_hi);
317452Sdf157793 			iocp->ioc_error = 0;
317552Sdf157793 			mp->b_datap->db_type = M_IOCACK;
317652Sdf157793 			break;
317752Sdf157793 
317852Sdf157793 		case TIOCMGET:
317952Sdf157793 			datamp = allocb(sizeof (int), BPRI_MED);
318052Sdf157793 			if (datamp == NULL) {
318152Sdf157793 				error = EAGAIN;
318252Sdf157793 				break;
318352Sdf157793 			}
318452Sdf157793 
318552Sdf157793 			mutex_enter(asy->asy_excl_hi);
318652Sdf157793 			*(int *)datamp->b_rptr = asymctl(asy, 0, TIOCMGET);
318752Sdf157793 			mutex_exit(asy->asy_excl_hi);
318852Sdf157793 
318952Sdf157793 			if (iocp->ioc_count == TRANSPARENT) {
319052Sdf157793 				mcopyout(mp, NULL, sizeof (int), NULL, datamp);
319152Sdf157793 			} else {
319252Sdf157793 				if (mp->b_cont != NULL)
319352Sdf157793 					freemsg(mp->b_cont);
319452Sdf157793 				mp->b_cont = datamp;
319552Sdf157793 				mp->b_cont->b_wptr += sizeof (int);
319652Sdf157793 				mp->b_datap->db_type = M_IOCACK;
319752Sdf157793 				iocp->ioc_count = sizeof (int);
319852Sdf157793 			}
319952Sdf157793 			break;
320052Sdf157793 
32015973Szk194757 		case CONSOPENPOLLEDIO:
32025973Szk194757 			/*
32035973Szk194757 			 * If we are driving a keyboard there is nothing
32045973Szk194757 			 * upstream to translate the scan codes. Therefore,
32055973Szk194757 			 * set the error code to ENOTSUP and NAK the request
32065973Szk194757 			 */
32075973Szk194757 			if (asy->asy_device_type == ASY_KEYBOARD) {
32085973Szk194757 				error = ENOTSUP;
32095973Szk194757 				break;
32105973Szk194757 			}
32115973Szk194757 
32125973Szk194757 			error = miocpullup(mp, sizeof (struct cons_polledio *));
32135973Szk194757 			if (error != 0)
32145973Szk194757 				break;
32155973Szk194757 
32165973Szk194757 			/*
32175973Szk194757 			 * send up a message block containing the
32185973Szk194757 			 * cons_polledio structure. This provides
32195973Szk194757 			 * handles to the putchar, getchar, ischar,
32205973Szk194757 			 * polledio_enter and polledio_exit functions.
32215973Szk194757 			 */
32225973Szk194757 			*(struct cons_polledio **)mp->b_cont->b_rptr =
32235973Szk194757 			    &asy->polledio;
32245973Szk194757 
32255973Szk194757 			mp->b_datap->db_type = M_IOCACK;
32265973Szk194757 			break;
32275973Szk194757 
32285973Szk194757 		case CONSCLOSEPOLLEDIO:
32295973Szk194757 			/*
32305973Szk194757 			 * If we are driving a keyboard we never successfully
32315973Szk194757 			 * called CONSOPENPOLLEDIO so set the error to
32325973Szk194757 			 * ENOTSUP and NAK the request.
32335973Szk194757 			 */
32345973Szk194757 			if (asy->asy_device_type == ASY_KEYBOARD) {
32355973Szk194757 				error = ENOTSUP;
32365973Szk194757 				break;
32375973Szk194757 			}
32385973Szk194757 
32395973Szk194757 			mp->b_datap->db_type = M_IOCACK;
32405973Szk194757 			iocp->ioc_error = 0;
32415973Szk194757 			iocp->ioc_rval = 0;
32425973Szk194757 			break;
32435973Szk194757 
324452Sdf157793 		default: /* unexpected ioctl type */
324552Sdf157793 			/*
324652Sdf157793 			 * If we don't understand it, it's an error.  NAK it.
324752Sdf157793 			 */
324852Sdf157793 			error = EINVAL;
324952Sdf157793 			break;
325052Sdf157793 		}
325152Sdf157793 	}
325252Sdf157793 	if (error != 0) {
325352Sdf157793 		iocp->ioc_error = error;
325452Sdf157793 		mp->b_datap->db_type = M_IOCNAK;
325552Sdf157793 	}
325652Sdf157793 	mutex_exit(asy->asy_excl);
325752Sdf157793 	qreply(wq, mp);
325852Sdf157793 }
325952Sdf157793 
326052Sdf157793 static void
asyrsrv(queue_t * q)326152Sdf157793 asyrsrv(queue_t *q)
326252Sdf157793 {
326352Sdf157793 	mblk_t *bp;
326452Sdf157793 	struct asyncline *async;
326552Sdf157793 
326652Sdf157793 	async = (struct asyncline *)q->q_ptr;
326752Sdf157793 
326852Sdf157793 	while (canputnext(q) && (bp = getq(q)))
326952Sdf157793 		putnext(q, bp);
327052Sdf157793 	ASYSETSOFT(async->async_common);
327152Sdf157793 	async->async_polltid = 0;
327252Sdf157793 }
327352Sdf157793 
327452Sdf157793 /*
327552Sdf157793  * Put procedure for write queue.
327652Sdf157793  * Respond to M_STOP, M_START, M_IOCTL, and M_FLUSH messages here;
327752Sdf157793  * set the flow control character for M_STOPI and M_STARTI messages;
327852Sdf157793  * queue up M_BREAK, M_DELAY, and M_DATA messages for processing
327952Sdf157793  * by the start routine, and then call the start routine; discard
328052Sdf157793  * everything else.  Note that this driver does not incorporate any
328152Sdf157793  * mechanism to negotiate to handle the canonicalization process.
328252Sdf157793  * It expects that these functions are handled in upper module(s),
328352Sdf157793  * as we do in ldterm.
328452Sdf157793  */
328552Sdf157793 static void
asywput(queue_t * q,mblk_t * mp)328652Sdf157793 asywput(queue_t *q, mblk_t *mp)
328752Sdf157793 {
328852Sdf157793 	register struct asyncline *async;
328952Sdf157793 	register struct asycom *asy;
329052Sdf157793 	int error;
329152Sdf157793 
329252Sdf157793 	async = (struct asyncline *)q->q_ptr;
329352Sdf157793 	asy = async->async_common;
329452Sdf157793 
329552Sdf157793 	switch (mp->b_datap->db_type) {
329652Sdf157793 
329752Sdf157793 	case M_STOP:
329852Sdf157793 		/*
329952Sdf157793 		 * Since we don't do real DMA, we can just let the
330052Sdf157793 		 * chip coast to a stop after applying the brakes.
330152Sdf157793 		 */
330252Sdf157793 		mutex_enter(asy->asy_excl);
330352Sdf157793 		async->async_flags |= ASYNC_STOPPED;
330452Sdf157793 		mutex_exit(asy->asy_excl);
330552Sdf157793 		freemsg(mp);
330652Sdf157793 		break;
330752Sdf157793 
330852Sdf157793 	case M_START:
330952Sdf157793 		mutex_enter(asy->asy_excl);
331052Sdf157793 		if (async->async_flags & ASYNC_STOPPED) {
331152Sdf157793 			async->async_flags &= ~ASYNC_STOPPED;
331252Sdf157793 			/*
331352Sdf157793 			 * If an output operation is in progress,
331452Sdf157793 			 * resume it.  Otherwise, prod the start
331552Sdf157793 			 * routine.
331652Sdf157793 			 */
331752Sdf157793 			if (async->async_ocnt > 0) {
331852Sdf157793 				mutex_enter(asy->asy_excl_hi);
331952Sdf157793 				async_resume(async);
332052Sdf157793 				mutex_exit(asy->asy_excl_hi);
332152Sdf157793 			} else {
332252Sdf157793 				async_start(async);
332352Sdf157793 			}
332452Sdf157793 		}
332552Sdf157793 		mutex_exit(asy->asy_excl);
332652Sdf157793 		freemsg(mp);
332752Sdf157793 		break;
332852Sdf157793 
332952Sdf157793 	case M_IOCTL:
333052Sdf157793 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
333152Sdf157793 
333252Sdf157793 		case TCSBRK:
333352Sdf157793 			error = miocpullup(mp, sizeof (int));
333452Sdf157793 			if (error != 0) {
333552Sdf157793 				miocnak(q, mp, 0, error);
333652Sdf157793 				return;
333752Sdf157793 			}
333852Sdf157793 
333952Sdf157793 			if (*(int *)mp->b_cont->b_rptr != 0) {
334052Sdf157793 #ifdef DEBUG
334152Sdf157793 				if (asydebug & ASY_DEBUG_CLOSE)
334252Sdf157793 					printf("asy%d: flush request.\n",
334352Sdf157793 					    UNIT(async->async_dev));
334452Sdf157793 #endif
334552Sdf157793 				(void) putq(q, mp);
334652Sdf157793 				mutex_enter(asy->asy_excl);
334752Sdf157793 				async_nstart(async, 1);
334852Sdf157793 				mutex_exit(asy->asy_excl);
334952Sdf157793 				break;
335052Sdf157793 			}
335152Sdf157793 			/*FALLTHROUGH*/
335252Sdf157793 		case TCSETSW:
335352Sdf157793 		case TCSETSF:
335452Sdf157793 		case TCSETAW:
335552Sdf157793 		case TCSETAF:
335652Sdf157793 			/*
335752Sdf157793 			 * The changes do not take effect until all
335852Sdf157793 			 * output queued before them is drained.
335952Sdf157793 			 * Put this message on the queue, so that
336052Sdf157793 			 * "async_start" will see it when it's done
336152Sdf157793 			 * with the output before it.  Poke the
336252Sdf157793 			 * start routine, just in case.
336352Sdf157793 			 */
336452Sdf157793 			(void) putq(q, mp);
336552Sdf157793 			mutex_enter(asy->asy_excl);
336652Sdf157793 			async_start(async);
336752Sdf157793 			mutex_exit(asy->asy_excl);
336852Sdf157793 			break;
336952Sdf157793 
337052Sdf157793 		default:
337152Sdf157793 			/*
337252Sdf157793 			 * Do it now.
337352Sdf157793 			 */
337452Sdf157793 			async_ioctl(async, q, mp, B_TRUE);
337552Sdf157793 			break;
337652Sdf157793 		}
337752Sdf157793 		break;
337852Sdf157793 
337952Sdf157793 	case M_FLUSH:
338052Sdf157793 		if (*mp->b_rptr & FLUSHW) {
338152Sdf157793 			mutex_enter(asy->asy_excl);
338252Sdf157793 
338352Sdf157793 			/*
338452Sdf157793 			 * Abort any output in progress.
338552Sdf157793 			 */
338652Sdf157793 			mutex_enter(asy->asy_excl_hi);
338752Sdf157793 			if (async->async_flags & ASYNC_BUSY) {
338852Sdf157793 				async->async_ocnt = 0;
338952Sdf157793 				async->async_flags &= ~ASYNC_BUSY;
339052Sdf157793 			}
339152Sdf157793 			mutex_exit(asy->asy_excl_hi);
339252Sdf157793 
339352Sdf157793 			/* Flush FIFO buffers */
339452Sdf157793 			if (asy->asy_use_fifo == FIFO_ON) {
339552Sdf157793 				OUTB(FIFOR, FIFO_ON | FIFODMA | FIFOTXFLSH |
339652Sdf157793 				    (asy->asy_trig_level & 0xff));
339752Sdf157793 			}
339852Sdf157793 
339952Sdf157793 			/*
340052Sdf157793 			 * Flush our write queue.
340152Sdf157793 			 */
340252Sdf157793 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
340352Sdf157793 			if (async->async_xmitblk != NULL) {
340452Sdf157793 				freeb(async->async_xmitblk);
340552Sdf157793 				async->async_xmitblk = NULL;
340652Sdf157793 			}
340752Sdf157793 
340852Sdf157793 			mutex_exit(asy->asy_excl);
340952Sdf157793 			*mp->b_rptr &= ~FLUSHW;	/* it has been flushed */
341052Sdf157793 		}
341152Sdf157793 		if (*mp->b_rptr & FLUSHR) {
341252Sdf157793 			/* Flush FIFO buffers */
341352Sdf157793 			if (asy->asy_use_fifo == FIFO_ON) {
341452Sdf157793 				OUTB(FIFOR, FIFO_ON | FIFODMA | FIFORXFLSH |
341552Sdf157793 				    (asy->asy_trig_level & 0xff));
341652Sdf157793 			}
341752Sdf157793 			flushq(RD(q), FLUSHDATA);
341852Sdf157793 			qreply(q, mp);	/* give the read queues a crack at it */
341952Sdf157793 		} else {
342052Sdf157793 			freemsg(mp);
342152Sdf157793 		}
342252Sdf157793 
342352Sdf157793 		/*
342452Sdf157793 		 * We must make sure we process messages that survive the
342552Sdf157793 		 * write-side flush.  Without this call, the close protocol
342652Sdf157793 		 * with ldterm can hang forever.  (ldterm will have sent us a
342752Sdf157793 		 * TCSBRK ioctl that it expects a response to.)
342852Sdf157793 		 */
342952Sdf157793 		mutex_enter(asy->asy_excl);
343052Sdf157793 		async_start(async);
343152Sdf157793 		mutex_exit(asy->asy_excl);
343252Sdf157793 		break;
343352Sdf157793 	case M_BREAK:
343452Sdf157793 	case M_DELAY:
343552Sdf157793 	case M_DATA:
343652Sdf157793 		/*
343752Sdf157793 		 * Queue the message up to be transmitted,
343852Sdf157793 		 * and poke the start routine.
343952Sdf157793 		 */
344052Sdf157793 		(void) putq(q, mp);
344152Sdf157793 		mutex_enter(asy->asy_excl);
344252Sdf157793 		async_start(async);
344352Sdf157793 		mutex_exit(asy->asy_excl);
344452Sdf157793 		break;
344552Sdf157793 
344652Sdf157793 	case M_STOPI:
344752Sdf157793 		mutex_enter(asy->asy_excl);
344852Sdf157793 		async->async_flowc = async->async_stopc;
344952Sdf157793 		async_start(async);		/* poke the start routine */
345052Sdf157793 		mutex_exit(asy->asy_excl);
345152Sdf157793 		freemsg(mp);
345252Sdf157793 		break;
345352Sdf157793 
345452Sdf157793 	case M_STARTI:
345552Sdf157793 		mutex_enter(asy->asy_excl);
345652Sdf157793 		async->async_flowc = async->async_startc;
345752Sdf157793 		async_start(async);		/* poke the start routine */
345852Sdf157793 		mutex_exit(asy->asy_excl);
345952Sdf157793 		freemsg(mp);
346052Sdf157793 		break;
346152Sdf157793 
346252Sdf157793 	case M_CTL:
346352Sdf157793 		if (MBLKL(mp) >= sizeof (struct iocblk) &&
346452Sdf157793 		    ((struct iocblk *)mp->b_rptr)->ioc_cmd == MC_POSIXQUERY) {
346552Sdf157793 			((struct iocblk *)mp->b_rptr)->ioc_cmd = MC_HAS_POSIX;
346652Sdf157793 			qreply(q, mp);
346752Sdf157793 		} else {
346852Sdf157793 			/*
346952Sdf157793 			 * These MC_SERVICE type messages are used by upper
347052Sdf157793 			 * modules to tell this driver to send input up
347152Sdf157793 			 * immediately, or that it can wait for normal
347252Sdf157793 			 * processing that may or may not be done.  Sun
347352Sdf157793 			 * requires these for the mouse module.
347452Sdf157793 			 * (XXX - for x86?)
347552Sdf157793 			 */
347652Sdf157793 			mutex_enter(asy->asy_excl);
347752Sdf157793 			switch (*mp->b_rptr) {
347852Sdf157793 
347952Sdf157793 			case MC_SERVICEIMM:
348052Sdf157793 				async->async_flags |= ASYNC_SERVICEIMM;
348152Sdf157793 				break;
348252Sdf157793 
348352Sdf157793 			case MC_SERVICEDEF:
348452Sdf157793 				async->async_flags &= ~ASYNC_SERVICEIMM;
348552Sdf157793 				break;
348652Sdf157793 			}
348752Sdf157793 			mutex_exit(asy->asy_excl);
348852Sdf157793 			freemsg(mp);
348952Sdf157793 		}
349052Sdf157793 		break;
349152Sdf157793 
349252Sdf157793 	case M_IOCDATA:
349352Sdf157793 		async_iocdata(q, mp);
349452Sdf157793 		break;
349552Sdf157793 
349652Sdf157793 	default:
349752Sdf157793 		freemsg(mp);
349852Sdf157793 		break;
349952Sdf157793 	}
350052Sdf157793 }
350152Sdf157793 
350252Sdf157793 /*
350352Sdf157793  * Retry an "ioctl", now that "bufcall" claims we may be able to allocate
350452Sdf157793  * the buffer we need.
350552Sdf157793  */
350652Sdf157793 static void
async_reioctl(void * arg)350752Sdf157793 async_reioctl(void *arg)
350852Sdf157793 {
350952Sdf157793 	struct asyncline *async = arg;
351052Sdf157793 	struct asycom *asy = async->async_common;
351152Sdf157793 	queue_t	*q;
351252Sdf157793 	mblk_t		*mp;
351352Sdf157793 
351452Sdf157793 	/*
351552Sdf157793 	 * The bufcall is no longer pending.
351652Sdf157793 	 */
351752Sdf157793 	mutex_enter(asy->asy_excl);
351852Sdf157793 	async->async_wbufcid = 0;
351952Sdf157793 	if ((q = async->async_ttycommon.t_writeq) == NULL) {
352052Sdf157793 		mutex_exit(asy->asy_excl);
352152Sdf157793 		return;
352252Sdf157793 	}
352352Sdf157793 	if ((mp = async->async_ttycommon.t_iocpending) != NULL) {
352452Sdf157793 		/* not pending any more */
352552Sdf157793 		async->async_ttycommon.t_iocpending = NULL;
352652Sdf157793 		mutex_exit(asy->asy_excl);
352752Sdf157793 		/* not in STREAMS queue; we no longer know if we're in wput */
352852Sdf157793 		async_ioctl(async, q, mp, B_TRUE);
352952Sdf157793 	} else
353052Sdf157793 		mutex_exit(asy->asy_excl);
353152Sdf157793 }
353252Sdf157793 
353352Sdf157793 static void
async_iocdata(queue_t * q,mblk_t * mp)353452Sdf157793 async_iocdata(queue_t *q, mblk_t *mp)
353552Sdf157793 {
353652Sdf157793 	struct asyncline	*async = (struct asyncline *)q->q_ptr;
353752Sdf157793 	struct asycom		*asy;
353852Sdf157793 	struct copyresp *csp;
353952Sdf157793 
354052Sdf157793 	asy = async->async_common;
354152Sdf157793 	csp = (struct copyresp *)mp->b_rptr;
354252Sdf157793 
354352Sdf157793 	if (csp->cp_rval != 0) {
354452Sdf157793 		freemsg(mp);
354552Sdf157793 		return;
354652Sdf157793 	}
354752Sdf157793 
354852Sdf157793 	mutex_enter(asy->asy_excl);
354952Sdf157793 
355052Sdf157793 	switch (csp->cp_cmd) {
355152Sdf157793 	case TIOCMSET:
355252Sdf157793 	case TIOCMBIS:
355352Sdf157793 	case TIOCMBIC:
355452Sdf157793 		if (mp->b_cont == NULL) {
355552Sdf157793 			mutex_exit(asy->asy_excl);
355652Sdf157793 			miocnak(q, mp, 0, EINVAL);
355752Sdf157793 			break;
355852Sdf157793 		}
355952Sdf157793 
356052Sdf157793 		mutex_enter(asy->asy_excl_hi);
356152Sdf157793 		(void) asymctl(asy, dmtoasy(*(int *)mp->b_cont->b_rptr),
35625973Szk194757 		    csp->cp_cmd);
356352Sdf157793 		mutex_exit(asy->asy_excl_hi);
356452Sdf157793 
356552Sdf157793 		freemsg(mp->b_cont);
356652Sdf157793 		mp->b_cont = NULL;
356752Sdf157793 		mutex_exit(asy->asy_excl);
356852Sdf157793 		miocack(q, mp, 0, 0);
356952Sdf157793 		break;
357052Sdf157793 
357152Sdf157793 	case TIOCMGET:
357252Sdf157793 		if (mp->b_cont != NULL) {
357352Sdf157793 			freemsg(mp->b_cont);
357452Sdf157793 			mp->b_cont = NULL;
357552Sdf157793 		}
357652Sdf157793 		mutex_exit(asy->asy_excl);
357752Sdf157793 		miocack(q, mp, 0, 0);
357852Sdf157793 		break;
357952Sdf157793 
358052Sdf157793 	default:
358152Sdf157793 		mutex_exit(asy->asy_excl);
358252Sdf157793 		miocnak(q, mp, 0, EINVAL);
358352Sdf157793 		break;
358452Sdf157793 	}
358552Sdf157793 }
358652Sdf157793 
358752Sdf157793 
358852Sdf157793 /*
358952Sdf157793  * Set or get the modem control status.
359052Sdf157793  */
359152Sdf157793 static int
asymctl(struct asycom * asy,int bits,int how)359252Sdf157793 asymctl(struct asycom *asy, int bits, int how)
359352Sdf157793 {
359452Sdf157793 	register int mcr_r, msr_r;
359552Sdf157793 
359652Sdf157793 	ASSERT(mutex_owned(asy->asy_excl_hi));
359752Sdf157793 	ASSERT(mutex_owned(asy->asy_excl));
359852Sdf157793 
359952Sdf157793 	/* Read Modem Control Registers */
360052Sdf157793 	mcr_r = INB(MCR);
360152Sdf157793 
360252Sdf157793 	switch (how) {
360352Sdf157793 
360452Sdf157793 	case TIOCMSET:
360552Sdf157793 		mcr_r = bits;
360652Sdf157793 		break;
360752Sdf157793 
360852Sdf157793 	case TIOCMBIS:
360952Sdf157793 		mcr_r |= bits;			/* Set bits from input	*/
361052Sdf157793 		break;
361152Sdf157793 
361252Sdf157793 	case TIOCMBIC:
361352Sdf157793 		mcr_r &= ~bits;			/* Set ~bits from input	*/
361452Sdf157793 		break;
361552Sdf157793 
361652Sdf157793 	case TIOCMGET:
361752Sdf157793 		/* Read Modem Status Registers */
361852Sdf157793 		if (INB(ICR) & MIEN)
361952Sdf157793 			msr_r = asy->asy_cached_msr;
362052Sdf157793 		else
362152Sdf157793 			msr_r = INB(MSR);
362252Sdf157793 		return (asytodm(mcr_r, msr_r));
362352Sdf157793 	}
362452Sdf157793 
362552Sdf157793 	OUTB(MCR, mcr_r);
362652Sdf157793 
362752Sdf157793 	return (mcr_r);
362852Sdf157793 }
362952Sdf157793 
363052Sdf157793 static int
asytodm(int mcr_r,int msr_r)363152Sdf157793 asytodm(int mcr_r, int msr_r)
363252Sdf157793 {
363352Sdf157793 	register int b = 0;
363452Sdf157793 
363552Sdf157793 
363652Sdf157793 	/* MCR registers */
363752Sdf157793 	if (mcr_r & RTS)
363852Sdf157793 		b |= TIOCM_RTS;
363952Sdf157793 
364052Sdf157793 	if (mcr_r & DTR)
364152Sdf157793 		b |= TIOCM_DTR;
364252Sdf157793 
364352Sdf157793 	/* MSR registers */
364452Sdf157793 	if (msr_r & DCD)
364552Sdf157793 		b |= TIOCM_CAR;
364652Sdf157793 
364752Sdf157793 	if (msr_r & CTS)
364852Sdf157793 		b |= TIOCM_CTS;
364952Sdf157793 
365052Sdf157793 	if (msr_r & DSR)
365152Sdf157793 		b |= TIOCM_DSR;
365252Sdf157793 
365352Sdf157793 	if (msr_r & RI)
365452Sdf157793 		b |= TIOCM_RNG;
365552Sdf157793 
365652Sdf157793 	return (b);
365752Sdf157793 }
365852Sdf157793 
365952Sdf157793 static int
dmtoasy(int bits)366052Sdf157793 dmtoasy(int bits)
366152Sdf157793 {
366252Sdf157793 	register int b = 0;
366352Sdf157793 
366452Sdf157793 #ifdef	CAN_NOT_SET	/* only DTR and RTS can be set */
366552Sdf157793 	if (bits & TIOCM_CAR)
366652Sdf157793 		b |= DCD;
366752Sdf157793 	if (bits & TIOCM_CTS)
366852Sdf157793 		b |= CTS;
366952Sdf157793 	if (bits & TIOCM_DSR)
367052Sdf157793 		b |= DSR;
367152Sdf157793 	if (bits & TIOCM_RNG)
367252Sdf157793 		b |= RI;
367352Sdf157793 #endif
367452Sdf157793 
367552Sdf157793 	if (bits & TIOCM_RTS)
367652Sdf157793 		b |= RTS;
367752Sdf157793 	if (bits & TIOCM_DTR)
367852Sdf157793 		b |= DTR;
367952Sdf157793 
368052Sdf157793 	return (b);
368152Sdf157793 }
368252Sdf157793 
368352Sdf157793 static void
asycheckflowcontrol_hw(struct asycom * asy)368452Sdf157793 asycheckflowcontrol_hw(struct asycom *asy)
368552Sdf157793 {
368652Sdf157793 	struct asyncline *async;
368752Sdf157793 	uchar_t	mcr, flag;
368852Sdf157793 
368952Sdf157793 	ASSERT(mutex_owned(asy->asy_excl_hi));
369052Sdf157793 
369152Sdf157793 	async = (struct asyncline *)asy->asy_priv;
369252Sdf157793 	ASSERT(async != NULL);
369352Sdf157793 
369452Sdf157793 	if (async->async_ttycommon.t_cflag & CRTSXOFF) {
369552Sdf157793 		mcr = INB(MCR);
369652Sdf157793 		flag = (async->async_flags & ASYNC_HW_IN_FLOW) ? 0 : RTS;
369752Sdf157793 		if (((mcr ^ flag) & RTS) != 0) {
369852Sdf157793 			OUTB(MCR, (mcr ^ RTS));
369952Sdf157793 		}
370052Sdf157793 	}
370152Sdf157793 }
370252Sdf157793 
370352Sdf157793 static boolean_t
asycheckflowcontrol_sw(struct asycom * asy)370452Sdf157793 asycheckflowcontrol_sw(struct asycom *asy)
370552Sdf157793 {
370652Sdf157793 	uchar_t		ss;
370752Sdf157793 	struct asyncline *async;
370852Sdf157793 	int rval = B_FALSE;
370952Sdf157793 
371052Sdf157793 	ASSERT(mutex_owned(asy->asy_excl_hi));
371152Sdf157793 
371252Sdf157793 	async = (struct asyncline *)asy->asy_priv;
371352Sdf157793 	ASSERT(async != NULL);
371452Sdf157793 
371552Sdf157793 	if ((ss = async->async_flowc) != '\0' && (INB(LSR) & XHRE)) {
371652Sdf157793 		/*
371752Sdf157793 		 * If we get this far, then we know that flowc is non-zero and
371852Sdf157793 		 * that there's transmit room available.  We've "handled" the
371952Sdf157793 		 * request now, so clear it.  If the user didn't ask for IXOFF,
372052Sdf157793 		 * then don't actually send anything, but wait for the next
372152Sdf157793 		 * opportunity.
372252Sdf157793 		 */
372352Sdf157793 		async->async_flowc = '\0';
372452Sdf157793 		if (async->async_ttycommon.t_iflag & IXOFF) {
372552Sdf157793 			async->async_flags |= ASYNC_BUSY;
372652Sdf157793 			OUTB(DAT, ss);
372752Sdf157793 			rval = B_TRUE;
372852Sdf157793 		}
372952Sdf157793 	}
373052Sdf157793 
373152Sdf157793 	return (rval);
373252Sdf157793 }
373352Sdf157793 
373452Sdf157793 /*
373552Sdf157793  * Check for abort character sequence
373652Sdf157793  */
373752Sdf157793 static boolean_t
abort_charseq_recognize(uchar_t ch)373852Sdf157793 abort_charseq_recognize(uchar_t ch)
373952Sdf157793 {
374052Sdf157793 	static int state = 0;
374152Sdf157793 #define	CNTRL(c) ((c)&037)
374252Sdf157793 	static char sequence[] = { '\r', '~', CNTRL('b') };
374352Sdf157793 
374452Sdf157793 	if (ch == sequence[state]) {
374552Sdf157793 		if (++state >= sizeof (sequence)) {
374652Sdf157793 			state = 0;
374752Sdf157793 			return (B_TRUE);
374852Sdf157793 		}
374952Sdf157793 	} else {
375052Sdf157793 		state = (ch == sequence[0]) ? 1 : 0;
375152Sdf157793 	}
375252Sdf157793 	return (B_FALSE);
375352Sdf157793 }
3754