xref: /onnv-gate/usr/src/uts/common/io/asy.c (revision 6990)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51106Smrj  * Common Development and Distribution License (the "License").
61106Smrj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211106Smrj 
220Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
230Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
240Sstevel@tonic-gate /*	  All Rights Reserved					*/
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
27*6990Sgd78059  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
280Sstevel@tonic-gate  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * Serial I/O driver for 8250/16450/16550A/16650/16750 chips.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/param.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/signal.h>
400Sstevel@tonic-gate #include <sys/stream.h>
410Sstevel@tonic-gate #include <sys/termio.h>
420Sstevel@tonic-gate #include <sys/errno.h>
430Sstevel@tonic-gate #include <sys/file.h>
440Sstevel@tonic-gate #include <sys/cmn_err.h>
450Sstevel@tonic-gate #include <sys/stropts.h>
460Sstevel@tonic-gate #include <sys/strsubr.h>
470Sstevel@tonic-gate #include <sys/strtty.h>
480Sstevel@tonic-gate #include <sys/debug.h>
490Sstevel@tonic-gate #include <sys/kbio.h>
500Sstevel@tonic-gate #include <sys/cred.h>
510Sstevel@tonic-gate #include <sys/stat.h>
520Sstevel@tonic-gate #include <sys/consdev.h>
530Sstevel@tonic-gate #include <sys/mkdev.h>
540Sstevel@tonic-gate #include <sys/kmem.h>
550Sstevel@tonic-gate #include <sys/cred.h>
560Sstevel@tonic-gate #include <sys/strsun.h>
570Sstevel@tonic-gate #ifdef DEBUG
580Sstevel@tonic-gate #include <sys/promif.h>
590Sstevel@tonic-gate #endif
600Sstevel@tonic-gate #include <sys/modctl.h>
610Sstevel@tonic-gate #include <sys/ddi.h>
620Sstevel@tonic-gate #include <sys/sunddi.h>
633359Smyers #include <sys/pci.h>
640Sstevel@tonic-gate #include <sys/asy.h>
650Sstevel@tonic-gate #include <sys/policy.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * set the RX FIFO trigger_level to half the RX FIFO size for now
690Sstevel@tonic-gate  * we may want to make this configurable later.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate static	int asy_trig_level = FIFO_TRIG_8;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate int asy_drain_check = 15000000;		/* tunable: exit drain check time */
740Sstevel@tonic-gate int asy_min_dtr_low = 500000;		/* tunable: minimum DTR down time */
750Sstevel@tonic-gate int asy_min_utbrk = 100000;		/* tunable: minumum untimed brk time */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate int asymaxchip = ASY16750;	/* tunable: limit chip support we look for */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Just in case someone has a chip with broken loopback mode, we provide a
810Sstevel@tonic-gate  * means to disable the loopback test. By default, we only loopback test
820Sstevel@tonic-gate  * UARTs which look like they have FIFOs bigger than 16 bytes.
830Sstevel@tonic-gate  * Set to 0 to suppress test, or to 2 to enable test on any size FIFO.
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate int asy_fifo_test = 1;		/* tunable: set to 0, 1, or 2 */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  * Allow ability to switch off testing of the scratch register.
890Sstevel@tonic-gate  * Some UART emulators might not have it. This will also disable the test
900Sstevel@tonic-gate  * for Exar/Startech ST16C650, as that requires use of the SCR register.
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate int asy_scr_test = 1;		/* tunable: set to 0 to disable SCR reg test */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * As we don't yet support on-chip flow control, it's a bad idea to put a
960Sstevel@tonic-gate  * large number of characters in the TX FIFO, since if other end tells us
970Sstevel@tonic-gate  * to stop transmitting, we can only stop filling the TX FIFO, but it will
980Sstevel@tonic-gate  * still carry on draining by itself, so remote end still gets what's left
990Sstevel@tonic-gate  * in the FIFO.
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate int asy_max_tx_fifo = 16;	/* tunable: max fill of TX FIFO */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #define	async_stopc	async_ttycommon.t_stopc
1040Sstevel@tonic-gate #define	async_startc	async_ttycommon.t_startc
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #define	ASY_INIT	1
1070Sstevel@tonic-gate #define	ASY_NOINIT	0
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /* enum value for sw and hw flow control action */
1100Sstevel@tonic-gate typedef enum {
1110Sstevel@tonic-gate 	FLOW_CHECK,
1120Sstevel@tonic-gate 	FLOW_STOP,
1130Sstevel@tonic-gate 	FLOW_START
1140Sstevel@tonic-gate } async_flowc_action;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate #ifdef DEBUG
1170Sstevel@tonic-gate #define	ASY_DEBUG_INIT	0x0001	/* Output msgs during driver initialization. */
1180Sstevel@tonic-gate #define	ASY_DEBUG_INPUT	0x0002	/* Report characters received during int. */
1190Sstevel@tonic-gate #define	ASY_DEBUG_EOT	0x0004	/* Output msgs when wait for xmit to finish. */
1200Sstevel@tonic-gate #define	ASY_DEBUG_CLOSE	0x0008	/* Output msgs when driver open/close called */
1210Sstevel@tonic-gate #define	ASY_DEBUG_HFLOW	0x0010	/* Output msgs when H/W flowcontrol is active */
1220Sstevel@tonic-gate #define	ASY_DEBUG_PROCS	0x0020	/* Output each proc name as it is entered. */
1230Sstevel@tonic-gate #define	ASY_DEBUG_STATE	0x0040	/* Output value of Interrupt Service Reg. */
1240Sstevel@tonic-gate #define	ASY_DEBUG_INTR	0x0080	/* Output value of Interrupt Service Reg. */
1250Sstevel@tonic-gate #define	ASY_DEBUG_OUT	0x0100	/* Output msgs about output events. */
1260Sstevel@tonic-gate #define	ASY_DEBUG_BUSY	0x0200	/* Output msgs when xmit is enabled/disabled */
1270Sstevel@tonic-gate #define	ASY_DEBUG_MODEM	0x0400	/* Output msgs about modem status & control. */
1280Sstevel@tonic-gate #define	ASY_DEBUG_MODM2	0x0800	/* Output msgs about modem status & control. */
1290Sstevel@tonic-gate #define	ASY_DEBUG_IOCTL	0x1000	/* Output msgs about ioctl messages. */
1300Sstevel@tonic-gate #define	ASY_DEBUG_CHIP	0x2000	/* Output msgs about chip identification. */
1310Sstevel@tonic-gate #define	ASY_DEBUG_SFLOW	0x4000	/* Output msgs when S/W flowcontrol is active */
1320Sstevel@tonic-gate #define	ASY_DEBUG(x) (debug & (x))
1330Sstevel@tonic-gate static	int debug  = 0;
1340Sstevel@tonic-gate #else
1350Sstevel@tonic-gate #define	ASY_DEBUG(x) B_FALSE
1360Sstevel@tonic-gate #endif
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /* pnpISA compressed device ids */
1390Sstevel@tonic-gate #define	pnpMTS0219 0xb6930219	/* Multitech MT5634ZTX modem */
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * PPS (Pulse Per Second) support.
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate void ddi_hardpps();
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate  * This is protected by the asy_excl_hi of the port on which PPS event
1470Sstevel@tonic-gate  * handling is enabled.  Note that only one port should have this enabled at
1480Sstevel@tonic-gate  * any one time.  Enabling PPS handling on multiple ports will result in
1490Sstevel@tonic-gate  * unpredictable (but benign) results.
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate static struct ppsclockev asy_ppsev;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate #ifdef PPSCLOCKLED
1540Sstevel@tonic-gate /* XXX Use these to observe PPS latencies and jitter on a scope */
1550Sstevel@tonic-gate #define	LED_ON
1560Sstevel@tonic-gate #define	LED_OFF
1570Sstevel@tonic-gate #else
1580Sstevel@tonic-gate #define	LED_ON
1590Sstevel@tonic-gate #define	LED_OFF
1600Sstevel@tonic-gate #endif
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate static	int max_asy_instance = -1;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static	uint_t	asysoftintr(caddr_t intarg);
1650Sstevel@tonic-gate static	uint_t	asyintr(caddr_t argasy);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate static boolean_t abort_charseq_recognize(uchar_t ch);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /* The async interrupt entry points */
1700Sstevel@tonic-gate static void	async_txint(struct asycom *asy);
1710Sstevel@tonic-gate static void	async_rxint(struct asycom *asy, uchar_t lsr);
1720Sstevel@tonic-gate static void	async_msint(struct asycom *asy);
1730Sstevel@tonic-gate static void	async_softint(struct asycom *asy);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate static void	async_ioctl(struct asyncline *async, queue_t *q, mblk_t *mp);
1760Sstevel@tonic-gate static void	async_reioctl(void *unit);
1770Sstevel@tonic-gate static void	async_iocdata(queue_t *q, mblk_t *mp);
1780Sstevel@tonic-gate static void	async_restart(void *arg);
1790Sstevel@tonic-gate static void	async_start(struct asyncline *async);
1800Sstevel@tonic-gate static void	async_nstart(struct asyncline *async, int mode);
1810Sstevel@tonic-gate static void	async_resume(struct asyncline *async);
1820Sstevel@tonic-gate static void	asy_program(struct asycom *asy, int mode);
1830Sstevel@tonic-gate static void	asyinit(struct asycom *asy);
1840Sstevel@tonic-gate static void	asy_waiteot(struct asycom *asy);
1851762Slt200341 static void	asyputchar(cons_polledio_arg_t, uchar_t c);
1861762Slt200341 static int	asygetchar(cons_polledio_arg_t);
1871762Slt200341 static boolean_t	asyischar(cons_polledio_arg_t);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate static int	asymctl(struct asycom *, int, int);
1900Sstevel@tonic-gate static int	asytodm(int, int);
1910Sstevel@tonic-gate static int	dmtoasy(int);
1920Sstevel@tonic-gate /*PRINTFLIKE2*/
1930Sstevel@tonic-gate static void	asyerror(int level, const char *fmt, ...) __KPRINTFLIKE(2);
1940Sstevel@tonic-gate static void	asy_parse_mode(dev_info_t *devi, struct asycom *asy);
1950Sstevel@tonic-gate static void	asy_soft_state_free(struct asycom *);
1960Sstevel@tonic-gate static char	*asy_hw_name(struct asycom *asy);
1970Sstevel@tonic-gate static void	async_hold_utbrk(void *arg);
1980Sstevel@tonic-gate static void	async_resume_utbrk(struct asyncline *async);
1990Sstevel@tonic-gate static void	async_dtr_free(struct asyncline *async);
2000Sstevel@tonic-gate static int	asy_identify_chip(dev_info_t *devi, struct asycom *asy);
2010Sstevel@tonic-gate static void	asy_reset_fifo(struct asycom *asy, uchar_t flags);
2020Sstevel@tonic-gate static int	asy_getproperty(dev_info_t *devi, struct asycom *asy,
2030Sstevel@tonic-gate 		    const char *property);
2040Sstevel@tonic-gate static boolean_t	async_flowcontrol_sw_input(struct asycom *asy,
2050Sstevel@tonic-gate 			    async_flowc_action onoff, int type);
2060Sstevel@tonic-gate static void	async_flowcontrol_sw_output(struct asycom *asy,
2070Sstevel@tonic-gate 		    async_flowc_action onoff);
2080Sstevel@tonic-gate static void	async_flowcontrol_hw_input(struct asycom *asy,
2090Sstevel@tonic-gate 		    async_flowc_action onoff, int type);
2100Sstevel@tonic-gate static void	async_flowcontrol_hw_output(struct asycom *asy,
2110Sstevel@tonic-gate 		    async_flowc_action onoff);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate #define	GET_PROP(devi, pname, pflag, pval, plen) \
2140Sstevel@tonic-gate 		(ddi_prop_op(DDI_DEV_T_ANY, (devi), PROP_LEN_AND_VAL_BUF, \
2150Sstevel@tonic-gate 		(pflag), (pname), (caddr_t)(pval), (plen)))
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate static ddi_iblock_cookie_t asy_soft_iblock;
2180Sstevel@tonic-gate ddi_softintr_t asy_softintr_id;
2190Sstevel@tonic-gate static	int asy_addedsoft = 0;
2200Sstevel@tonic-gate int	asysoftpend;	/* soft interrupt pending */
2210Sstevel@tonic-gate kmutex_t asy_soft_lock;	/* lock protecting asysoftpend */
2220Sstevel@tonic-gate kmutex_t asy_glob_lock; /* lock protecting global data manipulation */
2230Sstevel@tonic-gate void *asy_soft_state;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /* Standard COM port I/O addresses */
2260Sstevel@tonic-gate static const int standard_com_ports[] = {
2270Sstevel@tonic-gate 	COM1_IOADDR, COM2_IOADDR, COM3_IOADDR, COM4_IOADDR
2280Sstevel@tonic-gate };
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate static int *com_ports;
2310Sstevel@tonic-gate static uint_t num_com_ports;
2320Sstevel@tonic-gate 
2335295Srandyf #ifdef	DEBUG
2345295Srandyf /*
2355295Srandyf  * Set this to true to make the driver pretend to do a suspend.  Useful
2365295Srandyf  * for debugging suspend/resume code with a serial debugger.
2375295Srandyf  */
2385295Srandyf boolean_t	asy_nosuspend = B_FALSE;
2395295Srandyf #endif
2405295Srandyf 
2415295Srandyf 
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate  * Baud rate table. Indexed by #defines found in sys/termios.h
2440Sstevel@tonic-gate  */
2450Sstevel@tonic-gate ushort_t asyspdtab[] = {
2460Sstevel@tonic-gate 	0,	/* 0 baud rate */
2470Sstevel@tonic-gate 	0x900,	/* 50 baud rate */
2480Sstevel@tonic-gate 	0x600,	/* 75 baud rate */
2490Sstevel@tonic-gate 	0x417,	/* 110 baud rate (%0.026) */
2500Sstevel@tonic-gate 	0x359,	/* 134 baud rate (%0.058) */
2510Sstevel@tonic-gate 	0x300,	/* 150 baud rate */
2520Sstevel@tonic-gate 	0x240,	/* 200 baud rate */
2530Sstevel@tonic-gate 	0x180,	/* 300 baud rate */
2540Sstevel@tonic-gate 	0x0c0,	/* 600 baud rate */
2550Sstevel@tonic-gate 	0x060,	/* 1200 baud rate */
2560Sstevel@tonic-gate 	0x040,	/* 1800 baud rate */
2570Sstevel@tonic-gate 	0x030,	/* 2400 baud rate */
2580Sstevel@tonic-gate 	0x018,	/* 4800 baud rate */
2590Sstevel@tonic-gate 	0x00c,	/* 9600 baud rate */
2600Sstevel@tonic-gate 	0x006,	/* 19200 baud rate */
2610Sstevel@tonic-gate 	0x003,	/* 38400 baud rate */
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	0x002,	/* 57600 baud rate */
2640Sstevel@tonic-gate 	0x0,	/* 76800 baud rate not supported */
2650Sstevel@tonic-gate 	0x001,	/* 115200 baud rate */
2660Sstevel@tonic-gate 	0x0,	/* 153600 baud rate not supported */
2670Sstevel@tonic-gate 	0x0,	/* 0x8002 (SMC chip) 230400 baud rate not supported */
2680Sstevel@tonic-gate 	0x0,	/* 307200 baud rate not supported */
2690Sstevel@tonic-gate 	0x0,	/* 0x8001 (SMC chip) 460800 baud rate not supported */
2700Sstevel@tonic-gate 	0x0,	/* unused */
2710Sstevel@tonic-gate 	0x0,	/* unused */
2720Sstevel@tonic-gate 	0x0,	/* unused */
2730Sstevel@tonic-gate 	0x0,	/* unused */
2740Sstevel@tonic-gate 	0x0,	/* unused */
2750Sstevel@tonic-gate 	0x0,	/* unused */
2760Sstevel@tonic-gate 	0x0,	/* unused */
2770Sstevel@tonic-gate 	0x0,	/* unused */
2780Sstevel@tonic-gate 	0x0,	/* unused */
2790Sstevel@tonic-gate };
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate static int asyrsrv(queue_t *q);
2820Sstevel@tonic-gate static int asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr);
2830Sstevel@tonic-gate static int asyclose(queue_t *q, int flag, cred_t *credp);
2845295Srandyf static int asywputdo(queue_t *q, mblk_t *mp, boolean_t);
2850Sstevel@tonic-gate static int asywput(queue_t *q, mblk_t *mp);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate struct module_info asy_info = {
2880Sstevel@tonic-gate 	0,
2890Sstevel@tonic-gate 	"asy",
2900Sstevel@tonic-gate 	0,
2910Sstevel@tonic-gate 	INFPSZ,
2920Sstevel@tonic-gate 	4096,
2930Sstevel@tonic-gate 	128
2940Sstevel@tonic-gate };
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate static struct qinit asy_rint = {
2970Sstevel@tonic-gate 	putq,
2980Sstevel@tonic-gate 	asyrsrv,
2990Sstevel@tonic-gate 	asyopen,
3000Sstevel@tonic-gate 	asyclose,
3010Sstevel@tonic-gate 	NULL,
3020Sstevel@tonic-gate 	&asy_info,
3030Sstevel@tonic-gate 	NULL
3040Sstevel@tonic-gate };
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate static struct qinit asy_wint = {
3070Sstevel@tonic-gate 	asywput,
3080Sstevel@tonic-gate 	NULL,
3090Sstevel@tonic-gate 	NULL,
3100Sstevel@tonic-gate 	NULL,
3110Sstevel@tonic-gate 	NULL,
3120Sstevel@tonic-gate 	&asy_info,
3130Sstevel@tonic-gate 	NULL
3140Sstevel@tonic-gate };
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate struct streamtab asy_str_info = {
3170Sstevel@tonic-gate 	&asy_rint,
3180Sstevel@tonic-gate 	&asy_wint,
3190Sstevel@tonic-gate 	NULL,
3200Sstevel@tonic-gate 	NULL
3210Sstevel@tonic-gate };
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate static int asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
3240Sstevel@tonic-gate 		void **result);
3250Sstevel@tonic-gate static int asyprobe(dev_info_t *);
3260Sstevel@tonic-gate static int asyattach(dev_info_t *, ddi_attach_cmd_t);
3270Sstevel@tonic-gate static int asydetach(dev_info_t *, ddi_detach_cmd_t);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate static 	struct cb_ops cb_asy_ops = {
3300Sstevel@tonic-gate 	nodev,			/* cb_open */
3310Sstevel@tonic-gate 	nodev,			/* cb_close */
3320Sstevel@tonic-gate 	nodev,			/* cb_strategy */
3330Sstevel@tonic-gate 	nodev,			/* cb_print */
3340Sstevel@tonic-gate 	nodev,			/* cb_dump */
3350Sstevel@tonic-gate 	nodev,			/* cb_read */
3360Sstevel@tonic-gate 	nodev,			/* cb_write */
3370Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
3380Sstevel@tonic-gate 	nodev,			/* cb_devmap */
3390Sstevel@tonic-gate 	nodev,			/* cb_mmap */
3400Sstevel@tonic-gate 	nodev,			/* cb_segmap */
3410Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
3420Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
3430Sstevel@tonic-gate 	&asy_str_info,		/* cb_stream */
3440Sstevel@tonic-gate 	D_MP			/* cb_flag */
3450Sstevel@tonic-gate };
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate struct dev_ops asy_ops = {
3480Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
3490Sstevel@tonic-gate 	0,			/* devo_refcnt */
3500Sstevel@tonic-gate 	asyinfo,		/* devo_getinfo */
3510Sstevel@tonic-gate 	nulldev,		/* devo_identify */
3520Sstevel@tonic-gate 	asyprobe,		/* devo_probe */
3530Sstevel@tonic-gate 	asyattach,		/* devo_attach */
3540Sstevel@tonic-gate 	asydetach,		/* devo_detach */
3550Sstevel@tonic-gate 	nodev,			/* devo_reset */
3560Sstevel@tonic-gate 	&cb_asy_ops,		/* devo_cb_ops */
3570Sstevel@tonic-gate };
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate static struct modldrv modldrv = {
3600Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a driver */
3610Sstevel@tonic-gate 	"ASY driver %I%",
3620Sstevel@tonic-gate 	&asy_ops,	/* driver ops */
3630Sstevel@tonic-gate };
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate static struct modlinkage modlinkage = {
3660Sstevel@tonic-gate 	MODREV_1,
3670Sstevel@tonic-gate 	(void *)&modldrv,
3680Sstevel@tonic-gate 	NULL
3690Sstevel@tonic-gate };
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate int
3720Sstevel@tonic-gate _init(void)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	int i;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	i = ddi_soft_state_init(&asy_soft_state, sizeof (struct asycom), 2);
3770Sstevel@tonic-gate 	if (i == 0) {
3780Sstevel@tonic-gate 		mutex_init(&asy_glob_lock, NULL, MUTEX_DRIVER, NULL);
3790Sstevel@tonic-gate 		if ((i = mod_install(&modlinkage)) != 0) {
3800Sstevel@tonic-gate 			mutex_destroy(&asy_glob_lock);
3810Sstevel@tonic-gate 			ddi_soft_state_fini(&asy_soft_state);
3820Sstevel@tonic-gate 		} else {
3830Sstevel@tonic-gate 			DEBUGCONT2(ASY_DEBUG_INIT, "%s, debug = %x\n",
3840Sstevel@tonic-gate 			    modldrv.drv_linkinfo, debug);
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 	return (i);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate int
3910Sstevel@tonic-gate _fini(void)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	int i;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	if ((i = mod_remove(&modlinkage)) == 0) {
3960Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_INIT, "%s unloading\n",
3970Sstevel@tonic-gate 		    modldrv.drv_linkinfo);
3980Sstevel@tonic-gate 		ASSERT(max_asy_instance == -1);
3990Sstevel@tonic-gate 		mutex_destroy(&asy_glob_lock);
4000Sstevel@tonic-gate 		if (asy_addedsoft)
4010Sstevel@tonic-gate 			ddi_remove_softintr(asy_softintr_id);
4020Sstevel@tonic-gate 		asy_addedsoft = 0;
4030Sstevel@tonic-gate 		/* free "motherboard-serial-ports" property if allocated */
4040Sstevel@tonic-gate 		if (com_ports != NULL && com_ports != (int *)standard_com_ports)
4055295Srandyf 			ddi_prop_free(com_ports);
4060Sstevel@tonic-gate 		com_ports = NULL;
4070Sstevel@tonic-gate 		mutex_destroy(&asy_soft_lock);
4080Sstevel@tonic-gate 		ddi_soft_state_fini(&asy_soft_state);
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 	return (i);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate int
4140Sstevel@tonic-gate _info(struct modinfo *modinfop)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4195295Srandyf void
4205295Srandyf async_put_suspq(struct asycom *asy, mblk_t *mp)
4215295Srandyf {
4225295Srandyf 	struct asyncline *async = asy->asy_priv;
4235295Srandyf 
4245295Srandyf 	ASSERT(mutex_owned(&asy->asy_excl));
4255295Srandyf 
4265295Srandyf 	if (async->async_suspqf == NULL)
4275295Srandyf 		async->async_suspqf = mp;
4285295Srandyf 	else
4295295Srandyf 		async->async_suspqb->b_next = mp;
4305295Srandyf 
4315295Srandyf 	async->async_suspqb = mp;
4325295Srandyf }
4335295Srandyf 
4345295Srandyf static mblk_t *
4355295Srandyf async_get_suspq(struct asycom *asy)
4365295Srandyf {
4375295Srandyf 	struct asyncline *async = asy->asy_priv;
4385295Srandyf 	mblk_t *mp;
4395295Srandyf 
4405295Srandyf 	ASSERT(mutex_owned(&asy->asy_excl));
4415295Srandyf 
4425295Srandyf 	if ((mp = async->async_suspqf) != NULL) {
4435295Srandyf 		async->async_suspqf = mp->b_next;
4445295Srandyf 		mp->b_next = NULL;
4455295Srandyf 	} else {
4465295Srandyf 		async->async_suspqb = NULL;
4475295Srandyf 	}
4485295Srandyf 	return (mp);
4495295Srandyf }
4505295Srandyf 
4515295Srandyf static void
4525295Srandyf async_process_suspq(struct asycom *asy)
4535295Srandyf {
4545295Srandyf 	struct asyncline *async = asy->asy_priv;
4555295Srandyf 	mblk_t *mp;
4565295Srandyf 
4575295Srandyf 	ASSERT(mutex_owned(&asy->asy_excl));
4585295Srandyf 
4595295Srandyf 	while ((mp = async_get_suspq(asy)) != NULL) {
4605295Srandyf 		queue_t *q;
4615295Srandyf 
4625295Srandyf 		q = async->async_ttycommon.t_writeq;
4635295Srandyf 		ASSERT(q != NULL);
4645295Srandyf 		mutex_exit(&asy->asy_excl);
4655295Srandyf 		(void) asywputdo(q, mp, B_FALSE);
4665295Srandyf 		mutex_enter(&asy->asy_excl);
4675295Srandyf 	}
4685295Srandyf 	async->async_flags &= ~ASYNC_DDI_SUSPENDED;
4695295Srandyf 	cv_broadcast(&async->async_flags_cv);
4705295Srandyf }
4715295Srandyf 
4720Sstevel@tonic-gate static int
4733359Smyers asy_get_bus_type(dev_info_t *devinfo)
4743359Smyers {
4753359Smyers 	char	parent_type[16];
4763359Smyers 	int	parentlen;
4773359Smyers 
4783359Smyers 	parentlen = sizeof (parent_type);
4793359Smyers 
4803359Smyers 	if (ddi_prop_op(DDI_DEV_T_ANY, devinfo, PROP_LEN_AND_VAL_BUF, 0,
4813359Smyers 	    "device_type", (caddr_t)parent_type, &parentlen)
4823359Smyers 	    != DDI_PROP_SUCCESS && ddi_prop_op(DDI_DEV_T_ANY, devinfo,
4833359Smyers 	    PROP_LEN_AND_VAL_BUF, 0, "bus-type", (caddr_t)parent_type,
4843359Smyers 	    &parentlen) != DDI_PROP_SUCCESS) {
4853359Smyers 			cmn_err(CE_WARN,
4863359Smyers 			    "asy: can't figure out device type for"
4873359Smyers 			    " parent \"%s\"",
4883359Smyers 			    ddi_get_name(ddi_get_parent(devinfo)));
4893359Smyers 			return (ASY_BUS_UNKNOWN);
4903359Smyers 	}
4913359Smyers 	if (strcmp(parent_type, "isa") == 0)
4923359Smyers 		return (ASY_BUS_ISA);
4933359Smyers 	else if (strcmp(parent_type, "pci") == 0)
4943359Smyers 		return (ASY_BUS_PCI);
4953359Smyers 	else
4963359Smyers 		return (ASY_BUS_UNKNOWN);
4973359Smyers }
4983359Smyers 
4993359Smyers static int
5003359Smyers asy_get_io_regnum_pci(dev_info_t *devi, struct asycom *asy)
5013359Smyers {
5023359Smyers 	int reglen, nregs;
5033359Smyers 	int regnum, i;
5043359Smyers 	uint64_t size;
5053359Smyers 	struct pci_phys_spec *reglist;
5063359Smyers 
5073359Smyers 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
5083359Smyers 	    "reg", (caddr_t)&reglist, &reglen) != DDI_PROP_SUCCESS) {
5093359Smyers 		cmn_err(CE_WARN, "asy_get_io_regnum_pci: reg property"
5103359Smyers 		    " not found in devices property list");
5113359Smyers 		return (-1);
5123359Smyers 	}
5133359Smyers 
5143359Smyers 	/*
5153359Smyers 	 * PCI devices are assumed to not have broken FIFOs;
5163359Smyers 	 * Agere/Lucent Venus PCI modem chipsets are an example
5173359Smyers 	 */
5183359Smyers 	if (asy)
5193359Smyers 		asy->asy_flags2 |= ASY2_NO_LOOPBACK;
5203359Smyers 
5213359Smyers 	regnum = -1;
5223359Smyers 	nregs = reglen / sizeof (*reglist);
5233359Smyers 	for (i = 0; i < nregs; i++) {
5243359Smyers 		switch (reglist[i].pci_phys_hi & PCI_ADDR_MASK) {
5253359Smyers 		case PCI_ADDR_IO:		/* I/O bus reg property */
5263359Smyers 			if (regnum == -1) /* use only the first one */
5273359Smyers 				regnum = i;
5283359Smyers 			break;
5293359Smyers 
5303359Smyers 		default:
5313359Smyers 			break;
5323359Smyers 		}
5333359Smyers 	}
5343359Smyers 
5353359Smyers 	/* check for valid count of registers */
5363359Smyers 	if (regnum >= 0) {
5373359Smyers 		size = ((uint64_t)reglist[regnum].pci_size_low) |
5383359Smyers 		    ((uint64_t)reglist[regnum].pci_size_hi) << 32;
5393359Smyers 		if (size < 8)
5403359Smyers 			regnum = -1;
5413359Smyers 	}
5423359Smyers 	kmem_free(reglist, reglen);
5433359Smyers 	return (regnum);
5443359Smyers }
5453359Smyers 
5463359Smyers static int
5473359Smyers asy_get_io_regnum_isa(dev_info_t *devi, struct asycom *asy)
5483359Smyers {
5493359Smyers 	int reglen, nregs;
5503359Smyers 	int regnum, i;
5513359Smyers 	struct {
5523359Smyers 		uint_t bustype;
5533359Smyers 		int base;
5543359Smyers 		int size;
5553359Smyers 	} *reglist;
5563359Smyers 
5573359Smyers 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
5583359Smyers 	    "reg", (caddr_t)&reglist, &reglen) != DDI_PROP_SUCCESS) {
5593359Smyers 		cmn_err(CE_WARN, "asy_get_io_regnum: reg property not found "
5605295Srandyf 		    "in devices property list");
5613359Smyers 		return (-1);
5623359Smyers 	}
5633359Smyers 
5643359Smyers 	regnum = -1;
5653359Smyers 	nregs = reglen / sizeof (*reglist);
5663359Smyers 	for (i = 0; i < nregs; i++) {
5673359Smyers 		switch (reglist[i].bustype) {
5683359Smyers 		case 1:			/* I/O bus reg property */
5693359Smyers 			if (regnum == -1) /* only use the first one */
5703359Smyers 				regnum = i;
5713359Smyers 			break;
5723359Smyers 
5733359Smyers 		case pnpMTS0219:	/* Multitech MT5634ZTX modem */
5743359Smyers 			/* Venus chipset can't do loopback test */
5753359Smyers 			if (asy)
5763359Smyers 				asy->asy_flags2 |= ASY2_NO_LOOPBACK;
5773359Smyers 			break;
5783359Smyers 
5793359Smyers 		default:
5803359Smyers 			break;
5813359Smyers 		}
5823359Smyers 	}
5833359Smyers 
5843359Smyers 	/* check for valid count of registers */
5853359Smyers 	if ((regnum < 0) || (reglist[regnum].size < 8))
5863359Smyers 		regnum = -1;
5873359Smyers 	kmem_free(reglist, reglen);
5883359Smyers 	return (regnum);
5893359Smyers }
5903359Smyers 
5913359Smyers static int
5923359Smyers asy_get_io_regnum(dev_info_t *devinfo, struct asycom *asy)
5933359Smyers {
5943359Smyers 	switch (asy_get_bus_type(devinfo)) {
5953359Smyers 	case ASY_BUS_ISA:
5963359Smyers 		return (asy_get_io_regnum_isa(devinfo, asy));
5973359Smyers 	case ASY_BUS_PCI:
5983359Smyers 		return (asy_get_io_regnum_pci(devinfo, asy));
5993359Smyers 	default:
6003359Smyers 		return (-1);
6013359Smyers 	}
6023359Smyers }
6033359Smyers 
6043359Smyers static int
6050Sstevel@tonic-gate asydetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate 	int instance;
6080Sstevel@tonic-gate 	struct asycom *asy;
6090Sstevel@tonic-gate 	struct asyncline *async;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	instance = ddi_get_instance(devi);	/* find out which unit */
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, instance);
6140Sstevel@tonic-gate 	if (asy == NULL)
6150Sstevel@tonic-gate 		return (DDI_FAILURE);
6160Sstevel@tonic-gate 	async = asy->asy_priv;
6170Sstevel@tonic-gate 
6185295Srandyf 	switch (cmd) {
6195295Srandyf 	case DDI_DETACH:
6205295Srandyf 		DEBUGNOTE2(ASY_DEBUG_INIT, "asy%d: %s shutdown.",
6215295Srandyf 		    instance, asy_hw_name(asy));
6225295Srandyf 
6235295Srandyf 		/* cancel DTR hold timeout */
6245295Srandyf 		if (async->async_dtrtid != 0) {
6255295Srandyf 			(void) untimeout(async->async_dtrtid);
6265295Srandyf 			async->async_dtrtid = 0;
6275295Srandyf 		}
6285295Srandyf 
6295295Srandyf 		/* remove all minor device node(s) for this device */
6305295Srandyf 		ddi_remove_minor_node(devi, NULL);
6315295Srandyf 
6325295Srandyf 		mutex_destroy(&asy->asy_excl);
6335295Srandyf 		mutex_destroy(&asy->asy_excl_hi);
6345295Srandyf 		cv_destroy(&async->async_flags_cv);
6355295Srandyf 		ddi_remove_intr(devi, 0, asy->asy_iblock);
6365295Srandyf 		ddi_regs_map_free(&asy->asy_iohandle);
6375295Srandyf 		asy_soft_state_free(asy);
6385295Srandyf 		DEBUGNOTE1(ASY_DEBUG_INIT, "asy%d: shutdown complete",
6395295Srandyf 		    instance);
6405295Srandyf 		break;
6415295Srandyf 	case DDI_SUSPEND:
6425295Srandyf 		{
6435295Srandyf 		unsigned i;
6445295Srandyf 		uchar_t lsr;
6455295Srandyf 
6465295Srandyf #ifdef	DEBUG
6475295Srandyf 		if (asy_nosuspend)
6485295Srandyf 			return (DDI_SUCCESS);
6495295Srandyf #endif
6505295Srandyf 		mutex_enter(&asy->asy_excl);
6515295Srandyf 
6525295Srandyf 		ASSERT(async->async_ops >= 0);
6535295Srandyf 		while (async->async_ops > 0)
6545295Srandyf 			cv_wait(&async->async_ops_cv, &asy->asy_excl);
6555295Srandyf 
6565295Srandyf 		async->async_flags |= ASYNC_DDI_SUSPENDED;
6575295Srandyf 
6585295Srandyf 		/* Wait for timed break and delay to complete */
6595295Srandyf 		while ((async->async_flags & (ASYNC_BREAK|ASYNC_DELAY))) {
6605295Srandyf 			if (cv_wait_sig(&async->async_flags_cv, &asy->asy_excl)
6615295Srandyf 			    == 0) {
6625295Srandyf 				async_process_suspq(asy);
6635295Srandyf 				mutex_exit(&asy->asy_excl);
6645295Srandyf 				return (DDI_FAILURE);
6655295Srandyf 			}
6665295Srandyf 		}
6675295Srandyf 
6685295Srandyf 		/* Clear untimed break */
6695295Srandyf 		if (async->async_flags & ASYNC_OUT_SUSPEND)
6705295Srandyf 			async_resume_utbrk(async);
6715295Srandyf 
6725295Srandyf 		mutex_exit(&asy->asy_excl);
6735295Srandyf 
6745295Srandyf 		mutex_enter(&asy->asy_soft_sr);
6755295Srandyf 		mutex_enter(&asy->asy_excl);
6765295Srandyf 		if (async->async_wbufcid != 0) {
6775295Srandyf 			bufcall_id_t bcid = async->async_wbufcid;
6785295Srandyf 			async->async_wbufcid = 0;
6795295Srandyf 			async->async_flags |= ASYNC_RESUME_BUFCALL;
6805295Srandyf 			mutex_exit(&asy->asy_excl);
6815295Srandyf 			unbufcall(bcid);
6825295Srandyf 			mutex_enter(&asy->asy_excl);
6835295Srandyf 		}
6845295Srandyf 		mutex_enter(&asy->asy_excl_hi);
6855295Srandyf 
6865295Srandyf 		/* Disable interrupts from chip */
6875295Srandyf 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0);
6885295Srandyf 		asy->asy_flags |= ASY_DDI_SUSPENDED;
6895295Srandyf 
6905295Srandyf 		/* Process remaining RX characters and RX errors, if any */
6915295Srandyf 		lsr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR);
6925295Srandyf 		async_rxint(asy, lsr);
6935295Srandyf 
6945295Srandyf 		/* Wait for TX to drain */
6955295Srandyf 		for (i = 1000; i > 0; i--) {
6965295Srandyf 			lsr = ddi_get8(asy->asy_iohandle,
6975295Srandyf 			    asy->asy_ioaddr + LSR);
6985295Srandyf 			if ((lsr & (XSRE | XHRE)) == (XSRE | XHRE))
6995295Srandyf 				break;
7005295Srandyf 			delay(drv_usectohz(10000));
7015295Srandyf 		}
7025295Srandyf 		if (i == 0)
7035295Srandyf 			cmn_err(CE_WARN,
7045295Srandyf 			    "asy: transmitter wasn't drained before "
7055295Srandyf 			    "driver was suspended");
7065295Srandyf 
7075295Srandyf 		mutex_exit(&asy->asy_excl_hi);
7085295Srandyf 		mutex_exit(&asy->asy_excl);
7095295Srandyf 		mutex_exit(&asy->asy_soft_sr);
7105295Srandyf 		break;
7110Sstevel@tonic-gate 	}
7125295Srandyf 	default:
7135295Srandyf 		return (DDI_FAILURE);
7145295Srandyf 	}
7155295Srandyf 
7160Sstevel@tonic-gate 	return (DDI_SUCCESS);
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate /*
7200Sstevel@tonic-gate  * asyprobe
7210Sstevel@tonic-gate  * We don't bother probing for the hardware, as since Solaris 2.6, device
7220Sstevel@tonic-gate  * nodes are only created for auto-detected hardware or nodes explicitly
7230Sstevel@tonic-gate  * created by the user, e.g. via the DCA. However, we should check the
7240Sstevel@tonic-gate  * device node is at least vaguely usable, i.e. we have a block of 8 i/o
7250Sstevel@tonic-gate  * ports. This prevents attempting to attach to bogus serial ports which
7260Sstevel@tonic-gate  * some BIOSs still partially report when they are disabled in the BIOS.
7270Sstevel@tonic-gate  */
7280Sstevel@tonic-gate static int
7290Sstevel@tonic-gate asyprobe(dev_info_t *devi)
7300Sstevel@tonic-gate {
7313359Smyers 	return ((asy_get_io_regnum(devi, NULL) < 0) ?
7323359Smyers 	    DDI_PROBE_FAILURE : DDI_PROBE_DONTCARE);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate static int
7360Sstevel@tonic-gate asyattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7370Sstevel@tonic-gate {
7380Sstevel@tonic-gate 	int instance;
7390Sstevel@tonic-gate 	int mcr;
7400Sstevel@tonic-gate 	int ret;
7410Sstevel@tonic-gate 	int regnum = 0;
7420Sstevel@tonic-gate 	int i;
7430Sstevel@tonic-gate 	struct asycom *asy;
7443359Smyers 	char name[ASY_MINOR_LEN];
7450Sstevel@tonic-gate 	int status;
7460Sstevel@tonic-gate 	static ddi_device_acc_attr_t ioattr = {
7470Sstevel@tonic-gate 		DDI_DEVICE_ATTR_V0,
7480Sstevel@tonic-gate 		DDI_NEVERSWAP_ACC,
7490Sstevel@tonic-gate 		DDI_STRICTORDER_ACC,
7500Sstevel@tonic-gate 	};
7510Sstevel@tonic-gate 
7525295Srandyf 	instance = ddi_get_instance(devi);	/* find out which unit */
7535295Srandyf 
7545295Srandyf 	switch (cmd) {
7555295Srandyf 	case DDI_ATTACH:
7565295Srandyf 		break;
7575295Srandyf 	case DDI_RESUME:
7585295Srandyf 	{
7595295Srandyf 		struct asyncline *async;
7605295Srandyf 
7615295Srandyf #ifdef	DEBUG
7625295Srandyf 		if (asy_nosuspend)
7635295Srandyf 			return (DDI_SUCCESS);
7645295Srandyf #endif
7655295Srandyf 		asy = ddi_get_soft_state(asy_soft_state, instance);
7665295Srandyf 		if (asy == NULL)
7675295Srandyf 			return (DDI_FAILURE);
7685295Srandyf 
7695295Srandyf 		mutex_enter(&asy->asy_soft_sr);
7705295Srandyf 		mutex_enter(&asy->asy_excl);
7715295Srandyf 		mutex_enter(&asy->asy_excl_hi);
7725295Srandyf 
7735295Srandyf 		async = asy->asy_priv;
7745295Srandyf 		/* Disable interrupts */
7755295Srandyf 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0);
7765295Srandyf 		if (asy_identify_chip(devi, asy) != DDI_SUCCESS) {
7775295Srandyf 			mutex_exit(&asy->asy_excl_hi);
7785295Srandyf 			mutex_exit(&asy->asy_excl);
7795295Srandyf 			mutex_exit(&asy->asy_soft_sr);
7805295Srandyf 			cmn_err(CE_WARN, "Cannot identify UART chip at %p\n",
7815295Srandyf 			    (void *)asy->asy_ioaddr);
7825295Srandyf 			return (DDI_FAILURE);
7835295Srandyf 		}
7845295Srandyf 		asy->asy_flags &= ~ASY_DDI_SUSPENDED;
7855295Srandyf 		if (async->async_flags & ASYNC_ISOPEN) {
7865295Srandyf 			asy_program(asy, ASY_INIT);
7875295Srandyf 			/* Kick off output */
7885295Srandyf 			if (async->async_ocnt > 0) {
7895295Srandyf 				async_resume(async);
7905295Srandyf 			} else {
7915295Srandyf 				mutex_exit(&asy->asy_excl_hi);
7925295Srandyf 				if (async->async_xmitblk)
7935295Srandyf 					freeb(async->async_xmitblk);
7945295Srandyf 				async->async_xmitblk = NULL;
7955295Srandyf 				async_start(async);
7965295Srandyf 				mutex_enter(&asy->asy_excl_hi);
7975295Srandyf 			}
7985295Srandyf 			ASYSETSOFT(asy);
7995295Srandyf 		}
8005295Srandyf 		mutex_exit(&asy->asy_excl_hi);
8015295Srandyf 		mutex_exit(&asy->asy_excl);
8025295Srandyf 		mutex_exit(&asy->asy_soft_sr);
8035295Srandyf 
8045295Srandyf 		mutex_enter(&asy->asy_excl);
8055295Srandyf 		if (async->async_flags & ASYNC_RESUME_BUFCALL) {
8065295Srandyf 			async->async_wbufcid = bufcall(async->async_wbufcds,
8075295Srandyf 			    BPRI_HI, (void (*)(void *)) async_reioctl,
8085295Srandyf 			    (void *)(intptr_t)async->async_common->asy_unit);
8095295Srandyf 			async->async_flags &= ~ASYNC_RESUME_BUFCALL;
8105295Srandyf 		}
8115295Srandyf 		async_process_suspq(asy);
8125295Srandyf 		mutex_exit(&asy->asy_excl);
8135295Srandyf 		return (DDI_SUCCESS);
8145295Srandyf 	}
8155295Srandyf 	default:
8160Sstevel@tonic-gate 		return (DDI_FAILURE);
8175295Srandyf 	}
8185295Srandyf 
8190Sstevel@tonic-gate 	ret = ddi_soft_state_zalloc(asy_soft_state, instance);
8200Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
8210Sstevel@tonic-gate 		return (DDI_FAILURE);
8220Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, instance);
8230Sstevel@tonic-gate 	ASSERT(asy != NULL);	/* can't fail - we only just allocated it */
8240Sstevel@tonic-gate 	asy->asy_unit = instance;
8250Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
8260Sstevel@tonic-gate 	if (instance > max_asy_instance)
8270Sstevel@tonic-gate 		max_asy_instance = instance;
8280Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
8290Sstevel@tonic-gate 
8303359Smyers 	regnum = asy_get_io_regnum(devi, asy);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	if (regnum < 0 ||
8330Sstevel@tonic-gate 	    ddi_regs_map_setup(devi, regnum, (caddr_t *)&asy->asy_ioaddr,
8340Sstevel@tonic-gate 	    (offset_t)0, (offset_t)0, &ioattr, &asy->asy_iohandle)
8350Sstevel@tonic-gate 	    != DDI_SUCCESS) {
8360Sstevel@tonic-gate 		cmn_err(CE_WARN, "asy%d: could not map UART registers @ %p",
8370Sstevel@tonic-gate 		    instance, (void *)asy->asy_ioaddr);
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 		asy_soft_state_free(asy);
8400Sstevel@tonic-gate 		return (DDI_FAILURE);
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_INIT, "asy%dattach: UART @ %p\n",
8440Sstevel@tonic-gate 	    instance, (void *)asy->asy_ioaddr);
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
8470Sstevel@tonic-gate 	if (com_ports == NULL) {	/* need to initialize com_ports */
8480Sstevel@tonic-gate 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, devi, 0,
8490Sstevel@tonic-gate 		    "motherboard-serial-ports", &com_ports, &num_com_ports) !=
8500Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
8510Sstevel@tonic-gate 			/* Use our built-in COM[1234] values */
8520Sstevel@tonic-gate 			com_ports = (int *)standard_com_ports;
8530Sstevel@tonic-gate 			num_com_ports = sizeof (standard_com_ports) /
8540Sstevel@tonic-gate 			    sizeof (standard_com_ports[0]);
8550Sstevel@tonic-gate 		}
8560Sstevel@tonic-gate 		if (num_com_ports > 10) {
8570Sstevel@tonic-gate 			/* We run out of single digits for device properties */
8580Sstevel@tonic-gate 			num_com_ports = 10;
8590Sstevel@tonic-gate 			cmn_err(CE_WARN,
8600Sstevel@tonic-gate 			    "More than %d motherboard-serial-ports",
8610Sstevel@tonic-gate 			    num_com_ports);
8620Sstevel@tonic-gate 		}
8630Sstevel@tonic-gate 	}
8640Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	/*
8670Sstevel@tonic-gate 	 * Lookup the i/o address to see if this is a standard COM port
8680Sstevel@tonic-gate 	 * in which case we assign it the correct tty[a-d] to match the
8690Sstevel@tonic-gate 	 * COM port number, or some other i/o address in which case it
8700Sstevel@tonic-gate 	 * will be assigned /dev/term/[0123...] in some rather arbitrary
8710Sstevel@tonic-gate 	 * fashion.
8720Sstevel@tonic-gate 	 */
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	for (i = 0; i < num_com_ports; i++) {
8750Sstevel@tonic-gate 		if (asy->asy_ioaddr == (uint8_t *)(uintptr_t)com_ports[i]) {
8760Sstevel@tonic-gate 			asy->asy_com_port = i + 1;
8770Sstevel@tonic-gate 			break;
8780Sstevel@tonic-gate 		}
8790Sstevel@tonic-gate 	}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	/*
8820Sstevel@tonic-gate 	 * It appears that there was async hardware that on reset
8830Sstevel@tonic-gate 	 * did not clear ICR.  Hence when we get to
8840Sstevel@tonic-gate 	 * ddi_get_iblock_cookie below, this hardware would cause
8850Sstevel@tonic-gate 	 * the system to hang if there was input available.
8860Sstevel@tonic-gate 	 */
8870Sstevel@tonic-gate 
8881106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0x00);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	/* establish default usage */
8910Sstevel@tonic-gate 	asy->asy_mcr |= RTS|DTR;		/* do use RTS/DTR after open */
8920Sstevel@tonic-gate 	asy->asy_lcr = STOP1|BITS8;		/* default to 1 stop 8 bits */
8930Sstevel@tonic-gate 	asy->asy_bidx = B9600;			/* default to 9600  */
8940Sstevel@tonic-gate #ifdef DEBUG
8950Sstevel@tonic-gate 	asy->asy_msint_cnt = 0;			/* # of times in async_msint */
8960Sstevel@tonic-gate #endif
8970Sstevel@tonic-gate 	mcr = 0;				/* don't enable until open */
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	if (asy->asy_com_port != 0) {
9000Sstevel@tonic-gate 		/*
9010Sstevel@tonic-gate 		 * For motherboard ports, emulate tty eeprom properties.
9020Sstevel@tonic-gate 		 * Actually, we can't tell if a port is motherboard or not,
9030Sstevel@tonic-gate 		 * so for "motherboard ports", read standard DOS COM ports.
9040Sstevel@tonic-gate 		 */
9050Sstevel@tonic-gate 		switch (asy_getproperty(devi, asy, "ignore-cd")) {
9060Sstevel@tonic-gate 		case 0:				/* *-ignore-cd=False */
9070Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
9080Sstevel@tonic-gate 			    "asy%dattach: clear ASY_IGNORE_CD\n", instance);
9090Sstevel@tonic-gate 			asy->asy_flags &= ~ASY_IGNORE_CD; /* wait for cd */
9100Sstevel@tonic-gate 			break;
9110Sstevel@tonic-gate 		case 1:				/* *-ignore-cd=True */
9120Sstevel@tonic-gate 			/*FALLTHRU*/
9130Sstevel@tonic-gate 		default:			/* *-ignore-cd not defined */
9140Sstevel@tonic-gate 			/*
9150Sstevel@tonic-gate 			 * We set rather silly defaults of soft carrier on
9160Sstevel@tonic-gate 			 * and DTR/RTS raised here because it might be that
9170Sstevel@tonic-gate 			 * one of the motherboard ports is the system console.
9180Sstevel@tonic-gate 			 */
9190Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
9200Sstevel@tonic-gate 			    "asy%dattach: set ASY_IGNORE_CD, set RTS & DTR\n",
9210Sstevel@tonic-gate 			    instance);
9220Sstevel@tonic-gate 			mcr = asy->asy_mcr;		/* rts/dtr on */
9230Sstevel@tonic-gate 			asy->asy_flags |= ASY_IGNORE_CD;	/* ignore cd */
9240Sstevel@tonic-gate 			break;
9250Sstevel@tonic-gate 		}
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 		/* Property for not raising DTR/RTS */
9280Sstevel@tonic-gate 		switch (asy_getproperty(devi, asy, "rts-dtr-off")) {
9290Sstevel@tonic-gate 		case 0:				/* *-rts-dtr-off=False */
9300Sstevel@tonic-gate 			asy->asy_flags |= ASY_RTS_DTR_OFF;	/* OFF */
9310Sstevel@tonic-gate 			mcr = asy->asy_mcr;		/* rts/dtr on */
9320Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM, "asy%dattach: "
9330Sstevel@tonic-gate 			    "ASY_RTS_DTR_OFF set and DTR & RTS set\n",
9340Sstevel@tonic-gate 			    instance);
9350Sstevel@tonic-gate 			break;
9360Sstevel@tonic-gate 		case 1:				/* *-rts-dtr-off=True */
9370Sstevel@tonic-gate 			/*FALLTHRU*/
9380Sstevel@tonic-gate 		default:			/* *-rts-dtr-off undefined */
9390Sstevel@tonic-gate 			break;
9400Sstevel@tonic-gate 		}
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 		/* Parse property for tty modes */
9430Sstevel@tonic-gate 		asy_parse_mode(devi, asy);
9440Sstevel@tonic-gate 	} else {
9450Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_MODEM,
9460Sstevel@tonic-gate 		    "asy%dattach: clear ASY_IGNORE_CD, clear RTS & DTR\n",
9470Sstevel@tonic-gate 		    instance);
9480Sstevel@tonic-gate 		asy->asy_flags &= ~ASY_IGNORE_CD;	/* wait for cd */
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	/*
9520Sstevel@tonic-gate 	 * Initialize the port with default settings.
9530Sstevel@tonic-gate 	 */
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	asy->asy_fifo_buf = 1;
9560Sstevel@tonic-gate 	asy->asy_use_fifo = FIFO_OFF;
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	/*
9590Sstevel@tonic-gate 	 * Get icookie for mutexes initialization
9600Sstevel@tonic-gate 	 */
9610Sstevel@tonic-gate 	if ((ddi_get_iblock_cookie(devi, 0, &asy->asy_iblock) !=
9620Sstevel@tonic-gate 	    DDI_SUCCESS) ||
9630Sstevel@tonic-gate 	    (ddi_get_soft_iblock_cookie(devi, DDI_SOFTINT_MED,
9640Sstevel@tonic-gate 	    &asy_soft_iblock) != DDI_SUCCESS)) {
9650Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
9660Sstevel@tonic-gate 		cmn_err(CE_CONT,
9670Sstevel@tonic-gate 		    "asy%d: could not hook interrupt for UART @ %p\n",
9680Sstevel@tonic-gate 		    instance, (void *)asy->asy_ioaddr);
9690Sstevel@tonic-gate 		asy_soft_state_free(asy);
9700Sstevel@tonic-gate 		return (DDI_FAILURE);
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	/*
9740Sstevel@tonic-gate 	 * Initialize mutexes before accessing the hardware
9750Sstevel@tonic-gate 	 */
9760Sstevel@tonic-gate 	mutex_init(&asy->asy_excl, NULL, MUTEX_DRIVER, asy_soft_iblock);
9770Sstevel@tonic-gate 	mutex_init(&asy->asy_excl_hi, NULL, MUTEX_DRIVER,
9785295Srandyf 	    (void *)asy->asy_iblock);
9795295Srandyf 	mutex_init(&asy->asy_soft_sr, NULL, MUTEX_DRIVER, asy_soft_iblock);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
9820Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	if (asy_identify_chip(devi, asy) != DDI_SUCCESS) {
9850Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
9860Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
9870Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
9880Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
9895295Srandyf 		mutex_destroy(&asy->asy_soft_sr);
9900Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
9910Sstevel@tonic-gate 		cmn_err(CE_CONT, "Cannot identify UART chip at %p\n",
9920Sstevel@tonic-gate 		    (void *)asy->asy_ioaddr);
9930Sstevel@tonic-gate 		asy_soft_state_free(asy);
9940Sstevel@tonic-gate 		return (DDI_FAILURE);
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	/* disable all interrupts */
9981106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0);
9990Sstevel@tonic-gate 	/* select baud rate generator */
10001106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, DLAB);
10010Sstevel@tonic-gate 	/* Set the baud rate to 9600 */
10021106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + (DAT+DLL),
10035295Srandyf 	    asyspdtab[asy->asy_bidx] & 0xff);
10041106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + (DAT+DLH),
10055295Srandyf 	    (asyspdtab[asy->asy_bidx] >> 8) & 0xff);
10065295Srandyf 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, asy->asy_lcr);
10071106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR, mcr);
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
10100Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	/*
10130Sstevel@tonic-gate 	 * Set up the other components of the asycom structure for this port.
10140Sstevel@tonic-gate 	 */
10150Sstevel@tonic-gate 	asy->asy_dip = devi;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
10180Sstevel@tonic-gate 	if (asy_addedsoft == 0) { /* install the soft interrupt handler */
10190Sstevel@tonic-gate 		if (ddi_add_softintr(devi, DDI_SOFTINT_MED,
10200Sstevel@tonic-gate 		    &asy_softintr_id, NULL, 0, asysoftintr,
10210Sstevel@tonic-gate 		    (caddr_t)0) != DDI_SUCCESS) {
10220Sstevel@tonic-gate 			mutex_destroy(&asy->asy_excl);
10230Sstevel@tonic-gate 			mutex_destroy(&asy->asy_excl_hi);
10240Sstevel@tonic-gate 			ddi_regs_map_free(&asy->asy_iohandle);
10250Sstevel@tonic-gate 			mutex_exit(&asy_glob_lock);
10260Sstevel@tonic-gate 			cmn_err(CE_CONT,
10275295Srandyf 			    "Can not set soft interrupt for ASY driver\n");
10280Sstevel@tonic-gate 			asy_soft_state_free(asy);
10290Sstevel@tonic-gate 			return (DDI_FAILURE);
10300Sstevel@tonic-gate 		}
10310Sstevel@tonic-gate 		mutex_init(&asy_soft_lock, NULL, MUTEX_DRIVER,
10325295Srandyf 		    (void *)asy->asy_iblock);
10330Sstevel@tonic-gate 		asy_addedsoft++;
10340Sstevel@tonic-gate 	}
10350Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
10380Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	/*
10410Sstevel@tonic-gate 	 * Install interrupt handler for this device.
10420Sstevel@tonic-gate 	 */
10430Sstevel@tonic-gate 	if (ddi_add_intr(devi, 0, NULL, 0, asyintr,
10440Sstevel@tonic-gate 	    (caddr_t)asy) != DDI_SUCCESS) {
10450Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
10460Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
10470Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
10480Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
10490Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
10500Sstevel@tonic-gate 		cmn_err(CE_CONT,
10515295Srandyf 		    "Can not set device interrupt for ASY driver\n");
10520Sstevel@tonic-gate 		asy_soft_state_free(asy);
10530Sstevel@tonic-gate 		return (DDI_FAILURE);
10540Sstevel@tonic-gate 	}
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
10570Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	asyinit(asy);	/* initialize the asyncline structure */
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	/* create minor device nodes for this device */
10620Sstevel@tonic-gate 	if (asy->asy_com_port != 0) {
10630Sstevel@tonic-gate 		/*
10640Sstevel@tonic-gate 		 * For DOS COM ports, add letter suffix so
10650Sstevel@tonic-gate 		 * devfsadm can create correct link names.
10660Sstevel@tonic-gate 		 */
10670Sstevel@tonic-gate 		name[0] = asy->asy_com_port + 'a' - 1;
10680Sstevel@tonic-gate 		name[1] = '\0';
10690Sstevel@tonic-gate 	} else {
10700Sstevel@tonic-gate 		/*
10713359Smyers 		 * asy port which isn't a standard DOS COM
10723359Smyers 		 * port gets a numeric name based on instance
10730Sstevel@tonic-gate 		 */
10743359Smyers 		(void) snprintf(name, ASY_MINOR_LEN, "%d", instance);
10750Sstevel@tonic-gate 	}
10760Sstevel@tonic-gate 	status = ddi_create_minor_node(devi, name, S_IFCHR, instance,
10770Sstevel@tonic-gate 	    asy->asy_com_port != 0 ? DDI_NT_SERIAL_MB : DDI_NT_SERIAL, NULL);
10780Sstevel@tonic-gate 	if (status == DDI_SUCCESS) {
10790Sstevel@tonic-gate 		(void) strcat(name, ",cu");
10800Sstevel@tonic-gate 		status = ddi_create_minor_node(devi, name, S_IFCHR,
10810Sstevel@tonic-gate 		    OUTLINE | instance,
10820Sstevel@tonic-gate 		    asy->asy_com_port != 0 ? DDI_NT_SERIAL_MB_DO :
10830Sstevel@tonic-gate 		    DDI_NT_SERIAL_DO, NULL);
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
10870Sstevel@tonic-gate 		struct asyncline *async = asy->asy_priv;
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
10900Sstevel@tonic-gate 		ddi_remove_intr(devi, 0, asy->asy_iblock);
10910Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
10920Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
10930Sstevel@tonic-gate 		cv_destroy(&async->async_flags_cv);
10940Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
10950Sstevel@tonic-gate 		asy_soft_state_free(asy);
10960Sstevel@tonic-gate 		return (DDI_FAILURE);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	/*
11000Sstevel@tonic-gate 	 * Fill in the polled I/O structure.
11010Sstevel@tonic-gate 	 */
11020Sstevel@tonic-gate 	asy->polledio.cons_polledio_version = CONSPOLLEDIO_V0;
11031762Slt200341 	asy->polledio.cons_polledio_argument = (cons_polledio_arg_t)asy;
11040Sstevel@tonic-gate 	asy->polledio.cons_polledio_putchar = asyputchar;
11050Sstevel@tonic-gate 	asy->polledio.cons_polledio_getchar = asygetchar;
11060Sstevel@tonic-gate 	asy->polledio.cons_polledio_ischar = asyischar;
11070Sstevel@tonic-gate 	asy->polledio.cons_polledio_enter = NULL;
11080Sstevel@tonic-gate 	asy->polledio.cons_polledio_exit = NULL;
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	ddi_report_dev(devi);
11110Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_INIT, "asy%dattach: done\n", instance);
11120Sstevel@tonic-gate 	return (DDI_SUCCESS);
11130Sstevel@tonic-gate }
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate /*ARGSUSED*/
11160Sstevel@tonic-gate static int
11170Sstevel@tonic-gate asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
11180Sstevel@tonic-gate 	void **result)
11190Sstevel@tonic-gate {
11200Sstevel@tonic-gate 	dev_t dev = (dev_t)arg;
11210Sstevel@tonic-gate 	int instance, error;
11220Sstevel@tonic-gate 	struct asycom *asy;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	instance = UNIT(dev);
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	switch (infocmd) {
11270Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1128386Scth 		asy = ddi_get_soft_state(asy_soft_state, instance);
1129386Scth 		if ((asy == NULL) || (asy->asy_dip == NULL))
11300Sstevel@tonic-gate 			error = DDI_FAILURE;
11310Sstevel@tonic-gate 		else {
11320Sstevel@tonic-gate 			*result = (void *) asy->asy_dip;
11330Sstevel@tonic-gate 			error = DDI_SUCCESS;
11340Sstevel@tonic-gate 		}
11350Sstevel@tonic-gate 		break;
11360Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
11370Sstevel@tonic-gate 		*result = (void *)(intptr_t)instance;
11380Sstevel@tonic-gate 		error = DDI_SUCCESS;
11390Sstevel@tonic-gate 		break;
11400Sstevel@tonic-gate 	default:
11410Sstevel@tonic-gate 		error = DDI_FAILURE;
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 	return (error);
11440Sstevel@tonic-gate }
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate /* asy_getproperty -- walk through all name variants until we find a match */
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate static int
11490Sstevel@tonic-gate asy_getproperty(dev_info_t *devi, struct asycom *asy, const char *property)
11500Sstevel@tonic-gate {
11510Sstevel@tonic-gate 	int len;
11520Sstevel@tonic-gate 	int ret;
11530Sstevel@tonic-gate 	char letter = asy->asy_com_port + 'a' - 1;	/* for ttya */
11540Sstevel@tonic-gate 	char number = asy->asy_com_port + '0';		/* for COM1 */
11550Sstevel@tonic-gate 	char val[40];
11560Sstevel@tonic-gate 	char name[40];
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/* Property for ignoring DCD */
11590Sstevel@tonic-gate 	(void) sprintf(name, "tty%c-%s", letter, property);
11600Sstevel@tonic-gate 	len = sizeof (val);
11610Sstevel@tonic-gate 	ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
11620Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
11630Sstevel@tonic-gate 		(void) sprintf(name, "com%c-%s", number, property);
11640Sstevel@tonic-gate 		len = sizeof (val);
11655295Srandyf 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
11660Sstevel@tonic-gate 	}
11670Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
11680Sstevel@tonic-gate 		(void) sprintf(name, "tty0%c-%s", number, property);
11690Sstevel@tonic-gate 		len = sizeof (val);
11705295Srandyf 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
11710Sstevel@tonic-gate 	}
11720Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
11730Sstevel@tonic-gate 		(void) sprintf(name, "port-%c-%s", letter, property);
11740Sstevel@tonic-gate 		len = sizeof (val);
11755295Srandyf 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS)
11780Sstevel@tonic-gate 		return (-1);		/* property non-existant */
11790Sstevel@tonic-gate 	if (val[0] == 'f' || val[0] == 'F' || val[0] == '0')
11800Sstevel@tonic-gate 		return (0);		/* property false/0 */
11810Sstevel@tonic-gate 	return (1);			/* property true/!0 */
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate /* asy_soft_state_free - local wrapper for ddi_soft_state_free(9F) */
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate static void
11870Sstevel@tonic-gate asy_soft_state_free(struct asycom *asy)
11880Sstevel@tonic-gate {
11890Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
11900Sstevel@tonic-gate 	/* If we were the max_asy_instance, work out new value */
11910Sstevel@tonic-gate 	if (asy->asy_unit == max_asy_instance) {
11920Sstevel@tonic-gate 		while (--max_asy_instance >= 0) {
11930Sstevel@tonic-gate 			if (ddi_get_soft_state(asy_soft_state,
11940Sstevel@tonic-gate 			    max_asy_instance) != NULL)
11950Sstevel@tonic-gate 				break;
11960Sstevel@tonic-gate 		}
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	if (asy->asy_priv != NULL) {
12010Sstevel@tonic-gate 		kmem_free(asy->asy_priv, sizeof (struct asyncline));
12020Sstevel@tonic-gate 		asy->asy_priv = NULL;
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 	ddi_soft_state_free(asy_soft_state, asy->asy_unit);
12050Sstevel@tonic-gate }
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate static char *
12080Sstevel@tonic-gate asy_hw_name(struct asycom *asy)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate 	switch (asy->asy_hwtype) {
12110Sstevel@tonic-gate 	case ASY8250A:
12120Sstevel@tonic-gate 		return ("8250A/16450");
12130Sstevel@tonic-gate 	case ASY16550:
12140Sstevel@tonic-gate 		return ("16550");
12150Sstevel@tonic-gate 	case ASY16550A:
12160Sstevel@tonic-gate 		return ("16550A");
12170Sstevel@tonic-gate 	case ASY16650:
12180Sstevel@tonic-gate 		return ("16650");
12190Sstevel@tonic-gate 	case ASY16750:
12200Sstevel@tonic-gate 		return ("16750");
12210Sstevel@tonic-gate 	default:
12220Sstevel@tonic-gate 		DEBUGNOTE2(ASY_DEBUG_INIT,
12230Sstevel@tonic-gate 		    "asy%d: asy_hw_name: unknown asy_hwtype: %d",
12240Sstevel@tonic-gate 		    asy->asy_unit, asy->asy_hwtype);
12250Sstevel@tonic-gate 		return ("?");
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate }
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate static int
12300Sstevel@tonic-gate asy_identify_chip(dev_info_t *devi, struct asycom *asy)
12310Sstevel@tonic-gate {
12320Sstevel@tonic-gate 	int ret;
12330Sstevel@tonic-gate 	int mcr;
12340Sstevel@tonic-gate 	dev_t dev;
12350Sstevel@tonic-gate 	uint_t hwtype;
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	if (asy_scr_test) {
12380Sstevel@tonic-gate 		/* Check scratch register works. */
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 		/* write to scratch register */
12411106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + SCR, SCRTEST);
12420Sstevel@tonic-gate 		/* make sure that pattern doesn't just linger on the bus */
12431106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + FIFOR, 0x00);
12440Sstevel@tonic-gate 		/* read data back from scratch register */
12451106Smrj 		ret = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + SCR);
12460Sstevel@tonic-gate 		if (ret != SCRTEST) {
12470Sstevel@tonic-gate 			/*
12480Sstevel@tonic-gate 			 * Scratch register not working.
12490Sstevel@tonic-gate 			 * Probably not an async chip.
12500Sstevel@tonic-gate 			 * 8250 and 8250B don't have scratch registers,
12510Sstevel@tonic-gate 			 * but only worked in ancient PC XT's anyway.
12520Sstevel@tonic-gate 			 */
12530Sstevel@tonic-gate 			cmn_err(CE_CONT, "asy%d: UART @ %p "
12540Sstevel@tonic-gate 			    "scratch register: expected 0x5a, got 0x%02x\n",
12550Sstevel@tonic-gate 			    asy->asy_unit, (void *)asy->asy_ioaddr, ret);
12560Sstevel@tonic-gate 			return (DDI_FAILURE);
12570Sstevel@tonic-gate 		}
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 	/*
12600Sstevel@tonic-gate 	 * Use 16550 fifo reset sequence specified in NS application
12610Sstevel@tonic-gate 	 * note. Disable fifos until chip is initialized.
12620Sstevel@tonic-gate 	 */
12631106Smrj 	ddi_put8(asy->asy_iohandle,
12640Sstevel@tonic-gate 	    asy->asy_ioaddr + FIFOR, 0x00);	/* clear */
12651106Smrj 	ddi_put8(asy->asy_iohandle,
12660Sstevel@tonic-gate 	    asy->asy_ioaddr + FIFOR, FIFO_ON);	/* enable */
12671106Smrj 	ddi_put8(asy->asy_iohandle,
12680Sstevel@tonic-gate 	    asy->asy_ioaddr + FIFOR, FIFO_ON | FIFORXFLSH);
12690Sstevel@tonic-gate 						/* reset */
12700Sstevel@tonic-gate 	if (asymaxchip >= ASY16650 && asy_scr_test) {
12710Sstevel@tonic-gate 		/*
12720Sstevel@tonic-gate 		 * Reset 16650 enhanced regs also, in case we have one of these
12730Sstevel@tonic-gate 		 */
12741106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
12750Sstevel@tonic-gate 		    EFRACCESS);
12761106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + EFR,
12770Sstevel@tonic-gate 		    0);
12781106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
12790Sstevel@tonic-gate 		    STOP1|BITS8);
12800Sstevel@tonic-gate 	}
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	/*
12830Sstevel@tonic-gate 	 * See what sort of FIFO we have.
12840Sstevel@tonic-gate 	 * Try enabling it and see what chip makes of this.
12850Sstevel@tonic-gate 	 */
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	asy->asy_fifor = 0;
12880Sstevel@tonic-gate 	asy->asy_hwtype = asymaxchip; /* just for asy_reset_fifo() */
12890Sstevel@tonic-gate 	if (asymaxchip >= ASY16550A)
12900Sstevel@tonic-gate 		asy->asy_fifor |=
12910Sstevel@tonic-gate 		    FIFO_ON | FIFODMA | (asy_trig_level & 0xff);
12920Sstevel@tonic-gate 	if (asymaxchip >= ASY16650)
12930Sstevel@tonic-gate 		asy->asy_fifor |= FIFOEXTRA1 | FIFOEXTRA2;
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	asy_reset_fifo(asy, FIFOTXFLSH | FIFORXFLSH);
12960Sstevel@tonic-gate 
12971106Smrj 	mcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
12981106Smrj 	ret = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + ISR);
12990Sstevel@tonic-gate 	DEBUGCONT4(ASY_DEBUG_CHIP,
13000Sstevel@tonic-gate 	    "asy%d: probe fifo FIFOR=0x%02x ISR=0x%02x MCR=0x%02x\n",
13010Sstevel@tonic-gate 	    asy->asy_unit, asy->asy_fifor | FIFOTXFLSH | FIFORXFLSH,
13020Sstevel@tonic-gate 	    ret, mcr);
13030Sstevel@tonic-gate 	switch (ret & 0xf0) {
13040Sstevel@tonic-gate 	case 0x40:
13050Sstevel@tonic-gate 		hwtype = ASY16550; /* 16550 with broken FIFO */
13060Sstevel@tonic-gate 		asy->asy_fifor = 0;
13070Sstevel@tonic-gate 		break;
13080Sstevel@tonic-gate 	case 0xc0:
13090Sstevel@tonic-gate 		hwtype = ASY16550A;
13100Sstevel@tonic-gate 		asy->asy_fifo_buf = 16;
13110Sstevel@tonic-gate 		asy->asy_use_fifo = FIFO_ON;
13120Sstevel@tonic-gate 		asy->asy_fifor &= ~(FIFOEXTRA1 | FIFOEXTRA2);
13130Sstevel@tonic-gate 		break;
13140Sstevel@tonic-gate 	case 0xe0:
13150Sstevel@tonic-gate 		hwtype = ASY16650;
13160Sstevel@tonic-gate 		asy->asy_fifo_buf = 32;
13170Sstevel@tonic-gate 		asy->asy_use_fifo = FIFO_ON;
13180Sstevel@tonic-gate 		asy->asy_fifor &= ~(FIFOEXTRA1);
13190Sstevel@tonic-gate 		break;
13200Sstevel@tonic-gate 	case 0xf0:
13210Sstevel@tonic-gate 		/*
13220Sstevel@tonic-gate 		 * Note we get 0xff if chip didn't return us anything,
13230Sstevel@tonic-gate 		 * e.g. if there's no chip there.
13240Sstevel@tonic-gate 		 */
13250Sstevel@tonic-gate 		if (ret == 0xff) {
13260Sstevel@tonic-gate 			cmn_err(CE_CONT, "asy%d: UART @ %p "
13270Sstevel@tonic-gate 			    "interrupt register: got 0xff\n",
13280Sstevel@tonic-gate 			    asy->asy_unit, (void *)asy->asy_ioaddr);
13290Sstevel@tonic-gate 			return (DDI_FAILURE);
13300Sstevel@tonic-gate 		}
13310Sstevel@tonic-gate 		/*FALLTHRU*/
13320Sstevel@tonic-gate 	case 0xd0:
13330Sstevel@tonic-gate 		hwtype = ASY16750;
13340Sstevel@tonic-gate 		asy->asy_fifo_buf = 64;
13350Sstevel@tonic-gate 		asy->asy_use_fifo = FIFO_ON;
13360Sstevel@tonic-gate 		break;
13370Sstevel@tonic-gate 	default:
13380Sstevel@tonic-gate 		hwtype = ASY8250A; /* No FIFO */
13390Sstevel@tonic-gate 		asy->asy_fifor = 0;
13400Sstevel@tonic-gate 	}
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	if (hwtype > asymaxchip) {
13430Sstevel@tonic-gate 		cmn_err(CE_CONT, "asy%d: UART @ %p "
13440Sstevel@tonic-gate 		    "unexpected probe result: "
13450Sstevel@tonic-gate 		    "FIFOR=0x%02x ISR=0x%02x MCR=0x%02x\n",
13460Sstevel@tonic-gate 		    asy->asy_unit, (void *)asy->asy_ioaddr,
13470Sstevel@tonic-gate 		    asy->asy_fifor | FIFOTXFLSH | FIFORXFLSH, ret, mcr);
13480Sstevel@tonic-gate 		return (DDI_FAILURE);
13490Sstevel@tonic-gate 	}
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	/*
13520Sstevel@tonic-gate 	 * Now reset the FIFO operation appropriate for the chip type.
13530Sstevel@tonic-gate 	 * Note we must call asy_reset_fifo() before any possible
13540Sstevel@tonic-gate 	 * downgrade of the asy->asy_hwtype, or it may not disable
13550Sstevel@tonic-gate 	 * the more advanced features we specifically want downgraded.
13560Sstevel@tonic-gate 	 */
13570Sstevel@tonic-gate 	asy_reset_fifo(asy, 0);
13580Sstevel@tonic-gate 	asy->asy_hwtype = hwtype;
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	/*
13610Sstevel@tonic-gate 	 * Check for Exar/Startech ST16C650, which will still look like a
13620Sstevel@tonic-gate 	 * 16550A until we enable its enhanced mode.
13630Sstevel@tonic-gate 	 */
13640Sstevel@tonic-gate 	if (asy->asy_hwtype == ASY16550A && asymaxchip >= ASY16650 &&
13650Sstevel@tonic-gate 	    asy_scr_test) {
13660Sstevel@tonic-gate 		/* Enable enhanced mode register access */
13671106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
13680Sstevel@tonic-gate 		    EFRACCESS);
13690Sstevel@tonic-gate 		/* zero scratch register (not scratch register if enhanced) */
13701106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + SCR, 0);
13710Sstevel@tonic-gate 		/* Disable enhanced mode register access */
13721106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
13730Sstevel@tonic-gate 		    STOP1|BITS8);
13740Sstevel@tonic-gate 		/* read back scratch register */
13751106Smrj 		ret = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + SCR);
13760Sstevel@tonic-gate 		if (ret == SCRTEST) {
13770Sstevel@tonic-gate 			/* looks like we have an ST16650 -- enable it */
13781106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
13790Sstevel@tonic-gate 			    EFRACCESS);
13801106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + EFR,
13810Sstevel@tonic-gate 			    ENHENABLE);
13821106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
13830Sstevel@tonic-gate 			    STOP1|BITS8);
13840Sstevel@tonic-gate 			asy->asy_hwtype = ASY16650;
13850Sstevel@tonic-gate 			asy->asy_fifo_buf = 32;
13860Sstevel@tonic-gate 			asy->asy_fifor |= 0x10; /* 24 byte txfifo trigger */
13870Sstevel@tonic-gate 			asy_reset_fifo(asy, 0);
13880Sstevel@tonic-gate 		}
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	/*
13920Sstevel@tonic-gate 	 * If we think we might have a FIFO larger than 16 characters,
13930Sstevel@tonic-gate 	 * measure FIFO size and check it against expected.
13940Sstevel@tonic-gate 	 */
13950Sstevel@tonic-gate 	if (asy_fifo_test > 0 &&
13960Sstevel@tonic-gate 	    !(asy->asy_flags2 & ASY2_NO_LOOPBACK) &&
13970Sstevel@tonic-gate 	    (asy->asy_fifo_buf > 16 ||
13980Sstevel@tonic-gate 	    (asy_fifo_test > 1 && asy->asy_use_fifo == FIFO_ON) ||
13990Sstevel@tonic-gate 	    ASY_DEBUG(ASY_DEBUG_CHIP))) {
14000Sstevel@tonic-gate 		int i;
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 		/* Set baud rate to 57600 (fairly arbitrary choice) */
14031106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
14040Sstevel@tonic-gate 		    DLAB);
14051106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
14060Sstevel@tonic-gate 		    asyspdtab[B57600] & 0xff);
14071106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR,
14080Sstevel@tonic-gate 		    (asyspdtab[B57600] >> 8) & 0xff);
14090Sstevel@tonic-gate 		/* Set 8 bits, 1 stop bit */
14101106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
14110Sstevel@tonic-gate 		    STOP1|BITS8);
14120Sstevel@tonic-gate 		/* Set loopback mode */
14131106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
14140Sstevel@tonic-gate 		    DTR | RTS | ASY_LOOP | OUT1 | OUT2);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 		/* Overfill fifo */
14170Sstevel@tonic-gate 		for (i = 0; i < asy->asy_fifo_buf * 2; i++) {
14181106Smrj 			ddi_put8(asy->asy_iohandle,
14190Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT, i);
14200Sstevel@tonic-gate 		}
14210Sstevel@tonic-gate 		/*
14220Sstevel@tonic-gate 		 * Now there's an interesting question here about which
14230Sstevel@tonic-gate 		 * FIFO we're testing the size of, RX or TX. We just
14240Sstevel@tonic-gate 		 * filled the TX FIFO much faster than it can empty,
14250Sstevel@tonic-gate 		 * although it is possible one or two characters may
14260Sstevel@tonic-gate 		 * have gone from it to the TX shift register.
14270Sstevel@tonic-gate 		 * We wait for enough time for all the characters to
14280Sstevel@tonic-gate 		 * move into the RX FIFO and any excess characters to
14290Sstevel@tonic-gate 		 * have been lost, and then read all the RX FIFO. So
14300Sstevel@tonic-gate 		 * the answer we finally get will be the size which is
14310Sstevel@tonic-gate 		 * the MIN(RX FIFO,(TX FIFO + 1 or 2)). The critical
14320Sstevel@tonic-gate 		 * one is actually the TX FIFO, because if we overfill
14330Sstevel@tonic-gate 		 * it in normal operation, the excess characters are
14340Sstevel@tonic-gate 		 * lost with no warning.
14350Sstevel@tonic-gate 		 */
14360Sstevel@tonic-gate 		/*
14370Sstevel@tonic-gate 		 * Wait for characters to move into RX FIFO.
14380Sstevel@tonic-gate 		 * In theory, 200 * asy->asy_fifo_buf * 2 should be
14390Sstevel@tonic-gate 		 * enough. However, in practice it isn't always, so we
14400Sstevel@tonic-gate 		 * increase to 400 so some slow 16550A's finish, and we
14410Sstevel@tonic-gate 		 * increase to 3 so we spot more characters coming back
14420Sstevel@tonic-gate 		 * than we sent, in case that should ever happen.
14430Sstevel@tonic-gate 		 */
14440Sstevel@tonic-gate 		delay(drv_usectohz(400 * asy->asy_fifo_buf * 3));
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 		/* Now see how many characters we can read back */
14470Sstevel@tonic-gate 		for (i = 0; i < asy->asy_fifo_buf * 3; i++) {
14481106Smrj 			ret = ddi_get8(asy->asy_iohandle,
14490Sstevel@tonic-gate 			    asy->asy_ioaddr + LSR);
14500Sstevel@tonic-gate 			if (!(ret & RCA))
14510Sstevel@tonic-gate 				break;	/* FIFO emptied */
14521106Smrj 			(void) ddi_get8(asy->asy_iohandle,
14530Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT); /* lose another */
14540Sstevel@tonic-gate 		}
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 		DEBUGCONT3(ASY_DEBUG_CHIP,
14570Sstevel@tonic-gate 		    "asy%d FIFO size: expected=%d, measured=%d\n",
14580Sstevel@tonic-gate 		    asy->asy_unit, asy->asy_fifo_buf, i);
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 		hwtype = asy->asy_hwtype;
14610Sstevel@tonic-gate 		if (i < asy->asy_fifo_buf) {
14620Sstevel@tonic-gate 			/*
14630Sstevel@tonic-gate 			 * FIFO is somewhat smaller than we anticipated.
14640Sstevel@tonic-gate 			 * If we have 16 characters usable, then this
14650Sstevel@tonic-gate 			 * UART will probably work well enough in
14660Sstevel@tonic-gate 			 * 16550A mode. If less than 16 characters,
14670Sstevel@tonic-gate 			 * then we'd better not use it at all.
14680Sstevel@tonic-gate 			 * UARTs with busted FIFOs do crop up.
14690Sstevel@tonic-gate 			 */
14700Sstevel@tonic-gate 			if (i >= 16 && asy->asy_fifo_buf >= 16) {
14710Sstevel@tonic-gate 				/* fall back to a 16550A */
14720Sstevel@tonic-gate 				hwtype = ASY16550A;
14730Sstevel@tonic-gate 				asy->asy_fifo_buf = 16;
14740Sstevel@tonic-gate 				asy->asy_fifor &= ~(FIFOEXTRA1 | FIFOEXTRA2);
14750Sstevel@tonic-gate 			} else {
14760Sstevel@tonic-gate 				/* fall back to no FIFO at all */
14770Sstevel@tonic-gate 				hwtype = ASY16550;
14780Sstevel@tonic-gate 				asy->asy_fifo_buf = 1;
14790Sstevel@tonic-gate 				asy->asy_use_fifo = FIFO_OFF;
14800Sstevel@tonic-gate 				asy->asy_fifor &=
14810Sstevel@tonic-gate 				    ~(FIFO_ON | FIFOEXTRA1 | FIFOEXTRA2);
14820Sstevel@tonic-gate 			}
14830Sstevel@tonic-gate 		}
14840Sstevel@tonic-gate 		/*
14850Sstevel@tonic-gate 		 * We will need to reprogram the FIFO if we changed
14860Sstevel@tonic-gate 		 * our mind about how to drive it above, and in any
14870Sstevel@tonic-gate 		 * case, it would be a good idea to flush any garbage
14880Sstevel@tonic-gate 		 * out incase the loopback test left anything behind.
14890Sstevel@tonic-gate 		 * Again as earlier above, we must call asy_reset_fifo()
14900Sstevel@tonic-gate 		 * before any possible downgrade of asy->asy_hwtype.
14910Sstevel@tonic-gate 		 */
14920Sstevel@tonic-gate 		if (asy->asy_hwtype >= ASY16650 && hwtype < ASY16650) {
14930Sstevel@tonic-gate 			/* Disable 16650 enhanced mode */
14941106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
14950Sstevel@tonic-gate 			    EFRACCESS);
14961106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + EFR,
14970Sstevel@tonic-gate 			    0);
14981106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
14990Sstevel@tonic-gate 			    STOP1|BITS8);
15000Sstevel@tonic-gate 		}
15010Sstevel@tonic-gate 		asy_reset_fifo(asy, FIFOTXFLSH | FIFORXFLSH);
15020Sstevel@tonic-gate 		asy->asy_hwtype = hwtype;
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 		/* Clear loopback mode and restore DTR/RTS */
15051106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR, mcr);
15060Sstevel@tonic-gate 	}
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	DEBUGNOTE3(ASY_DEBUG_CHIP, "asy%d %s @ %p",
15090Sstevel@tonic-gate 	    asy->asy_unit, asy_hw_name(asy), (void *)asy->asy_ioaddr);
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	/* Make UART type visible in device tree for prtconf, etc */
15120Sstevel@tonic-gate 	dev = makedevice(DDI_MAJOR_T_UNKNOWN, asy->asy_unit);
15130Sstevel@tonic-gate 	(void) ddi_prop_update_string(dev, devi, "uart", asy_hw_name(asy));
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	if (asy->asy_hwtype == ASY16550)	/* for broken 16550's, */
15160Sstevel@tonic-gate 		asy->asy_hwtype = ASY8250A;	/* drive them as 8250A */
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	return (DDI_SUCCESS);
15190Sstevel@tonic-gate }
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate /*
15220Sstevel@tonic-gate  * asyinit() initializes the TTY protocol-private data for this channel
15230Sstevel@tonic-gate  * before enabling the interrupts.
15240Sstevel@tonic-gate  */
15250Sstevel@tonic-gate static void
15260Sstevel@tonic-gate asyinit(struct asycom *asy)
15270Sstevel@tonic-gate {
15280Sstevel@tonic-gate 	struct asyncline *async;
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	asy->asy_priv = kmem_zalloc(sizeof (struct asyncline), KM_SLEEP);
15310Sstevel@tonic-gate 	async = asy->asy_priv;
15320Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
15330Sstevel@tonic-gate 	async->async_common = asy;
15340Sstevel@tonic-gate 	cv_init(&async->async_flags_cv, NULL, CV_DRIVER, NULL);
15350Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate /*ARGSUSED3*/
15390Sstevel@tonic-gate static int
15400Sstevel@tonic-gate asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr)
15410Sstevel@tonic-gate {
15420Sstevel@tonic-gate 	struct asycom	*asy;
15430Sstevel@tonic-gate 	struct asyncline *async;
15440Sstevel@tonic-gate 	int		mcr;
15450Sstevel@tonic-gate 	int		unit;
15460Sstevel@tonic-gate 	int 		len;
15470Sstevel@tonic-gate 	struct termios 	*termiosp;
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	unit = UNIT(*dev);
15500Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_CLOSE, "asy%dopen\n", unit);
15510Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, unit);
15520Sstevel@tonic-gate 	if (asy == NULL)
15530Sstevel@tonic-gate 		return (ENXIO);		/* unit not configured */
15540Sstevel@tonic-gate 	async = asy->asy_priv;
15550Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate again:
15580Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	/*
15610Sstevel@tonic-gate 	 * Block waiting for carrier to come up, unless this is a no-delay open.
15620Sstevel@tonic-gate 	 */
15630Sstevel@tonic-gate 	if (!(async->async_flags & ASYNC_ISOPEN)) {
15640Sstevel@tonic-gate 		/*
15650Sstevel@tonic-gate 		 * Set the default termios settings (cflag).
15660Sstevel@tonic-gate 		 * Others are set in ldterm.
15670Sstevel@tonic-gate 		 */
15680Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_root_node(),
15710Sstevel@tonic-gate 		    0, "ttymodes",
15720Sstevel@tonic-gate 		    (caddr_t)&termiosp, &len) == DDI_PROP_SUCCESS &&
15730Sstevel@tonic-gate 		    len == sizeof (struct termios)) {
15740Sstevel@tonic-gate 			async->async_ttycommon.t_cflag = termiosp->c_cflag;
15750Sstevel@tonic-gate 			kmem_free(termiosp, len);
15760Sstevel@tonic-gate 		} else
15770Sstevel@tonic-gate 			cmn_err(CE_WARN,
15785295Srandyf 			    "asy: couldn't get ttymodes property!");
15790Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 		/* eeprom mode support - respect properties */
15820Sstevel@tonic-gate 		if (asy->asy_cflag)
15830Sstevel@tonic-gate 			async->async_ttycommon.t_cflag = asy->asy_cflag;
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 		async->async_ttycommon.t_iflag = 0;
15860Sstevel@tonic-gate 		async->async_ttycommon.t_iocpending = NULL;
15870Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_row = 0;
15880Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_col = 0;
15890Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_xpixel = 0;
15900Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_ypixel = 0;
15910Sstevel@tonic-gate 		async->async_dev = *dev;
15920Sstevel@tonic-gate 		async->async_wbufcid = 0;
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 		async->async_startc = CSTART;
15950Sstevel@tonic-gate 		async->async_stopc = CSTOP;
15960Sstevel@tonic-gate 		asy_program(asy, ASY_INIT);
15975295Srandyf 	} else
15985295Srandyf 		if ((async->async_ttycommon.t_flags & TS_XCLUDE) &&
15995295Srandyf 		    secpolicy_excl_open(cr) != 0) {
16000Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
16010Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
16020Sstevel@tonic-gate 		return (EBUSY);
16030Sstevel@tonic-gate 	} else if ((*dev & OUTLINE) && !(async->async_flags & ASYNC_OUT)) {
16040Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
16050Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
16060Sstevel@tonic-gate 		return (EBUSY);
16070Sstevel@tonic-gate 	}
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	if (*dev & OUTLINE)
16100Sstevel@tonic-gate 		async->async_flags |= ASYNC_OUT;
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	/* Raise DTR on every open, but delay if it was just lowered. */
16130Sstevel@tonic-gate 	while (async->async_flags & ASYNC_DTR_DELAY) {
16140Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_MODEM,
16150Sstevel@tonic-gate 		    "asy%dopen: waiting for the ASYNC_DTR_DELAY to be clear\n",
16160Sstevel@tonic-gate 		    unit);
16170Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
16180Sstevel@tonic-gate 		if (cv_wait_sig(&async->async_flags_cv,
16190Sstevel@tonic-gate 		    &asy->asy_excl) == 0) {
16200Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
16210Sstevel@tonic-gate 			    "asy%dopen: interrupted by signal, exiting\n",
16220Sstevel@tonic-gate 			    unit);
16230Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
16240Sstevel@tonic-gate 			return (EINTR);
16250Sstevel@tonic-gate 		}
16260Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
16270Sstevel@tonic-gate 	}
16280Sstevel@tonic-gate 
16291106Smrj 	mcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
16301106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
16315295Srandyf 	    mcr|(asy->asy_mcr&DTR));
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	DEBUGCONT3(ASY_DEBUG_INIT,
16345295Srandyf 	    "asy%dopen: \"Raise DTR on every open\": make mcr = %x, "
16355295Srandyf 	    "make TS_SOFTCAR = %s\n",
16365295Srandyf 	    unit, mcr|(asy->asy_mcr&DTR),
16375295Srandyf 	    (asy->asy_flags & ASY_IGNORE_CD) ? "ON" : "OFF");
16385295Srandyf 
16390Sstevel@tonic-gate 	if (asy->asy_flags & ASY_IGNORE_CD) {
16400Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_MODEM,
16415295Srandyf 		    "asy%dopen: ASY_IGNORE_CD set, set TS_SOFTCAR\n",
16425295Srandyf 		    unit);
16430Sstevel@tonic-gate 		async->async_ttycommon.t_flags |= TS_SOFTCAR;
16440Sstevel@tonic-gate 	}
16450Sstevel@tonic-gate 	else
16460Sstevel@tonic-gate 		async->async_ttycommon.t_flags &= ~TS_SOFTCAR;
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	/*
16490Sstevel@tonic-gate 	 * Check carrier.
16500Sstevel@tonic-gate 	 */
16511106Smrj 	asy->asy_msr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MSR);
16520Sstevel@tonic-gate 	DEBUGCONT3(ASY_DEBUG_INIT, "asy%dopen: TS_SOFTCAR is %s, "
16535295Srandyf 	    "MSR & DCD is %s\n",
16545295Srandyf 	    unit,
16555295Srandyf 	    (async->async_ttycommon.t_flags & TS_SOFTCAR) ? "set" : "clear",
16565295Srandyf 	    (asy->asy_msr & DCD) ? "set" : "clear");
16575295Srandyf 
16580Sstevel@tonic-gate 	if (asy->asy_msr & DCD)
16590Sstevel@tonic-gate 		async->async_flags |= ASYNC_CARR_ON;
16600Sstevel@tonic-gate 	else
16610Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_CARR_ON;
16620Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	/*
16650Sstevel@tonic-gate 	 * If FNDELAY and FNONBLOCK are clear, block until carrier up.
16660Sstevel@tonic-gate 	 * Quit on interrupt.
16670Sstevel@tonic-gate 	 */
16680Sstevel@tonic-gate 	if (!(flag & (FNDELAY|FNONBLOCK)) &&
16690Sstevel@tonic-gate 	    !(async->async_ttycommon.t_cflag & CLOCAL)) {
16700Sstevel@tonic-gate 		if ((!(async->async_flags & (ASYNC_CARR_ON|ASYNC_OUT)) &&
16710Sstevel@tonic-gate 		    !(async->async_ttycommon.t_flags & TS_SOFTCAR)) ||
16720Sstevel@tonic-gate 		    ((async->async_flags & ASYNC_OUT) &&
16730Sstevel@tonic-gate 		    !(*dev & OUTLINE))) {
16740Sstevel@tonic-gate 			async->async_flags |= ASYNC_WOPEN;
16750Sstevel@tonic-gate 			if (cv_wait_sig(&async->async_flags_cv,
16760Sstevel@tonic-gate 			    &asy->asy_excl) == B_FALSE) {
16770Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_WOPEN;
16780Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl);
16790Sstevel@tonic-gate 				return (EINTR);
16800Sstevel@tonic-gate 			}
16810Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_WOPEN;
16820Sstevel@tonic-gate 			goto again;
16830Sstevel@tonic-gate 		}
16840Sstevel@tonic-gate 	} else if ((async->async_flags & ASYNC_OUT) && !(*dev & OUTLINE)) {
16850Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
16860Sstevel@tonic-gate 		return (EBUSY);
16870Sstevel@tonic-gate 	}
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate 	async->async_ttycommon.t_readq = rq;
16900Sstevel@tonic-gate 	async->async_ttycommon.t_writeq = WR(rq);
16910Sstevel@tonic-gate 	rq->q_ptr = WR(rq)->q_ptr = (caddr_t)async;
16920Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
16930Sstevel@tonic-gate 	/*
16940Sstevel@tonic-gate 	 * Caution here -- qprocson sets the pointers that are used by canput
16950Sstevel@tonic-gate 	 * called by async_softint.  ASYNC_ISOPEN must *not* be set until those
16960Sstevel@tonic-gate 	 * pointers are valid.
16970Sstevel@tonic-gate 	 */
16980Sstevel@tonic-gate 	qprocson(rq);
16990Sstevel@tonic-gate 	async->async_flags |= ASYNC_ISOPEN;
17000Sstevel@tonic-gate 	async->async_polltid = 0;
17010Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_INIT, "asy%dopen: done\n", unit);
17020Sstevel@tonic-gate 	return (0);
17030Sstevel@tonic-gate }
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate static void
17060Sstevel@tonic-gate async_progress_check(void *arg)
17070Sstevel@tonic-gate {
17080Sstevel@tonic-gate 	struct asyncline *async = arg;
17090Sstevel@tonic-gate 	struct asycom	 *asy = async->async_common;
17100Sstevel@tonic-gate 	mblk_t *bp;
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	/*
17130Sstevel@tonic-gate 	 * We define "progress" as either waiting on a timed break or delay, or
17140Sstevel@tonic-gate 	 * having had at least one transmitter interrupt.  If none of these are
17150Sstevel@tonic-gate 	 * true, then just terminate the output and wake up that close thread.
17160Sstevel@tonic-gate 	 */
17170Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
17180Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
17190Sstevel@tonic-gate 	if (!(async->async_flags & (ASYNC_BREAK|ASYNC_DELAY|ASYNC_PROGRESS))) {
17200Sstevel@tonic-gate 		async->async_ocnt = 0;
17210Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_BUSY;
17220Sstevel@tonic-gate 		async->async_timer = 0;
17230Sstevel@tonic-gate 		bp = async->async_xmitblk;
17240Sstevel@tonic-gate 		async->async_xmitblk = NULL;
17250Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
17260Sstevel@tonic-gate 		if (bp != NULL)
17270Sstevel@tonic-gate 			freeb(bp);
17280Sstevel@tonic-gate 		/*
17290Sstevel@tonic-gate 		 * Since this timer is running, we know that we're in exit(2).
17300Sstevel@tonic-gate 		 * That means that the user can't possibly be waiting on any
17310Sstevel@tonic-gate 		 * valid ioctl(2) completion anymore, and we should just flush
17320Sstevel@tonic-gate 		 * everything.
17330Sstevel@tonic-gate 		 */
17340Sstevel@tonic-gate 		flushq(async->async_ttycommon.t_writeq, FLUSHALL);
17350Sstevel@tonic-gate 		cv_broadcast(&async->async_flags_cv);
17360Sstevel@tonic-gate 	} else {
17370Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_PROGRESS;
17380Sstevel@tonic-gate 		async->async_timer = timeout(async_progress_check, async,
17390Sstevel@tonic-gate 		    drv_usectohz(asy_drain_check));
17400Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
17410Sstevel@tonic-gate 	}
17420Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
17430Sstevel@tonic-gate }
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate /*
17460Sstevel@tonic-gate  * Release DTR so that asyopen() can raise it.
17470Sstevel@tonic-gate  */
17480Sstevel@tonic-gate static void
17490Sstevel@tonic-gate async_dtr_free(struct asyncline *async)
17500Sstevel@tonic-gate {
17510Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_MODEM,
17540Sstevel@tonic-gate 	    "async_dtr_free, clearing ASYNC_DTR_DELAY\n");
17550Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
17560Sstevel@tonic-gate 	async->async_flags &= ~ASYNC_DTR_DELAY;
17570Sstevel@tonic-gate 	async->async_dtrtid = 0;
17580Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
17590Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
17600Sstevel@tonic-gate }
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate /*
17630Sstevel@tonic-gate  * Close routine.
17640Sstevel@tonic-gate  */
17650Sstevel@tonic-gate /*ARGSUSED2*/
17660Sstevel@tonic-gate static int
17670Sstevel@tonic-gate asyclose(queue_t *q, int flag, cred_t *credp)
17680Sstevel@tonic-gate {
17690Sstevel@tonic-gate 	struct asyncline *async;
17700Sstevel@tonic-gate 	struct asycom	 *asy;
17710Sstevel@tonic-gate 	int icr, lcr;
17720Sstevel@tonic-gate #ifdef DEBUG
17730Sstevel@tonic-gate 	int instance;
17740Sstevel@tonic-gate #endif
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	async = (struct asyncline *)q->q_ptr;
17770Sstevel@tonic-gate 	ASSERT(async != NULL);
17780Sstevel@tonic-gate #ifdef DEBUG
17790Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
17800Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_CLOSE, "asy%dclose\n", instance);
17810Sstevel@tonic-gate #endif
17820Sstevel@tonic-gate 	asy = async->async_common;
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
17850Sstevel@tonic-gate 	async->async_flags |= ASYNC_CLOSING;
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	/*
17880Sstevel@tonic-gate 	 * Turn off PPS handling early to avoid events occuring during
17890Sstevel@tonic-gate 	 * close.  Also reset the DCD edge monitoring bit.
17900Sstevel@tonic-gate 	 */
17910Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
17920Sstevel@tonic-gate 	asy->asy_flags &= ~(ASY_PPS | ASY_PPS_EDGE);
17930Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	/*
17960Sstevel@tonic-gate 	 * There are two flavors of break -- timed (M_BREAK or TCSBRK) and
17970Sstevel@tonic-gate 	 * untimed (TIOCSBRK).  For the timed case, these are enqueued on our
17980Sstevel@tonic-gate 	 * write queue and there's a timer running, so we don't have to worry
17990Sstevel@tonic-gate 	 * about them.  For the untimed case, though, the user obviously made a
18000Sstevel@tonic-gate 	 * mistake, because these are handled immediately.  We'll terminate the
18010Sstevel@tonic-gate 	 * break now and honor his implicit request by discarding the rest of
18020Sstevel@tonic-gate 	 * the data.
18030Sstevel@tonic-gate 	 */
18040Sstevel@tonic-gate 	if (async->async_flags & ASYNC_OUT_SUSPEND) {
18050Sstevel@tonic-gate 		if (async->async_utbrktid != 0) {
18060Sstevel@tonic-gate 			(void) untimeout(async->async_utbrktid);
18070Sstevel@tonic-gate 			async->async_utbrktid = 0;
18080Sstevel@tonic-gate 		}
18090Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
18101106Smrj 		lcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
18111106Smrj 		ddi_put8(asy->asy_iohandle,
18120Sstevel@tonic-gate 		    asy->asy_ioaddr + LCR, (lcr & ~SETBREAK));
18130Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
18140Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_SUSPEND;
18150Sstevel@tonic-gate 		goto nodrain;
18160Sstevel@tonic-gate 	}
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 	/*
18190Sstevel@tonic-gate 	 * If the user told us not to delay the close ("non-blocking"), then
18200Sstevel@tonic-gate 	 * don't bother trying to drain.
18210Sstevel@tonic-gate 	 *
18220Sstevel@tonic-gate 	 * If the user did M_STOP (ASYNC_STOPPED), there's no hope of ever
18230Sstevel@tonic-gate 	 * getting an M_START (since these messages aren't enqueued), and the
18240Sstevel@tonic-gate 	 * only other way to clear the stop condition is by loss of DCD, which
18250Sstevel@tonic-gate 	 * would discard the queue data.  Thus, we drop the output data if
18260Sstevel@tonic-gate 	 * ASYNC_STOPPED is set.
18270Sstevel@tonic-gate 	 */
18280Sstevel@tonic-gate 	if ((flag & (FNDELAY|FNONBLOCK)) ||
18290Sstevel@tonic-gate 	    (async->async_flags & ASYNC_STOPPED)) {
18300Sstevel@tonic-gate 		goto nodrain;
18310Sstevel@tonic-gate 	}
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 	/*
18340Sstevel@tonic-gate 	 * If there's any pending output, then we have to try to drain it.
18350Sstevel@tonic-gate 	 * There are two main cases to be handled:
18360Sstevel@tonic-gate 	 *	- called by close(2): need to drain until done or until
18370Sstevel@tonic-gate 	 *	  a signal is received.  No timeout.
18380Sstevel@tonic-gate 	 *	- called by exit(2): need to drain while making progress
18390Sstevel@tonic-gate 	 *	  or until a timeout occurs.  No signals.
18400Sstevel@tonic-gate 	 *
18410Sstevel@tonic-gate 	 * If we can't rely on receiving a signal to get us out of a hung
18420Sstevel@tonic-gate 	 * session, then we have to use a timer.  In this case, we set a timer
18430Sstevel@tonic-gate 	 * to check for progress in sending the output data -- all that we ask
18440Sstevel@tonic-gate 	 * (at each interval) is that there's been some progress made.  Since
18450Sstevel@tonic-gate 	 * the interrupt routine grabs buffers from the write queue, we can't
18460Sstevel@tonic-gate 	 * trust changes in async_ocnt.  Instead, we use a progress flag.
18470Sstevel@tonic-gate 	 *
18480Sstevel@tonic-gate 	 * Note that loss of carrier will cause the output queue to be flushed,
18490Sstevel@tonic-gate 	 * and we'll wake up again and finish normally.
18500Sstevel@tonic-gate 	 */
18510Sstevel@tonic-gate 	if (!ddi_can_receive_sig() && asy_drain_check != 0) {
18520Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_PROGRESS;
18530Sstevel@tonic-gate 		async->async_timer = timeout(async_progress_check, async,
18540Sstevel@tonic-gate 		    drv_usectohz(asy_drain_check));
18550Sstevel@tonic-gate 	}
18560Sstevel@tonic-gate 	while (async->async_ocnt > 0 ||
18570Sstevel@tonic-gate 	    async->async_ttycommon.t_writeq->q_first != NULL ||
18580Sstevel@tonic-gate 	    (async->async_flags & (ASYNC_BUSY|ASYNC_BREAK|ASYNC_DELAY))) {
18590Sstevel@tonic-gate 		if (cv_wait_sig(&async->async_flags_cv, &asy->asy_excl) == 0)
18600Sstevel@tonic-gate 			break;
18610Sstevel@tonic-gate 	}
18620Sstevel@tonic-gate 	if (async->async_timer != 0) {
18630Sstevel@tonic-gate 		(void) untimeout(async->async_timer);
18640Sstevel@tonic-gate 		async->async_timer = 0;
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate nodrain:
18680Sstevel@tonic-gate 	async->async_ocnt = 0;
18690Sstevel@tonic-gate 	if (async->async_xmitblk != NULL)
18700Sstevel@tonic-gate 		freeb(async->async_xmitblk);
18710Sstevel@tonic-gate 	async->async_xmitblk = NULL;
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 	/*
18740Sstevel@tonic-gate 	 * If line has HUPCL set or is incompletely opened fix up the modem
18750Sstevel@tonic-gate 	 * lines.
18760Sstevel@tonic-gate 	 */
18775295Srandyf 	DEBUGCONT1(ASY_DEBUG_MODEM, "asy%dclose: next check HUPCL flag\n",
18785295Srandyf 	    instance);
18790Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
18800Sstevel@tonic-gate 	if ((async->async_ttycommon.t_cflag & HUPCL) ||
18810Sstevel@tonic-gate 	    (async->async_flags & ASYNC_WOPEN)) {
18820Sstevel@tonic-gate 		DEBUGCONT3(ASY_DEBUG_MODEM,
18835295Srandyf 		    "asy%dclose: HUPCL flag = %x, ASYNC_WOPEN flag = %x\n",
18845295Srandyf 		    instance,
18855295Srandyf 		    async->async_ttycommon.t_cflag & HUPCL,
18865295Srandyf 		    async->async_ttycommon.t_cflag & ASYNC_WOPEN);
18870Sstevel@tonic-gate 		async->async_flags |= ASYNC_DTR_DELAY;
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 		/* turn off DTR, RTS but NOT interrupt to 386 */
18900Sstevel@tonic-gate 		if (asy->asy_flags & (ASY_IGNORE_CD|ASY_RTS_DTR_OFF)) {
18910Sstevel@tonic-gate 			DEBUGCONT3(ASY_DEBUG_MODEM,
18925295Srandyf 			    "asy%dclose: ASY_IGNORE_CD flag = %x, "
18935295Srandyf 			    "ASY_RTS_DTR_OFF flag = %x\n",
18945295Srandyf 			    instance,
18955295Srandyf 			    asy->asy_flags & ASY_IGNORE_CD,
18965295Srandyf 			    asy->asy_flags & ASY_RTS_DTR_OFF);
18975295Srandyf 
18985295Srandyf 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
18995295Srandyf 			    asy->asy_mcr|OUT2);
19000Sstevel@tonic-gate 		} else {
19010Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
19020Sstevel@tonic-gate 			    "asy%dclose: Dropping DTR and RTS\n", instance);
19035295Srandyf 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
19045295Srandyf 			    OUT2);
19050Sstevel@tonic-gate 		}
19060Sstevel@tonic-gate 		async->async_dtrtid =
19070Sstevel@tonic-gate 		    timeout((void (*)())async_dtr_free,
19080Sstevel@tonic-gate 		    (caddr_t)async, drv_usectohz(asy_min_dtr_low));
19090Sstevel@tonic-gate 	}
19100Sstevel@tonic-gate 	/*
19110Sstevel@tonic-gate 	 * If nobody's using it now, turn off receiver interrupts.
19120Sstevel@tonic-gate 	 */
19130Sstevel@tonic-gate 	if ((async->async_flags & (ASYNC_WOPEN|ASYNC_ISOPEN)) == 0) {
19145295Srandyf 		icr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + ICR);
19151106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR,
19165295Srandyf 		    (icr & ~RIEN));
19170Sstevel@tonic-gate 	}
19180Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
19190Sstevel@tonic-gate out:
19200Sstevel@tonic-gate 	ttycommon_close(&async->async_ttycommon);
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 	/*
19230Sstevel@tonic-gate 	 * Cancel outstanding "bufcall" request.
19240Sstevel@tonic-gate 	 */
19250Sstevel@tonic-gate 	if (async->async_wbufcid != 0) {
19260Sstevel@tonic-gate 		unbufcall(async->async_wbufcid);
19270Sstevel@tonic-gate 		async->async_wbufcid = 0;
19280Sstevel@tonic-gate 	}
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	/* Note that qprocsoff can't be done until after interrupts are off */
19310Sstevel@tonic-gate 	qprocsoff(q);
19320Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
19330Sstevel@tonic-gate 	async->async_ttycommon.t_readq = NULL;
19340Sstevel@tonic-gate 	async->async_ttycommon.t_writeq = NULL;
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 	/*
19370Sstevel@tonic-gate 	 * Clear out device state, except persistant device property flags.
19380Sstevel@tonic-gate 	 */
19390Sstevel@tonic-gate 	async->async_flags &= (ASYNC_DTR_DELAY|ASY_RTS_DTR_OFF);
19400Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
19410Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_CLOSE, "asy%dclose: done\n", instance);
19440Sstevel@tonic-gate 	return (0);
19450Sstevel@tonic-gate }
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate static boolean_t
19480Sstevel@tonic-gate asy_isbusy(struct asycom *asy)
19490Sstevel@tonic-gate {
19500Sstevel@tonic-gate 	struct asyncline *async;
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_EOT, "asy_isbusy\n");
19530Sstevel@tonic-gate 	async = asy->asy_priv;
19540Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
19550Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
19565295Srandyf /*
19575295Srandyf  * XXXX this should be recoded
19585295Srandyf  */
19590Sstevel@tonic-gate 	return ((async->async_ocnt > 0) ||
19605295Srandyf 	    ((ddi_get8(asy->asy_iohandle,
19615295Srandyf 	    asy->asy_ioaddr + LSR) & (XSRE|XHRE)) == 0));
19620Sstevel@tonic-gate }
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate static void
19650Sstevel@tonic-gate asy_waiteot(struct asycom *asy)
19660Sstevel@tonic-gate {
19670Sstevel@tonic-gate 	/*
19680Sstevel@tonic-gate 	 * Wait for the current transmission block and the
19690Sstevel@tonic-gate 	 * current fifo data to transmit. Once this is done
19700Sstevel@tonic-gate 	 * we may go on.
19710Sstevel@tonic-gate 	 */
19720Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_EOT, "asy_waiteot\n");
19730Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
19740Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
19750Sstevel@tonic-gate 	while (asy_isbusy(asy)) {
19760Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
19770Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
19780Sstevel@tonic-gate 		drv_usecwait(10000);		/* wait .01 */
19790Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
19800Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
19810Sstevel@tonic-gate 	}
19820Sstevel@tonic-gate }
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate /* asy_reset_fifo -- flush fifos and [re]program fifo control register */
19850Sstevel@tonic-gate static void
19860Sstevel@tonic-gate asy_reset_fifo(struct asycom *asy, uchar_t flush)
19870Sstevel@tonic-gate {
19880Sstevel@tonic-gate 	uchar_t lcr;
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 	/* On a 16750, we have to set DLAB in order to set FIFOEXTRA. */
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	if (asy->asy_hwtype >= ASY16750) {
19931106Smrj 		lcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
19941106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
19950Sstevel@tonic-gate 		    lcr | DLAB);
19960Sstevel@tonic-gate 	}
19970Sstevel@tonic-gate 
19981106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + FIFOR,
19990Sstevel@tonic-gate 	    asy->asy_fifor | flush);
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 	/* Clear DLAB */
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	if (asy->asy_hwtype >= ASY16750) {
20041106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, lcr);
20050Sstevel@tonic-gate 	}
20060Sstevel@tonic-gate }
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate /*
20090Sstevel@tonic-gate  * Program the ASY port. Most of the async operation is based on the values
20100Sstevel@tonic-gate  * of 'c_iflag' and 'c_cflag'.
20110Sstevel@tonic-gate  */
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate #define	BAUDINDEX(cflg)	(((cflg) & CBAUDEXT) ? \
20140Sstevel@tonic-gate 			(((cflg) & CBAUD) + CBAUD + 1) : ((cflg) & CBAUD))
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate static void
20170Sstevel@tonic-gate asy_program(struct asycom *asy, int mode)
20180Sstevel@tonic-gate {
20190Sstevel@tonic-gate 	struct asyncline *async;
20200Sstevel@tonic-gate 	int baudrate, c_flag;
20210Sstevel@tonic-gate 	int icr, lcr;
20220Sstevel@tonic-gate 	int flush_reg;
20230Sstevel@tonic-gate 	int ocflags;
20240Sstevel@tonic-gate #ifdef DEBUG
20250Sstevel@tonic-gate 	int instance;
20260Sstevel@tonic-gate #endif
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
20290Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	async = asy->asy_priv;
20320Sstevel@tonic-gate #ifdef DEBUG
20330Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
20340Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_PROCS,
20355295Srandyf 	    "asy%d_program: mode = 0x%08X, enter\n", instance, mode);
20360Sstevel@tonic-gate #endif
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 	baudrate = BAUDINDEX(async->async_ttycommon.t_cflag);
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 	async->async_ttycommon.t_cflag &= ~(CIBAUD);
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	if (baudrate > CBAUD) {
20430Sstevel@tonic-gate 		async->async_ttycommon.t_cflag |= CIBAUDEXT;
20440Sstevel@tonic-gate 		async->async_ttycommon.t_cflag |=
20455295Srandyf 		    (((baudrate - CBAUD - 1) << IBSHIFT) & CIBAUD);
20460Sstevel@tonic-gate 	} else {
20470Sstevel@tonic-gate 		async->async_ttycommon.t_cflag &= ~CIBAUDEXT;
20480Sstevel@tonic-gate 		async->async_ttycommon.t_cflag |=
20495295Srandyf 		    ((baudrate << IBSHIFT) & CIBAUD);
20500Sstevel@tonic-gate 	}
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate 	c_flag = async->async_ttycommon.t_cflag &
20535295Srandyf 	    (CLOCAL|CREAD|CSTOPB|CSIZE|PARENB|PARODD|CBAUD|CBAUDEXT);
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 	/* disable interrupts */
20561106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0);
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 	ocflags = asy->asy_ocflag;
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate 	/* flush/reset the status registers */
20611106Smrj 	(void) ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + ISR);
20621106Smrj 	(void) ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR);
20631106Smrj 	asy->asy_msr = flush_reg = ddi_get8(asy->asy_iohandle,
20645295Srandyf 	    asy->asy_ioaddr + MSR);
20650Sstevel@tonic-gate 	/*
20660Sstevel@tonic-gate 	 * The device is programmed in the open sequence, if we
20670Sstevel@tonic-gate 	 * have to hardware handshake, then this is a good time
20680Sstevel@tonic-gate 	 * to check if the device can receive any data.
20690Sstevel@tonic-gate 	 */
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	if ((CRTSCTS & async->async_ttycommon.t_cflag) && !(flush_reg & CTS)) {
20720Sstevel@tonic-gate 		async_flowcontrol_hw_output(asy, FLOW_STOP);
20730Sstevel@tonic-gate 	} else {
20740Sstevel@tonic-gate 		/*
20750Sstevel@tonic-gate 		 * We can not use async_flowcontrol_hw_output(asy, FLOW_START)
20760Sstevel@tonic-gate 		 * here, because if CRTSCTS is clear, we need clear
20770Sstevel@tonic-gate 		 * ASYNC_HW_OUT_FLW bit.
20780Sstevel@tonic-gate 		 */
20790Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_HW_OUT_FLW;
20800Sstevel@tonic-gate 	}
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	/*
20830Sstevel@tonic-gate 	 * If IXON is not set, clear ASYNC_SW_OUT_FLW;
20840Sstevel@tonic-gate 	 * If IXON is set, no matter what IXON flag is before this
20850Sstevel@tonic-gate 	 * function call to asy_program,
20860Sstevel@tonic-gate 	 * we will use the old ASYNC_SW_OUT_FLW status.
20870Sstevel@tonic-gate 	 * Because of handling IXON in the driver, we also should re-calculate
20880Sstevel@tonic-gate 	 * the value of ASYNC_OUT_FLW_RESUME bit, but in fact,
20890Sstevel@tonic-gate 	 * the TCSET* commands which call asy_program
20900Sstevel@tonic-gate 	 * are put into the write queue, so there is no output needed to
20910Sstevel@tonic-gate 	 * be resumed at this point.
20920Sstevel@tonic-gate 	 */
20930Sstevel@tonic-gate 	if (!(IXON & async->async_ttycommon.t_iflag))
20940Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_SW_OUT_FLW;
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate 	/* manually flush receive buffer or fifo (workaround for buggy fifos) */
20970Sstevel@tonic-gate 	if (mode == ASY_INIT)
20980Sstevel@tonic-gate 		if (asy->asy_use_fifo == FIFO_ON) {
20990Sstevel@tonic-gate 			for (flush_reg = asy->asy_fifo_buf; flush_reg-- > 0; ) {
21001106Smrj 				(void) ddi_get8(asy->asy_iohandle,
21015295Srandyf 				    asy->asy_ioaddr + DAT);
21020Sstevel@tonic-gate 			}
21030Sstevel@tonic-gate 		} else {
21041106Smrj 			flush_reg = ddi_get8(asy->asy_iohandle,
21055295Srandyf 			    asy->asy_ioaddr + DAT);
21060Sstevel@tonic-gate 		}
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	if (ocflags != (c_flag & ~CLOCAL) || mode == ASY_INIT) {
21090Sstevel@tonic-gate 		/* Set line control */
21105295Srandyf 		lcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
21110Sstevel@tonic-gate 		lcr &= ~(WLS0|WLS1|STB|PEN|EPS);
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 		if (c_flag & CSTOPB)
21140Sstevel@tonic-gate 			lcr |= STB;	/* 2 stop bits */
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 		if (c_flag & PARENB)
21170Sstevel@tonic-gate 			lcr |= PEN;
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 		if ((c_flag & PARODD) == 0)
21200Sstevel@tonic-gate 			lcr |= EPS;
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 		switch (c_flag & CSIZE) {
21230Sstevel@tonic-gate 		case CS5:
21240Sstevel@tonic-gate 			lcr |= BITS5;
21250Sstevel@tonic-gate 			break;
21260Sstevel@tonic-gate 		case CS6:
21270Sstevel@tonic-gate 			lcr |= BITS6;
21280Sstevel@tonic-gate 			break;
21290Sstevel@tonic-gate 		case CS7:
21300Sstevel@tonic-gate 			lcr |= BITS7;
21310Sstevel@tonic-gate 			break;
21320Sstevel@tonic-gate 		case CS8:
21330Sstevel@tonic-gate 			lcr |= BITS8;
21340Sstevel@tonic-gate 			break;
21350Sstevel@tonic-gate 		}
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 		/* set the baud rate, unless it is "0" */
21385295Srandyf 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, DLAB);
21395295Srandyf 
21400Sstevel@tonic-gate 		if (baudrate != 0) {
21411106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
21425295Srandyf 			    asyspdtab[baudrate] & 0xff);
21431106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR,
21445295Srandyf 			    (asyspdtab[baudrate] >> 8) & 0xff);
21450Sstevel@tonic-gate 		}
21460Sstevel@tonic-gate 		/* set the line control modes */
21471106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, lcr);
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 		/*
21500Sstevel@tonic-gate 		 * If we have a FIFO buffer, enable/flush
21510Sstevel@tonic-gate 		 * at intialize time, flush if transitioning from
21520Sstevel@tonic-gate 		 * CREAD off to CREAD on.
21530Sstevel@tonic-gate 		 */
21540Sstevel@tonic-gate 		if ((ocflags & CREAD) == 0 && (c_flag & CREAD) ||
21550Sstevel@tonic-gate 		    mode == ASY_INIT)
21560Sstevel@tonic-gate 			if (asy->asy_use_fifo == FIFO_ON)
21570Sstevel@tonic-gate 				asy_reset_fifo(asy, FIFORXFLSH);
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 		/* remember the new cflags */
21600Sstevel@tonic-gate 		asy->asy_ocflag = c_flag & ~CLOCAL;
21610Sstevel@tonic-gate 	}
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 	if (baudrate == 0)
21641106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
21655295Srandyf 		    (asy->asy_mcr & RTS) | OUT2);
21660Sstevel@tonic-gate 	else
21671106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
21685295Srandyf 		    asy->asy_mcr | OUT2);
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate 	/*
21710Sstevel@tonic-gate 	 * Call the modem status interrupt handler to check for the carrier
21720Sstevel@tonic-gate 	 * in case CLOCAL was turned off after the carrier came on.
21730Sstevel@tonic-gate 	 * (Note: Modem status interrupt is not enabled if CLOCAL is ON.)
21740Sstevel@tonic-gate 	 */
21750Sstevel@tonic-gate 	async_msint(asy);
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 	/* Set interrupt control */
21780Sstevel@tonic-gate 	DEBUGCONT3(ASY_DEBUG_MODM2,
21795295Srandyf 	    "asy%d_program: c_flag & CLOCAL = %x t_cflag & CRTSCTS = %x\n",
21805295Srandyf 	    instance, c_flag & CLOCAL,
21815295Srandyf 	    async->async_ttycommon.t_cflag & CRTSCTS);
21825295Srandyf 
21830Sstevel@tonic-gate 	if ((c_flag & CLOCAL) && !(async->async_ttycommon.t_cflag & CRTSCTS))
21840Sstevel@tonic-gate 		/*
21850Sstevel@tonic-gate 		 * direct-wired line ignores DCD, so we don't enable modem
21860Sstevel@tonic-gate 		 * status interrupts.
21870Sstevel@tonic-gate 		 */
21880Sstevel@tonic-gate 		icr = (TIEN | SIEN);
21890Sstevel@tonic-gate 	else
21900Sstevel@tonic-gate 		icr = (TIEN | SIEN | MIEN);
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 	if (c_flag & CREAD)
21930Sstevel@tonic-gate 		icr |= RIEN;
21940Sstevel@tonic-gate 
21951106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, icr);
21960Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "asy%d_program: done\n", instance);
21970Sstevel@tonic-gate }
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate static boolean_t
22000Sstevel@tonic-gate asy_baudok(struct asycom *asy)
22010Sstevel@tonic-gate {
22020Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
22030Sstevel@tonic-gate 	int baudrate;
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 
22060Sstevel@tonic-gate 	baudrate = BAUDINDEX(async->async_ttycommon.t_cflag);
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	if (baudrate >= sizeof (asyspdtab)/sizeof (*asyspdtab))
22090Sstevel@tonic-gate 		return (0);
22100Sstevel@tonic-gate 
22110Sstevel@tonic-gate 	return (baudrate == 0 || asyspdtab[baudrate]);
22120Sstevel@tonic-gate }
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate /*
22150Sstevel@tonic-gate  * asyintr() is the High Level Interrupt Handler.
22160Sstevel@tonic-gate  *
22170Sstevel@tonic-gate  * There are four different interrupt types indexed by ISR register values:
22180Sstevel@tonic-gate  *		0: modem
22190Sstevel@tonic-gate  *		1: Tx holding register is empty, ready for next char
22200Sstevel@tonic-gate  *		2: Rx register now holds a char to be picked up
22210Sstevel@tonic-gate  *		3: error or break on line
22220Sstevel@tonic-gate  * This routine checks the Bit 0 (interrupt-not-pending) to determine if
22230Sstevel@tonic-gate  * the interrupt is from this port.
22240Sstevel@tonic-gate  */
22250Sstevel@tonic-gate uint_t
22260Sstevel@tonic-gate asyintr(caddr_t argasy)
22270Sstevel@tonic-gate {
22280Sstevel@tonic-gate 	struct asycom		*asy = (struct asycom *)argasy;
22290Sstevel@tonic-gate 	struct asyncline	*async;
22300Sstevel@tonic-gate 	int			ret_status = DDI_INTR_UNCLAIMED;
22310Sstevel@tonic-gate 	uchar_t			interrupt_id, lsr;
22320Sstevel@tonic-gate 
22331106Smrj 	interrupt_id = ddi_get8(asy->asy_iohandle,
22345295Srandyf 	    asy->asy_ioaddr + ISR) & 0x0F;
22350Sstevel@tonic-gate 	async = asy->asy_priv;
22365295Srandyf 
22370Sstevel@tonic-gate 	if ((async == NULL) || asy_addedsoft == 0 ||
22385295Srandyf 	    !(async->async_flags & (ASYNC_ISOPEN|ASYNC_WOPEN))) {
22390Sstevel@tonic-gate 		if (interrupt_id & NOINTERRUPT)
22400Sstevel@tonic-gate 			return (DDI_INTR_UNCLAIMED);
22410Sstevel@tonic-gate 		else {
22420Sstevel@tonic-gate 			/*
22430Sstevel@tonic-gate 			 * reset the device by:
22440Sstevel@tonic-gate 			 *	reading line status
22450Sstevel@tonic-gate 			 *	reading any data from data status register
22460Sstevel@tonic-gate 			 *	reading modem status
22470Sstevel@tonic-gate 			 */
22481106Smrj 			(void) ddi_get8(asy->asy_iohandle,
22495295Srandyf 			    asy->asy_ioaddr + LSR);
22501106Smrj 			(void) ddi_get8(asy->asy_iohandle,
22515295Srandyf 			    asy->asy_ioaddr + DAT);
22521106Smrj 			asy->asy_msr = ddi_get8(asy->asy_iohandle,
22535295Srandyf 			    asy->asy_ioaddr + MSR);
22540Sstevel@tonic-gate 			return (DDI_INTR_CLAIMED);
22550Sstevel@tonic-gate 		}
22560Sstevel@tonic-gate 	}
22575295Srandyf 
22580Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
22590Sstevel@tonic-gate 	/*
22600Sstevel@tonic-gate 	 * We will loop until the interrupt line is pulled low. asy
22610Sstevel@tonic-gate 	 * interrupt is edge triggered.
22620Sstevel@tonic-gate 	 */
22630Sstevel@tonic-gate 	/* CSTYLED */
22645295Srandyf 	for (;; interrupt_id =
22655295Srandyf 	    (ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + ISR) & 0x0F)) {
22665295Srandyf 
22670Sstevel@tonic-gate 		if (interrupt_id & NOINTERRUPT)
22680Sstevel@tonic-gate 			break;
22690Sstevel@tonic-gate 		ret_status = DDI_INTR_CLAIMED;
22700Sstevel@tonic-gate 
22715295Srandyf 		DEBUGCONT1(ASY_DEBUG_INTR, "asyintr: interrupt_id = 0x%d\n",
22725295Srandyf 		    interrupt_id);
22735295Srandyf 		lsr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR);
22740Sstevel@tonic-gate 		switch (interrupt_id) {
22750Sstevel@tonic-gate 		case RxRDY:
22760Sstevel@tonic-gate 		case RSTATUS:
22770Sstevel@tonic-gate 		case FFTMOUT:
22780Sstevel@tonic-gate 			/* receiver interrupt or receiver errors */
22790Sstevel@tonic-gate 			async_rxint(asy, lsr);
22800Sstevel@tonic-gate 			break;
22810Sstevel@tonic-gate 		case TxRDY:
22820Sstevel@tonic-gate 			/* transmit interrupt */
22830Sstevel@tonic-gate 			async_txint(asy);
22840Sstevel@tonic-gate 			continue;
22850Sstevel@tonic-gate 		case MSTATUS:
22860Sstevel@tonic-gate 			/* modem status interrupt */
22870Sstevel@tonic-gate 			async_msint(asy);
22880Sstevel@tonic-gate 			break;
22890Sstevel@tonic-gate 		}
22900Sstevel@tonic-gate 		if ((lsr & XHRE) && (async->async_flags & ASYNC_BUSY) &&
22910Sstevel@tonic-gate 		    (async->async_ocnt > 0))
22920Sstevel@tonic-gate 			async_txint(asy);
22930Sstevel@tonic-gate 	}
22940Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
22950Sstevel@tonic-gate 	return (ret_status);
22960Sstevel@tonic-gate }
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate /*
22990Sstevel@tonic-gate  * Transmitter interrupt service routine.
23000Sstevel@tonic-gate  * If there is more data to transmit in the current pseudo-DMA block,
23010Sstevel@tonic-gate  * send the next character if output is not stopped or draining.
23020Sstevel@tonic-gate  * Otherwise, queue up a soft interrupt.
23030Sstevel@tonic-gate  *
23040Sstevel@tonic-gate  * XXX -  Needs review for HW FIFOs.
23050Sstevel@tonic-gate  */
23060Sstevel@tonic-gate static void
23070Sstevel@tonic-gate async_txint(struct asycom *asy)
23080Sstevel@tonic-gate {
23090Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
23100Sstevel@tonic-gate 	int		fifo_len;
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 	/*
23130Sstevel@tonic-gate 	 * If ASYNC_BREAK or ASYNC_OUT_SUSPEND has been set, return to
23140Sstevel@tonic-gate 	 * asyintr()'s context to claim the interrupt without performing
23150Sstevel@tonic-gate 	 * any action. No character will be loaded into FIFO/THR until
23160Sstevel@tonic-gate 	 * timed or untimed break is removed
23170Sstevel@tonic-gate 	 */
23180Sstevel@tonic-gate 	if (async->async_flags & (ASYNC_BREAK|ASYNC_OUT_SUSPEND))
23190Sstevel@tonic-gate 		return;
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	fifo_len = asy->asy_fifo_buf; /* with FIFO buffers */
23220Sstevel@tonic-gate 	if (fifo_len > asy_max_tx_fifo)
23230Sstevel@tonic-gate 		fifo_len = asy_max_tx_fifo;
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 	if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
23260Sstevel@tonic-gate 		fifo_len--;
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 	if (async->async_ocnt > 0 && fifo_len > 0 &&
23290Sstevel@tonic-gate 	    !(async->async_flags &
23300Sstevel@tonic-gate 	    (ASYNC_HW_OUT_FLW|ASYNC_SW_OUT_FLW|ASYNC_STOPPED))) {
23310Sstevel@tonic-gate 		while (fifo_len-- > 0 && async->async_ocnt-- > 0) {
23321106Smrj 			ddi_put8(asy->asy_iohandle,
23330Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT, *async->async_optr++);
23340Sstevel@tonic-gate 		}
23350Sstevel@tonic-gate 		async->async_flags |= ASYNC_PROGRESS;
23360Sstevel@tonic-gate 	}
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate 	if (fifo_len <= 0)
23390Sstevel@tonic-gate 		return;
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 	ASYSETSOFT(asy);
23420Sstevel@tonic-gate }
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate /*
23450Sstevel@tonic-gate  * Interrupt on port: handle PPS event.  This function is only called
23460Sstevel@tonic-gate  * for a port on which PPS event handling has been enabled.
23470Sstevel@tonic-gate  */
23480Sstevel@tonic-gate static void
23490Sstevel@tonic-gate asy_ppsevent(struct asycom *asy, int msr)
23500Sstevel@tonic-gate {
23510Sstevel@tonic-gate 	if (asy->asy_flags & ASY_PPS_EDGE) {
23520Sstevel@tonic-gate 		/* Have seen leading edge, now look for and record drop */
23530Sstevel@tonic-gate 		if ((msr & DCD) == 0)
23540Sstevel@tonic-gate 			asy->asy_flags &= ~ASY_PPS_EDGE;
23550Sstevel@tonic-gate 		/*
23560Sstevel@tonic-gate 		 * Waiting for leading edge, look for rise; stamp event and
23570Sstevel@tonic-gate 		 * calibrate kernel clock.
23580Sstevel@tonic-gate 		 */
23590Sstevel@tonic-gate 	} else if (msr & DCD) {
23600Sstevel@tonic-gate 			/*
23610Sstevel@tonic-gate 			 * This code captures a timestamp at the designated
23620Sstevel@tonic-gate 			 * transition of the PPS signal (DCD asserted).  The
23630Sstevel@tonic-gate 			 * code provides a pointer to the timestamp, as well
23640Sstevel@tonic-gate 			 * as the hardware counter value at the capture.
23650Sstevel@tonic-gate 			 *
23660Sstevel@tonic-gate 			 * Note: the kernel has nano based time values while
23670Sstevel@tonic-gate 			 * NTP requires micro based, an in-line fast algorithm
23680Sstevel@tonic-gate 			 * to convert nsec to usec is used here -- see hrt2ts()
23690Sstevel@tonic-gate 			 * in common/os/timers.c for a full description.
23700Sstevel@tonic-gate 			 */
23710Sstevel@tonic-gate 			struct timeval *tvp = &asy_ppsev.tv;
23720Sstevel@tonic-gate 			timestruc_t ts;
23730Sstevel@tonic-gate 			long nsec, usec;
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 			asy->asy_flags |= ASY_PPS_EDGE;
23760Sstevel@tonic-gate 			LED_OFF;
23770Sstevel@tonic-gate 			gethrestime(&ts);
23780Sstevel@tonic-gate 			LED_ON;
23790Sstevel@tonic-gate 			nsec = ts.tv_nsec;
23800Sstevel@tonic-gate 			usec = nsec + (nsec >> 2);
23810Sstevel@tonic-gate 			usec = nsec + (usec >> 1);
23820Sstevel@tonic-gate 			usec = nsec + (usec >> 2);
23830Sstevel@tonic-gate 			usec = nsec + (usec >> 4);
23840Sstevel@tonic-gate 			usec = nsec - (usec >> 3);
23850Sstevel@tonic-gate 			usec = nsec + (usec >> 2);
23860Sstevel@tonic-gate 			usec = nsec + (usec >> 3);
23870Sstevel@tonic-gate 			usec = nsec + (usec >> 4);
23880Sstevel@tonic-gate 			usec = nsec + (usec >> 1);
23890Sstevel@tonic-gate 			usec = nsec + (usec >> 6);
23900Sstevel@tonic-gate 			tvp->tv_usec = usec >> 10;
23910Sstevel@tonic-gate 			tvp->tv_sec = ts.tv_sec;
23920Sstevel@tonic-gate 
23930Sstevel@tonic-gate 			++asy_ppsev.serial;
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 			/*
23960Sstevel@tonic-gate 			 * Because the kernel keeps a high-resolution time,
23970Sstevel@tonic-gate 			 * pass the current highres timestamp in tvp and zero
23980Sstevel@tonic-gate 			 * in usec.
23990Sstevel@tonic-gate 			 */
24000Sstevel@tonic-gate 			ddi_hardpps(tvp, 0);
24010Sstevel@tonic-gate 	}
24020Sstevel@tonic-gate }
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate /*
24050Sstevel@tonic-gate  * Receiver interrupt: RxRDY interrupt, FIFO timeout interrupt or receive
24060Sstevel@tonic-gate  * error interrupt.
24070Sstevel@tonic-gate  * Try to put the character into the circular buffer for this line; if it
24080Sstevel@tonic-gate  * overflows, indicate a circular buffer overrun. If this port is always
24090Sstevel@tonic-gate  * to be serviced immediately, or the character is a STOP character, or
24100Sstevel@tonic-gate  * more than 15 characters have arrived, queue up a soft interrupt to
24110Sstevel@tonic-gate  * drain the circular buffer.
24120Sstevel@tonic-gate  * XXX - needs review for hw FIFOs support.
24130Sstevel@tonic-gate  */
24140Sstevel@tonic-gate 
24150Sstevel@tonic-gate static void
24160Sstevel@tonic-gate async_rxint(struct asycom *asy, uchar_t lsr)
24170Sstevel@tonic-gate {
24180Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
24190Sstevel@tonic-gate 	uchar_t c;
24200Sstevel@tonic-gate 	uint_t s, needsoft = 0;
24210Sstevel@tonic-gate 	tty_common_t *tp;
24220Sstevel@tonic-gate 	int looplim = asy->asy_fifo_buf * 2;
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	tp = &async->async_ttycommon;
24250Sstevel@tonic-gate 	if (!(tp->t_cflag & CREAD)) {
24260Sstevel@tonic-gate 		while (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
24271106Smrj 			(void) (ddi_get8(asy->asy_iohandle,
24285295Srandyf 			    asy->asy_ioaddr + DAT) & 0xff);
24291106Smrj 			lsr = ddi_get8(asy->asy_iohandle,
24305295Srandyf 			    asy->asy_ioaddr + LSR);
24310Sstevel@tonic-gate 			if (looplim-- < 0)		/* limit loop */
24320Sstevel@tonic-gate 				break;
24330Sstevel@tonic-gate 		}
24340Sstevel@tonic-gate 		return; /* line is not open for read? */
24350Sstevel@tonic-gate 	}
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate 	while (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
24380Sstevel@tonic-gate 		c = 0;
24390Sstevel@tonic-gate 		s = 0;				/* reset error status */
24400Sstevel@tonic-gate 		if (lsr & RCA) {
24411106Smrj 			c = ddi_get8(asy->asy_iohandle,
24425295Srandyf 			    asy->asy_ioaddr + DAT) & 0xff;
24430Sstevel@tonic-gate 
24440Sstevel@tonic-gate 			/*
24450Sstevel@tonic-gate 			 * We handle XON/XOFF char if IXON is set,
24460Sstevel@tonic-gate 			 * but if received char is _POSIX_VDISABLE,
24470Sstevel@tonic-gate 			 * we left it to the up level module.
24480Sstevel@tonic-gate 			 */
24490Sstevel@tonic-gate 			if (tp->t_iflag & IXON) {
24500Sstevel@tonic-gate 				if ((c == async->async_stopc) &&
24510Sstevel@tonic-gate 				    (c != _POSIX_VDISABLE)) {
24520Sstevel@tonic-gate 					async_flowcontrol_sw_output(asy,
24530Sstevel@tonic-gate 					    FLOW_STOP);
24540Sstevel@tonic-gate 					goto check_looplim;
24550Sstevel@tonic-gate 				} else if ((c == async->async_startc) &&
24560Sstevel@tonic-gate 				    (c != _POSIX_VDISABLE)) {
24570Sstevel@tonic-gate 					async_flowcontrol_sw_output(asy,
24580Sstevel@tonic-gate 					    FLOW_START);
24590Sstevel@tonic-gate 					needsoft = 1;
24600Sstevel@tonic-gate 					goto check_looplim;
24610Sstevel@tonic-gate 				}
24620Sstevel@tonic-gate 				if ((tp->t_iflag & IXANY) &&
24630Sstevel@tonic-gate 				    (async->async_flags & ASYNC_SW_OUT_FLW)) {
24640Sstevel@tonic-gate 					async_flowcontrol_sw_output(asy,
24650Sstevel@tonic-gate 					    FLOW_START);
24660Sstevel@tonic-gate 					needsoft = 1;
24670Sstevel@tonic-gate 				}
24680Sstevel@tonic-gate 			}
24690Sstevel@tonic-gate 		}
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 		/*
24720Sstevel@tonic-gate 		 * Check for character break sequence
24730Sstevel@tonic-gate 		 */
24740Sstevel@tonic-gate 		if ((abort_enable == KIOCABORTALTERNATE) &&
24750Sstevel@tonic-gate 		    (asy->asy_flags & ASY_CONSOLE)) {
24760Sstevel@tonic-gate 			if (abort_charseq_recognize(c))
24770Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
24780Sstevel@tonic-gate 		}
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate 		/* Handle framing errors */
24810Sstevel@tonic-gate 		if (lsr & (PARERR|FRMERR|BRKDET|OVRRUN)) {
24820Sstevel@tonic-gate 			if (lsr & PARERR) {
24830Sstevel@tonic-gate 				if (tp->t_iflag & INPCK) /* parity enabled */
24840Sstevel@tonic-gate 					s |= PERROR;
24850Sstevel@tonic-gate 			}
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 			if (lsr & (FRMERR|BRKDET))
24880Sstevel@tonic-gate 				s |= FRERROR;
24890Sstevel@tonic-gate 			if (lsr & OVRRUN) {
24900Sstevel@tonic-gate 				async->async_hw_overrun = 1;
24910Sstevel@tonic-gate 				s |= OVERRUN;
24920Sstevel@tonic-gate 			}
24930Sstevel@tonic-gate 		}
24940Sstevel@tonic-gate 
24950Sstevel@tonic-gate 		if (s == 0)
24960Sstevel@tonic-gate 			if ((tp->t_iflag & PARMRK) &&
24970Sstevel@tonic-gate 			    !(tp->t_iflag & (IGNPAR|ISTRIP)) &&
24980Sstevel@tonic-gate 			    (c == 0377))
24990Sstevel@tonic-gate 				if (RING_POK(async, 2)) {
25000Sstevel@tonic-gate 					RING_PUT(async, 0377);
25010Sstevel@tonic-gate 					RING_PUT(async, c);
25020Sstevel@tonic-gate 				} else
25030Sstevel@tonic-gate 					async->async_sw_overrun = 1;
25040Sstevel@tonic-gate 			else
25050Sstevel@tonic-gate 				if (RING_POK(async, 1))
25060Sstevel@tonic-gate 					RING_PUT(async, c);
25070Sstevel@tonic-gate 				else
25080Sstevel@tonic-gate 					async->async_sw_overrun = 1;
25090Sstevel@tonic-gate 		else
25100Sstevel@tonic-gate 			if (s & FRERROR) /* Handle framing errors */
25110Sstevel@tonic-gate 				if (c == 0)
25120Sstevel@tonic-gate 					if ((asy->asy_flags & ASY_CONSOLE) &&
25130Sstevel@tonic-gate 					    (abort_enable !=
25140Sstevel@tonic-gate 					    KIOCABORTALTERNATE))
25150Sstevel@tonic-gate 						abort_sequence_enter((char *)0);
25160Sstevel@tonic-gate 					else
25170Sstevel@tonic-gate 						async->async_break++;
25180Sstevel@tonic-gate 				else
25190Sstevel@tonic-gate 					if (RING_POK(async, 1))
25200Sstevel@tonic-gate 						RING_MARK(async, c, s);
25210Sstevel@tonic-gate 					else
25220Sstevel@tonic-gate 						async->async_sw_overrun = 1;
25230Sstevel@tonic-gate 			else /* Parity errors are handled by ldterm */
25240Sstevel@tonic-gate 				if (RING_POK(async, 1))
25250Sstevel@tonic-gate 					RING_MARK(async, c, s);
25260Sstevel@tonic-gate 				else
25270Sstevel@tonic-gate 					async->async_sw_overrun = 1;
25280Sstevel@tonic-gate check_looplim:
25295295Srandyf 		lsr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR);
25300Sstevel@tonic-gate 		if (looplim-- < 0)		/* limit loop */
25310Sstevel@tonic-gate 			break;
25320Sstevel@tonic-gate 	}
25330Sstevel@tonic-gate 	if ((RING_CNT(async) > (RINGSIZE * 3)/4) &&
25340Sstevel@tonic-gate 	    !(async->async_inflow_source & IN_FLOW_RINGBUFF)) {
25350Sstevel@tonic-gate 		async_flowcontrol_hw_input(asy, FLOW_STOP, IN_FLOW_RINGBUFF);
25360Sstevel@tonic-gate 		(void) async_flowcontrol_sw_input(asy, FLOW_STOP,
25370Sstevel@tonic-gate 		    IN_FLOW_RINGBUFF);
25380Sstevel@tonic-gate 	}
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 	if ((async->async_flags & ASYNC_SERVICEIMM) || needsoft ||
25410Sstevel@tonic-gate 	    (RING_FRAC(async)) || (async->async_polltid == 0))
25420Sstevel@tonic-gate 		ASYSETSOFT(asy);	/* need a soft interrupt */
25430Sstevel@tonic-gate }
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate /*
25460Sstevel@tonic-gate  * Modem status interrupt.
25470Sstevel@tonic-gate  *
25480Sstevel@tonic-gate  * (Note: It is assumed that the MSR hasn't been read by asyintr().)
25490Sstevel@tonic-gate  */
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate static void
25520Sstevel@tonic-gate async_msint(struct asycom *asy)
25530Sstevel@tonic-gate {
25540Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
25550Sstevel@tonic-gate 	int msr, t_cflag = async->async_ttycommon.t_cflag;
25560Sstevel@tonic-gate #ifdef DEBUG
25570Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
25580Sstevel@tonic-gate #endif
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate async_msint_retry:
25610Sstevel@tonic-gate 	/* this resets the interrupt */
25621106Smrj 	msr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MSR);
25630Sstevel@tonic-gate 	DEBUGCONT10(ASY_DEBUG_STATE,
25645295Srandyf 	    "async%d_msint call #%d:\n"
25655295Srandyf 	    "   transition: %3s %3s %3s %3s\n"
25665295Srandyf 	    "current state: %3s %3s %3s %3s\n",
25675295Srandyf 	    instance,
25685295Srandyf 	    ++(asy->asy_msint_cnt),
25695295Srandyf 	    (msr & DCTS) ? "DCTS" : "    ",
25705295Srandyf 	    (msr & DDSR) ? "DDSR" : "    ",
25715295Srandyf 	    (msr & DRI)  ? "DRI " : "    ",
25725295Srandyf 	    (msr & DDCD) ? "DDCD" : "    ",
25735295Srandyf 	    (msr & CTS)  ? "CTS " : "    ",
25745295Srandyf 	    (msr & DSR)  ? "DSR " : "    ",
25755295Srandyf 	    (msr & RI)   ? "RI  " : "    ",
25765295Srandyf 	    (msr & DCD)  ? "DCD " : "    ");
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate 	/* If CTS status is changed, do H/W output flow control */
25790Sstevel@tonic-gate 	if ((t_cflag & CRTSCTS) && (((asy->asy_msr ^ msr) & CTS) != 0))
25800Sstevel@tonic-gate 		async_flowcontrol_hw_output(asy,
25810Sstevel@tonic-gate 		    msr & CTS ? FLOW_START : FLOW_STOP);
25820Sstevel@tonic-gate 	/*
25830Sstevel@tonic-gate 	 * Reading MSR resets the interrupt, we save the
25840Sstevel@tonic-gate 	 * value of msr so that other functions could examine MSR by
25850Sstevel@tonic-gate 	 * looking at asy_msr.
25860Sstevel@tonic-gate 	 */
25870Sstevel@tonic-gate 	asy->asy_msr = (uchar_t)msr;
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 	/* Handle PPS event */
25900Sstevel@tonic-gate 	if (asy->asy_flags & ASY_PPS)
25910Sstevel@tonic-gate 		asy_ppsevent(asy, msr);
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	async->async_ext++;
25940Sstevel@tonic-gate 	ASYSETSOFT(asy);
25950Sstevel@tonic-gate 	/*
25960Sstevel@tonic-gate 	 * We will make sure that the modem status presented to us
25970Sstevel@tonic-gate 	 * during the previous read has not changed. If the chip samples
25980Sstevel@tonic-gate 	 * the modem status on the falling edge of the interrupt line,
25990Sstevel@tonic-gate 	 * and uses this state as the base for detecting change of modem
26000Sstevel@tonic-gate 	 * status, we would miss a change of modem status event that occured
26010Sstevel@tonic-gate 	 * after we initiated a read MSR operation.
26020Sstevel@tonic-gate 	 */
26031106Smrj 	msr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MSR);
26040Sstevel@tonic-gate 	if (STATES(msr) != STATES(asy->asy_msr))
26050Sstevel@tonic-gate 		goto	async_msint_retry;
26060Sstevel@tonic-gate }
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate /*
26090Sstevel@tonic-gate  * Handle a second-stage interrupt.
26100Sstevel@tonic-gate  */
26110Sstevel@tonic-gate /*ARGSUSED*/
26120Sstevel@tonic-gate uint_t
26130Sstevel@tonic-gate asysoftintr(caddr_t intarg)
26140Sstevel@tonic-gate {
26150Sstevel@tonic-gate 	struct asycom *asy;
26160Sstevel@tonic-gate 	int rv;
26170Sstevel@tonic-gate 	int instance;
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 	/*
26200Sstevel@tonic-gate 	 * Test and clear soft interrupt.
26210Sstevel@tonic-gate 	 */
26220Sstevel@tonic-gate 	mutex_enter(&asy_soft_lock);
26230Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_PROCS, "asysoftintr: enter\n");
26240Sstevel@tonic-gate 	rv = asysoftpend;
26250Sstevel@tonic-gate 	if (rv != 0)
26260Sstevel@tonic-gate 		asysoftpend = 0;
26270Sstevel@tonic-gate 	mutex_exit(&asy_soft_lock);
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 	if (rv) {
26300Sstevel@tonic-gate 		/*
26310Sstevel@tonic-gate 		 * Note - we can optimize the loop by remembering the last
26320Sstevel@tonic-gate 		 * device that requested soft interrupt
26330Sstevel@tonic-gate 		 */
26340Sstevel@tonic-gate 		for (instance = 0; instance <= max_asy_instance; instance++) {
26350Sstevel@tonic-gate 			asy = ddi_get_soft_state(asy_soft_state, instance);
26360Sstevel@tonic-gate 			if (asy == NULL || asy->asy_priv == NULL)
26370Sstevel@tonic-gate 				continue;
26380Sstevel@tonic-gate 			mutex_enter(&asy_soft_lock);
26390Sstevel@tonic-gate 			if (asy->asy_flags & ASY_NEEDSOFT) {
26400Sstevel@tonic-gate 				asy->asy_flags &= ~ASY_NEEDSOFT;
26410Sstevel@tonic-gate 				mutex_exit(&asy_soft_lock);
26420Sstevel@tonic-gate 				async_softint(asy);
26430Sstevel@tonic-gate 			} else
26440Sstevel@tonic-gate 				mutex_exit(&asy_soft_lock);
26450Sstevel@tonic-gate 		}
26460Sstevel@tonic-gate 	}
26470Sstevel@tonic-gate 	return (rv ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED);
26480Sstevel@tonic-gate }
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate /*
26510Sstevel@tonic-gate  * Handle a software interrupt.
26520Sstevel@tonic-gate  */
26530Sstevel@tonic-gate static void
26540Sstevel@tonic-gate async_softint(struct asycom *asy)
26550Sstevel@tonic-gate {
26560Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
26570Sstevel@tonic-gate 	short	cc;
26580Sstevel@tonic-gate 	mblk_t	*bp;
26590Sstevel@tonic-gate 	queue_t	*q;
26600Sstevel@tonic-gate 	uchar_t	val;
26610Sstevel@tonic-gate 	uchar_t	c;
26620Sstevel@tonic-gate 	tty_common_t	*tp;
26630Sstevel@tonic-gate 	int nb;
26640Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
26650Sstevel@tonic-gate 
26660Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_softint\n", instance);
26670Sstevel@tonic-gate 	mutex_enter(&asy_soft_lock);
26680Sstevel@tonic-gate 	if (asy->asy_flags & ASY_DOINGSOFT) {
26690Sstevel@tonic-gate 		asy->asy_flags |= ASY_DOINGSOFT_RETRY;
26700Sstevel@tonic-gate 		mutex_exit(&asy_soft_lock);
26710Sstevel@tonic-gate 		return;
26720Sstevel@tonic-gate 	}
26730Sstevel@tonic-gate 	asy->asy_flags |= ASY_DOINGSOFT;
26740Sstevel@tonic-gate begin:
26750Sstevel@tonic-gate 	asy->asy_flags &= ~ASY_DOINGSOFT_RETRY;
26760Sstevel@tonic-gate 	mutex_exit(&asy_soft_lock);
26770Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
26780Sstevel@tonic-gate 	tp = &async->async_ttycommon;
26790Sstevel@tonic-gate 	q = tp->t_readq;
26800Sstevel@tonic-gate 	if (async->async_flags & ASYNC_OUT_FLW_RESUME) {
26810Sstevel@tonic-gate 		if (async->async_ocnt > 0) {
26820Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
26830Sstevel@tonic-gate 			async_resume(async);
26840Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
26850Sstevel@tonic-gate 		} else {
26860Sstevel@tonic-gate 			if (async->async_xmitblk)
26870Sstevel@tonic-gate 				freeb(async->async_xmitblk);
26880Sstevel@tonic-gate 			async->async_xmitblk = NULL;
26890Sstevel@tonic-gate 			async_start(async);
26900Sstevel@tonic-gate 		}
26910Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
26920Sstevel@tonic-gate 	}
26930Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
26940Sstevel@tonic-gate 	if (async->async_ext) {
26950Sstevel@tonic-gate 		async->async_ext = 0;
26960Sstevel@tonic-gate 		/* check for carrier up */
26970Sstevel@tonic-gate 		DEBUGCONT3(ASY_DEBUG_MODM2,
26985295Srandyf 		    "async%d_softint: asy_msr & DCD = %x, "
26995295Srandyf 		    "tp->t_flags & TS_SOFTCAR = %x\n",
27005295Srandyf 		    instance, asy->asy_msr & DCD, tp->t_flags & TS_SOFTCAR);
27015295Srandyf 
27020Sstevel@tonic-gate 		if (asy->asy_msr & DCD) {
27030Sstevel@tonic-gate 			/* carrier present */
27040Sstevel@tonic-gate 			if ((async->async_flags & ASYNC_CARR_ON) == 0) {
27050Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_MODM2,
27065295Srandyf 				    "async%d_softint: set ASYNC_CARR_ON\n",
27075295Srandyf 				    instance);
27080Sstevel@tonic-gate 				async->async_flags |= ASYNC_CARR_ON;
27090Sstevel@tonic-gate 				if (async->async_flags & ASYNC_ISOPEN) {
27100Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
27110Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
27120Sstevel@tonic-gate 					(void) putctl(q, M_UNHANGUP);
27130Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
27140Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
27150Sstevel@tonic-gate 				}
27160Sstevel@tonic-gate 				cv_broadcast(&async->async_flags_cv);
27170Sstevel@tonic-gate 			}
27180Sstevel@tonic-gate 		} else {
27190Sstevel@tonic-gate 			if ((async->async_flags & ASYNC_CARR_ON) &&
27200Sstevel@tonic-gate 			    !(tp->t_cflag & CLOCAL) &&
27210Sstevel@tonic-gate 			    !(tp->t_flags & TS_SOFTCAR)) {
27220Sstevel@tonic-gate 				int flushflag;
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_MODEM,
27255295Srandyf 				    "async%d_softint: carrier dropped, "
27265295Srandyf 				    "so drop DTR\n",
27275295Srandyf 				    instance);
27280Sstevel@tonic-gate 				/*
27290Sstevel@tonic-gate 				 * Carrier went away.
27300Sstevel@tonic-gate 				 * Drop DTR, abort any output in
27310Sstevel@tonic-gate 				 * progress, indicate that output is
27320Sstevel@tonic-gate 				 * not stopped, and send a hangup
27330Sstevel@tonic-gate 				 * notification upstream.
27340Sstevel@tonic-gate 				 */
27351106Smrj 				val = ddi_get8(asy->asy_iohandle,
27365295Srandyf 				    asy->asy_ioaddr + MCR);
27371106Smrj 				ddi_put8(asy->asy_iohandle,
27380Sstevel@tonic-gate 				    asy->asy_ioaddr + MCR, (val & ~DTR));
27395295Srandyf 
27400Sstevel@tonic-gate 				if (async->async_flags & ASYNC_BUSY) {
27415295Srandyf 					DEBUGCONT0(ASY_DEBUG_BUSY,
27420Sstevel@tonic-gate 					    "async_softint: "
27430Sstevel@tonic-gate 					    "Carrier dropped.  "
27440Sstevel@tonic-gate 					    "Clearing async_ocnt\n");
27455295Srandyf 					async->async_ocnt = 0;
27460Sstevel@tonic-gate 				}	/* if */
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_STOPPED;
27490Sstevel@tonic-gate 				if (async->async_flags & ASYNC_ISOPEN) {
27505295Srandyf 					mutex_exit(&asy->asy_excl_hi);
27515295Srandyf 					mutex_exit(&asy->asy_excl);
27525295Srandyf 					(void) putctl(q, M_HANGUP);
27535295Srandyf 					mutex_enter(&asy->asy_excl);
27545295Srandyf 					DEBUGCONT1(ASY_DEBUG_MODEM,
27555295Srandyf 					    "async%d_softint: "
27565295Srandyf 					    "putctl(q, M_HANGUP)\n",
27575295Srandyf 					    instance);
27585295Srandyf 					/*
27595295Srandyf 					 * Flush FIFO buffers
27605295Srandyf 					 * Any data left in there is invalid now
27615295Srandyf 					 */
27625295Srandyf 					if (asy->asy_use_fifo == FIFO_ON)
27635295Srandyf 						asy_reset_fifo(asy, FIFOTXFLSH);
27645295Srandyf 					/*
27655295Srandyf 					 * Flush our write queue if we have one.
27665295Srandyf 					 * If we're in the midst of close, then
27675295Srandyf 					 * flush everything. Don't leave stale
27685295Srandyf 					 * ioctls lying about.
27695295Srandyf 					 */
27705295Srandyf 					flushflag = (async->async_flags &
27715295Srandyf 					    ASYNC_CLOSING) ? FLUSHALL :
27725295Srandyf 					    FLUSHDATA;
27735295Srandyf 					flushq(tp->t_writeq, flushflag);
27745295Srandyf 
27755295Srandyf 					/* active msg */
27765295Srandyf 					bp = async->async_xmitblk;
27775295Srandyf 					if (bp != NULL) {
27785295Srandyf 						freeb(bp);
27795295Srandyf 						async->async_xmitblk = NULL;
27805295Srandyf 					}
27815295Srandyf 
27825295Srandyf 					mutex_enter(&asy->asy_excl_hi);
27835295Srandyf 					async->async_flags &= ~ASYNC_BUSY;
27845295Srandyf 					/*
27855295Srandyf 					 * This message warns of Carrier loss
27865295Srandyf 					 * with data left to transmit can hang
27875295Srandyf 					 * the system.
27885295Srandyf 					 */
27895295Srandyf 					DEBUGCONT0(ASY_DEBUG_MODEM,
27905295Srandyf 					    "async_softint: Flushing to "
27915295Srandyf 					    "prevent HUPCL hanging\n");
27920Sstevel@tonic-gate 				}	/* if (ASYNC_ISOPEN) */
27930Sstevel@tonic-gate 			}	/* if (ASYNC_CARR_ON && CLOCAL) */
27940Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_CARR_ON;
27950Sstevel@tonic-gate 			cv_broadcast(&async->async_flags_cv);
27960Sstevel@tonic-gate 		}	/* else */
27970Sstevel@tonic-gate 	}	/* if (async->async_ext) */
27980Sstevel@tonic-gate 
27990Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
28000Sstevel@tonic-gate 
28010Sstevel@tonic-gate 	/*
28020Sstevel@tonic-gate 	 * If data has been added to the circular buffer, remove
28030Sstevel@tonic-gate 	 * it from the buffer, and send it up the stream if there's
28040Sstevel@tonic-gate 	 * somebody listening. Try to do it 16 bytes at a time. If we
28050Sstevel@tonic-gate 	 * have more than 16 bytes to move, move 16 byte chunks and
28060Sstevel@tonic-gate 	 * leave the rest for next time around (maybe it will grow).
28070Sstevel@tonic-gate 	 */
28080Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
28090Sstevel@tonic-gate 	if (!(async->async_flags & ASYNC_ISOPEN)) {
28100Sstevel@tonic-gate 		RING_INIT(async);
28110Sstevel@tonic-gate 		goto rv;
28120Sstevel@tonic-gate 	}
28130Sstevel@tonic-gate 	if ((cc = RING_CNT(async)) <= 0)
28140Sstevel@tonic-gate 		goto rv;
28150Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
28160Sstevel@tonic-gate 
28170Sstevel@tonic-gate 	if (!canput(q)) {
28180Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
28190Sstevel@tonic-gate 		if (!(async->async_inflow_source & IN_FLOW_STREAMS)) {
28200Sstevel@tonic-gate 			async_flowcontrol_hw_input(asy, FLOW_STOP,
28210Sstevel@tonic-gate 			    IN_FLOW_STREAMS);
28220Sstevel@tonic-gate 			(void) async_flowcontrol_sw_input(asy, FLOW_STOP,
28230Sstevel@tonic-gate 			    IN_FLOW_STREAMS);
28240Sstevel@tonic-gate 		}
28250Sstevel@tonic-gate 		goto rv;
28260Sstevel@tonic-gate 	}
28270Sstevel@tonic-gate 	if (async->async_inflow_source & IN_FLOW_STREAMS) {
28280Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
28290Sstevel@tonic-gate 		async_flowcontrol_hw_input(asy, FLOW_START,
28300Sstevel@tonic-gate 		    IN_FLOW_STREAMS);
28310Sstevel@tonic-gate 		(void) async_flowcontrol_sw_input(asy, FLOW_START,
28320Sstevel@tonic-gate 		    IN_FLOW_STREAMS);
28330Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
28340Sstevel@tonic-gate 	}
28355295Srandyf 
28365295Srandyf 	DEBUGCONT2(ASY_DEBUG_INPUT, "async%d_softint: %d char(s) in queue.\n",
28375295Srandyf 	    instance, cc);
28385295Srandyf 
28390Sstevel@tonic-gate 	if (!(bp = allocb(cc, BPRI_MED))) {
28400Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
28410Sstevel@tonic-gate 		ttycommon_qfull(&async->async_ttycommon, q);
28420Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
28430Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
28440Sstevel@tonic-gate 		goto rv;
28450Sstevel@tonic-gate 	}
28460Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
28470Sstevel@tonic-gate 	do {
28480Sstevel@tonic-gate 		if (RING_ERR(async, S_ERRORS)) {
28490Sstevel@tonic-gate 			RING_UNMARK(async);
28500Sstevel@tonic-gate 			c = RING_GET(async);
28510Sstevel@tonic-gate 			break;
28520Sstevel@tonic-gate 		} else
28530Sstevel@tonic-gate 			*bp->b_wptr++ = RING_GET(async);
28540Sstevel@tonic-gate 	} while (--cc);
28550Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
28560Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
28570Sstevel@tonic-gate 	if (bp->b_wptr > bp->b_rptr) {
28580Sstevel@tonic-gate 			if (!canput(q)) {
28590Sstevel@tonic-gate 				asyerror(CE_NOTE, "asy%d: local queue full",
28605295Srandyf 				    instance);
28610Sstevel@tonic-gate 				freemsg(bp);
28620Sstevel@tonic-gate 			} else
28630Sstevel@tonic-gate 				(void) putq(q, bp);
28640Sstevel@tonic-gate 	} else
28650Sstevel@tonic-gate 		freemsg(bp);
28660Sstevel@tonic-gate 	/*
28670Sstevel@tonic-gate 	 * If we have a parity error, then send
28680Sstevel@tonic-gate 	 * up an M_BREAK with the "bad"
28690Sstevel@tonic-gate 	 * character as an argument. Let ldterm
28700Sstevel@tonic-gate 	 * figure out what to do with the error.
28710Sstevel@tonic-gate 	 */
28720Sstevel@tonic-gate 	if (cc) {
28730Sstevel@tonic-gate 		(void) putctl1(q, M_BREAK, c);
28740Sstevel@tonic-gate 		ASYSETSOFT(async->async_common);	/* finish cc chars */
28750Sstevel@tonic-gate 	}
28760Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
28770Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
28780Sstevel@tonic-gate rv:
28790Sstevel@tonic-gate 	if ((RING_CNT(async) < (RINGSIZE/4)) &&
28800Sstevel@tonic-gate 	    (async->async_inflow_source & IN_FLOW_RINGBUFF)) {
28810Sstevel@tonic-gate 		async_flowcontrol_hw_input(asy, FLOW_START, IN_FLOW_RINGBUFF);
28820Sstevel@tonic-gate 		(void) async_flowcontrol_sw_input(asy, FLOW_START,
28830Sstevel@tonic-gate 		    IN_FLOW_RINGBUFF);
28840Sstevel@tonic-gate 	}
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 	/*
28870Sstevel@tonic-gate 	 * If a transmission has finished, indicate that it's finished,
28880Sstevel@tonic-gate 	 * and start that line up again.
28890Sstevel@tonic-gate 	 */
28900Sstevel@tonic-gate 	if (async->async_break > 0) {
28910Sstevel@tonic-gate 		nb = async->async_break;
28920Sstevel@tonic-gate 		async->async_break = 0;
28930Sstevel@tonic-gate 		if (async->async_flags & ASYNC_ISOPEN) {
28940Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
28950Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
28960Sstevel@tonic-gate 			for (; nb > 0; nb--)
28970Sstevel@tonic-gate 				(void) putctl(q, M_BREAK);
28980Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
28990Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
29000Sstevel@tonic-gate 		}
29010Sstevel@tonic-gate 	}
29020Sstevel@tonic-gate 	if (async->async_ocnt <= 0 && (async->async_flags & ASYNC_BUSY)) {
29030Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_BUSY,
29040Sstevel@tonic-gate 		    "async%d_softint: Clearing ASYNC_BUSY.  async_ocnt=%d\n",
29050Sstevel@tonic-gate 		    instance,
29060Sstevel@tonic-gate 		    async->async_ocnt);
29070Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_BUSY;
29080Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
29090Sstevel@tonic-gate 		if (async->async_xmitblk)
29100Sstevel@tonic-gate 			freeb(async->async_xmitblk);
29110Sstevel@tonic-gate 		async->async_xmitblk = NULL;
29120Sstevel@tonic-gate 		async_start(async);
29130Sstevel@tonic-gate 		/*
29140Sstevel@tonic-gate 		 * If the flag isn't set after doing the async_start above, we
29150Sstevel@tonic-gate 		 * may have finished all the queued output.  Signal any thread
29160Sstevel@tonic-gate 		 * stuck in close.
29170Sstevel@tonic-gate 		 */
29180Sstevel@tonic-gate 		if (!(async->async_flags & ASYNC_BUSY))
29190Sstevel@tonic-gate 			cv_broadcast(&async->async_flags_cv);
29200Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
29210Sstevel@tonic-gate 	}
29220Sstevel@tonic-gate 	/*
29230Sstevel@tonic-gate 	 * A note about these overrun bits: all they do is *tell* someone
29240Sstevel@tonic-gate 	 * about an error- They do not track multiple errors. In fact,
29250Sstevel@tonic-gate 	 * you could consider them latched register bits if you like.
29260Sstevel@tonic-gate 	 * We are only interested in printing the error message once for
29270Sstevel@tonic-gate 	 * any cluster of overrun errrors.
29280Sstevel@tonic-gate 	 */
29290Sstevel@tonic-gate 	if (async->async_hw_overrun) {
29300Sstevel@tonic-gate 		if (async->async_flags & ASYNC_ISOPEN) {
29310Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
29320Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
29330Sstevel@tonic-gate 			asyerror(CE_NOTE, "asy%d: silo overflow", instance);
29340Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
29350Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
29360Sstevel@tonic-gate 		}
29370Sstevel@tonic-gate 		async->async_hw_overrun = 0;
29380Sstevel@tonic-gate 	}
29390Sstevel@tonic-gate 	if (async->async_sw_overrun) {
29400Sstevel@tonic-gate 		if (async->async_flags & ASYNC_ISOPEN) {
29410Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
29420Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
29430Sstevel@tonic-gate 			asyerror(CE_NOTE, "asy%d: ring buffer overflow",
29445295Srandyf 			    instance);
29450Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
29460Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
29470Sstevel@tonic-gate 		}
29480Sstevel@tonic-gate 		async->async_sw_overrun = 0;
29490Sstevel@tonic-gate 	}
29500Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
29510Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
29520Sstevel@tonic-gate 	mutex_enter(&asy_soft_lock);
29530Sstevel@tonic-gate 	if (asy->asy_flags & ASY_DOINGSOFT_RETRY) {
29540Sstevel@tonic-gate 		goto begin;
29550Sstevel@tonic-gate 	}
29560Sstevel@tonic-gate 	asy->asy_flags &= ~ASY_DOINGSOFT;
29570Sstevel@tonic-gate 	mutex_exit(&asy_soft_lock);
29580Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_softint: done\n", instance);
29590Sstevel@tonic-gate }
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate /*
29620Sstevel@tonic-gate  * Restart output on a line after a delay or break timer expired.
29630Sstevel@tonic-gate  */
29640Sstevel@tonic-gate static void
29650Sstevel@tonic-gate async_restart(void *arg)
29660Sstevel@tonic-gate {
29670Sstevel@tonic-gate 	struct asyncline *async = (struct asyncline *)arg;
29680Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
29690Sstevel@tonic-gate 	uchar_t lcr;
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 	/*
29720Sstevel@tonic-gate 	 * If break timer expired, turn off the break bit.
29730Sstevel@tonic-gate 	 */
29740Sstevel@tonic-gate #ifdef DEBUG
29750Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
29760Sstevel@tonic-gate 
29770Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_restart\n", instance);
29780Sstevel@tonic-gate #endif
29790Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
29800Sstevel@tonic-gate 	/*
29810Sstevel@tonic-gate 	 * If ASYNC_OUT_SUSPEND is also set, we don't really
29820Sstevel@tonic-gate 	 * clean the HW break, TIOCCBRK is responsible for this.
29830Sstevel@tonic-gate 	 */
29840Sstevel@tonic-gate 	if ((async->async_flags & ASYNC_BREAK) &&
29850Sstevel@tonic-gate 	    !(async->async_flags & ASYNC_OUT_SUSPEND)) {
29860Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
29875295Srandyf 		lcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
29881106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
29895295Srandyf 		    (lcr & ~SETBREAK));
29900Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
29910Sstevel@tonic-gate 	}
29920Sstevel@tonic-gate 	async->async_flags &= ~(ASYNC_DELAY|ASYNC_BREAK);
29930Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
29940Sstevel@tonic-gate 	async_start(async);
29950Sstevel@tonic-gate 
29960Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
29970Sstevel@tonic-gate }
29980Sstevel@tonic-gate 
29990Sstevel@tonic-gate static void
30000Sstevel@tonic-gate async_start(struct asyncline *async)
30010Sstevel@tonic-gate {
30020Sstevel@tonic-gate 	async_nstart(async, 0);
30030Sstevel@tonic-gate }
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate /*
30060Sstevel@tonic-gate  * Start output on a line, unless it's busy, frozen, or otherwise.
30070Sstevel@tonic-gate  */
30080Sstevel@tonic-gate /*ARGSUSED*/
30090Sstevel@tonic-gate static void
30100Sstevel@tonic-gate async_nstart(struct asyncline *async, int mode)
30110Sstevel@tonic-gate {
30120Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
30130Sstevel@tonic-gate 	int cc;
30140Sstevel@tonic-gate 	queue_t *q;
30150Sstevel@tonic-gate 	mblk_t *bp;
30160Sstevel@tonic-gate 	uchar_t *xmit_addr;
30170Sstevel@tonic-gate 	uchar_t	val;
30180Sstevel@tonic-gate 	int	fifo_len = 1;
30190Sstevel@tonic-gate 	boolean_t didsome;
30200Sstevel@tonic-gate 	mblk_t *nbp;
30210Sstevel@tonic-gate 
30220Sstevel@tonic-gate #ifdef DEBUG
30230Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
30240Sstevel@tonic-gate 
30250Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_nstart\n", instance);
30260Sstevel@tonic-gate #endif
30270Sstevel@tonic-gate 	if (asy->asy_use_fifo == FIFO_ON) {
30280Sstevel@tonic-gate 		fifo_len = asy->asy_fifo_buf; /* with FIFO buffers */
30290Sstevel@tonic-gate 		if (fifo_len > asy_max_tx_fifo)
30300Sstevel@tonic-gate 			fifo_len = asy_max_tx_fifo;
30310Sstevel@tonic-gate 	}
30320Sstevel@tonic-gate 
30330Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
30340Sstevel@tonic-gate 
30350Sstevel@tonic-gate 	/*
30360Sstevel@tonic-gate 	 * If the chip is busy (i.e., we're waiting for a break timeout
30370Sstevel@tonic-gate 	 * to expire, or for the current transmission to finish, or for
30380Sstevel@tonic-gate 	 * output to finish draining from chip), don't grab anything new.
30390Sstevel@tonic-gate 	 */
30400Sstevel@tonic-gate 	if (async->async_flags & (ASYNC_BREAK|ASYNC_BUSY)) {
30410Sstevel@tonic-gate 		DEBUGCONT2((mode? ASY_DEBUG_OUT : 0),
30425295Srandyf 		    "async%d_nstart: start %s.\n",
30435295Srandyf 		    instance,
30445295Srandyf 		    async->async_flags & ASYNC_BREAK ? "break" : "busy");
30450Sstevel@tonic-gate 		return;
30460Sstevel@tonic-gate 	}
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 	/*
30490Sstevel@tonic-gate 	 * Check only pended sw input flow control.
30500Sstevel@tonic-gate 	 */
30510Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
30520Sstevel@tonic-gate 	if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
30530Sstevel@tonic-gate 		fifo_len--;
30540Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate 	/*
30570Sstevel@tonic-gate 	 * If we're waiting for a delay timeout to expire, don't grab
30580Sstevel@tonic-gate 	 * anything new.
30590Sstevel@tonic-gate 	 */
30600Sstevel@tonic-gate 	if (async->async_flags & ASYNC_DELAY) {
30610Sstevel@tonic-gate 		DEBUGCONT1((mode? ASY_DEBUG_OUT : 0),
30625295Srandyf 		    "async%d_nstart: start ASYNC_DELAY.\n", instance);
30630Sstevel@tonic-gate 		return;
30640Sstevel@tonic-gate 	}
30650Sstevel@tonic-gate 
30660Sstevel@tonic-gate 	if ((q = async->async_ttycommon.t_writeq) == NULL) {
30670Sstevel@tonic-gate 		DEBUGCONT1((mode? ASY_DEBUG_OUT : 0),
30685295Srandyf 		    "async%d_nstart: start writeq is null.\n", instance);
30690Sstevel@tonic-gate 		return;	/* not attached to a stream */
30700Sstevel@tonic-gate 	}
30710Sstevel@tonic-gate 
30720Sstevel@tonic-gate 	for (;;) {
30730Sstevel@tonic-gate 		if ((bp = getq(q)) == NULL)
30740Sstevel@tonic-gate 			return;	/* no data to transmit */
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 		/*
30770Sstevel@tonic-gate 		 * We have a message block to work on.
30780Sstevel@tonic-gate 		 * Check whether it's a break, a delay, or an ioctl (the latter
30790Sstevel@tonic-gate 		 * occurs if the ioctl in question was waiting for the output
30800Sstevel@tonic-gate 		 * to drain).  If it's one of those, process it immediately.
30810Sstevel@tonic-gate 		 */
30820Sstevel@tonic-gate 		switch (bp->b_datap->db_type) {
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 		case M_BREAK:
30850Sstevel@tonic-gate 			/*
30860Sstevel@tonic-gate 			 * Set the break bit, and arrange for "async_restart"
30870Sstevel@tonic-gate 			 * to be called in 1/4 second; it will turn the
30880Sstevel@tonic-gate 			 * break bit off, and call "async_start" to grab
30890Sstevel@tonic-gate 			 * the next message.
30900Sstevel@tonic-gate 			 */
30910Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
30921106Smrj 			val = ddi_get8(asy->asy_iohandle,
30935295Srandyf 			    asy->asy_ioaddr + LCR);
30945295Srandyf 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
30955295Srandyf 			    (val | SETBREAK));
30960Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
30970Sstevel@tonic-gate 			async->async_flags |= ASYNC_BREAK;
30980Sstevel@tonic-gate 			(void) timeout(async_restart, (caddr_t)async,
30990Sstevel@tonic-gate 			    drv_usectohz(1000000)/4);
31000Sstevel@tonic-gate 			freemsg(bp);
31010Sstevel@tonic-gate 			return;	/* wait for this to finish */
31020Sstevel@tonic-gate 
31030Sstevel@tonic-gate 		case M_DELAY:
31040Sstevel@tonic-gate 			/*
31050Sstevel@tonic-gate 			 * Arrange for "async_restart" to be called when the
31060Sstevel@tonic-gate 			 * delay expires; it will turn ASYNC_DELAY off,
31070Sstevel@tonic-gate 			 * and call "async_start" to grab the next message.
31080Sstevel@tonic-gate 			 */
31090Sstevel@tonic-gate 			(void) timeout(async_restart, (caddr_t)async,
31100Sstevel@tonic-gate 			    (int)(*(unsigned char *)bp->b_rptr + 6));
31110Sstevel@tonic-gate 			async->async_flags |= ASYNC_DELAY;
31120Sstevel@tonic-gate 			freemsg(bp);
31130Sstevel@tonic-gate 			return;	/* wait for this to finish */
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 		case M_IOCTL:
31160Sstevel@tonic-gate 			/*
31170Sstevel@tonic-gate 			 * This ioctl was waiting for the output ahead of
31180Sstevel@tonic-gate 			 * it to drain; obviously, it has.  Do it, and
31190Sstevel@tonic-gate 			 * then grab the next message after it.
31200Sstevel@tonic-gate 			 */
31210Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
31220Sstevel@tonic-gate 			async_ioctl(async, q, bp);
31230Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
31240Sstevel@tonic-gate 			continue;
31250Sstevel@tonic-gate 		}
31260Sstevel@tonic-gate 
3127*6990Sgd78059 		while (bp != NULL && ((cc = MBLKL(bp)) == 0)) {
31280Sstevel@tonic-gate 			nbp = bp->b_cont;
31290Sstevel@tonic-gate 			freeb(bp);
31300Sstevel@tonic-gate 			bp = nbp;
31310Sstevel@tonic-gate 		}
31320Sstevel@tonic-gate 		if (bp != NULL)
31330Sstevel@tonic-gate 			break;
31340Sstevel@tonic-gate 	}
31350Sstevel@tonic-gate 
31360Sstevel@tonic-gate 	/*
31370Sstevel@tonic-gate 	 * We have data to transmit.  If output is stopped, put
31380Sstevel@tonic-gate 	 * it back and try again later.
31390Sstevel@tonic-gate 	 */
31400Sstevel@tonic-gate 	if (async->async_flags & (ASYNC_HW_OUT_FLW | ASYNC_SW_OUT_FLW |
31410Sstevel@tonic-gate 	    ASYNC_STOPPED | ASYNC_OUT_SUSPEND)) {
31420Sstevel@tonic-gate 		(void) putbq(q, bp);
31430Sstevel@tonic-gate 		return;
31440Sstevel@tonic-gate 	}
31450Sstevel@tonic-gate 
31460Sstevel@tonic-gate 	async->async_xmitblk = bp;
31470Sstevel@tonic-gate 	xmit_addr = bp->b_rptr;
31480Sstevel@tonic-gate 	bp = bp->b_cont;
31490Sstevel@tonic-gate 	if (bp != NULL)
31500Sstevel@tonic-gate 		(void) putbq(q, bp);	/* not done with this message yet */
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 	/*
31530Sstevel@tonic-gate 	 * In 5-bit mode, the high order bits are used
31540Sstevel@tonic-gate 	 * to indicate character sizes less than five,
31550Sstevel@tonic-gate 	 * so we need to explicitly mask before transmitting
31560Sstevel@tonic-gate 	 */
31570Sstevel@tonic-gate 	if ((async->async_ttycommon.t_cflag & CSIZE) == CS5) {
31580Sstevel@tonic-gate 		unsigned char *p = xmit_addr;
31590Sstevel@tonic-gate 		int cnt = cc;
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate 		while (cnt--)
31620Sstevel@tonic-gate 			*p++ &= (unsigned char) 0x1f;
31630Sstevel@tonic-gate 	}
31640Sstevel@tonic-gate 
31650Sstevel@tonic-gate 	/*
31660Sstevel@tonic-gate 	 * Set up this block for pseudo-DMA.
31670Sstevel@tonic-gate 	 */
31680Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
31690Sstevel@tonic-gate 	/*
31700Sstevel@tonic-gate 	 * If the transmitter is ready, shove the first
31710Sstevel@tonic-gate 	 * character out.
31720Sstevel@tonic-gate 	 */
31730Sstevel@tonic-gate 	didsome = B_FALSE;
31740Sstevel@tonic-gate 	while (--fifo_len >= 0 && cc > 0) {
31751106Smrj 		if (!(ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) &
31760Sstevel@tonic-gate 		    XHRE))
31770Sstevel@tonic-gate 			break;
31781106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
31790Sstevel@tonic-gate 		    *xmit_addr++);
31800Sstevel@tonic-gate 		cc--;
31810Sstevel@tonic-gate 		didsome = B_TRUE;
31820Sstevel@tonic-gate 	}
31830Sstevel@tonic-gate 	async->async_optr = xmit_addr;
31840Sstevel@tonic-gate 	async->async_ocnt = cc;
31850Sstevel@tonic-gate 	if (didsome)
31860Sstevel@tonic-gate 		async->async_flags |= ASYNC_PROGRESS;
31870Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_BUSY,
31885295Srandyf 	    "async%d_nstart: Set ASYNC_BUSY.  async_ocnt=%d\n",
31895295Srandyf 	    instance, async->async_ocnt);
31900Sstevel@tonic-gate 	async->async_flags |= ASYNC_BUSY;
31910Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
31920Sstevel@tonic-gate }
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate /*
31950Sstevel@tonic-gate  * Resume output by poking the transmitter.
31960Sstevel@tonic-gate  */
31970Sstevel@tonic-gate static void
31980Sstevel@tonic-gate async_resume(struct asyncline *async)
31990Sstevel@tonic-gate {
32000Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
32010Sstevel@tonic-gate #ifdef DEBUG
32020Sstevel@tonic-gate 	int instance;
32030Sstevel@tonic-gate #endif
32040Sstevel@tonic-gate 
32050Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
32060Sstevel@tonic-gate #ifdef DEBUG
32070Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
32080Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_resume\n", instance);
32090Sstevel@tonic-gate #endif
32100Sstevel@tonic-gate 
32111106Smrj 	if (ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) & XHRE) {
32120Sstevel@tonic-gate 		if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
32130Sstevel@tonic-gate 			return;
32140Sstevel@tonic-gate 		if (async->async_ocnt > 0 &&
32150Sstevel@tonic-gate 		    !(async->async_flags &
32160Sstevel@tonic-gate 		    (ASYNC_HW_OUT_FLW|ASYNC_SW_OUT_FLW|ASYNC_OUT_SUSPEND))) {
32171106Smrj 			ddi_put8(asy->asy_iohandle,
32180Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT, *async->async_optr++);
32190Sstevel@tonic-gate 			async->async_ocnt--;
32200Sstevel@tonic-gate 			async->async_flags |= ASYNC_PROGRESS;
32210Sstevel@tonic-gate 		}
32220Sstevel@tonic-gate 	}
32230Sstevel@tonic-gate }
32240Sstevel@tonic-gate 
32250Sstevel@tonic-gate /*
32260Sstevel@tonic-gate  * Hold the untimed break to last the minimum time.
32270Sstevel@tonic-gate  */
32280Sstevel@tonic-gate static void
32290Sstevel@tonic-gate async_hold_utbrk(void *arg)
32300Sstevel@tonic-gate {
32310Sstevel@tonic-gate 	struct asyncline *async = arg;
32320Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
32350Sstevel@tonic-gate 	async->async_flags &= ~ASYNC_HOLD_UTBRK;
32360Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
32370Sstevel@tonic-gate 	async->async_utbrktid = 0;
32380Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
32390Sstevel@tonic-gate }
32400Sstevel@tonic-gate 
32410Sstevel@tonic-gate /*
32420Sstevel@tonic-gate  * Resume the untimed break.
32430Sstevel@tonic-gate  */
32440Sstevel@tonic-gate static void
32450Sstevel@tonic-gate async_resume_utbrk(struct asyncline *async)
32460Sstevel@tonic-gate {
32470Sstevel@tonic-gate 	uchar_t	val;
32480Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
32490Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
32500Sstevel@tonic-gate 
32510Sstevel@tonic-gate 	/*
32520Sstevel@tonic-gate 	 * Because the wait time is very short,
32530Sstevel@tonic-gate 	 * so we use uninterruptably wait.
32540Sstevel@tonic-gate 	 */
32550Sstevel@tonic-gate 	while (async->async_flags & ASYNC_HOLD_UTBRK) {
32560Sstevel@tonic-gate 		cv_wait(&async->async_flags_cv, &asy->asy_excl);
32570Sstevel@tonic-gate 	}
32580Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
32590Sstevel@tonic-gate 	/*
32600Sstevel@tonic-gate 	 * Timed break and untimed break can exist simultaneously,
32610Sstevel@tonic-gate 	 * if ASYNC_BREAK is also set at here, we don't
32620Sstevel@tonic-gate 	 * really clean the HW break.
32630Sstevel@tonic-gate 	 */
32640Sstevel@tonic-gate 	if (!(async->async_flags & ASYNC_BREAK)) {
32651106Smrj 		val = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
32661106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
32670Sstevel@tonic-gate 		    (val & ~SETBREAK));
32680Sstevel@tonic-gate 	}
32690Sstevel@tonic-gate 	async->async_flags &= ~ASYNC_OUT_SUSPEND;
32700Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
32710Sstevel@tonic-gate 	if (async->async_ocnt > 0) {
32720Sstevel@tonic-gate 		async_resume(async);
32730Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
32740Sstevel@tonic-gate 	} else {
32750Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_BUSY;
32760Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
32770Sstevel@tonic-gate 		if (async->async_xmitblk != NULL) {
32780Sstevel@tonic-gate 			freeb(async->async_xmitblk);
32790Sstevel@tonic-gate 			async->async_xmitblk = NULL;
32800Sstevel@tonic-gate 		}
32810Sstevel@tonic-gate 		async_start(async);
32820Sstevel@tonic-gate 	}
32830Sstevel@tonic-gate }
32840Sstevel@tonic-gate 
32850Sstevel@tonic-gate /*
32860Sstevel@tonic-gate  * Process an "ioctl" message sent down to us.
32870Sstevel@tonic-gate  * Note that we don't need to get any locks until we are ready to access
32880Sstevel@tonic-gate  * the hardware.  Nothing we access until then is going to be altered
32890Sstevel@tonic-gate  * outside of the STREAMS framework, so we should be safe.
32900Sstevel@tonic-gate  */
32910Sstevel@tonic-gate int asydelay = 10000;
32920Sstevel@tonic-gate static void
32930Sstevel@tonic-gate async_ioctl(struct asyncline *async, queue_t *wq, mblk_t *mp)
32940Sstevel@tonic-gate {
32950Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
32960Sstevel@tonic-gate 	tty_common_t  *tp = &async->async_ttycommon;
32970Sstevel@tonic-gate 	struct iocblk *iocp;
32980Sstevel@tonic-gate 	unsigned datasize;
32990Sstevel@tonic-gate 	int error = 0;
33000Sstevel@tonic-gate 	uchar_t val;
33010Sstevel@tonic-gate 	mblk_t *datamp;
33020Sstevel@tonic-gate 	unsigned int index;
33030Sstevel@tonic-gate 
33040Sstevel@tonic-gate #ifdef DEBUG
33050Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
33060Sstevel@tonic-gate 
33070Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_ioctl\n", instance);
33080Sstevel@tonic-gate #endif
33090Sstevel@tonic-gate 
33100Sstevel@tonic-gate 	if (tp->t_iocpending != NULL) {
33110Sstevel@tonic-gate 		/*
33120Sstevel@tonic-gate 		 * We were holding an "ioctl" response pending the
33130Sstevel@tonic-gate 		 * availability of an "mblk" to hold data to be passed up;
33140Sstevel@tonic-gate 		 * another "ioctl" came through, which means that "ioctl"
33150Sstevel@tonic-gate 		 * must have timed out or been aborted.
33160Sstevel@tonic-gate 		 */
33170Sstevel@tonic-gate 		freemsg(async->async_ttycommon.t_iocpending);
33180Sstevel@tonic-gate 		async->async_ttycommon.t_iocpending = NULL;
33190Sstevel@tonic-gate 	}
33200Sstevel@tonic-gate 
33210Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
33220Sstevel@tonic-gate 
33230Sstevel@tonic-gate 	/*
33240Sstevel@tonic-gate 	 * For TIOCMGET and the PPS ioctls, do NOT call ttycommon_ioctl()
33250Sstevel@tonic-gate 	 * because this function frees up the message block (mp->b_cont) that
33260Sstevel@tonic-gate 	 * contains the user location where we pass back the results.
33270Sstevel@tonic-gate 	 *
33280Sstevel@tonic-gate 	 * Similarly, CONSOPENPOLLEDIO needs ioc_count, which ttycommon_ioctl
33290Sstevel@tonic-gate 	 * zaps.  We know that ttycommon_ioctl doesn't know any CONS*
33300Sstevel@tonic-gate 	 * ioctls, so keep the others safe too.
33310Sstevel@tonic-gate 	 */
33320Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_IOCTL, "async%d_ioctl: %s\n",
33335295Srandyf 	    instance,
33345295Srandyf 	    iocp->ioc_cmd == TIOCMGET ? "TIOCMGET" :
33355295Srandyf 	    iocp->ioc_cmd == TIOCMSET ? "TIOCMSET" :
33365295Srandyf 	    iocp->ioc_cmd == TIOCMBIS ? "TIOCMBIS" :
33375295Srandyf 	    iocp->ioc_cmd == TIOCMBIC ? "TIOCMBIC" :
33385295Srandyf 	    "other");
33395295Srandyf 
33400Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
33410Sstevel@tonic-gate 	case TIOCMGET:
33420Sstevel@tonic-gate 	case TIOCGPPS:
33430Sstevel@tonic-gate 	case TIOCSPPS:
33440Sstevel@tonic-gate 	case TIOCGPPSEV:
33450Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
33460Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
33470Sstevel@tonic-gate 	case CONSSETABORTENABLE:
33480Sstevel@tonic-gate 	case CONSGETABORTENABLE:
33490Sstevel@tonic-gate 		error = -1; /* Do Nothing */
33500Sstevel@tonic-gate 		break;
33510Sstevel@tonic-gate 	default:
33520Sstevel@tonic-gate 
33530Sstevel@tonic-gate 		/*
33540Sstevel@tonic-gate 		 * The only way in which "ttycommon_ioctl" can fail is if the
33550Sstevel@tonic-gate 		 * "ioctl" requires a response containing data to be returned
33560Sstevel@tonic-gate 		 * to the user, and no mblk could be allocated for the data.
33570Sstevel@tonic-gate 		 * No such "ioctl" alters our state.  Thus, we always go ahead
33580Sstevel@tonic-gate 		 * and do any state-changes the "ioctl" calls for.  If we
33590Sstevel@tonic-gate 		 * couldn't allocate the data, "ttycommon_ioctl" has stashed
33600Sstevel@tonic-gate 		 * the "ioctl" away safely, so we just call "bufcall" to
33610Sstevel@tonic-gate 		 * request that we be called back when we stand a better
33620Sstevel@tonic-gate 		 * chance of allocating the data.
33630Sstevel@tonic-gate 		 */
33640Sstevel@tonic-gate 		if ((datasize = ttycommon_ioctl(tp, wq, mp, &error)) != 0) {
33650Sstevel@tonic-gate 			if (async->async_wbufcid)
33660Sstevel@tonic-gate 				unbufcall(async->async_wbufcid);
33670Sstevel@tonic-gate 			async->async_wbufcid = bufcall(datasize, BPRI_HI,
33680Sstevel@tonic-gate 			    (void (*)(void *)) async_reioctl,
33690Sstevel@tonic-gate 			    (void *)(intptr_t)async->async_common->asy_unit);
33700Sstevel@tonic-gate 			return;
33710Sstevel@tonic-gate 		}
33720Sstevel@tonic-gate 	}
33730Sstevel@tonic-gate 
33740Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
33750Sstevel@tonic-gate 
33760Sstevel@tonic-gate 	if (error == 0) {
33770Sstevel@tonic-gate 		/*
33780Sstevel@tonic-gate 		 * "ttycommon_ioctl" did most of the work; we just use the
33790Sstevel@tonic-gate 		 * data it set up.
33800Sstevel@tonic-gate 		 */
33810Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate 		case TCSETS:
33840Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
33850Sstevel@tonic-gate 			if (asy_baudok(asy))
33860Sstevel@tonic-gate 				asy_program(asy, ASY_NOINIT);
33870Sstevel@tonic-gate 			else
33880Sstevel@tonic-gate 				error = EINVAL;
33890Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
33900Sstevel@tonic-gate 			break;
33910Sstevel@tonic-gate 		case TCSETSF:
33920Sstevel@tonic-gate 		case TCSETSW:
33930Sstevel@tonic-gate 		case TCSETA:
33940Sstevel@tonic-gate 		case TCSETAW:
33950Sstevel@tonic-gate 		case TCSETAF:
33960Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
33970Sstevel@tonic-gate 			if (!asy_baudok(asy))
33980Sstevel@tonic-gate 				error = EINVAL;
33990Sstevel@tonic-gate 			else {
34000Sstevel@tonic-gate 				if (asy_isbusy(asy))
34010Sstevel@tonic-gate 					asy_waiteot(asy);
34020Sstevel@tonic-gate 				asy_program(asy, ASY_NOINIT);
34030Sstevel@tonic-gate 			}
34040Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
34050Sstevel@tonic-gate 			break;
34060Sstevel@tonic-gate 		}
34070Sstevel@tonic-gate 	} else if (error < 0) {
34080Sstevel@tonic-gate 		/*
34090Sstevel@tonic-gate 		 * "ttycommon_ioctl" didn't do anything; we process it here.
34100Sstevel@tonic-gate 		 */
34110Sstevel@tonic-gate 		error = 0;
34120Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
34130Sstevel@tonic-gate 
34140Sstevel@tonic-gate 		case TIOCGPPS:
34150Sstevel@tonic-gate 			/*
34160Sstevel@tonic-gate 			 * Get PPS on/off.
34170Sstevel@tonic-gate 			 */
34180Sstevel@tonic-gate 			if (mp->b_cont != NULL)
34190Sstevel@tonic-gate 				freemsg(mp->b_cont);
34200Sstevel@tonic-gate 
34210Sstevel@tonic-gate 			mp->b_cont = allocb(sizeof (int), BPRI_HI);
34220Sstevel@tonic-gate 			if (mp->b_cont == NULL) {
34230Sstevel@tonic-gate 				error = ENOMEM;
34240Sstevel@tonic-gate 				break;
34250Sstevel@tonic-gate 			}
34260Sstevel@tonic-gate 			if (asy->asy_flags & ASY_PPS)
34270Sstevel@tonic-gate 				*(int *)mp->b_cont->b_wptr = 1;
34280Sstevel@tonic-gate 			else
34290Sstevel@tonic-gate 				*(int *)mp->b_cont->b_wptr = 0;
34300Sstevel@tonic-gate 			mp->b_cont->b_wptr += sizeof (int);
34310Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
34320Sstevel@tonic-gate 			iocp->ioc_count = sizeof (int);
34330Sstevel@tonic-gate 			break;
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 		case TIOCSPPS:
34360Sstevel@tonic-gate 			/*
34370Sstevel@tonic-gate 			 * Set PPS on/off.
34380Sstevel@tonic-gate 			 */
34390Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
34400Sstevel@tonic-gate 			if (error != 0)
34410Sstevel@tonic-gate 				break;
34420Sstevel@tonic-gate 
34430Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
34440Sstevel@tonic-gate 			if (*(int *)mp->b_cont->b_rptr)
34450Sstevel@tonic-gate 				asy->asy_flags |= ASY_PPS;
34460Sstevel@tonic-gate 			else
34470Sstevel@tonic-gate 				asy->asy_flags &= ~ASY_PPS;
34480Sstevel@tonic-gate 			/* Reset edge sense */
34490Sstevel@tonic-gate 			asy->asy_flags &= ~ASY_PPS_EDGE;
34500Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
34510Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
34520Sstevel@tonic-gate 			break;
34530Sstevel@tonic-gate 
34540Sstevel@tonic-gate 		case TIOCGPPSEV:
34550Sstevel@tonic-gate 		{
34560Sstevel@tonic-gate 			/*
34570Sstevel@tonic-gate 			 * Get PPS event data.
34580Sstevel@tonic-gate 			 */
34590Sstevel@tonic-gate 			mblk_t *bp;
34600Sstevel@tonic-gate 			void *buf;
34610Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
34620Sstevel@tonic-gate 			struct ppsclockev32 p32;
34630Sstevel@tonic-gate #endif
34640Sstevel@tonic-gate 			struct ppsclockev ppsclockev;
34650Sstevel@tonic-gate 
34660Sstevel@tonic-gate 			if (mp->b_cont != NULL) {
34670Sstevel@tonic-gate 				freemsg(mp->b_cont);
34680Sstevel@tonic-gate 				mp->b_cont = NULL;
34690Sstevel@tonic-gate 			}
34700Sstevel@tonic-gate 
34710Sstevel@tonic-gate 			if ((asy->asy_flags & ASY_PPS) == 0) {
34720Sstevel@tonic-gate 				error = ENXIO;
34730Sstevel@tonic-gate 				break;
34740Sstevel@tonic-gate 			}
34750Sstevel@tonic-gate 
34760Sstevel@tonic-gate 			/* Protect from incomplete asy_ppsev */
34770Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
34780Sstevel@tonic-gate 			ppsclockev = asy_ppsev;
34790Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
34820Sstevel@tonic-gate 			if ((iocp->ioc_flag & IOC_MODELS) != IOC_NATIVE) {
34830Sstevel@tonic-gate 				TIMEVAL_TO_TIMEVAL32(&p32.tv, &ppsclockev.tv);
34840Sstevel@tonic-gate 				p32.serial = ppsclockev.serial;
34850Sstevel@tonic-gate 				buf = &p32;
34860Sstevel@tonic-gate 				iocp->ioc_count = sizeof (struct ppsclockev32);
34870Sstevel@tonic-gate 			} else
34880Sstevel@tonic-gate #endif
34890Sstevel@tonic-gate 			{
34900Sstevel@tonic-gate 				buf = &ppsclockev;
34910Sstevel@tonic-gate 				iocp->ioc_count = sizeof (struct ppsclockev);
34920Sstevel@tonic-gate 			}
34930Sstevel@tonic-gate 
34940Sstevel@tonic-gate 			if ((bp = allocb(iocp->ioc_count, BPRI_HI)) == NULL) {
34950Sstevel@tonic-gate 				error = ENOMEM;
34960Sstevel@tonic-gate 				break;
34970Sstevel@tonic-gate 			}
34980Sstevel@tonic-gate 			mp->b_cont = bp;
34990Sstevel@tonic-gate 
35000Sstevel@tonic-gate 			bcopy(buf, bp->b_wptr, iocp->ioc_count);
35010Sstevel@tonic-gate 			bp->b_wptr += iocp->ioc_count;
35020Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
35030Sstevel@tonic-gate 			break;
35040Sstevel@tonic-gate 		}
35050Sstevel@tonic-gate 
35060Sstevel@tonic-gate 		case TCSBRK:
35070Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
35080Sstevel@tonic-gate 			if (error != 0)
35090Sstevel@tonic-gate 				break;
35100Sstevel@tonic-gate 
35110Sstevel@tonic-gate 			if (*(int *)mp->b_cont->b_rptr == 0) {
35120Sstevel@tonic-gate 
35130Sstevel@tonic-gate 				/*
35140Sstevel@tonic-gate 				 * XXX Arrangements to ensure that a break
35150Sstevel@tonic-gate 				 * isn't in progress should be sufficient.
35160Sstevel@tonic-gate 				 * This ugly delay() is the only thing
35170Sstevel@tonic-gate 				 * that seems to work on the NCR Worldmark.
35180Sstevel@tonic-gate 				 * It should be replaced. Note that an
35190Sstevel@tonic-gate 				 * asy_waiteot() also does not work.
35200Sstevel@tonic-gate 				 */
35210Sstevel@tonic-gate 				if (asydelay)
35220Sstevel@tonic-gate 					delay(drv_usectohz(asydelay));
35230Sstevel@tonic-gate 
35240Sstevel@tonic-gate 				while (async->async_flags & ASYNC_BREAK) {
35250Sstevel@tonic-gate 					cv_wait(&async->async_flags_cv,
35260Sstevel@tonic-gate 					    &asy->asy_excl);
35270Sstevel@tonic-gate 				}
35280Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
35290Sstevel@tonic-gate 				/*
35300Sstevel@tonic-gate 				 * We loop until the TSR is empty and then
35310Sstevel@tonic-gate 				 * set the break.  ASYNC_BREAK has been set
35320Sstevel@tonic-gate 				 * to ensure that no characters are
35330Sstevel@tonic-gate 				 * transmitted while the TSR is being
35340Sstevel@tonic-gate 				 * flushed and SOUT is being used for the
35350Sstevel@tonic-gate 				 * break signal.
35360Sstevel@tonic-gate 				 *
35370Sstevel@tonic-gate 				 * The wait period is equal to
35380Sstevel@tonic-gate 				 * clock / (baud * 16) * 16 * 2.
35390Sstevel@tonic-gate 				 */
35400Sstevel@tonic-gate 				index = BAUDINDEX(
35415295Srandyf 				    async->async_ttycommon.t_cflag);
35420Sstevel@tonic-gate 				async->async_flags |= ASYNC_BREAK;
35435295Srandyf 
35441106Smrj 				while ((ddi_get8(asy->asy_iohandle,
35450Sstevel@tonic-gate 				    asy->asy_ioaddr + LSR) & XSRE) == 0) {
35460Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
35470Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
35480Sstevel@tonic-gate 					drv_usecwait(
35495295Srandyf 					    32*asyspdtab[index] & 0xfff);
35500Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
35510Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
35520Sstevel@tonic-gate 				}
35530Sstevel@tonic-gate 				/*
35540Sstevel@tonic-gate 				 * Arrange for "async_restart"
35550Sstevel@tonic-gate 				 * to be called in 1/4 second;
35560Sstevel@tonic-gate 				 * it will turn the break bit off, and call
35570Sstevel@tonic-gate 				 * "async_start" to grab the next message.
35580Sstevel@tonic-gate 				 */
35591106Smrj 				val = ddi_get8(asy->asy_iohandle,
35605295Srandyf 				    asy->asy_ioaddr + LCR);
35611106Smrj 				ddi_put8(asy->asy_iohandle,
35625295Srandyf 				    asy->asy_ioaddr + LCR,
35635295Srandyf 				    (val | SETBREAK));
35640Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
35650Sstevel@tonic-gate 				(void) timeout(async_restart, (caddr_t)async,
35660Sstevel@tonic-gate 				    drv_usectohz(1000000)/4);
35670Sstevel@tonic-gate 			} else {
35680Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_OUT,
35695295Srandyf 				    "async%d_ioctl: wait for flush.\n",
35705295Srandyf 				    instance);
35710Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
35720Sstevel@tonic-gate 				asy_waiteot(asy);
35730Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
35740Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_OUT,
35755295Srandyf 				    "async%d_ioctl: ldterm satisfied.\n",
35765295Srandyf 				    instance);
35770Sstevel@tonic-gate 			}
35780Sstevel@tonic-gate 			break;
35790Sstevel@tonic-gate 
35800Sstevel@tonic-gate 		case TIOCSBRK:
35810Sstevel@tonic-gate 			if (!(async->async_flags & ASYNC_OUT_SUSPEND)) {
35820Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
35830Sstevel@tonic-gate 				async->async_flags |= ASYNC_OUT_SUSPEND;
35840Sstevel@tonic-gate 				async->async_flags |= ASYNC_HOLD_UTBRK;
35850Sstevel@tonic-gate 				index = BAUDINDEX(
35860Sstevel@tonic-gate 				    async->async_ttycommon.t_cflag);
35871106Smrj 				while ((ddi_get8(asy->asy_iohandle,
35880Sstevel@tonic-gate 				    asy->asy_ioaddr + LSR) & XSRE) == 0) {
35890Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
35900Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
35910Sstevel@tonic-gate 					drv_usecwait(
35920Sstevel@tonic-gate 					    32*asyspdtab[index] & 0xfff);
35930Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
35940Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
35950Sstevel@tonic-gate 				}
35961106Smrj 				val = ddi_get8(asy->asy_iohandle,
35970Sstevel@tonic-gate 				    asy->asy_ioaddr + LCR);
35981106Smrj 				ddi_put8(asy->asy_iohandle,
35990Sstevel@tonic-gate 				    asy->asy_ioaddr + LCR, (val | SETBREAK));
36000Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
36010Sstevel@tonic-gate 				/* wait for 100ms to hold BREAK */
36020Sstevel@tonic-gate 				async->async_utbrktid =
36030Sstevel@tonic-gate 				    timeout((void (*)())async_hold_utbrk,
36040Sstevel@tonic-gate 				    (caddr_t)async,
36050Sstevel@tonic-gate 				    drv_usectohz(asy_min_utbrk));
36060Sstevel@tonic-gate 			}
36070Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
36080Sstevel@tonic-gate 			break;
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate 		case TIOCCBRK:
36110Sstevel@tonic-gate 			if (async->async_flags & ASYNC_OUT_SUSPEND)
36120Sstevel@tonic-gate 				async_resume_utbrk(async);
36130Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
36140Sstevel@tonic-gate 			break;
36150Sstevel@tonic-gate 
36160Sstevel@tonic-gate 		case TIOCMSET:
36170Sstevel@tonic-gate 		case TIOCMBIS:
36180Sstevel@tonic-gate 		case TIOCMBIC:
36190Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT) {
36200Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
36215295Srandyf 				    "non-transparent\n", instance);
36220Sstevel@tonic-gate 
36230Sstevel@tonic-gate 				error = miocpullup(mp, sizeof (int));
36240Sstevel@tonic-gate 				if (error != 0)
36250Sstevel@tonic-gate 					break;
36260Sstevel@tonic-gate 
36270Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
36280Sstevel@tonic-gate 				(void) asymctl(asy,
36295295Srandyf 				    dmtoasy(*(int *)mp->b_cont->b_rptr),
36305295Srandyf 				    iocp->ioc_cmd);
36310Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
36320Sstevel@tonic-gate 				iocp->ioc_error = 0;
36330Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
36340Sstevel@tonic-gate 			} else {
36350Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
36365295Srandyf 				    "transparent\n", instance);
36370Sstevel@tonic-gate 				mcopyin(mp, NULL, sizeof (int), NULL);
36380Sstevel@tonic-gate 			}
36390Sstevel@tonic-gate 			break;
36400Sstevel@tonic-gate 
36410Sstevel@tonic-gate 		case TIOCMGET:
36420Sstevel@tonic-gate 			datamp = allocb(sizeof (int), BPRI_MED);
36430Sstevel@tonic-gate 			if (datamp == NULL) {
36440Sstevel@tonic-gate 				error = EAGAIN;
36450Sstevel@tonic-gate 				break;
36460Sstevel@tonic-gate 			}
36470Sstevel@tonic-gate 
36480Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
36490Sstevel@tonic-gate 			*(int *)datamp->b_rptr = asymctl(asy, 0, TIOCMGET);
36500Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 			if (iocp->ioc_count == TRANSPARENT) {
36530Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
36545295Srandyf 				    "transparent\n", instance);
36555295Srandyf 				mcopyout(mp, NULL, sizeof (int), NULL, datamp);
36560Sstevel@tonic-gate 			} else {
36570Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
36585295Srandyf 				    "non-transparent\n", instance);
36590Sstevel@tonic-gate 				mioc2ack(mp, datamp, sizeof (int), 0);
36600Sstevel@tonic-gate 			}
36610Sstevel@tonic-gate 			break;
36620Sstevel@tonic-gate 
36630Sstevel@tonic-gate 		case CONSOPENPOLLEDIO:
36640Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (struct cons_polledio *));
36650Sstevel@tonic-gate 			if (error != 0)
36660Sstevel@tonic-gate 				break;
36670Sstevel@tonic-gate 
36680Sstevel@tonic-gate 			*(struct cons_polledio **)mp->b_cont->b_rptr =
36695295Srandyf 			    &asy->polledio;
36700Sstevel@tonic-gate 
36710Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
36720Sstevel@tonic-gate 			break;
36730Sstevel@tonic-gate 
36740Sstevel@tonic-gate 		case CONSCLOSEPOLLEDIO:
36750Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
36760Sstevel@tonic-gate 			iocp->ioc_error = 0;
36770Sstevel@tonic-gate 			iocp->ioc_rval = 0;
36780Sstevel@tonic-gate 			break;
36790Sstevel@tonic-gate 
36800Sstevel@tonic-gate 		case CONSSETABORTENABLE:
36810Sstevel@tonic-gate 			error = secpolicy_console(iocp->ioc_cr);
36820Sstevel@tonic-gate 			if (error != 0)
36830Sstevel@tonic-gate 				break;
36840Sstevel@tonic-gate 
36850Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT) {
36860Sstevel@tonic-gate 				error = EINVAL;
36870Sstevel@tonic-gate 				break;
36880Sstevel@tonic-gate 			}
36890Sstevel@tonic-gate 
36900Sstevel@tonic-gate 			if (*(intptr_t *)mp->b_cont->b_rptr)
36910Sstevel@tonic-gate 				asy->asy_flags |= ASY_CONSOLE;
36920Sstevel@tonic-gate 			else
36930Sstevel@tonic-gate 				asy->asy_flags &= ~ASY_CONSOLE;
36940Sstevel@tonic-gate 
36950Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
36960Sstevel@tonic-gate 			iocp->ioc_error = 0;
36970Sstevel@tonic-gate 			iocp->ioc_rval = 0;
36980Sstevel@tonic-gate 			break;
36990Sstevel@tonic-gate 
37000Sstevel@tonic-gate 		case CONSGETABORTENABLE:
37010Sstevel@tonic-gate 			/*CONSTANTCONDITION*/
37020Sstevel@tonic-gate 			ASSERT(sizeof (boolean_t) <= sizeof (boolean_t *));
37030Sstevel@tonic-gate 			/*
37040Sstevel@tonic-gate 			 * Store the return value right in the payload
37050Sstevel@tonic-gate 			 * we were passed.  Crude.
37060Sstevel@tonic-gate 			 */
37070Sstevel@tonic-gate 			mcopyout(mp, NULL, sizeof (boolean_t), NULL, NULL);
37080Sstevel@tonic-gate 			*(boolean_t *)mp->b_cont->b_rptr =
37095295Srandyf 			    (asy->asy_flags & ASY_CONSOLE) != 0;
37100Sstevel@tonic-gate 			break;
37110Sstevel@tonic-gate 
37120Sstevel@tonic-gate 		default:
37130Sstevel@tonic-gate 			/*
37140Sstevel@tonic-gate 			 * If we don't understand it, it's an error.  NAK it.
37150Sstevel@tonic-gate 			 */
37160Sstevel@tonic-gate 			error = EINVAL;
37170Sstevel@tonic-gate 			break;
37180Sstevel@tonic-gate 		}
37190Sstevel@tonic-gate 	}
37200Sstevel@tonic-gate 	if (error != 0) {
37210Sstevel@tonic-gate 		iocp->ioc_error = error;
37220Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
37230Sstevel@tonic-gate 	}
37240Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
37250Sstevel@tonic-gate 	qreply(wq, mp);
37260Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_ioctl: done\n", instance);
37270Sstevel@tonic-gate }
37280Sstevel@tonic-gate 
37290Sstevel@tonic-gate static int
37300Sstevel@tonic-gate asyrsrv(queue_t *q)
37310Sstevel@tonic-gate {
37320Sstevel@tonic-gate 	mblk_t *bp;
37330Sstevel@tonic-gate 	struct asyncline *async;
37340Sstevel@tonic-gate 
37350Sstevel@tonic-gate 	async = (struct asyncline *)q->q_ptr;
37360Sstevel@tonic-gate 
37370Sstevel@tonic-gate 	while (canputnext(q) && (bp = getq(q)))
37380Sstevel@tonic-gate 		putnext(q, bp);
37390Sstevel@tonic-gate 	ASYSETSOFT(async->async_common);
37400Sstevel@tonic-gate 	async->async_polltid = 0;
37410Sstevel@tonic-gate 	return (0);
37420Sstevel@tonic-gate }
37430Sstevel@tonic-gate 
37440Sstevel@tonic-gate /*
37455295Srandyf  * The ASYWPUTDO_NOT_SUSP macro indicates to asywputdo() whether it should
37465295Srandyf  * handle messages as though the driver is operating normally or is
37475295Srandyf  * suspended.  In the suspended case, some or all of the processing may have
37485295Srandyf  * to be delayed until the driver is resumed.
37495295Srandyf  */
37505295Srandyf #define	ASYWPUTDO_NOT_SUSP(async, wput) \
37515295Srandyf 	!((wput) && ((async)->async_flags & ASYNC_DDI_SUSPENDED))
37525295Srandyf 
37535295Srandyf /*
37545295Srandyf  * Processing for write queue put procedure.
37550Sstevel@tonic-gate  * Respond to M_STOP, M_START, M_IOCTL, and M_FLUSH messages here;
37560Sstevel@tonic-gate  * set the flow control character for M_STOPI and M_STARTI messages;
37570Sstevel@tonic-gate  * queue up M_BREAK, M_DELAY, and M_DATA messages for processing
37580Sstevel@tonic-gate  * by the start routine, and then call the start routine; discard
37590Sstevel@tonic-gate  * everything else.  Note that this driver does not incorporate any
37600Sstevel@tonic-gate  * mechanism to negotiate to handle the canonicalization process.
37610Sstevel@tonic-gate  * It expects that these functions are handled in upper module(s),
37620Sstevel@tonic-gate  * as we do in ldterm.
37630Sstevel@tonic-gate  */
37640Sstevel@tonic-gate static int
37655295Srandyf asywputdo(queue_t *q, mblk_t *mp, boolean_t wput)
37660Sstevel@tonic-gate {
37670Sstevel@tonic-gate 	struct asyncline *async;
37680Sstevel@tonic-gate 	struct asycom *asy;
37690Sstevel@tonic-gate #ifdef DEBUG
37700Sstevel@tonic-gate 	int instance;
37710Sstevel@tonic-gate #endif
37720Sstevel@tonic-gate 	int error;
37730Sstevel@tonic-gate 
37740Sstevel@tonic-gate 	async = (struct asyncline *)q->q_ptr;
37755295Srandyf 
37760Sstevel@tonic-gate #ifdef DEBUG
37770Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
37780Sstevel@tonic-gate #endif
37790Sstevel@tonic-gate 	asy = async->async_common;
37800Sstevel@tonic-gate 
37810Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
37820Sstevel@tonic-gate 
37830Sstevel@tonic-gate 	case M_STOP:
37840Sstevel@tonic-gate 		/*
37850Sstevel@tonic-gate 		 * Since we don't do real DMA, we can just let the
37860Sstevel@tonic-gate 		 * chip coast to a stop after applying the brakes.
37870Sstevel@tonic-gate 		 */
37880Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
37890Sstevel@tonic-gate 		async->async_flags |= ASYNC_STOPPED;
37900Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
37910Sstevel@tonic-gate 		freemsg(mp);
37920Sstevel@tonic-gate 		break;
37930Sstevel@tonic-gate 
37940Sstevel@tonic-gate 	case M_START:
37950Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
37960Sstevel@tonic-gate 		if (async->async_flags & ASYNC_STOPPED) {
37970Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_STOPPED;
37985295Srandyf 			if (ASYWPUTDO_NOT_SUSP(async, wput)) {
37995295Srandyf 				/*
38005295Srandyf 				 * If an output operation is in progress,
38015295Srandyf 				 * resume it.  Otherwise, prod the start
38025295Srandyf 				 * routine.
38035295Srandyf 				 */
38045295Srandyf 				if (async->async_ocnt > 0) {
38055295Srandyf 					mutex_enter(&asy->asy_excl_hi);
38065295Srandyf 					async_resume(async);
38075295Srandyf 					mutex_exit(&asy->asy_excl_hi);
38085295Srandyf 				} else {
38095295Srandyf 					async_start(async);
38105295Srandyf 				}
38110Sstevel@tonic-gate 			}
38120Sstevel@tonic-gate 		}
38130Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
38140Sstevel@tonic-gate 		freemsg(mp);
38150Sstevel@tonic-gate 		break;
38160Sstevel@tonic-gate 
38170Sstevel@tonic-gate 	case M_IOCTL:
38180Sstevel@tonic-gate 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
38190Sstevel@tonic-gate 
38200Sstevel@tonic-gate 		case TCSBRK:
38210Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
38220Sstevel@tonic-gate 			if (error != 0) {
38230Sstevel@tonic-gate 				miocnak(q, mp, 0, error);
38240Sstevel@tonic-gate 				return (0);
38250Sstevel@tonic-gate 			}
38260Sstevel@tonic-gate 
38270Sstevel@tonic-gate 			if (*(int *)mp->b_cont->b_rptr != 0) {
38280Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_OUT,
38295295Srandyf 				    "async%d_ioctl: flush request.\n",
38305295Srandyf 				    instance);
38310Sstevel@tonic-gate 				(void) putq(q, mp);
38325295Srandyf 
38330Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl);
38345295Srandyf 				if (ASYWPUTDO_NOT_SUSP(async, wput)) {
38355295Srandyf 					/*
38365295Srandyf 					 * If an TIOCSBRK is in progress,
38375295Srandyf 					 * clean it as TIOCCBRK does,
38385295Srandyf 					 * then kick off output.
38395295Srandyf 					 * If TIOCSBRK is not in progress,
38405295Srandyf 					 * just kick off output.
38415295Srandyf 					 */
38425295Srandyf 					async_resume_utbrk(async);
38435295Srandyf 				}
38440Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl);
38450Sstevel@tonic-gate 				break;
38460Sstevel@tonic-gate 			}
38470Sstevel@tonic-gate 			/*FALLTHROUGH*/
38480Sstevel@tonic-gate 		case TCSETSW:
38490Sstevel@tonic-gate 		case TCSETSF:
38500Sstevel@tonic-gate 		case TCSETAW:
38510Sstevel@tonic-gate 		case TCSETAF:
38520Sstevel@tonic-gate 			/*
38530Sstevel@tonic-gate 			 * The changes do not take effect until all
38540Sstevel@tonic-gate 			 * output queued before them is drained.
38550Sstevel@tonic-gate 			 * Put this message on the queue, so that
38560Sstevel@tonic-gate 			 * "async_start" will see it when it's done
38570Sstevel@tonic-gate 			 * with the output before it.  Poke the
38580Sstevel@tonic-gate 			 * start routine, just in case.
38590Sstevel@tonic-gate 			 */
38600Sstevel@tonic-gate 			(void) putq(q, mp);
38615295Srandyf 
38620Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
38635295Srandyf 			if (ASYWPUTDO_NOT_SUSP(async, wput)) {
38645295Srandyf 				/*
38655295Srandyf 				 * If an TIOCSBRK is in progress,
38665295Srandyf 				 * clean it as TIOCCBRK does.
38675295Srandyf 				 * then kick off output.
38685295Srandyf 				 * If TIOCSBRK is not in progress,
38695295Srandyf 				 * just kick off output.
38705295Srandyf 				 */
38715295Srandyf 				async_resume_utbrk(async);
38725295Srandyf 			}
38730Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
38740Sstevel@tonic-gate 			break;
38750Sstevel@tonic-gate 
38760Sstevel@tonic-gate 		default:
38770Sstevel@tonic-gate 			/*
38780Sstevel@tonic-gate 			 * Do it now.
38790Sstevel@tonic-gate 			 */
38805295Srandyf 			mutex_enter(&asy->asy_excl);
38815295Srandyf 			if (ASYWPUTDO_NOT_SUSP(async, wput)) {
38825295Srandyf 				mutex_exit(&asy->asy_excl);
38835295Srandyf 				async_ioctl(async, q, mp);
38845295Srandyf 				break;
38855295Srandyf 			}
38865295Srandyf 			async_put_suspq(asy, mp);
38875295Srandyf 			mutex_exit(&asy->asy_excl);
38880Sstevel@tonic-gate 			break;
38890Sstevel@tonic-gate 		}
38900Sstevel@tonic-gate 		break;
38910Sstevel@tonic-gate 
38920Sstevel@tonic-gate 	case M_FLUSH:
38930Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
38940Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
38950Sstevel@tonic-gate 
38960Sstevel@tonic-gate 			/*
38970Sstevel@tonic-gate 			 * Abort any output in progress.
38980Sstevel@tonic-gate 			 */
38990Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
39000Sstevel@tonic-gate 			if (async->async_flags & ASYNC_BUSY) {
39015295Srandyf 				DEBUGCONT1(ASY_DEBUG_BUSY, "asy%dwput: "
39020Sstevel@tonic-gate 				    "Clearing async_ocnt, "
39030Sstevel@tonic-gate 				    "leaving ASYNC_BUSY set\n",
39040Sstevel@tonic-gate 				    instance);
39050Sstevel@tonic-gate 				async->async_ocnt = 0;
39060Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_BUSY;
39070Sstevel@tonic-gate 			} /* if */
39085295Srandyf 
39095295Srandyf 			if (ASYWPUTDO_NOT_SUSP(async, wput)) {
39105295Srandyf 				/* Flush FIFO buffers */
39115295Srandyf 				if (asy->asy_use_fifo == FIFO_ON) {
39125295Srandyf 					asy_reset_fifo(asy, FIFOTXFLSH);
39135295Srandyf 				}
39145295Srandyf 			}
39150Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
39160Sstevel@tonic-gate 
39170Sstevel@tonic-gate 			/* Flush FIFO buffers */
39180Sstevel@tonic-gate 			if (asy->asy_use_fifo == FIFO_ON) {
39190Sstevel@tonic-gate 				asy_reset_fifo(asy, FIFOTXFLSH);
39200Sstevel@tonic-gate 			}
39210Sstevel@tonic-gate 
39220Sstevel@tonic-gate 			/*
39230Sstevel@tonic-gate 			 * Flush our write queue.
39240Sstevel@tonic-gate 			 */
39250Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
39260Sstevel@tonic-gate 			if (async->async_xmitblk != NULL) {
39270Sstevel@tonic-gate 				freeb(async->async_xmitblk);
39280Sstevel@tonic-gate 				async->async_xmitblk = NULL;
39290Sstevel@tonic-gate 			}
39300Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
39310Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;	/* it has been flushed */
39320Sstevel@tonic-gate 		}
39330Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
39345295Srandyf 			if (ASYWPUTDO_NOT_SUSP(async, wput)) {
39355295Srandyf 				/* Flush FIFO buffers */
39365295Srandyf 				if (asy->asy_use_fifo == FIFO_ON) {
39375295Srandyf 					asy_reset_fifo(asy, FIFORXFLSH);
39385295Srandyf 				}
39390Sstevel@tonic-gate 			}
39400Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
39410Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
39420Sstevel@tonic-gate 		} else {
39430Sstevel@tonic-gate 			freemsg(mp);
39440Sstevel@tonic-gate 		}
39450Sstevel@tonic-gate 
39460Sstevel@tonic-gate 		/*
39470Sstevel@tonic-gate 		 * We must make sure we process messages that survive the
39480Sstevel@tonic-gate 		 * write-side flush.
39490Sstevel@tonic-gate 		 */
39505295Srandyf 		if (ASYWPUTDO_NOT_SUSP(async, wput)) {
39515295Srandyf 			mutex_enter(&asy->asy_excl);
39525295Srandyf 			async_start(async);
39535295Srandyf 			mutex_exit(&asy->asy_excl);
39545295Srandyf 		}
39550Sstevel@tonic-gate 		break;
39560Sstevel@tonic-gate 
39570Sstevel@tonic-gate 	case M_BREAK:
39580Sstevel@tonic-gate 	case M_DELAY:
39590Sstevel@tonic-gate 	case M_DATA:
39600Sstevel@tonic-gate 		/*
39610Sstevel@tonic-gate 		 * Queue the message up to be transmitted,
39620Sstevel@tonic-gate 		 * and poke the start routine.
39630Sstevel@tonic-gate 		 */
39640Sstevel@tonic-gate 		(void) putq(q, mp);
39655295Srandyf 		if (ASYWPUTDO_NOT_SUSP(async, wput)) {
39665295Srandyf 			mutex_enter(&asy->asy_excl);
39675295Srandyf 			async_start(async);
39685295Srandyf 			mutex_exit(&asy->asy_excl);
39695295Srandyf 		}
39700Sstevel@tonic-gate 		break;
39710Sstevel@tonic-gate 
39720Sstevel@tonic-gate 	case M_STOPI:
39730Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
39745295Srandyf 		if (ASYWPUTDO_NOT_SUSP(async, wput)) {
39755295Srandyf 			mutex_enter(&asy->asy_excl_hi);
39765295Srandyf 			if (!(async->async_inflow_source & IN_FLOW_USER)) {
39775295Srandyf 				async_flowcontrol_hw_input(asy, FLOW_STOP,
39785295Srandyf 				    IN_FLOW_USER);
39795295Srandyf 				(void) async_flowcontrol_sw_input(asy,
39805295Srandyf 				    FLOW_STOP, IN_FLOW_USER);
39815295Srandyf 			}
39825295Srandyf 			mutex_exit(&asy->asy_excl_hi);
39835295Srandyf 			mutex_exit(&asy->asy_excl);
39845295Srandyf 			freemsg(mp);
39855295Srandyf 			break;
39860Sstevel@tonic-gate 		}
39875295Srandyf 		async_put_suspq(asy, mp);
39880Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
39890Sstevel@tonic-gate 		break;
39900Sstevel@tonic-gate 
39910Sstevel@tonic-gate 	case M_STARTI:
39920Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
39935295Srandyf 		if (ASYWPUTDO_NOT_SUSP(async, wput)) {
39945295Srandyf 			mutex_enter(&asy->asy_excl_hi);
39955295Srandyf 			if (async->async_inflow_source & IN_FLOW_USER) {
39965295Srandyf 				async_flowcontrol_hw_input(asy, FLOW_START,
39975295Srandyf 				    IN_FLOW_USER);
39985295Srandyf 				(void) async_flowcontrol_sw_input(asy,
39995295Srandyf 				    FLOW_START, IN_FLOW_USER);
40005295Srandyf 			}
40015295Srandyf 			mutex_exit(&asy->asy_excl_hi);
40025295Srandyf 			mutex_exit(&asy->asy_excl);
40035295Srandyf 			freemsg(mp);
40045295Srandyf 			break;
40050Sstevel@tonic-gate 		}
40065295Srandyf 		async_put_suspq(asy, mp);
40070Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
40080Sstevel@tonic-gate 		break;
40090Sstevel@tonic-gate 
40100Sstevel@tonic-gate 	case M_CTL:
40110Sstevel@tonic-gate 		if (MBLKL(mp) >= sizeof (struct iocblk) &&
40120Sstevel@tonic-gate 		    ((struct iocblk *)mp->b_rptr)->ioc_cmd == MC_POSIXQUERY) {
40135295Srandyf 			mutex_enter(&asy->asy_excl);
40145295Srandyf 			if (ASYWPUTDO_NOT_SUSP(async, wput)) {
40155295Srandyf 				((struct iocblk *)mp->b_rptr)->ioc_cmd =
40165295Srandyf 				    MC_HAS_POSIX;
40175295Srandyf 				mutex_exit(&asy->asy_excl);
40185295Srandyf 				qreply(q, mp);
40195295Srandyf 				break;
40205295Srandyf 			} else {
40215295Srandyf 				async_put_suspq(asy, mp);
40225295Srandyf 			}
40230Sstevel@tonic-gate 		} else {
40240Sstevel@tonic-gate 			/*
40250Sstevel@tonic-gate 			 * These MC_SERVICE type messages are used by upper
40260Sstevel@tonic-gate 			 * modules to tell this driver to send input up
40270Sstevel@tonic-gate 			 * immediately, or that it can wait for normal
40280Sstevel@tonic-gate 			 * processing that may or may not be done.  Sun
40290Sstevel@tonic-gate 			 * requires these for the mouse module.
40300Sstevel@tonic-gate 			 * (XXX - for x86?)
40310Sstevel@tonic-gate 			 */
40320Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
40330Sstevel@tonic-gate 			switch (*mp->b_rptr) {
40340Sstevel@tonic-gate 
40350Sstevel@tonic-gate 			case MC_SERVICEIMM:
40360Sstevel@tonic-gate 				async->async_flags |= ASYNC_SERVICEIMM;
40370Sstevel@tonic-gate 				break;
40380Sstevel@tonic-gate 
40390Sstevel@tonic-gate 			case MC_SERVICEDEF:
40400Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_SERVICEIMM;
40410Sstevel@tonic-gate 				break;
40420Sstevel@tonic-gate 			}
40430Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
40440Sstevel@tonic-gate 			freemsg(mp);
40450Sstevel@tonic-gate 		}
40460Sstevel@tonic-gate 		break;
40470Sstevel@tonic-gate 
40480Sstevel@tonic-gate 	case M_IOCDATA:
40495295Srandyf 		mutex_enter(&asy->asy_excl);
40505295Srandyf 		if (ASYWPUTDO_NOT_SUSP(async, wput)) {
40515295Srandyf 			mutex_exit(&asy->asy_excl);
40525295Srandyf 			async_iocdata(q, mp);
40535295Srandyf 			break;
40545295Srandyf 		}
40555295Srandyf 		async_put_suspq(asy, mp);
40565295Srandyf 		mutex_exit(&asy->asy_excl);
40570Sstevel@tonic-gate 		break;
40580Sstevel@tonic-gate 
40590Sstevel@tonic-gate 	default:
40600Sstevel@tonic-gate 		freemsg(mp);
40610Sstevel@tonic-gate 		break;
40620Sstevel@tonic-gate 	}
40630Sstevel@tonic-gate 	return (0);
40640Sstevel@tonic-gate }
40650Sstevel@tonic-gate 
40665295Srandyf static int
40675295Srandyf asywput(queue_t *q, mblk_t *mp)
40685295Srandyf {
40695295Srandyf 	return (asywputdo(q, mp, B_TRUE));
40705295Srandyf }
40715295Srandyf 
40720Sstevel@tonic-gate /*
40730Sstevel@tonic-gate  * Retry an "ioctl", now that "bufcall" claims we may be able to allocate
40740Sstevel@tonic-gate  * the buffer we need.
40750Sstevel@tonic-gate  */
40760Sstevel@tonic-gate static void
40770Sstevel@tonic-gate async_reioctl(void *unit)
40780Sstevel@tonic-gate {
40790Sstevel@tonic-gate 	int instance = (uintptr_t)unit;
40800Sstevel@tonic-gate 	struct asyncline *async;
40810Sstevel@tonic-gate 	struct asycom *asy;
40820Sstevel@tonic-gate 	queue_t	*q;
40830Sstevel@tonic-gate 	mblk_t	*mp;
40840Sstevel@tonic-gate 
40850Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, instance);
40860Sstevel@tonic-gate 	ASSERT(asy != NULL);
40870Sstevel@tonic-gate 	async = asy->asy_priv;
40880Sstevel@tonic-gate 
40890Sstevel@tonic-gate 	/*
40900Sstevel@tonic-gate 	 * The bufcall is no longer pending.
40910Sstevel@tonic-gate 	 */
40920Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
40930Sstevel@tonic-gate 	async->async_wbufcid = 0;
40940Sstevel@tonic-gate 	if ((q = async->async_ttycommon.t_writeq) == NULL) {
40950Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
40960Sstevel@tonic-gate 		return;
40970Sstevel@tonic-gate 	}
40980Sstevel@tonic-gate 	if ((mp = async->async_ttycommon.t_iocpending) != NULL) {
40990Sstevel@tonic-gate 		/* not pending any more */
41000Sstevel@tonic-gate 		async->async_ttycommon.t_iocpending = NULL;
41010Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
41020Sstevel@tonic-gate 		async_ioctl(async, q, mp);
41030Sstevel@tonic-gate 	} else
41040Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
41050Sstevel@tonic-gate }
41060Sstevel@tonic-gate 
41070Sstevel@tonic-gate static void
41080Sstevel@tonic-gate async_iocdata(queue_t *q, mblk_t *mp)
41090Sstevel@tonic-gate {
41100Sstevel@tonic-gate 	struct asyncline	*async = (struct asyncline *)q->q_ptr;
41110Sstevel@tonic-gate 	struct asycom		*asy;
41120Sstevel@tonic-gate 	struct iocblk *ip;
41130Sstevel@tonic-gate 	struct copyresp *csp;
41140Sstevel@tonic-gate #ifdef DEBUG
41150Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
41160Sstevel@tonic-gate #endif
41170Sstevel@tonic-gate 
41180Sstevel@tonic-gate 	asy = async->async_common;
41190Sstevel@tonic-gate 	ip = (struct iocblk *)mp->b_rptr;
41200Sstevel@tonic-gate 	csp = (struct copyresp *)mp->b_rptr;
41210Sstevel@tonic-gate 
41220Sstevel@tonic-gate 	if (csp->cp_rval != 0) {
41230Sstevel@tonic-gate 		if (csp->cp_private)
41240Sstevel@tonic-gate 			freemsg(csp->cp_private);
41250Sstevel@tonic-gate 		freemsg(mp);
41260Sstevel@tonic-gate 		return;
41270Sstevel@tonic-gate 	}
41280Sstevel@tonic-gate 
41290Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
41300Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_MODEM, "async%d_iocdata: case %s\n",
41315295Srandyf 	    instance,
41325295Srandyf 	    csp->cp_cmd == TIOCMGET ? "TIOCMGET" :
41335295Srandyf 	    csp->cp_cmd == TIOCMSET ? "TIOCMSET" :
41345295Srandyf 	    csp->cp_cmd == TIOCMBIS ? "TIOCMBIS" :
41355295Srandyf 	    "TIOCMBIC");
41360Sstevel@tonic-gate 	switch (csp->cp_cmd) {
41370Sstevel@tonic-gate 
41380Sstevel@tonic-gate 	case TIOCMGET:
41390Sstevel@tonic-gate 		if (mp->b_cont) {
41400Sstevel@tonic-gate 			freemsg(mp->b_cont);
41410Sstevel@tonic-gate 			mp->b_cont = NULL;
41420Sstevel@tonic-gate 		}
41430Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
41440Sstevel@tonic-gate 		ip->ioc_error = 0;
41450Sstevel@tonic-gate 		ip->ioc_count = 0;
41460Sstevel@tonic-gate 		ip->ioc_rval = 0;
41470Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct iocblk);
41480Sstevel@tonic-gate 		break;
41490Sstevel@tonic-gate 
41500Sstevel@tonic-gate 	case TIOCMSET:
41510Sstevel@tonic-gate 	case TIOCMBIS:
41520Sstevel@tonic-gate 	case TIOCMBIC:
41530Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
41545295Srandyf 		(void) asymctl(asy, dmtoasy(*(int *)mp->b_cont->b_rptr),
41555295Srandyf 		    csp->cp_cmd);
41560Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
41570Sstevel@tonic-gate 		mioc2ack(mp, NULL, 0, 0);
41580Sstevel@tonic-gate 		break;
41590Sstevel@tonic-gate 
41600Sstevel@tonic-gate 	default:
41610Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
41620Sstevel@tonic-gate 		ip->ioc_error = EINVAL;
41630Sstevel@tonic-gate 		break;
41640Sstevel@tonic-gate 	}
41650Sstevel@tonic-gate 	qreply(q, mp);
41660Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
41670Sstevel@tonic-gate }
41680Sstevel@tonic-gate 
41690Sstevel@tonic-gate /*
41700Sstevel@tonic-gate  * debugger/console support routines.
41710Sstevel@tonic-gate  */
41720Sstevel@tonic-gate 
41730Sstevel@tonic-gate /*
41740Sstevel@tonic-gate  * put a character out
41750Sstevel@tonic-gate  * Do not use interrupts.  If char is LF, put out CR, LF.
41760Sstevel@tonic-gate  */
41770Sstevel@tonic-gate static void
41781762Slt200341 asyputchar(cons_polledio_arg_t arg, uchar_t c)
41790Sstevel@tonic-gate {
41800Sstevel@tonic-gate 	struct asycom *asy = (struct asycom *)arg;
41810Sstevel@tonic-gate 
41820Sstevel@tonic-gate 	if (c == '\n')
41830Sstevel@tonic-gate 		asyputchar(arg, '\r');
41840Sstevel@tonic-gate 
41851106Smrj 	while ((ddi_get8(asy->asy_iohandle,
41860Sstevel@tonic-gate 	    asy->asy_ioaddr + LSR) & XHRE) == 0) {
41870Sstevel@tonic-gate 		/* wait for xmit to finish */
41880Sstevel@tonic-gate 		drv_usecwait(10);
41890Sstevel@tonic-gate 	}
41900Sstevel@tonic-gate 
41910Sstevel@tonic-gate 	/* put the character out */
41921106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT, c);
41930Sstevel@tonic-gate }
41940Sstevel@tonic-gate 
41950Sstevel@tonic-gate /*
41960Sstevel@tonic-gate  * See if there's a character available. If no character is
41970Sstevel@tonic-gate  * available, return 0. Run in polled mode, no interrupts.
41980Sstevel@tonic-gate  */
41990Sstevel@tonic-gate static boolean_t
42001762Slt200341 asyischar(cons_polledio_arg_t arg)
42010Sstevel@tonic-gate {
42020Sstevel@tonic-gate 	struct asycom *asy = (struct asycom *)arg;
42030Sstevel@tonic-gate 
42045295Srandyf 	return ((ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) & RCA)
42055295Srandyf 	    != 0);
42060Sstevel@tonic-gate }
42070Sstevel@tonic-gate 
42080Sstevel@tonic-gate /*
42090Sstevel@tonic-gate  * Get a character. Run in polled mode, no interrupts.
42100Sstevel@tonic-gate  */
42110Sstevel@tonic-gate static int
42121762Slt200341 asygetchar(cons_polledio_arg_t arg)
42130Sstevel@tonic-gate {
42140Sstevel@tonic-gate 	struct asycom *asy = (struct asycom *)arg;
42150Sstevel@tonic-gate 
42160Sstevel@tonic-gate 	while (!asyischar(arg))
42170Sstevel@tonic-gate 		drv_usecwait(10);
42185295Srandyf 	return (ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + DAT));
42190Sstevel@tonic-gate }
42200Sstevel@tonic-gate 
42210Sstevel@tonic-gate /*
42220Sstevel@tonic-gate  * Set or get the modem control status.
42230Sstevel@tonic-gate  */
42240Sstevel@tonic-gate static int
42250Sstevel@tonic-gate asymctl(struct asycom *asy, int bits, int how)
42260Sstevel@tonic-gate {
42270Sstevel@tonic-gate 	int mcr_r, msr_r;
42280Sstevel@tonic-gate 	int instance = asy->asy_unit;
42290Sstevel@tonic-gate 
42300Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
42310Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
42320Sstevel@tonic-gate 
42330Sstevel@tonic-gate 	/* Read Modem Control Registers */
42341106Smrj 	mcr_r = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
42350Sstevel@tonic-gate 
42360Sstevel@tonic-gate 	switch (how) {
42370Sstevel@tonic-gate 
42380Sstevel@tonic-gate 	case TIOCMSET:
42390Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM,
42405295Srandyf 		    "asy%dmctl: TIOCMSET, bits = %x\n", instance, bits);
42410Sstevel@tonic-gate 		mcr_r = bits;		/* Set bits	*/
42420Sstevel@tonic-gate 		break;
42430Sstevel@tonic-gate 
42440Sstevel@tonic-gate 	case TIOCMBIS:
42450Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM, "asy%dmctl: TIOCMBIS, bits = %x\n",
42465295Srandyf 		    instance, bits);
42470Sstevel@tonic-gate 		mcr_r |= bits;		/* Mask in bits	*/
42480Sstevel@tonic-gate 		break;
42490Sstevel@tonic-gate 
42500Sstevel@tonic-gate 	case TIOCMBIC:
42510Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM, "asy%dmctl: TIOCMBIC, bits = %x\n",
42525295Srandyf 		    instance, bits);
42530Sstevel@tonic-gate 		mcr_r &= ~bits;		/* Mask out bits */
42540Sstevel@tonic-gate 		break;
42550Sstevel@tonic-gate 
42560Sstevel@tonic-gate 	case TIOCMGET:
42570Sstevel@tonic-gate 		/* Read Modem Status Registers */
42580Sstevel@tonic-gate 		/*
42590Sstevel@tonic-gate 		 * If modem interrupts are enabled, we return the
42600Sstevel@tonic-gate 		 * saved value of msr. We read MSR only in async_msint()
42610Sstevel@tonic-gate 		 */
42621106Smrj 		if (ddi_get8(asy->asy_iohandle,
42630Sstevel@tonic-gate 		    asy->asy_ioaddr + ICR) & MIEN) {
42640Sstevel@tonic-gate 			msr_r = asy->asy_msr;
42650Sstevel@tonic-gate 			DEBUGCONT2(ASY_DEBUG_MODEM,
42665295Srandyf 			    "asy%dmctl: TIOCMGET, read msr_r = %x\n",
42675295Srandyf 			    instance, msr_r);
42680Sstevel@tonic-gate 		} else {
42691106Smrj 			msr_r = ddi_get8(asy->asy_iohandle,
42705295Srandyf 			    asy->asy_ioaddr + MSR);
42710Sstevel@tonic-gate 			DEBUGCONT2(ASY_DEBUG_MODEM,
42725295Srandyf 			    "asy%dmctl: TIOCMGET, read MSR = %x\n",
42735295Srandyf 			    instance, msr_r);
42740Sstevel@tonic-gate 		}
42750Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM, "asy%dtodm: modem_lines = %x\n",
42765295Srandyf 		    instance, asytodm(mcr_r, msr_r));
42770Sstevel@tonic-gate 		return (asytodm(mcr_r, msr_r));
42780Sstevel@tonic-gate 	}
42790Sstevel@tonic-gate 
42801106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR, mcr_r);
42810Sstevel@tonic-gate 
42820Sstevel@tonic-gate 	return (mcr_r);
42830Sstevel@tonic-gate }
42840Sstevel@tonic-gate 
42850Sstevel@tonic-gate static int
42860Sstevel@tonic-gate asytodm(int mcr_r, int msr_r)
42870Sstevel@tonic-gate {
42880Sstevel@tonic-gate 	int b = 0;
42890Sstevel@tonic-gate 
42900Sstevel@tonic-gate 	/* MCR registers */
42910Sstevel@tonic-gate 	if (mcr_r & RTS)
42920Sstevel@tonic-gate 		b |= TIOCM_RTS;
42930Sstevel@tonic-gate 
42940Sstevel@tonic-gate 	if (mcr_r & DTR)
42950Sstevel@tonic-gate 		b |= TIOCM_DTR;
42960Sstevel@tonic-gate 
42970Sstevel@tonic-gate 	/* MSR registers */
42980Sstevel@tonic-gate 	if (msr_r & DCD)
42990Sstevel@tonic-gate 		b |= TIOCM_CAR;
43000Sstevel@tonic-gate 
43010Sstevel@tonic-gate 	if (msr_r & CTS)
43020Sstevel@tonic-gate 		b |= TIOCM_CTS;
43030Sstevel@tonic-gate 
43040Sstevel@tonic-gate 	if (msr_r & DSR)
43050Sstevel@tonic-gate 		b |= TIOCM_DSR;
43060Sstevel@tonic-gate 
43070Sstevel@tonic-gate 	if (msr_r & RI)
43080Sstevel@tonic-gate 		b |= TIOCM_RNG;
43090Sstevel@tonic-gate 	return (b);
43100Sstevel@tonic-gate }
43110Sstevel@tonic-gate 
43120Sstevel@tonic-gate static int
43130Sstevel@tonic-gate dmtoasy(int bits)
43140Sstevel@tonic-gate {
43150Sstevel@tonic-gate 	int b = 0;
43160Sstevel@tonic-gate 
43170Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_MODEM, "dmtoasy: bits = %x\n", bits);
43180Sstevel@tonic-gate #ifdef	CAN_NOT_SET	/* only DTR and RTS can be set */
43190Sstevel@tonic-gate 	if (bits & TIOCM_CAR)
43200Sstevel@tonic-gate 		b |= DCD;
43210Sstevel@tonic-gate 	if (bits & TIOCM_CTS)
43220Sstevel@tonic-gate 		b |= CTS;
43230Sstevel@tonic-gate 	if (bits & TIOCM_DSR)
43240Sstevel@tonic-gate 		b |= DSR;
43250Sstevel@tonic-gate 	if (bits & TIOCM_RNG)
43260Sstevel@tonic-gate 		b |= RI;
43270Sstevel@tonic-gate #endif
43280Sstevel@tonic-gate 
43290Sstevel@tonic-gate 	if (bits & TIOCM_RTS) {
43300Sstevel@tonic-gate 		DEBUGCONT0(ASY_DEBUG_MODEM, "dmtoasy: set b & RTS\n");
43310Sstevel@tonic-gate 		b |= RTS;
43320Sstevel@tonic-gate 	}
43330Sstevel@tonic-gate 	if (bits & TIOCM_DTR) {
43340Sstevel@tonic-gate 		DEBUGCONT0(ASY_DEBUG_MODEM, "dmtoasy: set b & DTR\n");
43350Sstevel@tonic-gate 		b |= DTR;
43360Sstevel@tonic-gate 	}
43370Sstevel@tonic-gate 
43380Sstevel@tonic-gate 	return (b);
43390Sstevel@tonic-gate }
43400Sstevel@tonic-gate 
43410Sstevel@tonic-gate static void
43420Sstevel@tonic-gate asyerror(int level, const char *fmt, ...)
43430Sstevel@tonic-gate {
43440Sstevel@tonic-gate 	va_list adx;
43450Sstevel@tonic-gate 	static	time_t	last;
43460Sstevel@tonic-gate 	static	const char *lastfmt;
43470Sstevel@tonic-gate 	time_t	now;
43480Sstevel@tonic-gate 
43490Sstevel@tonic-gate 	/*
43500Sstevel@tonic-gate 	 * Don't print the same error message too often.
43510Sstevel@tonic-gate 	 * Print the message only if we have not printed the
43520Sstevel@tonic-gate 	 * message within the last second.
43530Sstevel@tonic-gate 	 * Note: that fmt cannot be a pointer to a string
43540Sstevel@tonic-gate 	 * stored on the stack. The fmt pointer
43550Sstevel@tonic-gate 	 * must be in the data segment otherwise lastfmt would point
43560Sstevel@tonic-gate 	 * to non-sense.
43570Sstevel@tonic-gate 	 */
43580Sstevel@tonic-gate 	now = gethrestime_sec();
43590Sstevel@tonic-gate 	if (last == now && lastfmt == fmt)
43600Sstevel@tonic-gate 		return;
43610Sstevel@tonic-gate 
43620Sstevel@tonic-gate 	last = now;
43630Sstevel@tonic-gate 	lastfmt = fmt;
43640Sstevel@tonic-gate 
43650Sstevel@tonic-gate 	va_start(adx, fmt);
43660Sstevel@tonic-gate 	vcmn_err(level, fmt, adx);
43670Sstevel@tonic-gate 	va_end(adx);
43680Sstevel@tonic-gate }
43690Sstevel@tonic-gate 
43700Sstevel@tonic-gate /*
43710Sstevel@tonic-gate  * asy_parse_mode(dev_info_t *devi, struct asycom *asy)
43720Sstevel@tonic-gate  * The value of this property is in the form of "9600,8,n,1,-"
43730Sstevel@tonic-gate  * 1) speed: 9600, 4800, ...
43740Sstevel@tonic-gate  * 2) data bits
43750Sstevel@tonic-gate  * 3) parity: n(none), e(even), o(odd)
43760Sstevel@tonic-gate  * 4) stop bits
43770Sstevel@tonic-gate  * 5) handshake: -(none), h(hardware: rts/cts), s(software: xon/off)
43780Sstevel@tonic-gate  *
43790Sstevel@tonic-gate  * This parsing came from a SPARCstation eeprom.
43800Sstevel@tonic-gate  */
43810Sstevel@tonic-gate static void
43820Sstevel@tonic-gate asy_parse_mode(dev_info_t *devi, struct asycom *asy)
43830Sstevel@tonic-gate {
43840Sstevel@tonic-gate 	char		name[40];
43850Sstevel@tonic-gate 	char		val[40];
43860Sstevel@tonic-gate 	int		len;
43870Sstevel@tonic-gate 	int		ret;
43880Sstevel@tonic-gate 	char		*p;
43890Sstevel@tonic-gate 	char		*p1;
43900Sstevel@tonic-gate 
43910Sstevel@tonic-gate 	ASSERT(asy->asy_com_port != 0);
43920Sstevel@tonic-gate 
43930Sstevel@tonic-gate 	/*
43940Sstevel@tonic-gate 	 * Parse the ttyx-mode property
43950Sstevel@tonic-gate 	 */
43960Sstevel@tonic-gate 	(void) sprintf(name, "tty%c-mode", asy->asy_com_port + 'a' - 1);
43970Sstevel@tonic-gate 	len = sizeof (val);
43980Sstevel@tonic-gate 	ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
43990Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
44000Sstevel@tonic-gate 		(void) sprintf(name, "com%c-mode", asy->asy_com_port + '0');
44010Sstevel@tonic-gate 		len = sizeof (val);
44020Sstevel@tonic-gate 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
44030Sstevel@tonic-gate 	}
44040Sstevel@tonic-gate 
44050Sstevel@tonic-gate 	/* no property to parse */
44060Sstevel@tonic-gate 	asy->asy_cflag = 0;
44070Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS)
44080Sstevel@tonic-gate 		return;
44090Sstevel@tonic-gate 
44100Sstevel@tonic-gate 	p = val;
44110Sstevel@tonic-gate 	/* ---- baud rate ---- */
44120Sstevel@tonic-gate 	asy->asy_cflag = CREAD|B9600;		/* initial default */
44130Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0) {
44140Sstevel@tonic-gate 		*p1++ = '\0';
44150Sstevel@tonic-gate 	} else {
44160Sstevel@tonic-gate 		asy->asy_cflag |= BITS8;	/* add default bits */
44170Sstevel@tonic-gate 		return;
44180Sstevel@tonic-gate 	}
44190Sstevel@tonic-gate 
44200Sstevel@tonic-gate 	if (strcmp(p, "110") == 0)
44210Sstevel@tonic-gate 		asy->asy_bidx = B110;
44220Sstevel@tonic-gate 	else if (strcmp(p, "150") == 0)
44230Sstevel@tonic-gate 		asy->asy_bidx = B150;
44240Sstevel@tonic-gate 	else if (strcmp(p, "300") == 0)
44250Sstevel@tonic-gate 		asy->asy_bidx = B300;
44260Sstevel@tonic-gate 	else if (strcmp(p, "600") == 0)
44270Sstevel@tonic-gate 		asy->asy_bidx = B600;
44280Sstevel@tonic-gate 	else if (strcmp(p, "1200") == 0)
44290Sstevel@tonic-gate 		asy->asy_bidx = B1200;
44300Sstevel@tonic-gate 	else if (strcmp(p, "2400") == 0)
44310Sstevel@tonic-gate 		asy->asy_bidx = B2400;
44320Sstevel@tonic-gate 	else if (strcmp(p, "4800") == 0)
44330Sstevel@tonic-gate 		asy->asy_bidx = B4800;
44340Sstevel@tonic-gate 	else if (strcmp(p, "9600") == 0)
44350Sstevel@tonic-gate 		asy->asy_bidx = B9600;
44360Sstevel@tonic-gate 	else if (strcmp(p, "19200") == 0)
44370Sstevel@tonic-gate 		asy->asy_bidx = B19200;
44380Sstevel@tonic-gate 	else if (strcmp(p, "38400") == 0)
44390Sstevel@tonic-gate 		asy->asy_bidx = B38400;
44400Sstevel@tonic-gate 	else if (strcmp(p, "57600") == 0)
44410Sstevel@tonic-gate 		asy->asy_bidx = B57600;
44420Sstevel@tonic-gate 	else if (strcmp(p, "115200") == 0)
44430Sstevel@tonic-gate 		asy->asy_bidx = B115200;
44440Sstevel@tonic-gate 	else
44450Sstevel@tonic-gate 		asy->asy_bidx = B9600;
44460Sstevel@tonic-gate 
44470Sstevel@tonic-gate 	asy->asy_cflag &= ~CBAUD;
44480Sstevel@tonic-gate 	if (asy->asy_bidx > CBAUD) {	/* > 38400 uses the CBAUDEXT bit */
44490Sstevel@tonic-gate 		asy->asy_cflag |= CBAUDEXT;
44500Sstevel@tonic-gate 		asy->asy_cflag |= asy->asy_bidx - CBAUD - 1;
44510Sstevel@tonic-gate 	} else {
44520Sstevel@tonic-gate 		asy->asy_cflag |= asy->asy_bidx;
44530Sstevel@tonic-gate 	}
44540Sstevel@tonic-gate 
44550Sstevel@tonic-gate 	ASSERT(asy->asy_bidx == BAUDINDEX(asy->asy_cflag));
44560Sstevel@tonic-gate 
44570Sstevel@tonic-gate 	/* ---- Next item is data bits ---- */
44580Sstevel@tonic-gate 	p = p1;
44590Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0)  {
44600Sstevel@tonic-gate 		*p1++ = '\0';
44610Sstevel@tonic-gate 	} else {
44620Sstevel@tonic-gate 		asy->asy_cflag |= BITS8;	/* add default bits */
44630Sstevel@tonic-gate 		return;
44640Sstevel@tonic-gate 	}
44650Sstevel@tonic-gate 	switch (*p) {
44660Sstevel@tonic-gate 		default:
44670Sstevel@tonic-gate 		case '8':
44680Sstevel@tonic-gate 			asy->asy_cflag |= CS8;
44690Sstevel@tonic-gate 			asy->asy_lcr = BITS8;
44700Sstevel@tonic-gate 			break;
44710Sstevel@tonic-gate 		case '7':
44720Sstevel@tonic-gate 			asy->asy_cflag |= CS7;
44730Sstevel@tonic-gate 			asy->asy_lcr = BITS7;
44740Sstevel@tonic-gate 			break;
44750Sstevel@tonic-gate 		case '6':
44760Sstevel@tonic-gate 			asy->asy_cflag |= CS6;
44770Sstevel@tonic-gate 			asy->asy_lcr = BITS6;
44780Sstevel@tonic-gate 			break;
44790Sstevel@tonic-gate 		case '5':
44800Sstevel@tonic-gate 			/* LINTED: CS5 is currently zero (but might change) */
44810Sstevel@tonic-gate 			asy->asy_cflag |= CS5;
44820Sstevel@tonic-gate 			asy->asy_lcr = BITS5;
44830Sstevel@tonic-gate 			break;
44840Sstevel@tonic-gate 	}
44850Sstevel@tonic-gate 
44860Sstevel@tonic-gate 	/* ---- Parity info ---- */
44870Sstevel@tonic-gate 	p = p1;
44880Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0)  {
44890Sstevel@tonic-gate 		*p1++ = '\0';
44900Sstevel@tonic-gate 	} else {
44910Sstevel@tonic-gate 		return;
44920Sstevel@tonic-gate 	}
44930Sstevel@tonic-gate 	switch (*p)  {
44940Sstevel@tonic-gate 		default:
44950Sstevel@tonic-gate 		case 'n':
44960Sstevel@tonic-gate 			break;
44970Sstevel@tonic-gate 		case 'e':
44980Sstevel@tonic-gate 			asy->asy_cflag |= PARENB;
44990Sstevel@tonic-gate 			asy->asy_lcr |= PEN; break;
45000Sstevel@tonic-gate 		case 'o':
45010Sstevel@tonic-gate 			asy->asy_cflag |= PARENB|PARODD;
45020Sstevel@tonic-gate 			asy->asy_lcr |= PEN|EPS;
45030Sstevel@tonic-gate 			break;
45040Sstevel@tonic-gate 	}
45050Sstevel@tonic-gate 
45060Sstevel@tonic-gate 	/* ---- Find stop bits ---- */
45070Sstevel@tonic-gate 	p = p1;
45080Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0)  {
45090Sstevel@tonic-gate 		*p1++ = '\0';
45100Sstevel@tonic-gate 	} else {
45110Sstevel@tonic-gate 		return;
45120Sstevel@tonic-gate 	}
45130Sstevel@tonic-gate 	if (*p == '2') {
45140Sstevel@tonic-gate 		asy->asy_cflag |= CSTOPB;
45150Sstevel@tonic-gate 		asy->asy_lcr |= STB;
45160Sstevel@tonic-gate 	}
45170Sstevel@tonic-gate 
45180Sstevel@tonic-gate 	/* ---- handshake is next ---- */
45190Sstevel@tonic-gate 	p = p1;
45200Sstevel@tonic-gate 	if (p) {
45210Sstevel@tonic-gate 		if ((p1 = strchr(p, ',')) != 0)
45220Sstevel@tonic-gate 			*p1++ = '\0';
45230Sstevel@tonic-gate 
45240Sstevel@tonic-gate 		if (*p == 'h')
45250Sstevel@tonic-gate 			asy->asy_cflag |= CRTSCTS;
45260Sstevel@tonic-gate 		else if (*p == 's')
45270Sstevel@tonic-gate 			asy->asy_cflag |= CRTSXOFF;
45280Sstevel@tonic-gate 	}
45290Sstevel@tonic-gate }
45300Sstevel@tonic-gate 
45310Sstevel@tonic-gate /*
45320Sstevel@tonic-gate  * Check for abort character sequence
45330Sstevel@tonic-gate  */
45340Sstevel@tonic-gate static boolean_t
45350Sstevel@tonic-gate abort_charseq_recognize(uchar_t ch)
45360Sstevel@tonic-gate {
45370Sstevel@tonic-gate 	static int state = 0;
45380Sstevel@tonic-gate #define	CNTRL(c) ((c)&037)
45390Sstevel@tonic-gate 	static char sequence[] = { '\r', '~', CNTRL('b') };
45400Sstevel@tonic-gate 
45410Sstevel@tonic-gate 	if (ch == sequence[state]) {
45420Sstevel@tonic-gate 		if (++state >= sizeof (sequence)) {
45430Sstevel@tonic-gate 			state = 0;
45440Sstevel@tonic-gate 			return (B_TRUE);
45450Sstevel@tonic-gate 		}
45460Sstevel@tonic-gate 	} else {
45470Sstevel@tonic-gate 		state = (ch == sequence[0]) ? 1 : 0;
45480Sstevel@tonic-gate 	}
45490Sstevel@tonic-gate 	return (B_FALSE);
45500Sstevel@tonic-gate }
45510Sstevel@tonic-gate 
45520Sstevel@tonic-gate /*
45530Sstevel@tonic-gate  * Flow control functions
45540Sstevel@tonic-gate  */
45550Sstevel@tonic-gate /*
45560Sstevel@tonic-gate  * Software input flow control
45570Sstevel@tonic-gate  * This function can execute software input flow control sucessfully
45580Sstevel@tonic-gate  * at most of situations except that the line is in BREAK status
45590Sstevel@tonic-gate  * (timed and untimed break).
45600Sstevel@tonic-gate  * INPUT VALUE of onoff:
45610Sstevel@tonic-gate  *               FLOW_START means to send out a XON char
45620Sstevel@tonic-gate  *                          and clear SW input flow control flag.
45630Sstevel@tonic-gate  *               FLOW_STOP means to send out a XOFF char
45640Sstevel@tonic-gate  *                          and set SW input flow control flag.
45650Sstevel@tonic-gate  *               FLOW_CHECK means to check whether there is pending XON/XOFF
45660Sstevel@tonic-gate  *                          if it is true, send it out.
45670Sstevel@tonic-gate  * INPUT VALUE of type:
45680Sstevel@tonic-gate  *		 IN_FLOW_RINGBUFF means flow control is due to RING BUFFER
45690Sstevel@tonic-gate  *		 IN_FLOW_STREAMS means flow control is due to STREAMS
45700Sstevel@tonic-gate  *		 IN_FLOW_USER means flow control is due to user's commands
45710Sstevel@tonic-gate  * RETURN VALUE: B_FALSE means no flow control char is sent
45720Sstevel@tonic-gate  *               B_TRUE means one flow control char is sent
45730Sstevel@tonic-gate  */
45740Sstevel@tonic-gate static boolean_t
45750Sstevel@tonic-gate async_flowcontrol_sw_input(struct asycom *asy, async_flowc_action onoff,
45760Sstevel@tonic-gate     int type)
45770Sstevel@tonic-gate {
45780Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
45790Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
45800Sstevel@tonic-gate 	int rval = B_FALSE;
45810Sstevel@tonic-gate 
45820Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
45830Sstevel@tonic-gate 
45840Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_iflag & IXOFF))
45850Sstevel@tonic-gate 		return (rval);
45860Sstevel@tonic-gate 
45870Sstevel@tonic-gate 	/*
45880Sstevel@tonic-gate 	 * If we get this far, then we know IXOFF is set.
45890Sstevel@tonic-gate 	 */
45900Sstevel@tonic-gate 	switch (onoff) {
45910Sstevel@tonic-gate 	case FLOW_STOP:
45920Sstevel@tonic-gate 		async->async_inflow_source |= type;
45930Sstevel@tonic-gate 
45940Sstevel@tonic-gate 		/*
45950Sstevel@tonic-gate 		 * We'll send an XOFF character for each of up to
45960Sstevel@tonic-gate 		 * three different input flow control attempts to stop input.
45970Sstevel@tonic-gate 		 * If we already send out one XOFF, but FLOW_STOP comes again,
45980Sstevel@tonic-gate 		 * it seems that input flow control becomes more serious,
45990Sstevel@tonic-gate 		 * then send XOFF again.
46000Sstevel@tonic-gate 		 */
46010Sstevel@tonic-gate 		if (async->async_inflow_source & (IN_FLOW_RINGBUFF |
46020Sstevel@tonic-gate 		    IN_FLOW_STREAMS | IN_FLOW_USER))
46030Sstevel@tonic-gate 			async->async_flags |= ASYNC_SW_IN_FLOW |
46040Sstevel@tonic-gate 			    ASYNC_SW_IN_NEEDED;
46050Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_SFLOW, "async%d: input sflow stop, "
46060Sstevel@tonic-gate 		    "type = %x\n", instance, async->async_inflow_source);
46070Sstevel@tonic-gate 		break;
46080Sstevel@tonic-gate 	case FLOW_START:
46090Sstevel@tonic-gate 		async->async_inflow_source &= ~type;
46100Sstevel@tonic-gate 		if (async->async_inflow_source == 0) {
46110Sstevel@tonic-gate 			async->async_flags = (async->async_flags &
46120Sstevel@tonic-gate 			    ~ASYNC_SW_IN_FLOW) | ASYNC_SW_IN_NEEDED;
46130Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_SFLOW, "async%d: "
46140Sstevel@tonic-gate 			    "input sflow start\n", instance);
46150Sstevel@tonic-gate 		}
46160Sstevel@tonic-gate 		break;
46170Sstevel@tonic-gate 	default:
46180Sstevel@tonic-gate 		break;
46190Sstevel@tonic-gate 	}
46200Sstevel@tonic-gate 
46210Sstevel@tonic-gate 	if (((async->async_flags & (ASYNC_SW_IN_NEEDED | ASYNC_BREAK |
46220Sstevel@tonic-gate 	    ASYNC_OUT_SUSPEND)) == ASYNC_SW_IN_NEEDED) &&
46231106Smrj 	    (ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) & XHRE)) {
46240Sstevel@tonic-gate 		/*
46250Sstevel@tonic-gate 		 * If we get this far, then we know we need to send out
46260Sstevel@tonic-gate 		 * XON or XOFF char.
46270Sstevel@tonic-gate 		 */
46280Sstevel@tonic-gate 		async->async_flags = (async->async_flags &
46290Sstevel@tonic-gate 		    ~ASYNC_SW_IN_NEEDED) | ASYNC_BUSY;
46301106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
46310Sstevel@tonic-gate 		    async->async_flags & ASYNC_SW_IN_FLOW ?
46320Sstevel@tonic-gate 		    async->async_stopc : async->async_startc);
46330Sstevel@tonic-gate 		rval = B_TRUE;
46340Sstevel@tonic-gate 	}
46350Sstevel@tonic-gate 	return (rval);
46360Sstevel@tonic-gate }
46370Sstevel@tonic-gate 
46380Sstevel@tonic-gate /*
46390Sstevel@tonic-gate  * Software output flow control
46400Sstevel@tonic-gate  * This function can be executed sucessfully at any situation.
46410Sstevel@tonic-gate  * It does not handle HW, and just change the SW output flow control flag.
46420Sstevel@tonic-gate  * INPUT VALUE of onoff:
46430Sstevel@tonic-gate  *                 FLOW_START means to clear SW output flow control flag,
46440Sstevel@tonic-gate  *			also combine with HW output flow control status to
46450Sstevel@tonic-gate  *			determine if we need to set ASYNC_OUT_FLW_RESUME.
46460Sstevel@tonic-gate  *                 FLOW_STOP means to set SW output flow control flag,
46470Sstevel@tonic-gate  *			also clear ASYNC_OUT_FLW_RESUME.
46480Sstevel@tonic-gate  */
46490Sstevel@tonic-gate static void
46500Sstevel@tonic-gate async_flowcontrol_sw_output(struct asycom *asy, async_flowc_action onoff)
46510Sstevel@tonic-gate {
46520Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
46530Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
46540Sstevel@tonic-gate 
46550Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
46560Sstevel@tonic-gate 
46570Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_iflag & IXON))
46580Sstevel@tonic-gate 		return;
46590Sstevel@tonic-gate 
46600Sstevel@tonic-gate 	switch (onoff) {
46610Sstevel@tonic-gate 	case FLOW_STOP:
46620Sstevel@tonic-gate 		async->async_flags |= ASYNC_SW_OUT_FLW;
46630Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
46640Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_SFLOW, "async%d: output sflow stop\n",
46650Sstevel@tonic-gate 		    instance);
46660Sstevel@tonic-gate 		break;
46670Sstevel@tonic-gate 	case FLOW_START:
46680Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_SW_OUT_FLW;
46690Sstevel@tonic-gate 		if (!(async->async_flags & ASYNC_HW_OUT_FLW))
46700Sstevel@tonic-gate 			async->async_flags |= ASYNC_OUT_FLW_RESUME;
46710Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_SFLOW, "async%d: output sflow start\n",
46720Sstevel@tonic-gate 		    instance);
46730Sstevel@tonic-gate 		break;
46740Sstevel@tonic-gate 	default:
46750Sstevel@tonic-gate 		break;
46760Sstevel@tonic-gate 	}
46770Sstevel@tonic-gate }
46780Sstevel@tonic-gate 
46790Sstevel@tonic-gate /*
46800Sstevel@tonic-gate  * Hardware input flow control
46810Sstevel@tonic-gate  * This function can be executed sucessfully at any situation.
46820Sstevel@tonic-gate  * It directly changes RTS depending on input parameter onoff.
46830Sstevel@tonic-gate  * INPUT VALUE of onoff:
46840Sstevel@tonic-gate  *       FLOW_START means to clear HW input flow control flag,
46850Sstevel@tonic-gate  *                  and pull up RTS if it is low.
46860Sstevel@tonic-gate  *       FLOW_STOP means to set HW input flow control flag,
46870Sstevel@tonic-gate  *                  and low RTS if it is high.
46880Sstevel@tonic-gate  * INPUT VALUE of type:
46890Sstevel@tonic-gate  *		 IN_FLOW_RINGBUFF means flow control is due to RING BUFFER
46900Sstevel@tonic-gate  *		 IN_FLOW_STREAMS means flow control is due to STREAMS
46910Sstevel@tonic-gate  *		 IN_FLOW_USER means flow control is due to user's commands
46920Sstevel@tonic-gate  */
46930Sstevel@tonic-gate static void
46940Sstevel@tonic-gate async_flowcontrol_hw_input(struct asycom *asy, async_flowc_action onoff,
46950Sstevel@tonic-gate     int type)
46960Sstevel@tonic-gate {
46970Sstevel@tonic-gate 	uchar_t	mcr;
46980Sstevel@tonic-gate 	uchar_t	flag;
46990Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
47000Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
47010Sstevel@tonic-gate 
47020Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
47030Sstevel@tonic-gate 
47040Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_cflag & CRTSXOFF))
47050Sstevel@tonic-gate 		return;
47060Sstevel@tonic-gate 
47070Sstevel@tonic-gate 	switch (onoff) {
47080Sstevel@tonic-gate 	case FLOW_STOP:
47090Sstevel@tonic-gate 		async->async_inflow_source |= type;
47100Sstevel@tonic-gate 		if (async->async_inflow_source & (IN_FLOW_RINGBUFF |
47110Sstevel@tonic-gate 		    IN_FLOW_STREAMS | IN_FLOW_USER))
47120Sstevel@tonic-gate 			async->async_flags |= ASYNC_HW_IN_FLOW;
47130Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_HFLOW, "async%d: input hflow stop, "
47140Sstevel@tonic-gate 		    "type = %x\n", instance, async->async_inflow_source);
47150Sstevel@tonic-gate 		break;
47160Sstevel@tonic-gate 	case FLOW_START:
47170Sstevel@tonic-gate 		async->async_inflow_source &= ~type;
47180Sstevel@tonic-gate 		if (async->async_inflow_source == 0) {
47190Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_HW_IN_FLOW;
47200Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_HFLOW, "async%d: "
47210Sstevel@tonic-gate 			    "input hflow start\n", instance);
47220Sstevel@tonic-gate 		}
47230Sstevel@tonic-gate 		break;
47240Sstevel@tonic-gate 	default:
47250Sstevel@tonic-gate 		break;
47260Sstevel@tonic-gate 	}
47271106Smrj 	mcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
47280Sstevel@tonic-gate 	flag = (async->async_flags & ASYNC_HW_IN_FLOW) ? 0 : RTS;
47290Sstevel@tonic-gate 
47300Sstevel@tonic-gate 	if (((mcr ^ flag) & RTS) != 0) {
47311106Smrj 		ddi_put8(asy->asy_iohandle,
47320Sstevel@tonic-gate 		    asy->asy_ioaddr + MCR, (mcr ^ RTS));
47330Sstevel@tonic-gate 	}
47340Sstevel@tonic-gate }
47350Sstevel@tonic-gate 
47360Sstevel@tonic-gate /*
47370Sstevel@tonic-gate  * Hardware output flow control
47380Sstevel@tonic-gate  * This function can execute HW output flow control sucessfully
47390Sstevel@tonic-gate  * at any situation.
47400Sstevel@tonic-gate  * It doesn't really change RTS, and just change
47410Sstevel@tonic-gate  * HW output flow control flag depending on CTS status.
47420Sstevel@tonic-gate  * INPUT VALUE of onoff:
47430Sstevel@tonic-gate  *                FLOW_START means to clear HW output flow control flag.
47440Sstevel@tonic-gate  *			also combine with SW output flow control status to
47450Sstevel@tonic-gate  *			determine if we need to set ASYNC_OUT_FLW_RESUME.
47460Sstevel@tonic-gate  *                FLOW_STOP means to set HW output flow control flag.
47470Sstevel@tonic-gate  *			also clear ASYNC_OUT_FLW_RESUME.
47480Sstevel@tonic-gate  */
47490Sstevel@tonic-gate static void
47500Sstevel@tonic-gate async_flowcontrol_hw_output(struct asycom *asy, async_flowc_action onoff)
47510Sstevel@tonic-gate {
47520Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
47530Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
47540Sstevel@tonic-gate 
47550Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
47560Sstevel@tonic-gate 
47570Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_cflag & CRTSCTS))
47580Sstevel@tonic-gate 		return;
47590Sstevel@tonic-gate 
47600Sstevel@tonic-gate 	switch (onoff) {
47610Sstevel@tonic-gate 	case FLOW_STOP:
47620Sstevel@tonic-gate 		async->async_flags |= ASYNC_HW_OUT_FLW;
47630Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
47640Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_HFLOW, "async%d: output hflow stop\n",
47650Sstevel@tonic-gate 		    instance);
47660Sstevel@tonic-gate 		break;
47670Sstevel@tonic-gate 	case FLOW_START:
47680Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_HW_OUT_FLW;
47690Sstevel@tonic-gate 		if (!(async->async_flags & ASYNC_SW_OUT_FLW))
47700Sstevel@tonic-gate 			async->async_flags |= ASYNC_OUT_FLW_RESUME;
47710Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_HFLOW, "async%d: output hflow start\n",
47720Sstevel@tonic-gate 		    instance);
47730Sstevel@tonic-gate 		break;
47740Sstevel@tonic-gate 	default:
47750Sstevel@tonic-gate 		break;
47760Sstevel@tonic-gate 	}
47770Sstevel@tonic-gate }
4778