xref: /onnv-gate/usr/src/uts/common/io/asy.c (revision 3359)
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*3359Smyers  * Copyright 2007 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>
63*3359Smyers #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 
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate  * Baud rate table. Indexed by #defines found in sys/termios.h
2350Sstevel@tonic-gate  */
2360Sstevel@tonic-gate ushort_t asyspdtab[] = {
2370Sstevel@tonic-gate 	0,	/* 0 baud rate */
2380Sstevel@tonic-gate 	0x900,	/* 50 baud rate */
2390Sstevel@tonic-gate 	0x600,	/* 75 baud rate */
2400Sstevel@tonic-gate 	0x417,	/* 110 baud rate (%0.026) */
2410Sstevel@tonic-gate 	0x359,	/* 134 baud rate (%0.058) */
2420Sstevel@tonic-gate 	0x300,	/* 150 baud rate */
2430Sstevel@tonic-gate 	0x240,	/* 200 baud rate */
2440Sstevel@tonic-gate 	0x180,	/* 300 baud rate */
2450Sstevel@tonic-gate 	0x0c0,	/* 600 baud rate */
2460Sstevel@tonic-gate 	0x060,	/* 1200 baud rate */
2470Sstevel@tonic-gate 	0x040,	/* 1800 baud rate */
2480Sstevel@tonic-gate 	0x030,	/* 2400 baud rate */
2490Sstevel@tonic-gate 	0x018,	/* 4800 baud rate */
2500Sstevel@tonic-gate 	0x00c,	/* 9600 baud rate */
2510Sstevel@tonic-gate 	0x006,	/* 19200 baud rate */
2520Sstevel@tonic-gate 	0x003,	/* 38400 baud rate */
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	0x002,	/* 57600 baud rate */
2550Sstevel@tonic-gate 	0x0,	/* 76800 baud rate not supported */
2560Sstevel@tonic-gate 	0x001,	/* 115200 baud rate */
2570Sstevel@tonic-gate 	0x0,	/* 153600 baud rate not supported */
2580Sstevel@tonic-gate 	0x0,	/* 0x8002 (SMC chip) 230400 baud rate not supported */
2590Sstevel@tonic-gate 	0x0,	/* 307200 baud rate not supported */
2600Sstevel@tonic-gate 	0x0,	/* 0x8001 (SMC chip) 460800 baud rate not supported */
2610Sstevel@tonic-gate 	0x0,	/* unused */
2620Sstevel@tonic-gate 	0x0,	/* unused */
2630Sstevel@tonic-gate 	0x0,	/* unused */
2640Sstevel@tonic-gate 	0x0,	/* unused */
2650Sstevel@tonic-gate 	0x0,	/* unused */
2660Sstevel@tonic-gate 	0x0,	/* unused */
2670Sstevel@tonic-gate 	0x0,	/* unused */
2680Sstevel@tonic-gate 	0x0,	/* unused */
2690Sstevel@tonic-gate 	0x0,	/* unused */
2700Sstevel@tonic-gate };
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate static int asyrsrv(queue_t *q);
2730Sstevel@tonic-gate static int asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr);
2740Sstevel@tonic-gate static int asyclose(queue_t *q, int flag, cred_t *credp);
2750Sstevel@tonic-gate static int asywput(queue_t *q, mblk_t *mp);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate struct module_info asy_info = {
2780Sstevel@tonic-gate 	0,
2790Sstevel@tonic-gate 	"asy",
2800Sstevel@tonic-gate 	0,
2810Sstevel@tonic-gate 	INFPSZ,
2820Sstevel@tonic-gate 	4096,
2830Sstevel@tonic-gate 	128
2840Sstevel@tonic-gate };
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate static struct qinit asy_rint = {
2870Sstevel@tonic-gate 	putq,
2880Sstevel@tonic-gate 	asyrsrv,
2890Sstevel@tonic-gate 	asyopen,
2900Sstevel@tonic-gate 	asyclose,
2910Sstevel@tonic-gate 	NULL,
2920Sstevel@tonic-gate 	&asy_info,
2930Sstevel@tonic-gate 	NULL
2940Sstevel@tonic-gate };
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate static struct qinit asy_wint = {
2970Sstevel@tonic-gate 	asywput,
2980Sstevel@tonic-gate 	NULL,
2990Sstevel@tonic-gate 	NULL,
3000Sstevel@tonic-gate 	NULL,
3010Sstevel@tonic-gate 	NULL,
3020Sstevel@tonic-gate 	&asy_info,
3030Sstevel@tonic-gate 	NULL
3040Sstevel@tonic-gate };
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate struct streamtab asy_str_info = {
3070Sstevel@tonic-gate 	&asy_rint,
3080Sstevel@tonic-gate 	&asy_wint,
3090Sstevel@tonic-gate 	NULL,
3100Sstevel@tonic-gate 	NULL
3110Sstevel@tonic-gate };
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate static int asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
3140Sstevel@tonic-gate 		void **result);
3150Sstevel@tonic-gate static int asyprobe(dev_info_t *);
3160Sstevel@tonic-gate static int asyattach(dev_info_t *, ddi_attach_cmd_t);
3170Sstevel@tonic-gate static int asydetach(dev_info_t *, ddi_detach_cmd_t);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate static 	struct cb_ops cb_asy_ops = {
3200Sstevel@tonic-gate 	nodev,			/* cb_open */
3210Sstevel@tonic-gate 	nodev,			/* cb_close */
3220Sstevel@tonic-gate 	nodev,			/* cb_strategy */
3230Sstevel@tonic-gate 	nodev,			/* cb_print */
3240Sstevel@tonic-gate 	nodev,			/* cb_dump */
3250Sstevel@tonic-gate 	nodev,			/* cb_read */
3260Sstevel@tonic-gate 	nodev,			/* cb_write */
3270Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
3280Sstevel@tonic-gate 	nodev,			/* cb_devmap */
3290Sstevel@tonic-gate 	nodev,			/* cb_mmap */
3300Sstevel@tonic-gate 	nodev,			/* cb_segmap */
3310Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
3320Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
3330Sstevel@tonic-gate 	&asy_str_info,		/* cb_stream */
3340Sstevel@tonic-gate 	D_MP			/* cb_flag */
3350Sstevel@tonic-gate };
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate struct dev_ops asy_ops = {
3380Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
3390Sstevel@tonic-gate 	0,			/* devo_refcnt */
3400Sstevel@tonic-gate 	asyinfo,		/* devo_getinfo */
3410Sstevel@tonic-gate 	nulldev,		/* devo_identify */
3420Sstevel@tonic-gate 	asyprobe,		/* devo_probe */
3430Sstevel@tonic-gate 	asyattach,		/* devo_attach */
3440Sstevel@tonic-gate 	asydetach,		/* devo_detach */
3450Sstevel@tonic-gate 	nodev,			/* devo_reset */
3460Sstevel@tonic-gate 	&cb_asy_ops,		/* devo_cb_ops */
3470Sstevel@tonic-gate };
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate static struct modldrv modldrv = {
3500Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a driver */
3510Sstevel@tonic-gate 	"ASY driver %I%",
3520Sstevel@tonic-gate 	&asy_ops,	/* driver ops */
3530Sstevel@tonic-gate };
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate static struct modlinkage modlinkage = {
3560Sstevel@tonic-gate 	MODREV_1,
3570Sstevel@tonic-gate 	(void *)&modldrv,
3580Sstevel@tonic-gate 	NULL
3590Sstevel@tonic-gate };
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate int
3620Sstevel@tonic-gate _init(void)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	int i;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	i = ddi_soft_state_init(&asy_soft_state, sizeof (struct asycom), 2);
3670Sstevel@tonic-gate 	if (i == 0) {
3680Sstevel@tonic-gate 		mutex_init(&asy_glob_lock, NULL, MUTEX_DRIVER, NULL);
3690Sstevel@tonic-gate 		if ((i = mod_install(&modlinkage)) != 0) {
3700Sstevel@tonic-gate 			mutex_destroy(&asy_glob_lock);
3710Sstevel@tonic-gate 			ddi_soft_state_fini(&asy_soft_state);
3720Sstevel@tonic-gate 		} else {
3730Sstevel@tonic-gate 			DEBUGCONT2(ASY_DEBUG_INIT, "%s, debug = %x\n",
3740Sstevel@tonic-gate 			    modldrv.drv_linkinfo, debug);
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 	return (i);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate int
3810Sstevel@tonic-gate _fini(void)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate 	int i;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	if ((i = mod_remove(&modlinkage)) == 0) {
3860Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_INIT, "%s unloading\n",
3870Sstevel@tonic-gate 		    modldrv.drv_linkinfo);
3880Sstevel@tonic-gate 		ASSERT(max_asy_instance == -1);
3890Sstevel@tonic-gate 		mutex_destroy(&asy_glob_lock);
3900Sstevel@tonic-gate 		if (asy_addedsoft)
3910Sstevel@tonic-gate 			ddi_remove_softintr(asy_softintr_id);
3920Sstevel@tonic-gate 		asy_addedsoft = 0;
3930Sstevel@tonic-gate 		/* free "motherboard-serial-ports" property if allocated */
3940Sstevel@tonic-gate 		if (com_ports != NULL && com_ports != (int *)standard_com_ports)
3950Sstevel@tonic-gate 		    ddi_prop_free(com_ports);
3960Sstevel@tonic-gate 		com_ports = NULL;
3970Sstevel@tonic-gate 		mutex_destroy(&asy_soft_lock);
3980Sstevel@tonic-gate 		ddi_soft_state_fini(&asy_soft_state);
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 	return (i);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate int
4040Sstevel@tonic-gate _info(struct modinfo *modinfop)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate static int
410*3359Smyers asy_get_bus_type(dev_info_t *devinfo)
411*3359Smyers {
412*3359Smyers 	char	parent_type[16];
413*3359Smyers 	int	parentlen;
414*3359Smyers 
415*3359Smyers 	parentlen = sizeof (parent_type);
416*3359Smyers 
417*3359Smyers 	if (ddi_prop_op(DDI_DEV_T_ANY, devinfo, PROP_LEN_AND_VAL_BUF, 0,
418*3359Smyers 	    "device_type", (caddr_t)parent_type, &parentlen)
419*3359Smyers 	    != DDI_PROP_SUCCESS && ddi_prop_op(DDI_DEV_T_ANY, devinfo,
420*3359Smyers 	    PROP_LEN_AND_VAL_BUF, 0, "bus-type", (caddr_t)parent_type,
421*3359Smyers 	    &parentlen) != DDI_PROP_SUCCESS) {
422*3359Smyers 			cmn_err(CE_WARN,
423*3359Smyers 			    "asy: can't figure out device type for"
424*3359Smyers 			    " parent \"%s\"",
425*3359Smyers 			    ddi_get_name(ddi_get_parent(devinfo)));
426*3359Smyers 			return (ASY_BUS_UNKNOWN);
427*3359Smyers 	}
428*3359Smyers 	if (strcmp(parent_type, "isa") == 0)
429*3359Smyers 		return (ASY_BUS_ISA);
430*3359Smyers 	else if (strcmp(parent_type, "pci") == 0)
431*3359Smyers 		return (ASY_BUS_PCI);
432*3359Smyers 	else
433*3359Smyers 		return (ASY_BUS_UNKNOWN);
434*3359Smyers }
435*3359Smyers 
436*3359Smyers static int
437*3359Smyers asy_get_io_regnum_pci(dev_info_t *devi, struct asycom *asy)
438*3359Smyers {
439*3359Smyers 	int reglen, nregs;
440*3359Smyers 	int regnum, i;
441*3359Smyers 	uint64_t size;
442*3359Smyers 	struct pci_phys_spec *reglist;
443*3359Smyers 
444*3359Smyers 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
445*3359Smyers 	    "reg", (caddr_t)&reglist, &reglen) != DDI_PROP_SUCCESS) {
446*3359Smyers 		cmn_err(CE_WARN, "asy_get_io_regnum_pci: reg property"
447*3359Smyers 		    " not found in devices property list");
448*3359Smyers 		return (-1);
449*3359Smyers 	}
450*3359Smyers 
451*3359Smyers 	/*
452*3359Smyers 	 * PCI devices are assumed to not have broken FIFOs;
453*3359Smyers 	 * Agere/Lucent Venus PCI modem chipsets are an example
454*3359Smyers 	 */
455*3359Smyers 	if (asy)
456*3359Smyers 		asy->asy_flags2 |= ASY2_NO_LOOPBACK;
457*3359Smyers 
458*3359Smyers 	regnum = -1;
459*3359Smyers 	nregs = reglen / sizeof (*reglist);
460*3359Smyers 	for (i = 0; i < nregs; i++) {
461*3359Smyers 		switch (reglist[i].pci_phys_hi & PCI_ADDR_MASK) {
462*3359Smyers 		case PCI_ADDR_IO:		/* I/O bus reg property */
463*3359Smyers 			if (regnum == -1) /* use only the first one */
464*3359Smyers 				regnum = i;
465*3359Smyers 			break;
466*3359Smyers 
467*3359Smyers 		default:
468*3359Smyers 			break;
469*3359Smyers 		}
470*3359Smyers 	}
471*3359Smyers 
472*3359Smyers 	/* check for valid count of registers */
473*3359Smyers 	if (regnum >= 0) {
474*3359Smyers 		size = ((uint64_t)reglist[regnum].pci_size_low) |
475*3359Smyers 		    ((uint64_t)reglist[regnum].pci_size_hi) << 32;
476*3359Smyers 		if (size < 8)
477*3359Smyers 			regnum = -1;
478*3359Smyers 	}
479*3359Smyers 	kmem_free(reglist, reglen);
480*3359Smyers 	return (regnum);
481*3359Smyers }
482*3359Smyers 
483*3359Smyers static int
484*3359Smyers asy_get_io_regnum_isa(dev_info_t *devi, struct asycom *asy)
485*3359Smyers {
486*3359Smyers 	int reglen, nregs;
487*3359Smyers 	int regnum, i;
488*3359Smyers 	struct {
489*3359Smyers 		uint_t bustype;
490*3359Smyers 		int base;
491*3359Smyers 		int size;
492*3359Smyers 	} *reglist;
493*3359Smyers 
494*3359Smyers 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
495*3359Smyers 	    "reg", (caddr_t)&reglist, &reglen) != DDI_PROP_SUCCESS) {
496*3359Smyers 		cmn_err(CE_WARN, "asy_get_io_regnum: reg property not found "
497*3359Smyers 			"in devices property list");
498*3359Smyers 		return (-1);
499*3359Smyers 	}
500*3359Smyers 
501*3359Smyers 	regnum = -1;
502*3359Smyers 	nregs = reglen / sizeof (*reglist);
503*3359Smyers 	for (i = 0; i < nregs; i++) {
504*3359Smyers 		switch (reglist[i].bustype) {
505*3359Smyers 		case 1:			/* I/O bus reg property */
506*3359Smyers 			if (regnum == -1) /* only use the first one */
507*3359Smyers 				regnum = i;
508*3359Smyers 			break;
509*3359Smyers 
510*3359Smyers 		case pnpMTS0219:	/* Multitech MT5634ZTX modem */
511*3359Smyers 			/* Venus chipset can't do loopback test */
512*3359Smyers 			if (asy)
513*3359Smyers 				asy->asy_flags2 |= ASY2_NO_LOOPBACK;
514*3359Smyers 			break;
515*3359Smyers 
516*3359Smyers 		default:
517*3359Smyers 			break;
518*3359Smyers 		}
519*3359Smyers 	}
520*3359Smyers 
521*3359Smyers 	/* check for valid count of registers */
522*3359Smyers 	if ((regnum < 0) || (reglist[regnum].size < 8))
523*3359Smyers 		regnum = -1;
524*3359Smyers 	kmem_free(reglist, reglen);
525*3359Smyers 	return (regnum);
526*3359Smyers }
527*3359Smyers 
528*3359Smyers static int
529*3359Smyers asy_get_io_regnum(dev_info_t *devinfo, struct asycom *asy)
530*3359Smyers {
531*3359Smyers 	switch (asy_get_bus_type(devinfo)) {
532*3359Smyers 	case ASY_BUS_ISA:
533*3359Smyers 		return (asy_get_io_regnum_isa(devinfo, asy));
534*3359Smyers 	case ASY_BUS_PCI:
535*3359Smyers 		return (asy_get_io_regnum_pci(devinfo, asy));
536*3359Smyers 	default:
537*3359Smyers 		return (-1);
538*3359Smyers 	}
539*3359Smyers }
540*3359Smyers 
541*3359Smyers static int
5420Sstevel@tonic-gate asydetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	int instance;
5450Sstevel@tonic-gate 	struct asycom *asy;
5460Sstevel@tonic-gate 	struct asyncline *async;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
5490Sstevel@tonic-gate 		return (DDI_FAILURE);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	instance = ddi_get_instance(devi);	/* find out which unit */
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, instance);
5540Sstevel@tonic-gate 	if (asy == NULL)
5550Sstevel@tonic-gate 		return (DDI_FAILURE);
5560Sstevel@tonic-gate 	async = asy->asy_priv;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	DEBUGNOTE2(ASY_DEBUG_INIT, "asy%d: %s shutdown.",
5590Sstevel@tonic-gate 	    instance, asy_hw_name(asy));
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	/* cancel DTR hold timeout */
5620Sstevel@tonic-gate 	if (async->async_dtrtid != 0) {
5630Sstevel@tonic-gate 		(void) untimeout(async->async_dtrtid);
5640Sstevel@tonic-gate 		async->async_dtrtid = 0;
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/* remove all minor device node(s) for this device */
5680Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	mutex_destroy(&asy->asy_excl);
5710Sstevel@tonic-gate 	mutex_destroy(&asy->asy_excl_hi);
5720Sstevel@tonic-gate 	cv_destroy(&async->async_flags_cv);
5730Sstevel@tonic-gate 	ddi_remove_intr(devi, 0, asy->asy_iblock);
5740Sstevel@tonic-gate 	ddi_regs_map_free(&asy->asy_iohandle);
5750Sstevel@tonic-gate 	asy_soft_state_free(asy);
5760Sstevel@tonic-gate 	DEBUGNOTE1(ASY_DEBUG_INIT, "asy%d: shutdown complete", instance);
5770Sstevel@tonic-gate 	return (DDI_SUCCESS);
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * asyprobe
5820Sstevel@tonic-gate  * We don't bother probing for the hardware, as since Solaris 2.6, device
5830Sstevel@tonic-gate  * nodes are only created for auto-detected hardware or nodes explicitly
5840Sstevel@tonic-gate  * created by the user, e.g. via the DCA. However, we should check the
5850Sstevel@tonic-gate  * device node is at least vaguely usable, i.e. we have a block of 8 i/o
5860Sstevel@tonic-gate  * ports. This prevents attempting to attach to bogus serial ports which
5870Sstevel@tonic-gate  * some BIOSs still partially report when they are disabled in the BIOS.
5880Sstevel@tonic-gate  */
5890Sstevel@tonic-gate static int
5900Sstevel@tonic-gate asyprobe(dev_info_t *devi)
5910Sstevel@tonic-gate {
592*3359Smyers 	return ((asy_get_io_regnum(devi, NULL) < 0) ?
593*3359Smyers 	    DDI_PROBE_FAILURE : DDI_PROBE_DONTCARE);
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate static int
5970Sstevel@tonic-gate asyattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate 	int instance;
6000Sstevel@tonic-gate 	int mcr;
6010Sstevel@tonic-gate 	int ret;
6020Sstevel@tonic-gate 	int regnum = 0;
6030Sstevel@tonic-gate 	int i;
6040Sstevel@tonic-gate 	struct asycom *asy;
605*3359Smyers 	char name[ASY_MINOR_LEN];
6060Sstevel@tonic-gate 	int status;
6070Sstevel@tonic-gate 	static ddi_device_acc_attr_t ioattr = {
6080Sstevel@tonic-gate 		DDI_DEVICE_ATTR_V0,
6090Sstevel@tonic-gate 		DDI_NEVERSWAP_ACC,
6100Sstevel@tonic-gate 		DDI_STRICTORDER_ACC,
6110Sstevel@tonic-gate 	};
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
6140Sstevel@tonic-gate 		return (DDI_FAILURE);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	instance = ddi_get_instance(devi);	/* find out which unit */
6170Sstevel@tonic-gate 	ret = ddi_soft_state_zalloc(asy_soft_state, instance);
6180Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
6190Sstevel@tonic-gate 		return (DDI_FAILURE);
6200Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, instance);
6210Sstevel@tonic-gate 	ASSERT(asy != NULL);	/* can't fail - we only just allocated it */
6220Sstevel@tonic-gate 	asy->asy_unit = instance;
6230Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
6240Sstevel@tonic-gate 	if (instance > max_asy_instance)
6250Sstevel@tonic-gate 		max_asy_instance = instance;
6260Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
6270Sstevel@tonic-gate 
628*3359Smyers 	regnum = asy_get_io_regnum(devi, asy);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	if (regnum < 0 ||
6310Sstevel@tonic-gate 	    ddi_regs_map_setup(devi, regnum, (caddr_t *)&asy->asy_ioaddr,
6320Sstevel@tonic-gate 	    (offset_t)0, (offset_t)0, &ioattr, &asy->asy_iohandle)
6330Sstevel@tonic-gate 	    != DDI_SUCCESS) {
6340Sstevel@tonic-gate 		cmn_err(CE_WARN, "asy%d: could not map UART registers @ %p",
6350Sstevel@tonic-gate 		    instance, (void *)asy->asy_ioaddr);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 		asy_soft_state_free(asy);
6380Sstevel@tonic-gate 		return (DDI_FAILURE);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_INIT, "asy%dattach: UART @ %p\n",
6420Sstevel@tonic-gate 	    instance, (void *)asy->asy_ioaddr);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
6450Sstevel@tonic-gate 	if (com_ports == NULL) {	/* need to initialize com_ports */
6460Sstevel@tonic-gate 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, devi, 0,
6470Sstevel@tonic-gate 		    "motherboard-serial-ports", &com_ports, &num_com_ports) !=
6480Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
6490Sstevel@tonic-gate 			/* Use our built-in COM[1234] values */
6500Sstevel@tonic-gate 			com_ports = (int *)standard_com_ports;
6510Sstevel@tonic-gate 			num_com_ports = sizeof (standard_com_ports) /
6520Sstevel@tonic-gate 			    sizeof (standard_com_ports[0]);
6530Sstevel@tonic-gate 		}
6540Sstevel@tonic-gate 		if (num_com_ports > 10) {
6550Sstevel@tonic-gate 			/* We run out of single digits for device properties */
6560Sstevel@tonic-gate 			num_com_ports = 10;
6570Sstevel@tonic-gate 			cmn_err(CE_WARN,
6580Sstevel@tonic-gate 			    "More than %d motherboard-serial-ports",
6590Sstevel@tonic-gate 			    num_com_ports);
6600Sstevel@tonic-gate 		}
6610Sstevel@tonic-gate 	}
6620Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	/*
6650Sstevel@tonic-gate 	 * Lookup the i/o address to see if this is a standard COM port
6660Sstevel@tonic-gate 	 * in which case we assign it the correct tty[a-d] to match the
6670Sstevel@tonic-gate 	 * COM port number, or some other i/o address in which case it
6680Sstevel@tonic-gate 	 * will be assigned /dev/term/[0123...] in some rather arbitrary
6690Sstevel@tonic-gate 	 * fashion.
6700Sstevel@tonic-gate 	 */
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	for (i = 0; i < num_com_ports; i++) {
6730Sstevel@tonic-gate 		if (asy->asy_ioaddr == (uint8_t *)(uintptr_t)com_ports[i]) {
6740Sstevel@tonic-gate 			asy->asy_com_port = i + 1;
6750Sstevel@tonic-gate 			break;
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	/*
6800Sstevel@tonic-gate 	 * It appears that there was async hardware that on reset
6810Sstevel@tonic-gate 	 * did not clear ICR.  Hence when we get to
6820Sstevel@tonic-gate 	 * ddi_get_iblock_cookie below, this hardware would cause
6830Sstevel@tonic-gate 	 * the system to hang if there was input available.
6840Sstevel@tonic-gate 	 */
6850Sstevel@tonic-gate 
6861106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0x00);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	/* establish default usage */
6890Sstevel@tonic-gate 	asy->asy_mcr |= RTS|DTR;		/* do use RTS/DTR after open */
6900Sstevel@tonic-gate 	asy->asy_lcr = STOP1|BITS8;		/* default to 1 stop 8 bits */
6910Sstevel@tonic-gate 	asy->asy_bidx = B9600;			/* default to 9600  */
6920Sstevel@tonic-gate #ifdef DEBUG
6930Sstevel@tonic-gate 	asy->asy_msint_cnt = 0;			/* # of times in async_msint */
6940Sstevel@tonic-gate #endif
6950Sstevel@tonic-gate 	mcr = 0;				/* don't enable until open */
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	if (asy->asy_com_port != 0) {
6980Sstevel@tonic-gate 		/*
6990Sstevel@tonic-gate 		 * For motherboard ports, emulate tty eeprom properties.
7000Sstevel@tonic-gate 		 * Actually, we can't tell if a port is motherboard or not,
7010Sstevel@tonic-gate 		 * so for "motherboard ports", read standard DOS COM ports.
7020Sstevel@tonic-gate 		 */
7030Sstevel@tonic-gate 		switch (asy_getproperty(devi, asy, "ignore-cd")) {
7040Sstevel@tonic-gate 		case 0:				/* *-ignore-cd=False */
7050Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
7060Sstevel@tonic-gate 			    "asy%dattach: clear ASY_IGNORE_CD\n", instance);
7070Sstevel@tonic-gate 			asy->asy_flags &= ~ASY_IGNORE_CD; /* wait for cd */
7080Sstevel@tonic-gate 			break;
7090Sstevel@tonic-gate 		case 1:				/* *-ignore-cd=True */
7100Sstevel@tonic-gate 			/*FALLTHRU*/
7110Sstevel@tonic-gate 		default:			/* *-ignore-cd not defined */
7120Sstevel@tonic-gate 			/*
7130Sstevel@tonic-gate 			 * We set rather silly defaults of soft carrier on
7140Sstevel@tonic-gate 			 * and DTR/RTS raised here because it might be that
7150Sstevel@tonic-gate 			 * one of the motherboard ports is the system console.
7160Sstevel@tonic-gate 			 */
7170Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
7180Sstevel@tonic-gate 			    "asy%dattach: set ASY_IGNORE_CD, set RTS & DTR\n",
7190Sstevel@tonic-gate 			    instance);
7200Sstevel@tonic-gate 			mcr = asy->asy_mcr;		/* rts/dtr on */
7210Sstevel@tonic-gate 			asy->asy_flags |= ASY_IGNORE_CD;	/* ignore cd */
7220Sstevel@tonic-gate 			break;
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 		/* Property for not raising DTR/RTS */
7260Sstevel@tonic-gate 		switch (asy_getproperty(devi, asy, "rts-dtr-off")) {
7270Sstevel@tonic-gate 		case 0:				/* *-rts-dtr-off=False */
7280Sstevel@tonic-gate 			asy->asy_flags |= ASY_RTS_DTR_OFF;	/* OFF */
7290Sstevel@tonic-gate 			mcr = asy->asy_mcr;		/* rts/dtr on */
7300Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM, "asy%dattach: "
7310Sstevel@tonic-gate 			    "ASY_RTS_DTR_OFF set and DTR & RTS set\n",
7320Sstevel@tonic-gate 			    instance);
7330Sstevel@tonic-gate 			break;
7340Sstevel@tonic-gate 		case 1:				/* *-rts-dtr-off=True */
7350Sstevel@tonic-gate 			/*FALLTHRU*/
7360Sstevel@tonic-gate 		default:			/* *-rts-dtr-off undefined */
7370Sstevel@tonic-gate 			break;
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		/* Parse property for tty modes */
7410Sstevel@tonic-gate 		asy_parse_mode(devi, asy);
7420Sstevel@tonic-gate 	} else {
7430Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_MODEM,
7440Sstevel@tonic-gate 		    "asy%dattach: clear ASY_IGNORE_CD, clear RTS & DTR\n",
7450Sstevel@tonic-gate 		    instance);
7460Sstevel@tonic-gate 		asy->asy_flags &= ~ASY_IGNORE_CD;	/* wait for cd */
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	/*
7500Sstevel@tonic-gate 	 * Initialize the port with default settings.
7510Sstevel@tonic-gate 	 */
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	asy->asy_fifo_buf = 1;
7540Sstevel@tonic-gate 	asy->asy_use_fifo = FIFO_OFF;
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	/*
7570Sstevel@tonic-gate 	 * Get icookie for mutexes initialization
7580Sstevel@tonic-gate 	 */
7590Sstevel@tonic-gate 	if ((ddi_get_iblock_cookie(devi, 0, &asy->asy_iblock) !=
7600Sstevel@tonic-gate 	    DDI_SUCCESS) ||
7610Sstevel@tonic-gate 	    (ddi_get_soft_iblock_cookie(devi, DDI_SOFTINT_MED,
7620Sstevel@tonic-gate 	    &asy_soft_iblock) != DDI_SUCCESS)) {
7630Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
7640Sstevel@tonic-gate 		cmn_err(CE_CONT,
7650Sstevel@tonic-gate 		    "asy%d: could not hook interrupt for UART @ %p\n",
7660Sstevel@tonic-gate 		    instance, (void *)asy->asy_ioaddr);
7670Sstevel@tonic-gate 		asy_soft_state_free(asy);
7680Sstevel@tonic-gate 		return (DDI_FAILURE);
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	/*
7720Sstevel@tonic-gate 	 * Initialize mutexes before accessing the hardware
7730Sstevel@tonic-gate 	 */
7740Sstevel@tonic-gate 	mutex_init(&asy->asy_excl, NULL, MUTEX_DRIVER, asy_soft_iblock);
7750Sstevel@tonic-gate 	mutex_init(&asy->asy_excl_hi, NULL, MUTEX_DRIVER,
7760Sstevel@tonic-gate 		(void *)asy->asy_iblock);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
7790Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if (asy_identify_chip(devi, asy) != DDI_SUCCESS) {
7820Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
7830Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
7840Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
7850Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
7860Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
7870Sstevel@tonic-gate 		cmn_err(CE_CONT, "Cannot identify UART chip at %p\n",
7880Sstevel@tonic-gate 		    (void *)asy->asy_ioaddr);
7890Sstevel@tonic-gate 		asy_soft_state_free(asy);
7900Sstevel@tonic-gate 		return (DDI_FAILURE);
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	/* disable all interrupts */
7941106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0);
7950Sstevel@tonic-gate 	/* select baud rate generator */
7961106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, DLAB);
7970Sstevel@tonic-gate 	/* Set the baud rate to 9600 */
7981106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + (DAT+DLL),
7990Sstevel@tonic-gate 		asyspdtab[asy->asy_bidx] & 0xff);
8001106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + (DAT+DLH),
8010Sstevel@tonic-gate 		(asyspdtab[asy->asy_bidx] >> 8) & 0xff);
8021106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
8030Sstevel@tonic-gate 		asy->asy_lcr);
8041106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR, mcr);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
8070Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/*
8100Sstevel@tonic-gate 	 * Set up the other components of the asycom structure for this port.
8110Sstevel@tonic-gate 	 */
8120Sstevel@tonic-gate 	asy->asy_dip = devi;
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
8150Sstevel@tonic-gate 	if (asy_addedsoft == 0) { /* install the soft interrupt handler */
8160Sstevel@tonic-gate 		if (ddi_add_softintr(devi, DDI_SOFTINT_MED,
8170Sstevel@tonic-gate 		    &asy_softintr_id, NULL, 0, asysoftintr,
8180Sstevel@tonic-gate 		    (caddr_t)0) != DDI_SUCCESS) {
8190Sstevel@tonic-gate 			mutex_destroy(&asy->asy_excl);
8200Sstevel@tonic-gate 			mutex_destroy(&asy->asy_excl_hi);
8210Sstevel@tonic-gate 			ddi_regs_map_free(&asy->asy_iohandle);
8220Sstevel@tonic-gate 			mutex_exit(&asy_glob_lock);
8230Sstevel@tonic-gate 			cmn_err(CE_CONT,
8240Sstevel@tonic-gate 				"Can not set soft interrupt for ASY driver\n");
8250Sstevel@tonic-gate 			asy_soft_state_free(asy);
8260Sstevel@tonic-gate 			return (DDI_FAILURE);
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 		mutex_init(&asy_soft_lock, NULL, MUTEX_DRIVER,
8290Sstevel@tonic-gate 			(void *)asy->asy_iblock);
8300Sstevel@tonic-gate 		asy_addedsoft++;
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
8350Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	/*
8380Sstevel@tonic-gate 	 * Install interrupt handler for this device.
8390Sstevel@tonic-gate 	 */
8400Sstevel@tonic-gate 	if (ddi_add_intr(devi, 0, NULL, 0, asyintr,
8410Sstevel@tonic-gate 	    (caddr_t)asy) != DDI_SUCCESS) {
8420Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
8430Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
8440Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
8450Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
8460Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
8470Sstevel@tonic-gate 		cmn_err(CE_CONT,
8480Sstevel@tonic-gate 			"Can not set device interrupt for ASY driver\n");
8490Sstevel@tonic-gate 		asy_soft_state_free(asy);
8500Sstevel@tonic-gate 		return (DDI_FAILURE);
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
8540Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	asyinit(asy);	/* initialize the asyncline structure */
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	/* create minor device nodes for this device */
8590Sstevel@tonic-gate 	if (asy->asy_com_port != 0) {
8600Sstevel@tonic-gate 		/*
8610Sstevel@tonic-gate 		 * For DOS COM ports, add letter suffix so
8620Sstevel@tonic-gate 		 * devfsadm can create correct link names.
8630Sstevel@tonic-gate 		 */
8640Sstevel@tonic-gate 		name[0] = asy->asy_com_port + 'a' - 1;
8650Sstevel@tonic-gate 		name[1] = '\0';
8660Sstevel@tonic-gate 	} else {
8670Sstevel@tonic-gate 		/*
868*3359Smyers 		 * asy port which isn't a standard DOS COM
869*3359Smyers 		 * port gets a numeric name based on instance
8700Sstevel@tonic-gate 		 */
871*3359Smyers 		(void) snprintf(name, ASY_MINOR_LEN, "%d", instance);
8720Sstevel@tonic-gate 	}
8730Sstevel@tonic-gate 	status = ddi_create_minor_node(devi, name, S_IFCHR, instance,
8740Sstevel@tonic-gate 	    asy->asy_com_port != 0 ? DDI_NT_SERIAL_MB : DDI_NT_SERIAL, NULL);
8750Sstevel@tonic-gate 	if (status == DDI_SUCCESS) {
8760Sstevel@tonic-gate 		(void) strcat(name, ",cu");
8770Sstevel@tonic-gate 		status = ddi_create_minor_node(devi, name, S_IFCHR,
8780Sstevel@tonic-gate 		    OUTLINE | instance,
8790Sstevel@tonic-gate 		    asy->asy_com_port != 0 ? DDI_NT_SERIAL_MB_DO :
8800Sstevel@tonic-gate 		    DDI_NT_SERIAL_DO, NULL);
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
8840Sstevel@tonic-gate 		struct asyncline *async = asy->asy_priv;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
8870Sstevel@tonic-gate 		ddi_remove_intr(devi, 0, asy->asy_iblock);
8880Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
8890Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
8900Sstevel@tonic-gate 		cv_destroy(&async->async_flags_cv);
8910Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
8920Sstevel@tonic-gate 		asy_soft_state_free(asy);
8930Sstevel@tonic-gate 		return (DDI_FAILURE);
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	/*
8970Sstevel@tonic-gate 	 * Fill in the polled I/O structure.
8980Sstevel@tonic-gate 	 */
8990Sstevel@tonic-gate 	asy->polledio.cons_polledio_version = CONSPOLLEDIO_V0;
9001762Slt200341 	asy->polledio.cons_polledio_argument = (cons_polledio_arg_t)asy;
9010Sstevel@tonic-gate 	asy->polledio.cons_polledio_putchar = asyputchar;
9020Sstevel@tonic-gate 	asy->polledio.cons_polledio_getchar = asygetchar;
9030Sstevel@tonic-gate 	asy->polledio.cons_polledio_ischar = asyischar;
9040Sstevel@tonic-gate 	asy->polledio.cons_polledio_enter = NULL;
9050Sstevel@tonic-gate 	asy->polledio.cons_polledio_exit = NULL;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	ddi_report_dev(devi);
9080Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_INIT, "asy%dattach: done\n", instance);
9090Sstevel@tonic-gate 	return (DDI_SUCCESS);
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate /*ARGSUSED*/
9130Sstevel@tonic-gate static int
9140Sstevel@tonic-gate asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
9150Sstevel@tonic-gate 	void **result)
9160Sstevel@tonic-gate {
9170Sstevel@tonic-gate 	dev_t dev = (dev_t)arg;
9180Sstevel@tonic-gate 	int instance, error;
9190Sstevel@tonic-gate 	struct asycom *asy;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	instance = UNIT(dev);
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	switch (infocmd) {
9240Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
925386Scth 		asy = ddi_get_soft_state(asy_soft_state, instance);
926386Scth 		if ((asy == NULL) || (asy->asy_dip == NULL))
9270Sstevel@tonic-gate 			error = DDI_FAILURE;
9280Sstevel@tonic-gate 		else {
9290Sstevel@tonic-gate 			*result = (void *) asy->asy_dip;
9300Sstevel@tonic-gate 			error = DDI_SUCCESS;
9310Sstevel@tonic-gate 		}
9320Sstevel@tonic-gate 		break;
9330Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
9340Sstevel@tonic-gate 		*result = (void *)(intptr_t)instance;
9350Sstevel@tonic-gate 		error = DDI_SUCCESS;
9360Sstevel@tonic-gate 		break;
9370Sstevel@tonic-gate 	default:
9380Sstevel@tonic-gate 		error = DDI_FAILURE;
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 	return (error);
9410Sstevel@tonic-gate }
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate /* asy_getproperty -- walk through all name variants until we find a match */
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate static int
9460Sstevel@tonic-gate asy_getproperty(dev_info_t *devi, struct asycom *asy, const char *property)
9470Sstevel@tonic-gate {
9480Sstevel@tonic-gate 	int len;
9490Sstevel@tonic-gate 	int ret;
9500Sstevel@tonic-gate 	char letter = asy->asy_com_port + 'a' - 1;	/* for ttya */
9510Sstevel@tonic-gate 	char number = asy->asy_com_port + '0';		/* for COM1 */
9520Sstevel@tonic-gate 	char val[40];
9530Sstevel@tonic-gate 	char name[40];
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	/* Property for ignoring DCD */
9560Sstevel@tonic-gate 	(void) sprintf(name, "tty%c-%s", letter, property);
9570Sstevel@tonic-gate 	len = sizeof (val);
9580Sstevel@tonic-gate 	ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
9590Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
9600Sstevel@tonic-gate 		(void) sprintf(name, "com%c-%s", number, property);
9610Sstevel@tonic-gate 		len = sizeof (val);
9620Sstevel@tonic-gate 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val,
9630Sstevel@tonic-gate 				&len);
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
9660Sstevel@tonic-gate 		(void) sprintf(name, "tty0%c-%s", number, property);
9670Sstevel@tonic-gate 		len = sizeof (val);
9680Sstevel@tonic-gate 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val,
9690Sstevel@tonic-gate 				&len);
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
9720Sstevel@tonic-gate 		(void) sprintf(name, "port-%c-%s", letter, property);
9730Sstevel@tonic-gate 		len = sizeof (val);
9740Sstevel@tonic-gate 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val,
9750Sstevel@tonic-gate 				&len);
9760Sstevel@tonic-gate 	}
9770Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS)
9780Sstevel@tonic-gate 		return (-1);		/* property non-existant */
9790Sstevel@tonic-gate 	if (val[0] == 'f' || val[0] == 'F' || val[0] == '0')
9800Sstevel@tonic-gate 		return (0);		/* property false/0 */
9810Sstevel@tonic-gate 	return (1);			/* property true/!0 */
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate /* asy_soft_state_free - local wrapper for ddi_soft_state_free(9F) */
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate static void
9870Sstevel@tonic-gate asy_soft_state_free(struct asycom *asy)
9880Sstevel@tonic-gate {
9890Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
9900Sstevel@tonic-gate 	/* If we were the max_asy_instance, work out new value */
9910Sstevel@tonic-gate 	if (asy->asy_unit == max_asy_instance) {
9920Sstevel@tonic-gate 		while (--max_asy_instance >= 0) {
9930Sstevel@tonic-gate 			if (ddi_get_soft_state(asy_soft_state,
9940Sstevel@tonic-gate 			    max_asy_instance) != NULL)
9950Sstevel@tonic-gate 				break;
9960Sstevel@tonic-gate 		}
9970Sstevel@tonic-gate 	}
9980Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	if (asy->asy_priv != NULL) {
10010Sstevel@tonic-gate 		kmem_free(asy->asy_priv, sizeof (struct asyncline));
10020Sstevel@tonic-gate 		asy->asy_priv = NULL;
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate 	ddi_soft_state_free(asy_soft_state, asy->asy_unit);
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate static char *
10080Sstevel@tonic-gate asy_hw_name(struct asycom *asy)
10090Sstevel@tonic-gate {
10100Sstevel@tonic-gate 	switch (asy->asy_hwtype) {
10110Sstevel@tonic-gate 	case ASY8250A:
10120Sstevel@tonic-gate 		return ("8250A/16450");
10130Sstevel@tonic-gate 	case ASY16550:
10140Sstevel@tonic-gate 		return ("16550");
10150Sstevel@tonic-gate 	case ASY16550A:
10160Sstevel@tonic-gate 		return ("16550A");
10170Sstevel@tonic-gate 	case ASY16650:
10180Sstevel@tonic-gate 		return ("16650");
10190Sstevel@tonic-gate 	case ASY16750:
10200Sstevel@tonic-gate 		return ("16750");
10210Sstevel@tonic-gate 	default:
10220Sstevel@tonic-gate 		DEBUGNOTE2(ASY_DEBUG_INIT,
10230Sstevel@tonic-gate 		    "asy%d: asy_hw_name: unknown asy_hwtype: %d",
10240Sstevel@tonic-gate 		    asy->asy_unit, asy->asy_hwtype);
10250Sstevel@tonic-gate 		return ("?");
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate }
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate static int
10300Sstevel@tonic-gate asy_identify_chip(dev_info_t *devi, struct asycom *asy)
10310Sstevel@tonic-gate {
10320Sstevel@tonic-gate 	int ret;
10330Sstevel@tonic-gate 	int mcr;
10340Sstevel@tonic-gate 	dev_t dev;
10350Sstevel@tonic-gate 	uint_t hwtype;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	if (asy_scr_test) {
10380Sstevel@tonic-gate 		/* Check scratch register works. */
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 		/* write to scratch register */
10411106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + SCR, SCRTEST);
10420Sstevel@tonic-gate 		/* make sure that pattern doesn't just linger on the bus */
10431106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + FIFOR, 0x00);
10440Sstevel@tonic-gate 		/* read data back from scratch register */
10451106Smrj 		ret = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + SCR);
10460Sstevel@tonic-gate 		if (ret != SCRTEST) {
10470Sstevel@tonic-gate 			/*
10480Sstevel@tonic-gate 			 * Scratch register not working.
10490Sstevel@tonic-gate 			 * Probably not an async chip.
10500Sstevel@tonic-gate 			 * 8250 and 8250B don't have scratch registers,
10510Sstevel@tonic-gate 			 * but only worked in ancient PC XT's anyway.
10520Sstevel@tonic-gate 			 */
10530Sstevel@tonic-gate 			cmn_err(CE_CONT, "asy%d: UART @ %p "
10540Sstevel@tonic-gate 			    "scratch register: expected 0x5a, got 0x%02x\n",
10550Sstevel@tonic-gate 			    asy->asy_unit, (void *)asy->asy_ioaddr, ret);
10560Sstevel@tonic-gate 			return (DDI_FAILURE);
10570Sstevel@tonic-gate 		}
10580Sstevel@tonic-gate 	}
10590Sstevel@tonic-gate 	/*
10600Sstevel@tonic-gate 	 * Use 16550 fifo reset sequence specified in NS application
10610Sstevel@tonic-gate 	 * note. Disable fifos until chip is initialized.
10620Sstevel@tonic-gate 	 */
10631106Smrj 	ddi_put8(asy->asy_iohandle,
10640Sstevel@tonic-gate 	    asy->asy_ioaddr + FIFOR, 0x00);	/* clear */
10651106Smrj 	ddi_put8(asy->asy_iohandle,
10660Sstevel@tonic-gate 	    asy->asy_ioaddr + FIFOR, FIFO_ON);	/* enable */
10671106Smrj 	ddi_put8(asy->asy_iohandle,
10680Sstevel@tonic-gate 	    asy->asy_ioaddr + FIFOR, FIFO_ON | FIFORXFLSH);
10690Sstevel@tonic-gate 						/* reset */
10700Sstevel@tonic-gate 	if (asymaxchip >= ASY16650 && asy_scr_test) {
10710Sstevel@tonic-gate 		/*
10720Sstevel@tonic-gate 		 * Reset 16650 enhanced regs also, in case we have one of these
10730Sstevel@tonic-gate 		 */
10741106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
10750Sstevel@tonic-gate 		    EFRACCESS);
10761106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + EFR,
10770Sstevel@tonic-gate 		    0);
10781106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
10790Sstevel@tonic-gate 		    STOP1|BITS8);
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	/*
10830Sstevel@tonic-gate 	 * See what sort of FIFO we have.
10840Sstevel@tonic-gate 	 * Try enabling it and see what chip makes of this.
10850Sstevel@tonic-gate 	 */
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	asy->asy_fifor = 0;
10880Sstevel@tonic-gate 	asy->asy_hwtype = asymaxchip; /* just for asy_reset_fifo() */
10890Sstevel@tonic-gate 	if (asymaxchip >= ASY16550A)
10900Sstevel@tonic-gate 		asy->asy_fifor |=
10910Sstevel@tonic-gate 		    FIFO_ON | FIFODMA | (asy_trig_level & 0xff);
10920Sstevel@tonic-gate 	if (asymaxchip >= ASY16650)
10930Sstevel@tonic-gate 		asy->asy_fifor |= FIFOEXTRA1 | FIFOEXTRA2;
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	asy_reset_fifo(asy, FIFOTXFLSH | FIFORXFLSH);
10960Sstevel@tonic-gate 
10971106Smrj 	mcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
10981106Smrj 	ret = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + ISR);
10990Sstevel@tonic-gate 	DEBUGCONT4(ASY_DEBUG_CHIP,
11000Sstevel@tonic-gate 	    "asy%d: probe fifo FIFOR=0x%02x ISR=0x%02x MCR=0x%02x\n",
11010Sstevel@tonic-gate 	    asy->asy_unit, asy->asy_fifor | FIFOTXFLSH | FIFORXFLSH,
11020Sstevel@tonic-gate 	    ret, mcr);
11030Sstevel@tonic-gate 	switch (ret & 0xf0) {
11040Sstevel@tonic-gate 	case 0x40:
11050Sstevel@tonic-gate 		hwtype = ASY16550; /* 16550 with broken FIFO */
11060Sstevel@tonic-gate 		asy->asy_fifor = 0;
11070Sstevel@tonic-gate 		break;
11080Sstevel@tonic-gate 	case 0xc0:
11090Sstevel@tonic-gate 		hwtype = ASY16550A;
11100Sstevel@tonic-gate 		asy->asy_fifo_buf = 16;
11110Sstevel@tonic-gate 		asy->asy_use_fifo = FIFO_ON;
11120Sstevel@tonic-gate 		asy->asy_fifor &= ~(FIFOEXTRA1 | FIFOEXTRA2);
11130Sstevel@tonic-gate 		break;
11140Sstevel@tonic-gate 	case 0xe0:
11150Sstevel@tonic-gate 		hwtype = ASY16650;
11160Sstevel@tonic-gate 		asy->asy_fifo_buf = 32;
11170Sstevel@tonic-gate 		asy->asy_use_fifo = FIFO_ON;
11180Sstevel@tonic-gate 		asy->asy_fifor &= ~(FIFOEXTRA1);
11190Sstevel@tonic-gate 		break;
11200Sstevel@tonic-gate 	case 0xf0:
11210Sstevel@tonic-gate 		/*
11220Sstevel@tonic-gate 		 * Note we get 0xff if chip didn't return us anything,
11230Sstevel@tonic-gate 		 * e.g. if there's no chip there.
11240Sstevel@tonic-gate 		 */
11250Sstevel@tonic-gate 		if (ret == 0xff) {
11260Sstevel@tonic-gate 			cmn_err(CE_CONT, "asy%d: UART @ %p "
11270Sstevel@tonic-gate 			    "interrupt register: got 0xff\n",
11280Sstevel@tonic-gate 			    asy->asy_unit, (void *)asy->asy_ioaddr);
11290Sstevel@tonic-gate 			return (DDI_FAILURE);
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 		/*FALLTHRU*/
11320Sstevel@tonic-gate 	case 0xd0:
11330Sstevel@tonic-gate 		hwtype = ASY16750;
11340Sstevel@tonic-gate 		asy->asy_fifo_buf = 64;
11350Sstevel@tonic-gate 		asy->asy_use_fifo = FIFO_ON;
11360Sstevel@tonic-gate 		break;
11370Sstevel@tonic-gate 	default:
11380Sstevel@tonic-gate 		hwtype = ASY8250A; /* No FIFO */
11390Sstevel@tonic-gate 		asy->asy_fifor = 0;
11400Sstevel@tonic-gate 	}
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	if (hwtype > asymaxchip) {
11430Sstevel@tonic-gate 		cmn_err(CE_CONT, "asy%d: UART @ %p "
11440Sstevel@tonic-gate 		    "unexpected probe result: "
11450Sstevel@tonic-gate 		    "FIFOR=0x%02x ISR=0x%02x MCR=0x%02x\n",
11460Sstevel@tonic-gate 		    asy->asy_unit, (void *)asy->asy_ioaddr,
11470Sstevel@tonic-gate 		    asy->asy_fifor | FIFOTXFLSH | FIFORXFLSH, ret, mcr);
11480Sstevel@tonic-gate 		return (DDI_FAILURE);
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	/*
11520Sstevel@tonic-gate 	 * Now reset the FIFO operation appropriate for the chip type.
11530Sstevel@tonic-gate 	 * Note we must call asy_reset_fifo() before any possible
11540Sstevel@tonic-gate 	 * downgrade of the asy->asy_hwtype, or it may not disable
11550Sstevel@tonic-gate 	 * the more advanced features we specifically want downgraded.
11560Sstevel@tonic-gate 	 */
11570Sstevel@tonic-gate 	asy_reset_fifo(asy, 0);
11580Sstevel@tonic-gate 	asy->asy_hwtype = hwtype;
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	/*
11610Sstevel@tonic-gate 	 * Check for Exar/Startech ST16C650, which will still look like a
11620Sstevel@tonic-gate 	 * 16550A until we enable its enhanced mode.
11630Sstevel@tonic-gate 	 */
11640Sstevel@tonic-gate 	if (asy->asy_hwtype == ASY16550A && asymaxchip >= ASY16650 &&
11650Sstevel@tonic-gate 	    asy_scr_test) {
11660Sstevel@tonic-gate 		/* Enable enhanced mode register access */
11671106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
11680Sstevel@tonic-gate 		    EFRACCESS);
11690Sstevel@tonic-gate 		/* zero scratch register (not scratch register if enhanced) */
11701106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + SCR, 0);
11710Sstevel@tonic-gate 		/* Disable enhanced mode register access */
11721106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
11730Sstevel@tonic-gate 		    STOP1|BITS8);
11740Sstevel@tonic-gate 		/* read back scratch register */
11751106Smrj 		ret = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + SCR);
11760Sstevel@tonic-gate 		if (ret == SCRTEST) {
11770Sstevel@tonic-gate 			/* looks like we have an ST16650 -- enable it */
11781106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
11790Sstevel@tonic-gate 			    EFRACCESS);
11801106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + EFR,
11810Sstevel@tonic-gate 			    ENHENABLE);
11821106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
11830Sstevel@tonic-gate 			    STOP1|BITS8);
11840Sstevel@tonic-gate 			asy->asy_hwtype = ASY16650;
11850Sstevel@tonic-gate 			asy->asy_fifo_buf = 32;
11860Sstevel@tonic-gate 			asy->asy_fifor |= 0x10; /* 24 byte txfifo trigger */
11870Sstevel@tonic-gate 			asy_reset_fifo(asy, 0);
11880Sstevel@tonic-gate 		}
11890Sstevel@tonic-gate 	}
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	/*
11920Sstevel@tonic-gate 	 * If we think we might have a FIFO larger than 16 characters,
11930Sstevel@tonic-gate 	 * measure FIFO size and check it against expected.
11940Sstevel@tonic-gate 	 */
11950Sstevel@tonic-gate 	if (asy_fifo_test > 0 &&
11960Sstevel@tonic-gate 	    !(asy->asy_flags2 & ASY2_NO_LOOPBACK) &&
11970Sstevel@tonic-gate 	    (asy->asy_fifo_buf > 16 ||
11980Sstevel@tonic-gate 	    (asy_fifo_test > 1 && asy->asy_use_fifo == FIFO_ON) ||
11990Sstevel@tonic-gate 	    ASY_DEBUG(ASY_DEBUG_CHIP))) {
12000Sstevel@tonic-gate 		int i;
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 		/* Set baud rate to 57600 (fairly arbitrary choice) */
12031106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
12040Sstevel@tonic-gate 		    DLAB);
12051106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
12060Sstevel@tonic-gate 		    asyspdtab[B57600] & 0xff);
12071106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR,
12080Sstevel@tonic-gate 		    (asyspdtab[B57600] >> 8) & 0xff);
12090Sstevel@tonic-gate 		/* Set 8 bits, 1 stop bit */
12101106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
12110Sstevel@tonic-gate 		    STOP1|BITS8);
12120Sstevel@tonic-gate 		/* Set loopback mode */
12131106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
12140Sstevel@tonic-gate 		    DTR | RTS | ASY_LOOP | OUT1 | OUT2);
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 		/* Overfill fifo */
12170Sstevel@tonic-gate 		for (i = 0; i < asy->asy_fifo_buf * 2; i++) {
12181106Smrj 			ddi_put8(asy->asy_iohandle,
12190Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT, i);
12200Sstevel@tonic-gate 		}
12210Sstevel@tonic-gate 		/*
12220Sstevel@tonic-gate 		 * Now there's an interesting question here about which
12230Sstevel@tonic-gate 		 * FIFO we're testing the size of, RX or TX. We just
12240Sstevel@tonic-gate 		 * filled the TX FIFO much faster than it can empty,
12250Sstevel@tonic-gate 		 * although it is possible one or two characters may
12260Sstevel@tonic-gate 		 * have gone from it to the TX shift register.
12270Sstevel@tonic-gate 		 * We wait for enough time for all the characters to
12280Sstevel@tonic-gate 		 * move into the RX FIFO and any excess characters to
12290Sstevel@tonic-gate 		 * have been lost, and then read all the RX FIFO. So
12300Sstevel@tonic-gate 		 * the answer we finally get will be the size which is
12310Sstevel@tonic-gate 		 * the MIN(RX FIFO,(TX FIFO + 1 or 2)). The critical
12320Sstevel@tonic-gate 		 * one is actually the TX FIFO, because if we overfill
12330Sstevel@tonic-gate 		 * it in normal operation, the excess characters are
12340Sstevel@tonic-gate 		 * lost with no warning.
12350Sstevel@tonic-gate 		 */
12360Sstevel@tonic-gate 		/*
12370Sstevel@tonic-gate 		 * Wait for characters to move into RX FIFO.
12380Sstevel@tonic-gate 		 * In theory, 200 * asy->asy_fifo_buf * 2 should be
12390Sstevel@tonic-gate 		 * enough. However, in practice it isn't always, so we
12400Sstevel@tonic-gate 		 * increase to 400 so some slow 16550A's finish, and we
12410Sstevel@tonic-gate 		 * increase to 3 so we spot more characters coming back
12420Sstevel@tonic-gate 		 * than we sent, in case that should ever happen.
12430Sstevel@tonic-gate 		 */
12440Sstevel@tonic-gate 		delay(drv_usectohz(400 * asy->asy_fifo_buf * 3));
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 		/* Now see how many characters we can read back */
12470Sstevel@tonic-gate 		for (i = 0; i < asy->asy_fifo_buf * 3; i++) {
12481106Smrj 			ret = ddi_get8(asy->asy_iohandle,
12490Sstevel@tonic-gate 			    asy->asy_ioaddr + LSR);
12500Sstevel@tonic-gate 			if (!(ret & RCA))
12510Sstevel@tonic-gate 				break;	/* FIFO emptied */
12521106Smrj 			(void) ddi_get8(asy->asy_iohandle,
12530Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT); /* lose another */
12540Sstevel@tonic-gate 		}
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 		DEBUGCONT3(ASY_DEBUG_CHIP,
12570Sstevel@tonic-gate 		    "asy%d FIFO size: expected=%d, measured=%d\n",
12580Sstevel@tonic-gate 		    asy->asy_unit, asy->asy_fifo_buf, i);
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 		hwtype = asy->asy_hwtype;
12610Sstevel@tonic-gate 		if (i < asy->asy_fifo_buf) {
12620Sstevel@tonic-gate 			/*
12630Sstevel@tonic-gate 			 * FIFO is somewhat smaller than we anticipated.
12640Sstevel@tonic-gate 			 * If we have 16 characters usable, then this
12650Sstevel@tonic-gate 			 * UART will probably work well enough in
12660Sstevel@tonic-gate 			 * 16550A mode. If less than 16 characters,
12670Sstevel@tonic-gate 			 * then we'd better not use it at all.
12680Sstevel@tonic-gate 			 * UARTs with busted FIFOs do crop up.
12690Sstevel@tonic-gate 			 */
12700Sstevel@tonic-gate 			if (i >= 16 && asy->asy_fifo_buf >= 16) {
12710Sstevel@tonic-gate 				/* fall back to a 16550A */
12720Sstevel@tonic-gate 				hwtype = ASY16550A;
12730Sstevel@tonic-gate 				asy->asy_fifo_buf = 16;
12740Sstevel@tonic-gate 				asy->asy_fifor &= ~(FIFOEXTRA1 | FIFOEXTRA2);
12750Sstevel@tonic-gate 			} else {
12760Sstevel@tonic-gate 				/* fall back to no FIFO at all */
12770Sstevel@tonic-gate 				hwtype = ASY16550;
12780Sstevel@tonic-gate 				asy->asy_fifo_buf = 1;
12790Sstevel@tonic-gate 				asy->asy_use_fifo = FIFO_OFF;
12800Sstevel@tonic-gate 				asy->asy_fifor &=
12810Sstevel@tonic-gate 				    ~(FIFO_ON | FIFOEXTRA1 | FIFOEXTRA2);
12820Sstevel@tonic-gate 			}
12830Sstevel@tonic-gate 		}
12840Sstevel@tonic-gate 		/*
12850Sstevel@tonic-gate 		 * We will need to reprogram the FIFO if we changed
12860Sstevel@tonic-gate 		 * our mind about how to drive it above, and in any
12870Sstevel@tonic-gate 		 * case, it would be a good idea to flush any garbage
12880Sstevel@tonic-gate 		 * out incase the loopback test left anything behind.
12890Sstevel@tonic-gate 		 * Again as earlier above, we must call asy_reset_fifo()
12900Sstevel@tonic-gate 		 * before any possible downgrade of asy->asy_hwtype.
12910Sstevel@tonic-gate 		 */
12920Sstevel@tonic-gate 		if (asy->asy_hwtype >= ASY16650 && hwtype < ASY16650) {
12930Sstevel@tonic-gate 			/* Disable 16650 enhanced mode */
12941106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
12950Sstevel@tonic-gate 			    EFRACCESS);
12961106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + EFR,
12970Sstevel@tonic-gate 			    0);
12981106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
12990Sstevel@tonic-gate 			    STOP1|BITS8);
13000Sstevel@tonic-gate 		}
13010Sstevel@tonic-gate 		asy_reset_fifo(asy, FIFOTXFLSH | FIFORXFLSH);
13020Sstevel@tonic-gate 		asy->asy_hwtype = hwtype;
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 		/* Clear loopback mode and restore DTR/RTS */
13051106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR, mcr);
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	DEBUGNOTE3(ASY_DEBUG_CHIP, "asy%d %s @ %p",
13090Sstevel@tonic-gate 	    asy->asy_unit, asy_hw_name(asy), (void *)asy->asy_ioaddr);
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	/* Make UART type visible in device tree for prtconf, etc */
13120Sstevel@tonic-gate 	dev = makedevice(DDI_MAJOR_T_UNKNOWN, asy->asy_unit);
13130Sstevel@tonic-gate 	(void) ddi_prop_update_string(dev, devi, "uart", asy_hw_name(asy));
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	if (asy->asy_hwtype == ASY16550)	/* for broken 16550's, */
13160Sstevel@tonic-gate 		asy->asy_hwtype = ASY8250A;	/* drive them as 8250A */
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	return (DDI_SUCCESS);
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate /*
13220Sstevel@tonic-gate  * asyinit() initializes the TTY protocol-private data for this channel
13230Sstevel@tonic-gate  * before enabling the interrupts.
13240Sstevel@tonic-gate  */
13250Sstevel@tonic-gate static void
13260Sstevel@tonic-gate asyinit(struct asycom *asy)
13270Sstevel@tonic-gate {
13280Sstevel@tonic-gate 	struct asyncline *async;
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	asy->asy_priv = kmem_zalloc(sizeof (struct asyncline), KM_SLEEP);
13310Sstevel@tonic-gate 	async = asy->asy_priv;
13320Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
13330Sstevel@tonic-gate 	async->async_common = asy;
13340Sstevel@tonic-gate 	cv_init(&async->async_flags_cv, NULL, CV_DRIVER, NULL);
13350Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
13360Sstevel@tonic-gate }
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate /*ARGSUSED3*/
13390Sstevel@tonic-gate static int
13400Sstevel@tonic-gate asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr)
13410Sstevel@tonic-gate {
13420Sstevel@tonic-gate 	struct asycom	*asy;
13430Sstevel@tonic-gate 	struct asyncline *async;
13440Sstevel@tonic-gate 	int		mcr;
13450Sstevel@tonic-gate 	int		unit;
13460Sstevel@tonic-gate 	int 		len;
13470Sstevel@tonic-gate 	struct termios 	*termiosp;
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	unit = UNIT(*dev);
13500Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_CLOSE, "asy%dopen\n", unit);
13510Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, unit);
13520Sstevel@tonic-gate 	if (asy == NULL)
13530Sstevel@tonic-gate 		return (ENXIO);		/* unit not configured */
13540Sstevel@tonic-gate 	async = asy->asy_priv;
13550Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate again:
13580Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	/*
13610Sstevel@tonic-gate 	 * Block waiting for carrier to come up, unless this is a no-delay open.
13620Sstevel@tonic-gate 	 */
13630Sstevel@tonic-gate 	if (!(async->async_flags & ASYNC_ISOPEN)) {
13640Sstevel@tonic-gate 		/*
13650Sstevel@tonic-gate 		 * Set the default termios settings (cflag).
13660Sstevel@tonic-gate 		 * Others are set in ldterm.
13670Sstevel@tonic-gate 		 */
13680Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_root_node(),
13710Sstevel@tonic-gate 		    0, "ttymodes",
13720Sstevel@tonic-gate 		    (caddr_t)&termiosp, &len) == DDI_PROP_SUCCESS &&
13730Sstevel@tonic-gate 		    len == sizeof (struct termios)) {
13740Sstevel@tonic-gate 			async->async_ttycommon.t_cflag = termiosp->c_cflag;
13750Sstevel@tonic-gate 			kmem_free(termiosp, len);
13760Sstevel@tonic-gate 		} else
13770Sstevel@tonic-gate 			cmn_err(CE_WARN,
13780Sstevel@tonic-gate 				"asy: couldn't get ttymodes property!");
13790Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 		/* eeprom mode support - respect properties */
13820Sstevel@tonic-gate 		if (asy->asy_cflag)
13830Sstevel@tonic-gate 			async->async_ttycommon.t_cflag = asy->asy_cflag;
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 		async->async_ttycommon.t_iflag = 0;
13860Sstevel@tonic-gate 		async->async_ttycommon.t_iocpending = NULL;
13870Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_row = 0;
13880Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_col = 0;
13890Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_xpixel = 0;
13900Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_ypixel = 0;
13910Sstevel@tonic-gate 		async->async_dev = *dev;
13920Sstevel@tonic-gate 		async->async_wbufcid = 0;
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 		async->async_startc = CSTART;
13950Sstevel@tonic-gate 		async->async_stopc = CSTOP;
13960Sstevel@tonic-gate 		asy_program(asy, ASY_INIT);
13970Sstevel@tonic-gate 	} else if ((async->async_ttycommon.t_flags & TS_XCLUDE) &&
13980Sstevel@tonic-gate 						secpolicy_excl_open(cr) != 0) {
13990Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
14000Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
14010Sstevel@tonic-gate 		return (EBUSY);
14020Sstevel@tonic-gate 	} else if ((*dev & OUTLINE) && !(async->async_flags & ASYNC_OUT)) {
14030Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
14040Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
14050Sstevel@tonic-gate 		return (EBUSY);
14060Sstevel@tonic-gate 	}
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	if (*dev & OUTLINE)
14090Sstevel@tonic-gate 		async->async_flags |= ASYNC_OUT;
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	/* Raise DTR on every open, but delay if it was just lowered. */
14120Sstevel@tonic-gate 	while (async->async_flags & ASYNC_DTR_DELAY) {
14130Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_MODEM,
14140Sstevel@tonic-gate 		    "asy%dopen: waiting for the ASYNC_DTR_DELAY to be clear\n",
14150Sstevel@tonic-gate 		    unit);
14160Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
14170Sstevel@tonic-gate 		if (cv_wait_sig(&async->async_flags_cv,
14180Sstevel@tonic-gate 		    &asy->asy_excl) == 0) {
14190Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
14200Sstevel@tonic-gate 			    "asy%dopen: interrupted by signal, exiting\n",
14210Sstevel@tonic-gate 			    unit);
14220Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
14230Sstevel@tonic-gate 			return (EINTR);
14240Sstevel@tonic-gate 		}
14250Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
14260Sstevel@tonic-gate 	}
14270Sstevel@tonic-gate 
14281106Smrj 	mcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
14291106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
14300Sstevel@tonic-gate 		mcr|(asy->asy_mcr&DTR));
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	DEBUGCONT3(ASY_DEBUG_INIT,
14330Sstevel@tonic-gate 		"asy%dopen: \"Raise DTR on every open\": make mcr = %x, "
14340Sstevel@tonic-gate 		"make TS_SOFTCAR = %s\n",
14350Sstevel@tonic-gate 		unit, mcr|(asy->asy_mcr&DTR),
14360Sstevel@tonic-gate 		(asy->asy_flags & ASY_IGNORE_CD) ? "ON" : "OFF");
14370Sstevel@tonic-gate 	if (asy->asy_flags & ASY_IGNORE_CD) {
14380Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_MODEM,
14390Sstevel@tonic-gate 			"asy%dopen: ASY_IGNORE_CD set, set TS_SOFTCAR\n",
14400Sstevel@tonic-gate 			unit);
14410Sstevel@tonic-gate 		async->async_ttycommon.t_flags |= TS_SOFTCAR;
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 	else
14440Sstevel@tonic-gate 		async->async_ttycommon.t_flags &= ~TS_SOFTCAR;
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	/*
14470Sstevel@tonic-gate 	 * Check carrier.
14480Sstevel@tonic-gate 	 */
14491106Smrj 	asy->asy_msr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MSR);
14500Sstevel@tonic-gate 	DEBUGCONT3(ASY_DEBUG_INIT, "asy%dopen: TS_SOFTCAR is %s, "
14510Sstevel@tonic-gate 		"MSR & DCD is %s\n",
14520Sstevel@tonic-gate 		unit,
14530Sstevel@tonic-gate 		(async->async_ttycommon.t_flags & TS_SOFTCAR) ? "set" : "clear",
14540Sstevel@tonic-gate 		(asy->asy_msr & DCD) ? "set" : "clear");
14550Sstevel@tonic-gate 	if (asy->asy_msr & DCD)
14560Sstevel@tonic-gate 		async->async_flags |= ASYNC_CARR_ON;
14570Sstevel@tonic-gate 	else
14580Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_CARR_ON;
14590Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	/*
14620Sstevel@tonic-gate 	 * If FNDELAY and FNONBLOCK are clear, block until carrier up.
14630Sstevel@tonic-gate 	 * Quit on interrupt.
14640Sstevel@tonic-gate 	 */
14650Sstevel@tonic-gate 	if (!(flag & (FNDELAY|FNONBLOCK)) &&
14660Sstevel@tonic-gate 	    !(async->async_ttycommon.t_cflag & CLOCAL)) {
14670Sstevel@tonic-gate 		if ((!(async->async_flags & (ASYNC_CARR_ON|ASYNC_OUT)) &&
14680Sstevel@tonic-gate 		    !(async->async_ttycommon.t_flags & TS_SOFTCAR)) ||
14690Sstevel@tonic-gate 		    ((async->async_flags & ASYNC_OUT) &&
14700Sstevel@tonic-gate 		    !(*dev & OUTLINE))) {
14710Sstevel@tonic-gate 			async->async_flags |= ASYNC_WOPEN;
14720Sstevel@tonic-gate 			if (cv_wait_sig(&async->async_flags_cv,
14730Sstevel@tonic-gate 			    &asy->asy_excl) == B_FALSE) {
14740Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_WOPEN;
14750Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl);
14760Sstevel@tonic-gate 				return (EINTR);
14770Sstevel@tonic-gate 			}
14780Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_WOPEN;
14790Sstevel@tonic-gate 			goto again;
14800Sstevel@tonic-gate 		}
14810Sstevel@tonic-gate 	} else if ((async->async_flags & ASYNC_OUT) && !(*dev & OUTLINE)) {
14820Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
14830Sstevel@tonic-gate 		return (EBUSY);
14840Sstevel@tonic-gate 	}
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate 	async->async_ttycommon.t_readq = rq;
14870Sstevel@tonic-gate 	async->async_ttycommon.t_writeq = WR(rq);
14880Sstevel@tonic-gate 	rq->q_ptr = WR(rq)->q_ptr = (caddr_t)async;
14890Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
14900Sstevel@tonic-gate 	/*
14910Sstevel@tonic-gate 	 * Caution here -- qprocson sets the pointers that are used by canput
14920Sstevel@tonic-gate 	 * called by async_softint.  ASYNC_ISOPEN must *not* be set until those
14930Sstevel@tonic-gate 	 * pointers are valid.
14940Sstevel@tonic-gate 	 */
14950Sstevel@tonic-gate 	qprocson(rq);
14960Sstevel@tonic-gate 	async->async_flags |= ASYNC_ISOPEN;
14970Sstevel@tonic-gate 	async->async_polltid = 0;
14980Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_INIT, "asy%dopen: done\n", unit);
14990Sstevel@tonic-gate 	return (0);
15000Sstevel@tonic-gate }
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate static void
15030Sstevel@tonic-gate async_progress_check(void *arg)
15040Sstevel@tonic-gate {
15050Sstevel@tonic-gate 	struct asyncline *async = arg;
15060Sstevel@tonic-gate 	struct asycom	 *asy = async->async_common;
15070Sstevel@tonic-gate 	mblk_t *bp;
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	/*
15100Sstevel@tonic-gate 	 * We define "progress" as either waiting on a timed break or delay, or
15110Sstevel@tonic-gate 	 * having had at least one transmitter interrupt.  If none of these are
15120Sstevel@tonic-gate 	 * true, then just terminate the output and wake up that close thread.
15130Sstevel@tonic-gate 	 */
15140Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
15150Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
15160Sstevel@tonic-gate 	if (!(async->async_flags & (ASYNC_BREAK|ASYNC_DELAY|ASYNC_PROGRESS))) {
15170Sstevel@tonic-gate 		async->async_ocnt = 0;
15180Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_BUSY;
15190Sstevel@tonic-gate 		async->async_timer = 0;
15200Sstevel@tonic-gate 		bp = async->async_xmitblk;
15210Sstevel@tonic-gate 		async->async_xmitblk = NULL;
15220Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
15230Sstevel@tonic-gate 		if (bp != NULL)
15240Sstevel@tonic-gate 			freeb(bp);
15250Sstevel@tonic-gate 		/*
15260Sstevel@tonic-gate 		 * Since this timer is running, we know that we're in exit(2).
15270Sstevel@tonic-gate 		 * That means that the user can't possibly be waiting on any
15280Sstevel@tonic-gate 		 * valid ioctl(2) completion anymore, and we should just flush
15290Sstevel@tonic-gate 		 * everything.
15300Sstevel@tonic-gate 		 */
15310Sstevel@tonic-gate 		flushq(async->async_ttycommon.t_writeq, FLUSHALL);
15320Sstevel@tonic-gate 		cv_broadcast(&async->async_flags_cv);
15330Sstevel@tonic-gate 	} else {
15340Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_PROGRESS;
15350Sstevel@tonic-gate 		async->async_timer = timeout(async_progress_check, async,
15360Sstevel@tonic-gate 		    drv_usectohz(asy_drain_check));
15370Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
15380Sstevel@tonic-gate 	}
15390Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
15400Sstevel@tonic-gate }
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate /*
15430Sstevel@tonic-gate  * Release DTR so that asyopen() can raise it.
15440Sstevel@tonic-gate  */
15450Sstevel@tonic-gate static void
15460Sstevel@tonic-gate async_dtr_free(struct asyncline *async)
15470Sstevel@tonic-gate {
15480Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_MODEM,
15510Sstevel@tonic-gate 	    "async_dtr_free, clearing ASYNC_DTR_DELAY\n");
15520Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
15530Sstevel@tonic-gate 	async->async_flags &= ~ASYNC_DTR_DELAY;
15540Sstevel@tonic-gate 	async->async_dtrtid = 0;
15550Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
15560Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
15570Sstevel@tonic-gate }
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate /*
15600Sstevel@tonic-gate  * Close routine.
15610Sstevel@tonic-gate  */
15620Sstevel@tonic-gate /*ARGSUSED2*/
15630Sstevel@tonic-gate static int
15640Sstevel@tonic-gate asyclose(queue_t *q, int flag, cred_t *credp)
15650Sstevel@tonic-gate {
15660Sstevel@tonic-gate 	struct asyncline *async;
15670Sstevel@tonic-gate 	struct asycom	 *asy;
15680Sstevel@tonic-gate 	int icr, lcr;
15690Sstevel@tonic-gate #ifdef DEBUG
15700Sstevel@tonic-gate 	int instance;
15710Sstevel@tonic-gate #endif
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	async = (struct asyncline *)q->q_ptr;
15740Sstevel@tonic-gate 	ASSERT(async != NULL);
15750Sstevel@tonic-gate #ifdef DEBUG
15760Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
15770Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_CLOSE, "asy%dclose\n", instance);
15780Sstevel@tonic-gate #endif
15790Sstevel@tonic-gate 	asy = async->async_common;
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
15820Sstevel@tonic-gate 	async->async_flags |= ASYNC_CLOSING;
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	/*
15850Sstevel@tonic-gate 	 * Turn off PPS handling early to avoid events occuring during
15860Sstevel@tonic-gate 	 * close.  Also reset the DCD edge monitoring bit.
15870Sstevel@tonic-gate 	 */
15880Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
15890Sstevel@tonic-gate 	asy->asy_flags &= ~(ASY_PPS | ASY_PPS_EDGE);
15900Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	/*
15930Sstevel@tonic-gate 	 * There are two flavors of break -- timed (M_BREAK or TCSBRK) and
15940Sstevel@tonic-gate 	 * untimed (TIOCSBRK).  For the timed case, these are enqueued on our
15950Sstevel@tonic-gate 	 * write queue and there's a timer running, so we don't have to worry
15960Sstevel@tonic-gate 	 * about them.  For the untimed case, though, the user obviously made a
15970Sstevel@tonic-gate 	 * mistake, because these are handled immediately.  We'll terminate the
15980Sstevel@tonic-gate 	 * break now and honor his implicit request by discarding the rest of
15990Sstevel@tonic-gate 	 * the data.
16000Sstevel@tonic-gate 	 */
16010Sstevel@tonic-gate 	if (async->async_flags & ASYNC_OUT_SUSPEND) {
16020Sstevel@tonic-gate 		if (async->async_utbrktid != 0) {
16030Sstevel@tonic-gate 			(void) untimeout(async->async_utbrktid);
16040Sstevel@tonic-gate 			async->async_utbrktid = 0;
16050Sstevel@tonic-gate 		}
16060Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
16071106Smrj 		lcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
16081106Smrj 		ddi_put8(asy->asy_iohandle,
16090Sstevel@tonic-gate 		    asy->asy_ioaddr + LCR, (lcr & ~SETBREAK));
16100Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
16110Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_SUSPEND;
16120Sstevel@tonic-gate 		goto nodrain;
16130Sstevel@tonic-gate 	}
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	/*
16160Sstevel@tonic-gate 	 * If the user told us not to delay the close ("non-blocking"), then
16170Sstevel@tonic-gate 	 * don't bother trying to drain.
16180Sstevel@tonic-gate 	 *
16190Sstevel@tonic-gate 	 * If the user did M_STOP (ASYNC_STOPPED), there's no hope of ever
16200Sstevel@tonic-gate 	 * getting an M_START (since these messages aren't enqueued), and the
16210Sstevel@tonic-gate 	 * only other way to clear the stop condition is by loss of DCD, which
16220Sstevel@tonic-gate 	 * would discard the queue data.  Thus, we drop the output data if
16230Sstevel@tonic-gate 	 * ASYNC_STOPPED is set.
16240Sstevel@tonic-gate 	 */
16250Sstevel@tonic-gate 	if ((flag & (FNDELAY|FNONBLOCK)) ||
16260Sstevel@tonic-gate 	    (async->async_flags & ASYNC_STOPPED)) {
16270Sstevel@tonic-gate 		goto nodrain;
16280Sstevel@tonic-gate 	}
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	/*
16310Sstevel@tonic-gate 	 * If there's any pending output, then we have to try to drain it.
16320Sstevel@tonic-gate 	 * There are two main cases to be handled:
16330Sstevel@tonic-gate 	 *	- called by close(2): need to drain until done or until
16340Sstevel@tonic-gate 	 *	  a signal is received.  No timeout.
16350Sstevel@tonic-gate 	 *	- called by exit(2): need to drain while making progress
16360Sstevel@tonic-gate 	 *	  or until a timeout occurs.  No signals.
16370Sstevel@tonic-gate 	 *
16380Sstevel@tonic-gate 	 * If we can't rely on receiving a signal to get us out of a hung
16390Sstevel@tonic-gate 	 * session, then we have to use a timer.  In this case, we set a timer
16400Sstevel@tonic-gate 	 * to check for progress in sending the output data -- all that we ask
16410Sstevel@tonic-gate 	 * (at each interval) is that there's been some progress made.  Since
16420Sstevel@tonic-gate 	 * the interrupt routine grabs buffers from the write queue, we can't
16430Sstevel@tonic-gate 	 * trust changes in async_ocnt.  Instead, we use a progress flag.
16440Sstevel@tonic-gate 	 *
16450Sstevel@tonic-gate 	 * Note that loss of carrier will cause the output queue to be flushed,
16460Sstevel@tonic-gate 	 * and we'll wake up again and finish normally.
16470Sstevel@tonic-gate 	 */
16480Sstevel@tonic-gate 	if (!ddi_can_receive_sig() && asy_drain_check != 0) {
16490Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_PROGRESS;
16500Sstevel@tonic-gate 		async->async_timer = timeout(async_progress_check, async,
16510Sstevel@tonic-gate 		    drv_usectohz(asy_drain_check));
16520Sstevel@tonic-gate 	}
16530Sstevel@tonic-gate 	while (async->async_ocnt > 0 ||
16540Sstevel@tonic-gate 	    async->async_ttycommon.t_writeq->q_first != NULL ||
16550Sstevel@tonic-gate 	    (async->async_flags & (ASYNC_BUSY|ASYNC_BREAK|ASYNC_DELAY))) {
16560Sstevel@tonic-gate 		if (cv_wait_sig(&async->async_flags_cv, &asy->asy_excl) == 0)
16570Sstevel@tonic-gate 			break;
16580Sstevel@tonic-gate 	}
16590Sstevel@tonic-gate 	if (async->async_timer != 0) {
16600Sstevel@tonic-gate 		(void) untimeout(async->async_timer);
16610Sstevel@tonic-gate 		async->async_timer = 0;
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate nodrain:
16650Sstevel@tonic-gate 	async->async_ocnt = 0;
16660Sstevel@tonic-gate 	if (async->async_xmitblk != NULL)
16670Sstevel@tonic-gate 		freeb(async->async_xmitblk);
16680Sstevel@tonic-gate 	async->async_xmitblk = NULL;
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	/*
16710Sstevel@tonic-gate 	 * If line has HUPCL set or is incompletely opened fix up the modem
16720Sstevel@tonic-gate 	 * lines.
16730Sstevel@tonic-gate 	 */
16740Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_MODEM,
16750Sstevel@tonic-gate 		"asy%dclose: next check HUPCL flag\n", instance);
16760Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
16770Sstevel@tonic-gate 	if ((async->async_ttycommon.t_cflag & HUPCL) ||
16780Sstevel@tonic-gate 	    (async->async_flags & ASYNC_WOPEN)) {
16790Sstevel@tonic-gate 		DEBUGCONT3(ASY_DEBUG_MODEM,
16800Sstevel@tonic-gate 			"asy%dclose: HUPCL flag = %x, ASYNC_WOPEN flag = %x\n",
16810Sstevel@tonic-gate 			instance,
16820Sstevel@tonic-gate 			async->async_ttycommon.t_cflag & HUPCL,
16830Sstevel@tonic-gate 			async->async_ttycommon.t_cflag & ASYNC_WOPEN);
16840Sstevel@tonic-gate 		async->async_flags |= ASYNC_DTR_DELAY;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 		/* turn off DTR, RTS but NOT interrupt to 386 */
16870Sstevel@tonic-gate 		if (asy->asy_flags & (ASY_IGNORE_CD|ASY_RTS_DTR_OFF)) {
16880Sstevel@tonic-gate 			DEBUGCONT3(ASY_DEBUG_MODEM,
16890Sstevel@tonic-gate 				"asy%dclose: ASY_IGNORE_CD flag = %x, "
16900Sstevel@tonic-gate 				"ASY_RTS_DTR_OFF flag = %x\n",
16910Sstevel@tonic-gate 				instance,
16920Sstevel@tonic-gate 				asy->asy_flags & ASY_IGNORE_CD,
16930Sstevel@tonic-gate 				asy->asy_flags & ASY_RTS_DTR_OFF);
16941106Smrj 			ddi_put8(asy->asy_iohandle,
16950Sstevel@tonic-gate 				asy->asy_ioaddr + MCR, asy->asy_mcr|OUT2);
16960Sstevel@tonic-gate 		} else {
16970Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
16980Sstevel@tonic-gate 			    "asy%dclose: Dropping DTR and RTS\n", instance);
16991106Smrj 			ddi_put8(asy->asy_iohandle,
17000Sstevel@tonic-gate 				asy->asy_ioaddr + MCR, OUT2);
17010Sstevel@tonic-gate 		}
17020Sstevel@tonic-gate 		async->async_dtrtid =
17030Sstevel@tonic-gate 		    timeout((void (*)())async_dtr_free,
17040Sstevel@tonic-gate 		    (caddr_t)async, drv_usectohz(asy_min_dtr_low));
17050Sstevel@tonic-gate 	}
17060Sstevel@tonic-gate 	/*
17070Sstevel@tonic-gate 	 * If nobody's using it now, turn off receiver interrupts.
17080Sstevel@tonic-gate 	 */
17090Sstevel@tonic-gate 	if ((async->async_flags & (ASYNC_WOPEN|ASYNC_ISOPEN)) == 0) {
17101106Smrj 		icr = ddi_get8(asy->asy_iohandle,
17110Sstevel@tonic-gate 			asy->asy_ioaddr + ICR);
17121106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR,
17130Sstevel@tonic-gate 			(icr & ~RIEN));
17140Sstevel@tonic-gate 	}
17150Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
17160Sstevel@tonic-gate out:
17170Sstevel@tonic-gate 	ttycommon_close(&async->async_ttycommon);
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 	/*
17200Sstevel@tonic-gate 	 * Cancel outstanding "bufcall" request.
17210Sstevel@tonic-gate 	 */
17220Sstevel@tonic-gate 	if (async->async_wbufcid != 0) {
17230Sstevel@tonic-gate 		unbufcall(async->async_wbufcid);
17240Sstevel@tonic-gate 		async->async_wbufcid = 0;
17250Sstevel@tonic-gate 	}
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	/* Note that qprocsoff can't be done until after interrupts are off */
17280Sstevel@tonic-gate 	qprocsoff(q);
17290Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
17300Sstevel@tonic-gate 	async->async_ttycommon.t_readq = NULL;
17310Sstevel@tonic-gate 	async->async_ttycommon.t_writeq = NULL;
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 	/*
17340Sstevel@tonic-gate 	 * Clear out device state, except persistant device property flags.
17350Sstevel@tonic-gate 	 */
17360Sstevel@tonic-gate 	async->async_flags &= (ASYNC_DTR_DELAY|ASY_RTS_DTR_OFF);
17370Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
17380Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_CLOSE, "asy%dclose: done\n", instance);
17410Sstevel@tonic-gate 	return (0);
17420Sstevel@tonic-gate }
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate static boolean_t
17450Sstevel@tonic-gate asy_isbusy(struct asycom *asy)
17460Sstevel@tonic-gate {
17470Sstevel@tonic-gate 	struct asyncline *async;
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_EOT, "asy_isbusy\n");
17500Sstevel@tonic-gate 	async = asy->asy_priv;
17510Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
17520Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
17530Sstevel@tonic-gate 	return ((async->async_ocnt > 0) ||
17541106Smrj 		((ddi_get8(asy->asy_iohandle,
17550Sstevel@tonic-gate 		    asy->asy_ioaddr + LSR) & (XSRE|XHRE)) == 0));
17560Sstevel@tonic-gate }
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate static void
17590Sstevel@tonic-gate asy_waiteot(struct asycom *asy)
17600Sstevel@tonic-gate {
17610Sstevel@tonic-gate 	/*
17620Sstevel@tonic-gate 	 * Wait for the current transmission block and the
17630Sstevel@tonic-gate 	 * current fifo data to transmit. Once this is done
17640Sstevel@tonic-gate 	 * we may go on.
17650Sstevel@tonic-gate 	 */
17660Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_EOT, "asy_waiteot\n");
17670Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
17680Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
17690Sstevel@tonic-gate 	while (asy_isbusy(asy)) {
17700Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
17710Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
17720Sstevel@tonic-gate 		drv_usecwait(10000);		/* wait .01 */
17730Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
17740Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
17750Sstevel@tonic-gate 	}
17760Sstevel@tonic-gate }
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate /* asy_reset_fifo -- flush fifos and [re]program fifo control register */
17790Sstevel@tonic-gate static void
17800Sstevel@tonic-gate asy_reset_fifo(struct asycom *asy, uchar_t flush)
17810Sstevel@tonic-gate {
17820Sstevel@tonic-gate 	uchar_t lcr;
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	/* On a 16750, we have to set DLAB in order to set FIFOEXTRA. */
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	if (asy->asy_hwtype >= ASY16750) {
17871106Smrj 		lcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
17881106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
17890Sstevel@tonic-gate 		    lcr | DLAB);
17900Sstevel@tonic-gate 	}
17910Sstevel@tonic-gate 
17921106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + FIFOR,
17930Sstevel@tonic-gate 	    asy->asy_fifor | flush);
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	/* Clear DLAB */
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	if (asy->asy_hwtype >= ASY16750) {
17981106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, lcr);
17990Sstevel@tonic-gate 	}
18000Sstevel@tonic-gate }
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate /*
18030Sstevel@tonic-gate  * Program the ASY port. Most of the async operation is based on the values
18040Sstevel@tonic-gate  * of 'c_iflag' and 'c_cflag'.
18050Sstevel@tonic-gate  */
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate #define	BAUDINDEX(cflg)	(((cflg) & CBAUDEXT) ? \
18080Sstevel@tonic-gate 			(((cflg) & CBAUD) + CBAUD + 1) : ((cflg) & CBAUD))
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate static void
18110Sstevel@tonic-gate asy_program(struct asycom *asy, int mode)
18120Sstevel@tonic-gate {
18130Sstevel@tonic-gate 	struct asyncline *async;
18140Sstevel@tonic-gate 	int baudrate, c_flag;
18150Sstevel@tonic-gate 	int icr, lcr;
18160Sstevel@tonic-gate 	int flush_reg;
18170Sstevel@tonic-gate 	int ocflags;
18180Sstevel@tonic-gate #ifdef DEBUG
18190Sstevel@tonic-gate 	int instance;
18200Sstevel@tonic-gate #endif
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
18230Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	async = asy->asy_priv;
18260Sstevel@tonic-gate #ifdef DEBUG
18270Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
18280Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_PROCS,
18290Sstevel@tonic-gate 		"asy%d_program: mode = 0x%08X, enter\n", instance, mode);
18300Sstevel@tonic-gate #endif
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate 	baudrate = BAUDINDEX(async->async_ttycommon.t_cflag);
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 	async->async_ttycommon.t_cflag &= ~(CIBAUD);
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 	if (baudrate > CBAUD) {
18370Sstevel@tonic-gate 		async->async_ttycommon.t_cflag |= CIBAUDEXT;
18380Sstevel@tonic-gate 		async->async_ttycommon.t_cflag |=
18390Sstevel@tonic-gate 			(((baudrate - CBAUD - 1) << IBSHIFT) & CIBAUD);
18400Sstevel@tonic-gate 	} else {
18410Sstevel@tonic-gate 		async->async_ttycommon.t_cflag &= ~CIBAUDEXT;
18420Sstevel@tonic-gate 		async->async_ttycommon.t_cflag |=
18430Sstevel@tonic-gate 			((baudrate << IBSHIFT) & CIBAUD);
18440Sstevel@tonic-gate 	}
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	c_flag = async->async_ttycommon.t_cflag &
18470Sstevel@tonic-gate 		(CLOCAL|CREAD|CSTOPB|CSIZE|PARENB|PARODD|CBAUD|CBAUDEXT);
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	/* disable interrupts */
18501106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0);
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 	ocflags = asy->asy_ocflag;
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 	/* flush/reset the status registers */
18551106Smrj 	(void) ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + ISR);
18561106Smrj 	(void) ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR);
18571106Smrj 	asy->asy_msr = flush_reg = ddi_get8(asy->asy_iohandle,
18580Sstevel@tonic-gate 					asy->asy_ioaddr + MSR);
18590Sstevel@tonic-gate 	/*
18600Sstevel@tonic-gate 	 * The device is programmed in the open sequence, if we
18610Sstevel@tonic-gate 	 * have to hardware handshake, then this is a good time
18620Sstevel@tonic-gate 	 * to check if the device can receive any data.
18630Sstevel@tonic-gate 	 */
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	if ((CRTSCTS & async->async_ttycommon.t_cflag) && !(flush_reg & CTS)) {
18660Sstevel@tonic-gate 		async_flowcontrol_hw_output(asy, FLOW_STOP);
18670Sstevel@tonic-gate 	} else {
18680Sstevel@tonic-gate 		/*
18690Sstevel@tonic-gate 		 * We can not use async_flowcontrol_hw_output(asy, FLOW_START)
18700Sstevel@tonic-gate 		 * here, because if CRTSCTS is clear, we need clear
18710Sstevel@tonic-gate 		 * ASYNC_HW_OUT_FLW bit.
18720Sstevel@tonic-gate 		 */
18730Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_HW_OUT_FLW;
18740Sstevel@tonic-gate 	}
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 	/*
18770Sstevel@tonic-gate 	 * If IXON is not set, clear ASYNC_SW_OUT_FLW;
18780Sstevel@tonic-gate 	 * If IXON is set, no matter what IXON flag is before this
18790Sstevel@tonic-gate 	 * function call to asy_program,
18800Sstevel@tonic-gate 	 * we will use the old ASYNC_SW_OUT_FLW status.
18810Sstevel@tonic-gate 	 * Because of handling IXON in the driver, we also should re-calculate
18820Sstevel@tonic-gate 	 * the value of ASYNC_OUT_FLW_RESUME bit, but in fact,
18830Sstevel@tonic-gate 	 * the TCSET* commands which call asy_program
18840Sstevel@tonic-gate 	 * are put into the write queue, so there is no output needed to
18850Sstevel@tonic-gate 	 * be resumed at this point.
18860Sstevel@tonic-gate 	 */
18870Sstevel@tonic-gate 	if (!(IXON & async->async_ttycommon.t_iflag))
18880Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_SW_OUT_FLW;
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 	/* manually flush receive buffer or fifo (workaround for buggy fifos) */
18910Sstevel@tonic-gate 	if (mode == ASY_INIT)
18920Sstevel@tonic-gate 		if (asy->asy_use_fifo == FIFO_ON) {
18930Sstevel@tonic-gate 			for (flush_reg = asy->asy_fifo_buf; flush_reg-- > 0; ) {
18941106Smrj 				(void) ddi_get8(asy->asy_iohandle,
18950Sstevel@tonic-gate 						asy->asy_ioaddr + DAT);
18960Sstevel@tonic-gate 			}
18970Sstevel@tonic-gate 		} else {
18981106Smrj 			flush_reg = ddi_get8(asy->asy_iohandle,
18990Sstevel@tonic-gate 					asy->asy_ioaddr + DAT);
19000Sstevel@tonic-gate 		}
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	if (ocflags != (c_flag & ~CLOCAL) || mode == ASY_INIT) {
19030Sstevel@tonic-gate 		/* Set line control */
19041106Smrj 		lcr = ddi_get8(asy->asy_iohandle,
19050Sstevel@tonic-gate 			asy->asy_ioaddr + LCR);
19060Sstevel@tonic-gate 		lcr &= ~(WLS0|WLS1|STB|PEN|EPS);
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 		if (c_flag & CSTOPB)
19090Sstevel@tonic-gate 			lcr |= STB;	/* 2 stop bits */
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 		if (c_flag & PARENB)
19120Sstevel@tonic-gate 			lcr |= PEN;
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 		if ((c_flag & PARODD) == 0)
19150Sstevel@tonic-gate 			lcr |= EPS;
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 		switch (c_flag & CSIZE) {
19180Sstevel@tonic-gate 		case CS5:
19190Sstevel@tonic-gate 			lcr |= BITS5;
19200Sstevel@tonic-gate 			break;
19210Sstevel@tonic-gate 		case CS6:
19220Sstevel@tonic-gate 			lcr |= BITS6;
19230Sstevel@tonic-gate 			break;
19240Sstevel@tonic-gate 		case CS7:
19250Sstevel@tonic-gate 			lcr |= BITS7;
19260Sstevel@tonic-gate 			break;
19270Sstevel@tonic-gate 		case CS8:
19280Sstevel@tonic-gate 			lcr |= BITS8;
19290Sstevel@tonic-gate 			break;
19300Sstevel@tonic-gate 		}
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 		/* set the baud rate, unless it is "0" */
19331106Smrj 		ddi_put8(asy->asy_iohandle,
19340Sstevel@tonic-gate 			asy->asy_ioaddr + LCR, DLAB);
19350Sstevel@tonic-gate 		if (baudrate != 0) {
19361106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
19370Sstevel@tonic-gate 				asyspdtab[baudrate] & 0xff);
19381106Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR,
19390Sstevel@tonic-gate 				(asyspdtab[baudrate] >> 8) & 0xff);
19400Sstevel@tonic-gate 		}
19410Sstevel@tonic-gate 		/* set the line control modes */
19421106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, lcr);
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 		/*
19450Sstevel@tonic-gate 		 * If we have a FIFO buffer, enable/flush
19460Sstevel@tonic-gate 		 * at intialize time, flush if transitioning from
19470Sstevel@tonic-gate 		 * CREAD off to CREAD on.
19480Sstevel@tonic-gate 		 */
19490Sstevel@tonic-gate 		if ((ocflags & CREAD) == 0 && (c_flag & CREAD) ||
19500Sstevel@tonic-gate 		    mode == ASY_INIT)
19510Sstevel@tonic-gate 			if (asy->asy_use_fifo == FIFO_ON)
19520Sstevel@tonic-gate 				asy_reset_fifo(asy, FIFORXFLSH);
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 		/* remember the new cflags */
19550Sstevel@tonic-gate 		asy->asy_ocflag = c_flag & ~CLOCAL;
19560Sstevel@tonic-gate 	}
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	if (baudrate == 0)
19591106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
19600Sstevel@tonic-gate 			(asy->asy_mcr & RTS) | OUT2);
19610Sstevel@tonic-gate 	else
19621106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
19630Sstevel@tonic-gate 			asy->asy_mcr | OUT2);
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	/*
19660Sstevel@tonic-gate 	 * Call the modem status interrupt handler to check for the carrier
19670Sstevel@tonic-gate 	 * in case CLOCAL was turned off after the carrier came on.
19680Sstevel@tonic-gate 	 * (Note: Modem status interrupt is not enabled if CLOCAL is ON.)
19690Sstevel@tonic-gate 	 */
19700Sstevel@tonic-gate 	async_msint(asy);
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 	/* Set interrupt control */
19730Sstevel@tonic-gate 	DEBUGCONT3(ASY_DEBUG_MODM2,
19740Sstevel@tonic-gate 		"asy%d_program: c_flag & CLOCAL = %x t_cflag & CRTSCTS = %x\n",
19750Sstevel@tonic-gate 		instance,
19760Sstevel@tonic-gate 		c_flag & CLOCAL,
19770Sstevel@tonic-gate 		async->async_ttycommon.t_cflag & CRTSCTS);
19780Sstevel@tonic-gate 	if ((c_flag & CLOCAL) && !(async->async_ttycommon.t_cflag & CRTSCTS))
19790Sstevel@tonic-gate 		/*
19800Sstevel@tonic-gate 		 * direct-wired line ignores DCD, so we don't enable modem
19810Sstevel@tonic-gate 		 * status interrupts.
19820Sstevel@tonic-gate 		 */
19830Sstevel@tonic-gate 		icr = (TIEN | SIEN);
19840Sstevel@tonic-gate 	else
19850Sstevel@tonic-gate 		icr = (TIEN | SIEN | MIEN);
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 	if (c_flag & CREAD)
19880Sstevel@tonic-gate 		icr |= RIEN;
19890Sstevel@tonic-gate 
19901106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, icr);
19910Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "asy%d_program: done\n", instance);
19920Sstevel@tonic-gate }
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate static boolean_t
19950Sstevel@tonic-gate asy_baudok(struct asycom *asy)
19960Sstevel@tonic-gate {
19970Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
19980Sstevel@tonic-gate 	int baudrate;
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 	baudrate = BAUDINDEX(async->async_ttycommon.t_cflag);
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	if (baudrate >= sizeof (asyspdtab)/sizeof (*asyspdtab))
20040Sstevel@tonic-gate 		return (0);
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 	return (baudrate == 0 || asyspdtab[baudrate]);
20070Sstevel@tonic-gate }
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate /*
20100Sstevel@tonic-gate  * asyintr() is the High Level Interrupt Handler.
20110Sstevel@tonic-gate  *
20120Sstevel@tonic-gate  * There are four different interrupt types indexed by ISR register values:
20130Sstevel@tonic-gate  *		0: modem
20140Sstevel@tonic-gate  *		1: Tx holding register is empty, ready for next char
20150Sstevel@tonic-gate  *		2: Rx register now holds a char to be picked up
20160Sstevel@tonic-gate  *		3: error or break on line
20170Sstevel@tonic-gate  * This routine checks the Bit 0 (interrupt-not-pending) to determine if
20180Sstevel@tonic-gate  * the interrupt is from this port.
20190Sstevel@tonic-gate  */
20200Sstevel@tonic-gate uint_t
20210Sstevel@tonic-gate asyintr(caddr_t argasy)
20220Sstevel@tonic-gate {
20230Sstevel@tonic-gate 	struct asycom		*asy = (struct asycom *)argasy;
20240Sstevel@tonic-gate 	struct asyncline	*async;
20250Sstevel@tonic-gate 	int			ret_status = DDI_INTR_UNCLAIMED;
20260Sstevel@tonic-gate 	uchar_t			interrupt_id, lsr;
20270Sstevel@tonic-gate 
20281106Smrj 	interrupt_id = ddi_get8(asy->asy_iohandle,
20290Sstevel@tonic-gate 				asy->asy_ioaddr + ISR) & 0x0F;
20300Sstevel@tonic-gate 	async = asy->asy_priv;
20310Sstevel@tonic-gate 	if ((async == NULL) || asy_addedsoft == 0 ||
20320Sstevel@tonic-gate 		!(async->async_flags & (ASYNC_ISOPEN|ASYNC_WOPEN))) {
20330Sstevel@tonic-gate 		if (interrupt_id & NOINTERRUPT)
20340Sstevel@tonic-gate 			return (DDI_INTR_UNCLAIMED);
20350Sstevel@tonic-gate 		else {
20360Sstevel@tonic-gate 			/*
20370Sstevel@tonic-gate 			 * reset the device by:
20380Sstevel@tonic-gate 			 *	reading line status
20390Sstevel@tonic-gate 			 *	reading any data from data status register
20400Sstevel@tonic-gate 			 *	reading modem status
20410Sstevel@tonic-gate 			 */
20421106Smrj 			(void) ddi_get8(asy->asy_iohandle,
20430Sstevel@tonic-gate 					asy->asy_ioaddr + LSR);
20441106Smrj 			(void) ddi_get8(asy->asy_iohandle,
20450Sstevel@tonic-gate 					asy->asy_ioaddr + DAT);
20461106Smrj 			asy->asy_msr = ddi_get8(asy->asy_iohandle,
20470Sstevel@tonic-gate 						asy->asy_ioaddr + MSR);
20480Sstevel@tonic-gate 			return (DDI_INTR_CLAIMED);
20490Sstevel@tonic-gate 		}
20500Sstevel@tonic-gate 	}
20510Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
20520Sstevel@tonic-gate 	/*
20530Sstevel@tonic-gate 	 * We will loop until the interrupt line is pulled low. asy
20540Sstevel@tonic-gate 	 * interrupt is edge triggered.
20550Sstevel@tonic-gate 	 */
20560Sstevel@tonic-gate 	/* CSTYLED */
20571106Smrj 	for (;; interrupt_id = (ddi_get8(asy->asy_iohandle,
20580Sstevel@tonic-gate 					asy->asy_ioaddr + ISR) & 0x0F)) {
20590Sstevel@tonic-gate 		if (interrupt_id & NOINTERRUPT)
20600Sstevel@tonic-gate 			break;
20610Sstevel@tonic-gate 		ret_status = DDI_INTR_CLAIMED;
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_INTR,
20640Sstevel@tonic-gate 			"asyintr: interrupt_id = 0x%d\n", interrupt_id);
20651106Smrj 		lsr = ddi_get8(asy->asy_iohandle,
20660Sstevel@tonic-gate 			asy->asy_ioaddr + LSR);
20670Sstevel@tonic-gate 		switch (interrupt_id) {
20680Sstevel@tonic-gate 		case RxRDY:
20690Sstevel@tonic-gate 		case RSTATUS:
20700Sstevel@tonic-gate 		case FFTMOUT:
20710Sstevel@tonic-gate 			/* receiver interrupt or receiver errors */
20720Sstevel@tonic-gate 			async_rxint(asy, lsr);
20730Sstevel@tonic-gate 			break;
20740Sstevel@tonic-gate 		case TxRDY:
20750Sstevel@tonic-gate 			/* transmit interrupt */
20760Sstevel@tonic-gate 			async_txint(asy);
20770Sstevel@tonic-gate 			continue;
20780Sstevel@tonic-gate 		case MSTATUS:
20790Sstevel@tonic-gate 			/* modem status interrupt */
20800Sstevel@tonic-gate 			async_msint(asy);
20810Sstevel@tonic-gate 			break;
20820Sstevel@tonic-gate 		}
20830Sstevel@tonic-gate 		if ((lsr & XHRE) && (async->async_flags & ASYNC_BUSY) &&
20840Sstevel@tonic-gate 		    (async->async_ocnt > 0))
20850Sstevel@tonic-gate 			async_txint(asy);
20860Sstevel@tonic-gate 	}
20870Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
20880Sstevel@tonic-gate 	return (ret_status);
20890Sstevel@tonic-gate }
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate /*
20920Sstevel@tonic-gate  * Transmitter interrupt service routine.
20930Sstevel@tonic-gate  * If there is more data to transmit in the current pseudo-DMA block,
20940Sstevel@tonic-gate  * send the next character if output is not stopped or draining.
20950Sstevel@tonic-gate  * Otherwise, queue up a soft interrupt.
20960Sstevel@tonic-gate  *
20970Sstevel@tonic-gate  * XXX -  Needs review for HW FIFOs.
20980Sstevel@tonic-gate  */
20990Sstevel@tonic-gate static void
21000Sstevel@tonic-gate async_txint(struct asycom *asy)
21010Sstevel@tonic-gate {
21020Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
21030Sstevel@tonic-gate 	int		fifo_len;
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	/*
21060Sstevel@tonic-gate 	 * If ASYNC_BREAK or ASYNC_OUT_SUSPEND has been set, return to
21070Sstevel@tonic-gate 	 * asyintr()'s context to claim the interrupt without performing
21080Sstevel@tonic-gate 	 * any action. No character will be loaded into FIFO/THR until
21090Sstevel@tonic-gate 	 * timed or untimed break is removed
21100Sstevel@tonic-gate 	 */
21110Sstevel@tonic-gate 	if (async->async_flags & (ASYNC_BREAK|ASYNC_OUT_SUSPEND))
21120Sstevel@tonic-gate 		return;
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate 	fifo_len = asy->asy_fifo_buf; /* with FIFO buffers */
21150Sstevel@tonic-gate 	if (fifo_len > asy_max_tx_fifo)
21160Sstevel@tonic-gate 		fifo_len = asy_max_tx_fifo;
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
21190Sstevel@tonic-gate 		fifo_len--;
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 	if (async->async_ocnt > 0 && fifo_len > 0 &&
21220Sstevel@tonic-gate 	    !(async->async_flags &
21230Sstevel@tonic-gate 	    (ASYNC_HW_OUT_FLW|ASYNC_SW_OUT_FLW|ASYNC_STOPPED))) {
21240Sstevel@tonic-gate 		while (fifo_len-- > 0 && async->async_ocnt-- > 0) {
21251106Smrj 			ddi_put8(asy->asy_iohandle,
21260Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT, *async->async_optr++);
21270Sstevel@tonic-gate 		}
21280Sstevel@tonic-gate 		async->async_flags |= ASYNC_PROGRESS;
21290Sstevel@tonic-gate 	}
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate 	if (fifo_len <= 0)
21320Sstevel@tonic-gate 		return;
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	ASYSETSOFT(asy);
21350Sstevel@tonic-gate }
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate /*
21380Sstevel@tonic-gate  * Interrupt on port: handle PPS event.  This function is only called
21390Sstevel@tonic-gate  * for a port on which PPS event handling has been enabled.
21400Sstevel@tonic-gate  */
21410Sstevel@tonic-gate static void
21420Sstevel@tonic-gate asy_ppsevent(struct asycom *asy, int msr)
21430Sstevel@tonic-gate {
21440Sstevel@tonic-gate 	if (asy->asy_flags & ASY_PPS_EDGE) {
21450Sstevel@tonic-gate 		/* Have seen leading edge, now look for and record drop */
21460Sstevel@tonic-gate 		if ((msr & DCD) == 0)
21470Sstevel@tonic-gate 			asy->asy_flags &= ~ASY_PPS_EDGE;
21480Sstevel@tonic-gate 		/*
21490Sstevel@tonic-gate 		 * Waiting for leading edge, look for rise; stamp event and
21500Sstevel@tonic-gate 		 * calibrate kernel clock.
21510Sstevel@tonic-gate 		 */
21520Sstevel@tonic-gate 	} else if (msr & DCD) {
21530Sstevel@tonic-gate 			/*
21540Sstevel@tonic-gate 			 * This code captures a timestamp at the designated
21550Sstevel@tonic-gate 			 * transition of the PPS signal (DCD asserted).  The
21560Sstevel@tonic-gate 			 * code provides a pointer to the timestamp, as well
21570Sstevel@tonic-gate 			 * as the hardware counter value at the capture.
21580Sstevel@tonic-gate 			 *
21590Sstevel@tonic-gate 			 * Note: the kernel has nano based time values while
21600Sstevel@tonic-gate 			 * NTP requires micro based, an in-line fast algorithm
21610Sstevel@tonic-gate 			 * to convert nsec to usec is used here -- see hrt2ts()
21620Sstevel@tonic-gate 			 * in common/os/timers.c for a full description.
21630Sstevel@tonic-gate 			 */
21640Sstevel@tonic-gate 			struct timeval *tvp = &asy_ppsev.tv;
21650Sstevel@tonic-gate 			timestruc_t ts;
21660Sstevel@tonic-gate 			long nsec, usec;
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 			asy->asy_flags |= ASY_PPS_EDGE;
21690Sstevel@tonic-gate 			LED_OFF;
21700Sstevel@tonic-gate 			gethrestime(&ts);
21710Sstevel@tonic-gate 			LED_ON;
21720Sstevel@tonic-gate 			nsec = ts.tv_nsec;
21730Sstevel@tonic-gate 			usec = nsec + (nsec >> 2);
21740Sstevel@tonic-gate 			usec = nsec + (usec >> 1);
21750Sstevel@tonic-gate 			usec = nsec + (usec >> 2);
21760Sstevel@tonic-gate 			usec = nsec + (usec >> 4);
21770Sstevel@tonic-gate 			usec = nsec - (usec >> 3);
21780Sstevel@tonic-gate 			usec = nsec + (usec >> 2);
21790Sstevel@tonic-gate 			usec = nsec + (usec >> 3);
21800Sstevel@tonic-gate 			usec = nsec + (usec >> 4);
21810Sstevel@tonic-gate 			usec = nsec + (usec >> 1);
21820Sstevel@tonic-gate 			usec = nsec + (usec >> 6);
21830Sstevel@tonic-gate 			tvp->tv_usec = usec >> 10;
21840Sstevel@tonic-gate 			tvp->tv_sec = ts.tv_sec;
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 			++asy_ppsev.serial;
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 			/*
21890Sstevel@tonic-gate 			 * Because the kernel keeps a high-resolution time,
21900Sstevel@tonic-gate 			 * pass the current highres timestamp in tvp and zero
21910Sstevel@tonic-gate 			 * in usec.
21920Sstevel@tonic-gate 			 */
21930Sstevel@tonic-gate 			ddi_hardpps(tvp, 0);
21940Sstevel@tonic-gate 	}
21950Sstevel@tonic-gate }
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate /*
21980Sstevel@tonic-gate  * Receiver interrupt: RxRDY interrupt, FIFO timeout interrupt or receive
21990Sstevel@tonic-gate  * error interrupt.
22000Sstevel@tonic-gate  * Try to put the character into the circular buffer for this line; if it
22010Sstevel@tonic-gate  * overflows, indicate a circular buffer overrun. If this port is always
22020Sstevel@tonic-gate  * to be serviced immediately, or the character is a STOP character, or
22030Sstevel@tonic-gate  * more than 15 characters have arrived, queue up a soft interrupt to
22040Sstevel@tonic-gate  * drain the circular buffer.
22050Sstevel@tonic-gate  * XXX - needs review for hw FIFOs support.
22060Sstevel@tonic-gate  */
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate static void
22090Sstevel@tonic-gate async_rxint(struct asycom *asy, uchar_t lsr)
22100Sstevel@tonic-gate {
22110Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
22120Sstevel@tonic-gate 	uchar_t c;
22130Sstevel@tonic-gate 	uint_t s, needsoft = 0;
22140Sstevel@tonic-gate 	tty_common_t *tp;
22150Sstevel@tonic-gate 	int looplim = asy->asy_fifo_buf * 2;
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 	tp = &async->async_ttycommon;
22180Sstevel@tonic-gate 	if (!(tp->t_cflag & CREAD)) {
22190Sstevel@tonic-gate 		while (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
22201106Smrj 			(void) (ddi_get8(asy->asy_iohandle,
22210Sstevel@tonic-gate 					asy->asy_ioaddr + DAT) & 0xff);
22221106Smrj 			lsr = ddi_get8(asy->asy_iohandle,
22230Sstevel@tonic-gate 					asy->asy_ioaddr + LSR);
22240Sstevel@tonic-gate 			if (looplim-- < 0)		/* limit loop */
22250Sstevel@tonic-gate 				break;
22260Sstevel@tonic-gate 		}
22270Sstevel@tonic-gate 		return; /* line is not open for read? */
22280Sstevel@tonic-gate 	}
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 	while (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
22310Sstevel@tonic-gate 		c = 0;
22320Sstevel@tonic-gate 		s = 0;				/* reset error status */
22330Sstevel@tonic-gate 		if (lsr & RCA) {
22341106Smrj 			c = ddi_get8(asy->asy_iohandle,
22350Sstevel@tonic-gate 				asy->asy_ioaddr + DAT) & 0xff;
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 			/*
22380Sstevel@tonic-gate 			 * We handle XON/XOFF char if IXON is set,
22390Sstevel@tonic-gate 			 * but if received char is _POSIX_VDISABLE,
22400Sstevel@tonic-gate 			 * we left it to the up level module.
22410Sstevel@tonic-gate 			 */
22420Sstevel@tonic-gate 			if (tp->t_iflag & IXON) {
22430Sstevel@tonic-gate 				if ((c == async->async_stopc) &&
22440Sstevel@tonic-gate 				    (c != _POSIX_VDISABLE)) {
22450Sstevel@tonic-gate 					async_flowcontrol_sw_output(asy,
22460Sstevel@tonic-gate 					    FLOW_STOP);
22470Sstevel@tonic-gate 					goto check_looplim;
22480Sstevel@tonic-gate 				} else if ((c == async->async_startc) &&
22490Sstevel@tonic-gate 				    (c != _POSIX_VDISABLE)) {
22500Sstevel@tonic-gate 					async_flowcontrol_sw_output(asy,
22510Sstevel@tonic-gate 					    FLOW_START);
22520Sstevel@tonic-gate 					needsoft = 1;
22530Sstevel@tonic-gate 					goto check_looplim;
22540Sstevel@tonic-gate 				}
22550Sstevel@tonic-gate 				if ((tp->t_iflag & IXANY) &&
22560Sstevel@tonic-gate 				    (async->async_flags & ASYNC_SW_OUT_FLW)) {
22570Sstevel@tonic-gate 					async_flowcontrol_sw_output(asy,
22580Sstevel@tonic-gate 					    FLOW_START);
22590Sstevel@tonic-gate 					needsoft = 1;
22600Sstevel@tonic-gate 				}
22610Sstevel@tonic-gate 			}
22620Sstevel@tonic-gate 		}
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 		/*
22650Sstevel@tonic-gate 		 * Check for character break sequence
22660Sstevel@tonic-gate 		 */
22670Sstevel@tonic-gate 		if ((abort_enable == KIOCABORTALTERNATE) &&
22680Sstevel@tonic-gate 		    (asy->asy_flags & ASY_CONSOLE)) {
22690Sstevel@tonic-gate 			if (abort_charseq_recognize(c))
22700Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
22710Sstevel@tonic-gate 		}
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 		/* Handle framing errors */
22740Sstevel@tonic-gate 		if (lsr & (PARERR|FRMERR|BRKDET|OVRRUN)) {
22750Sstevel@tonic-gate 			if (lsr & PARERR) {
22760Sstevel@tonic-gate 				if (tp->t_iflag & INPCK) /* parity enabled */
22770Sstevel@tonic-gate 					s |= PERROR;
22780Sstevel@tonic-gate 			}
22790Sstevel@tonic-gate 
22800Sstevel@tonic-gate 			if (lsr & (FRMERR|BRKDET))
22810Sstevel@tonic-gate 				s |= FRERROR;
22820Sstevel@tonic-gate 			if (lsr & OVRRUN) {
22830Sstevel@tonic-gate 				async->async_hw_overrun = 1;
22840Sstevel@tonic-gate 				s |= OVERRUN;
22850Sstevel@tonic-gate 			}
22860Sstevel@tonic-gate 		}
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 		if (s == 0)
22890Sstevel@tonic-gate 			if ((tp->t_iflag & PARMRK) &&
22900Sstevel@tonic-gate 			    !(tp->t_iflag & (IGNPAR|ISTRIP)) &&
22910Sstevel@tonic-gate 			    (c == 0377))
22920Sstevel@tonic-gate 				if (RING_POK(async, 2)) {
22930Sstevel@tonic-gate 					RING_PUT(async, 0377);
22940Sstevel@tonic-gate 					RING_PUT(async, c);
22950Sstevel@tonic-gate 				} else
22960Sstevel@tonic-gate 					async->async_sw_overrun = 1;
22970Sstevel@tonic-gate 			else
22980Sstevel@tonic-gate 				if (RING_POK(async, 1))
22990Sstevel@tonic-gate 					RING_PUT(async, c);
23000Sstevel@tonic-gate 				else
23010Sstevel@tonic-gate 					async->async_sw_overrun = 1;
23020Sstevel@tonic-gate 		else
23030Sstevel@tonic-gate 			if (s & FRERROR) /* Handle framing errors */
23040Sstevel@tonic-gate 				if (c == 0)
23050Sstevel@tonic-gate 					if ((asy->asy_flags & ASY_CONSOLE) &&
23060Sstevel@tonic-gate 					    (abort_enable !=
23070Sstevel@tonic-gate 					    KIOCABORTALTERNATE))
23080Sstevel@tonic-gate 						abort_sequence_enter((char *)0);
23090Sstevel@tonic-gate 					else
23100Sstevel@tonic-gate 						async->async_break++;
23110Sstevel@tonic-gate 				else
23120Sstevel@tonic-gate 					if (RING_POK(async, 1))
23130Sstevel@tonic-gate 						RING_MARK(async, c, s);
23140Sstevel@tonic-gate 					else
23150Sstevel@tonic-gate 						async->async_sw_overrun = 1;
23160Sstevel@tonic-gate 			else /* Parity errors are handled by ldterm */
23170Sstevel@tonic-gate 				if (RING_POK(async, 1))
23180Sstevel@tonic-gate 					RING_MARK(async, c, s);
23190Sstevel@tonic-gate 				else
23200Sstevel@tonic-gate 					async->async_sw_overrun = 1;
23210Sstevel@tonic-gate check_looplim:
23221106Smrj 		lsr = ddi_get8(asy->asy_iohandle,
23230Sstevel@tonic-gate 			asy->asy_ioaddr + LSR);
23240Sstevel@tonic-gate 		if (looplim-- < 0)		/* limit loop */
23250Sstevel@tonic-gate 			break;
23260Sstevel@tonic-gate 	}
23270Sstevel@tonic-gate 	if ((RING_CNT(async) > (RINGSIZE * 3)/4) &&
23280Sstevel@tonic-gate 	    !(async->async_inflow_source & IN_FLOW_RINGBUFF)) {
23290Sstevel@tonic-gate 		async_flowcontrol_hw_input(asy, FLOW_STOP, IN_FLOW_RINGBUFF);
23300Sstevel@tonic-gate 		(void) async_flowcontrol_sw_input(asy, FLOW_STOP,
23310Sstevel@tonic-gate 		    IN_FLOW_RINGBUFF);
23320Sstevel@tonic-gate 	}
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 	if ((async->async_flags & ASYNC_SERVICEIMM) || needsoft ||
23350Sstevel@tonic-gate 	    (RING_FRAC(async)) || (async->async_polltid == 0))
23360Sstevel@tonic-gate 		ASYSETSOFT(asy);	/* need a soft interrupt */
23370Sstevel@tonic-gate }
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate /*
23400Sstevel@tonic-gate  * Modem status interrupt.
23410Sstevel@tonic-gate  *
23420Sstevel@tonic-gate  * (Note: It is assumed that the MSR hasn't been read by asyintr().)
23430Sstevel@tonic-gate  */
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate static void
23460Sstevel@tonic-gate async_msint(struct asycom *asy)
23470Sstevel@tonic-gate {
23480Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
23490Sstevel@tonic-gate 	int msr, t_cflag = async->async_ttycommon.t_cflag;
23500Sstevel@tonic-gate #ifdef DEBUG
23510Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
23520Sstevel@tonic-gate #endif
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate async_msint_retry:
23550Sstevel@tonic-gate 	/* this resets the interrupt */
23561106Smrj 	msr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MSR);
23570Sstevel@tonic-gate 	DEBUGCONT10(ASY_DEBUG_STATE,
23580Sstevel@tonic-gate 		"async%d_msint call #%d:\n"
23590Sstevel@tonic-gate 		"   transition: %3s %3s %3s %3s\n"
23600Sstevel@tonic-gate 		"current state: %3s %3s %3s %3s\n",
23610Sstevel@tonic-gate 		instance,
23620Sstevel@tonic-gate 		++(asy->asy_msint_cnt),
23630Sstevel@tonic-gate 		(msr & DCTS) ? "DCTS" : "    ",
23640Sstevel@tonic-gate 		(msr & DDSR) ? "DDSR" : "    ",
23650Sstevel@tonic-gate 		(msr & DRI)  ? "DRI " : "    ",
23660Sstevel@tonic-gate 		(msr & DDCD) ? "DDCD" : "    ",
23670Sstevel@tonic-gate 		(msr & CTS)  ? "CTS " : "    ",
23680Sstevel@tonic-gate 		(msr & DSR)  ? "DSR " : "    ",
23690Sstevel@tonic-gate 		(msr & RI)   ? "RI  " : "    ",
23700Sstevel@tonic-gate 		(msr & DCD)  ? "DCD " : "    ");
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 	/* If CTS status is changed, do H/W output flow control */
23730Sstevel@tonic-gate 	if ((t_cflag & CRTSCTS) && (((asy->asy_msr ^ msr) & CTS) != 0))
23740Sstevel@tonic-gate 		async_flowcontrol_hw_output(asy,
23750Sstevel@tonic-gate 		    msr & CTS ? FLOW_START : FLOW_STOP);
23760Sstevel@tonic-gate 	/*
23770Sstevel@tonic-gate 	 * Reading MSR resets the interrupt, we save the
23780Sstevel@tonic-gate 	 * value of msr so that other functions could examine MSR by
23790Sstevel@tonic-gate 	 * looking at asy_msr.
23800Sstevel@tonic-gate 	 */
23810Sstevel@tonic-gate 	asy->asy_msr = (uchar_t)msr;
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 	/* Handle PPS event */
23840Sstevel@tonic-gate 	if (asy->asy_flags & ASY_PPS)
23850Sstevel@tonic-gate 		asy_ppsevent(asy, msr);
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 	async->async_ext++;
23880Sstevel@tonic-gate 	ASYSETSOFT(asy);
23890Sstevel@tonic-gate 	/*
23900Sstevel@tonic-gate 	 * We will make sure that the modem status presented to us
23910Sstevel@tonic-gate 	 * during the previous read has not changed. If the chip samples
23920Sstevel@tonic-gate 	 * the modem status on the falling edge of the interrupt line,
23930Sstevel@tonic-gate 	 * and uses this state as the base for detecting change of modem
23940Sstevel@tonic-gate 	 * status, we would miss a change of modem status event that occured
23950Sstevel@tonic-gate 	 * after we initiated a read MSR operation.
23960Sstevel@tonic-gate 	 */
23971106Smrj 	msr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MSR);
23980Sstevel@tonic-gate 	if (STATES(msr) != STATES(asy->asy_msr))
23990Sstevel@tonic-gate 		goto	async_msint_retry;
24000Sstevel@tonic-gate }
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate /*
24030Sstevel@tonic-gate  * Handle a second-stage interrupt.
24040Sstevel@tonic-gate  */
24050Sstevel@tonic-gate /*ARGSUSED*/
24060Sstevel@tonic-gate uint_t
24070Sstevel@tonic-gate asysoftintr(caddr_t intarg)
24080Sstevel@tonic-gate {
24090Sstevel@tonic-gate 	struct asycom *asy;
24100Sstevel@tonic-gate 	int rv;
24110Sstevel@tonic-gate 	int instance;
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate 	/*
24140Sstevel@tonic-gate 	 * Test and clear soft interrupt.
24150Sstevel@tonic-gate 	 */
24160Sstevel@tonic-gate 	mutex_enter(&asy_soft_lock);
24170Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_PROCS, "asysoftintr: enter\n");
24180Sstevel@tonic-gate 	rv = asysoftpend;
24190Sstevel@tonic-gate 	if (rv != 0)
24200Sstevel@tonic-gate 		asysoftpend = 0;
24210Sstevel@tonic-gate 	mutex_exit(&asy_soft_lock);
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	if (rv) {
24240Sstevel@tonic-gate 		/*
24250Sstevel@tonic-gate 		 * Note - we can optimize the loop by remembering the last
24260Sstevel@tonic-gate 		 * device that requested soft interrupt
24270Sstevel@tonic-gate 		 */
24280Sstevel@tonic-gate 		for (instance = 0; instance <= max_asy_instance; instance++) {
24290Sstevel@tonic-gate 			asy = ddi_get_soft_state(asy_soft_state, instance);
24300Sstevel@tonic-gate 			if (asy == NULL || asy->asy_priv == NULL)
24310Sstevel@tonic-gate 				continue;
24320Sstevel@tonic-gate 			mutex_enter(&asy_soft_lock);
24330Sstevel@tonic-gate 			if (asy->asy_flags & ASY_NEEDSOFT) {
24340Sstevel@tonic-gate 				asy->asy_flags &= ~ASY_NEEDSOFT;
24350Sstevel@tonic-gate 				mutex_exit(&asy_soft_lock);
24360Sstevel@tonic-gate 				async_softint(asy);
24370Sstevel@tonic-gate 			} else
24380Sstevel@tonic-gate 				mutex_exit(&asy_soft_lock);
24390Sstevel@tonic-gate 		}
24400Sstevel@tonic-gate 	}
24410Sstevel@tonic-gate 	return (rv ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED);
24420Sstevel@tonic-gate }
24430Sstevel@tonic-gate 
24440Sstevel@tonic-gate /*
24450Sstevel@tonic-gate  * Handle a software interrupt.
24460Sstevel@tonic-gate  */
24470Sstevel@tonic-gate static void
24480Sstevel@tonic-gate async_softint(struct asycom *asy)
24490Sstevel@tonic-gate {
24500Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
24510Sstevel@tonic-gate 	short	cc;
24520Sstevel@tonic-gate 	mblk_t	*bp;
24530Sstevel@tonic-gate 	queue_t	*q;
24540Sstevel@tonic-gate 	uchar_t	val;
24550Sstevel@tonic-gate 	uchar_t	c;
24560Sstevel@tonic-gate 	tty_common_t	*tp;
24570Sstevel@tonic-gate 	int nb;
24580Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_softint\n", instance);
24610Sstevel@tonic-gate 	mutex_enter(&asy_soft_lock);
24620Sstevel@tonic-gate 	if (asy->asy_flags & ASY_DOINGSOFT) {
24630Sstevel@tonic-gate 		asy->asy_flags |= ASY_DOINGSOFT_RETRY;
24640Sstevel@tonic-gate 		mutex_exit(&asy_soft_lock);
24650Sstevel@tonic-gate 		return;
24660Sstevel@tonic-gate 	}
24670Sstevel@tonic-gate 	asy->asy_flags |= ASY_DOINGSOFT;
24680Sstevel@tonic-gate begin:
24690Sstevel@tonic-gate 	asy->asy_flags &= ~ASY_DOINGSOFT_RETRY;
24700Sstevel@tonic-gate 	mutex_exit(&asy_soft_lock);
24710Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
24720Sstevel@tonic-gate 	tp = &async->async_ttycommon;
24730Sstevel@tonic-gate 	q = tp->t_readq;
24740Sstevel@tonic-gate 	if (async->async_flags & ASYNC_OUT_FLW_RESUME) {
24750Sstevel@tonic-gate 		if (async->async_ocnt > 0) {
24760Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
24770Sstevel@tonic-gate 			async_resume(async);
24780Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
24790Sstevel@tonic-gate 		} else {
24800Sstevel@tonic-gate 			if (async->async_xmitblk)
24810Sstevel@tonic-gate 				freeb(async->async_xmitblk);
24820Sstevel@tonic-gate 			async->async_xmitblk = NULL;
24830Sstevel@tonic-gate 			async_start(async);
24840Sstevel@tonic-gate 		}
24850Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
24860Sstevel@tonic-gate 	}
24870Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
24880Sstevel@tonic-gate 	if (async->async_ext) {
24890Sstevel@tonic-gate 		async->async_ext = 0;
24900Sstevel@tonic-gate 		/* check for carrier up */
24910Sstevel@tonic-gate 		DEBUGCONT3(ASY_DEBUG_MODM2,
24920Sstevel@tonic-gate 			"async%d_softint: asy_msr & DCD = %x, "
24930Sstevel@tonic-gate 			"tp->t_flags & TS_SOFTCAR = %x\n",
24940Sstevel@tonic-gate 			instance,
24950Sstevel@tonic-gate 			asy->asy_msr & DCD,
24960Sstevel@tonic-gate 			tp->t_flags & TS_SOFTCAR);
24970Sstevel@tonic-gate 		if (asy->asy_msr & DCD) {
24980Sstevel@tonic-gate 			/* carrier present */
24990Sstevel@tonic-gate 			if ((async->async_flags & ASYNC_CARR_ON) == 0) {
25000Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_MODM2,
25010Sstevel@tonic-gate 					"async%d_softint: set ASYNC_CARR_ON\n",
25020Sstevel@tonic-gate 					instance);
25030Sstevel@tonic-gate 				async->async_flags |= ASYNC_CARR_ON;
25040Sstevel@tonic-gate 				if (async->async_flags & ASYNC_ISOPEN) {
25050Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
25060Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
25070Sstevel@tonic-gate 					(void) putctl(q, M_UNHANGUP);
25080Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
25090Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
25100Sstevel@tonic-gate 				}
25110Sstevel@tonic-gate 				cv_broadcast(&async->async_flags_cv);
25120Sstevel@tonic-gate 			}
25130Sstevel@tonic-gate 		} else {
25140Sstevel@tonic-gate 			if ((async->async_flags & ASYNC_CARR_ON) &&
25150Sstevel@tonic-gate 			    !(tp->t_cflag & CLOCAL) &&
25160Sstevel@tonic-gate 			    !(tp->t_flags & TS_SOFTCAR)) {
25170Sstevel@tonic-gate 				int flushflag;
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_MODEM,
25200Sstevel@tonic-gate 					"async%d_softint: carrier dropped, "
25210Sstevel@tonic-gate 					"so drop DTR\n",
25220Sstevel@tonic-gate 					instance);
25230Sstevel@tonic-gate 				/*
25240Sstevel@tonic-gate 				 * Carrier went away.
25250Sstevel@tonic-gate 				 * Drop DTR, abort any output in
25260Sstevel@tonic-gate 				 * progress, indicate that output is
25270Sstevel@tonic-gate 				 * not stopped, and send a hangup
25280Sstevel@tonic-gate 				 * notification upstream.
25290Sstevel@tonic-gate 				 */
25301106Smrj 				val = ddi_get8(asy->asy_iohandle,
25310Sstevel@tonic-gate 					asy->asy_ioaddr + MCR);
25321106Smrj 				ddi_put8(asy->asy_iohandle,
25330Sstevel@tonic-gate 				    asy->asy_ioaddr + MCR, (val & ~DTR));
25340Sstevel@tonic-gate 				if (async->async_flags & ASYNC_BUSY) {
25350Sstevel@tonic-gate 				    DEBUGCONT0(ASY_DEBUG_BUSY,
25360Sstevel@tonic-gate 					    "async_softint: "
25370Sstevel@tonic-gate 					    "Carrier dropped.  "
25380Sstevel@tonic-gate 					    "Clearing async_ocnt\n");
25390Sstevel@tonic-gate 				    async->async_ocnt = 0;
25400Sstevel@tonic-gate 				}	/* if */
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_STOPPED;
25430Sstevel@tonic-gate 				if (async->async_flags & ASYNC_ISOPEN) {
25440Sstevel@tonic-gate 				    mutex_exit(&asy->asy_excl_hi);
25450Sstevel@tonic-gate 				    mutex_exit(&asy->asy_excl);
25460Sstevel@tonic-gate 				    (void) putctl(q, M_HANGUP);
25470Sstevel@tonic-gate 				    mutex_enter(&asy->asy_excl);
25480Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_MODEM,
25490Sstevel@tonic-gate 					"async%d_softint: "
25500Sstevel@tonic-gate 					"putctl(q, M_HANGUP)\n",
25510Sstevel@tonic-gate 					instance);
25520Sstevel@tonic-gate 				/*
25530Sstevel@tonic-gate 				 * Flush FIFO buffers
25540Sstevel@tonic-gate 				 * Any data left in there is invalid now
25550Sstevel@tonic-gate 				 */
25560Sstevel@tonic-gate 				if (asy->asy_use_fifo == FIFO_ON)
25570Sstevel@tonic-gate 					asy_reset_fifo(asy, FIFOTXFLSH);
25580Sstevel@tonic-gate 				/*
25590Sstevel@tonic-gate 				 * Flush our write queue if we have one.
25600Sstevel@tonic-gate 				 *
25610Sstevel@tonic-gate 				 * If we're in the midst of close, then flush
25620Sstevel@tonic-gate 				 * everything.  Don't leave stale ioctls lying
25630Sstevel@tonic-gate 				 * about.
25640Sstevel@tonic-gate 				 */
25650Sstevel@tonic-gate 				flushflag = (async->async_flags &
25660Sstevel@tonic-gate 				    ASYNC_CLOSING) ? FLUSHALL : FLUSHDATA;
25670Sstevel@tonic-gate 				flushq(tp->t_writeq, flushflag);
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 				bp = async->async_xmitblk; /* active msg */
25700Sstevel@tonic-gate 				if (bp != NULL) {
25710Sstevel@tonic-gate 					freeb(bp);
25720Sstevel@tonic-gate 					async->async_xmitblk = NULL;
25730Sstevel@tonic-gate 				}
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
25760Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_BUSY;
25770Sstevel@tonic-gate 				/*
25780Sstevel@tonic-gate 				 * This message warns of Carrier loss
25790Sstevel@tonic-gate 				 * with data left to transmit can hang the
25800Sstevel@tonic-gate 				 * system.
25810Sstevel@tonic-gate 				 */
25820Sstevel@tonic-gate 				DEBUGCONT0(ASY_DEBUG_MODEM,
25830Sstevel@tonic-gate 					"async_softint: Flushing to "
25840Sstevel@tonic-gate 					"prevent HUPCL hanging\n");
25850Sstevel@tonic-gate 				}	/* if (ASYNC_ISOPEN) */
25860Sstevel@tonic-gate 			}	/* if (ASYNC_CARR_ON && CLOCAL) */
25870Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_CARR_ON;
25880Sstevel@tonic-gate 			cv_broadcast(&async->async_flags_cv);
25890Sstevel@tonic-gate 		}	/* else */
25900Sstevel@tonic-gate 	}	/* if (async->async_ext) */
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate 	/*
25950Sstevel@tonic-gate 	 * If data has been added to the circular buffer, remove
25960Sstevel@tonic-gate 	 * it from the buffer, and send it up the stream if there's
25970Sstevel@tonic-gate 	 * somebody listening. Try to do it 16 bytes at a time. If we
25980Sstevel@tonic-gate 	 * have more than 16 bytes to move, move 16 byte chunks and
25990Sstevel@tonic-gate 	 * leave the rest for next time around (maybe it will grow).
26000Sstevel@tonic-gate 	 */
26010Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
26020Sstevel@tonic-gate 	if (!(async->async_flags & ASYNC_ISOPEN)) {
26030Sstevel@tonic-gate 		RING_INIT(async);
26040Sstevel@tonic-gate 		goto rv;
26050Sstevel@tonic-gate 	}
26060Sstevel@tonic-gate 	if ((cc = RING_CNT(async)) <= 0)
26070Sstevel@tonic-gate 		goto rv;
26080Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate 	if (!canput(q)) {
26110Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
26120Sstevel@tonic-gate 		if (!(async->async_inflow_source & IN_FLOW_STREAMS)) {
26130Sstevel@tonic-gate 			async_flowcontrol_hw_input(asy, FLOW_STOP,
26140Sstevel@tonic-gate 			    IN_FLOW_STREAMS);
26150Sstevel@tonic-gate 			(void) async_flowcontrol_sw_input(asy, FLOW_STOP,
26160Sstevel@tonic-gate 			    IN_FLOW_STREAMS);
26170Sstevel@tonic-gate 		}
26180Sstevel@tonic-gate 		goto rv;
26190Sstevel@tonic-gate 	}
26200Sstevel@tonic-gate 	if (async->async_inflow_source & IN_FLOW_STREAMS) {
26210Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
26220Sstevel@tonic-gate 		async_flowcontrol_hw_input(asy, FLOW_START,
26230Sstevel@tonic-gate 		    IN_FLOW_STREAMS);
26240Sstevel@tonic-gate 		(void) async_flowcontrol_sw_input(asy, FLOW_START,
26250Sstevel@tonic-gate 		    IN_FLOW_STREAMS);
26260Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
26270Sstevel@tonic-gate 	}
26280Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_INPUT,
26290Sstevel@tonic-gate 		"async%d_softint: %d char(s) in queue.\n", instance, cc);
26300Sstevel@tonic-gate 	if (!(bp = allocb(cc, BPRI_MED))) {
26310Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
26320Sstevel@tonic-gate 		ttycommon_qfull(&async->async_ttycommon, q);
26330Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
26340Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
26350Sstevel@tonic-gate 		goto rv;
26360Sstevel@tonic-gate 	}
26370Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
26380Sstevel@tonic-gate 	do {
26390Sstevel@tonic-gate 		if (RING_ERR(async, S_ERRORS)) {
26400Sstevel@tonic-gate 			RING_UNMARK(async);
26410Sstevel@tonic-gate 			c = RING_GET(async);
26420Sstevel@tonic-gate 			break;
26430Sstevel@tonic-gate 		} else
26440Sstevel@tonic-gate 			*bp->b_wptr++ = RING_GET(async);
26450Sstevel@tonic-gate 	} while (--cc);
26460Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
26470Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
26480Sstevel@tonic-gate 	if (bp->b_wptr > bp->b_rptr) {
26490Sstevel@tonic-gate 			if (!canput(q)) {
26500Sstevel@tonic-gate 				asyerror(CE_NOTE, "asy%d: local queue full",
26510Sstevel@tonic-gate 					instance);
26520Sstevel@tonic-gate 				freemsg(bp);
26530Sstevel@tonic-gate 			} else
26540Sstevel@tonic-gate 				(void) putq(q, bp);
26550Sstevel@tonic-gate 	} else
26560Sstevel@tonic-gate 		freemsg(bp);
26570Sstevel@tonic-gate 	/*
26580Sstevel@tonic-gate 	 * If we have a parity error, then send
26590Sstevel@tonic-gate 	 * up an M_BREAK with the "bad"
26600Sstevel@tonic-gate 	 * character as an argument. Let ldterm
26610Sstevel@tonic-gate 	 * figure out what to do with the error.
26620Sstevel@tonic-gate 	 */
26630Sstevel@tonic-gate 	if (cc) {
26640Sstevel@tonic-gate 		(void) putctl1(q, M_BREAK, c);
26650Sstevel@tonic-gate 		ASYSETSOFT(async->async_common);	/* finish cc chars */
26660Sstevel@tonic-gate 	}
26670Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
26680Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
26690Sstevel@tonic-gate rv:
26700Sstevel@tonic-gate 	if ((RING_CNT(async) < (RINGSIZE/4)) &&
26710Sstevel@tonic-gate 	    (async->async_inflow_source & IN_FLOW_RINGBUFF)) {
26720Sstevel@tonic-gate 		async_flowcontrol_hw_input(asy, FLOW_START, IN_FLOW_RINGBUFF);
26730Sstevel@tonic-gate 		(void) async_flowcontrol_sw_input(asy, FLOW_START,
26740Sstevel@tonic-gate 		    IN_FLOW_RINGBUFF);
26750Sstevel@tonic-gate 	}
26760Sstevel@tonic-gate 
26770Sstevel@tonic-gate 	/*
26780Sstevel@tonic-gate 	 * If a transmission has finished, indicate that it's finished,
26790Sstevel@tonic-gate 	 * and start that line up again.
26800Sstevel@tonic-gate 	 */
26810Sstevel@tonic-gate 	if (async->async_break > 0) {
26820Sstevel@tonic-gate 		nb = async->async_break;
26830Sstevel@tonic-gate 		async->async_break = 0;
26840Sstevel@tonic-gate 		if (async->async_flags & ASYNC_ISOPEN) {
26850Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
26860Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
26870Sstevel@tonic-gate 			for (; nb > 0; nb--)
26880Sstevel@tonic-gate 				(void) putctl(q, M_BREAK);
26890Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
26900Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
26910Sstevel@tonic-gate 		}
26920Sstevel@tonic-gate 	}
26930Sstevel@tonic-gate 	if (async->async_ocnt <= 0 && (async->async_flags & ASYNC_BUSY)) {
26940Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_BUSY,
26950Sstevel@tonic-gate 		    "async%d_softint: Clearing ASYNC_BUSY.  async_ocnt=%d\n",
26960Sstevel@tonic-gate 		    instance,
26970Sstevel@tonic-gate 		    async->async_ocnt);
26980Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_BUSY;
26990Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
27000Sstevel@tonic-gate 		if (async->async_xmitblk)
27010Sstevel@tonic-gate 			freeb(async->async_xmitblk);
27020Sstevel@tonic-gate 		async->async_xmitblk = NULL;
27030Sstevel@tonic-gate 		async_start(async);
27040Sstevel@tonic-gate 		/*
27050Sstevel@tonic-gate 		 * If the flag isn't set after doing the async_start above, we
27060Sstevel@tonic-gate 		 * may have finished all the queued output.  Signal any thread
27070Sstevel@tonic-gate 		 * stuck in close.
27080Sstevel@tonic-gate 		 */
27090Sstevel@tonic-gate 		if (!(async->async_flags & ASYNC_BUSY))
27100Sstevel@tonic-gate 			cv_broadcast(&async->async_flags_cv);
27110Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
27120Sstevel@tonic-gate 	}
27130Sstevel@tonic-gate 	/*
27140Sstevel@tonic-gate 	 * A note about these overrun bits: all they do is *tell* someone
27150Sstevel@tonic-gate 	 * about an error- They do not track multiple errors. In fact,
27160Sstevel@tonic-gate 	 * you could consider them latched register bits if you like.
27170Sstevel@tonic-gate 	 * We are only interested in printing the error message once for
27180Sstevel@tonic-gate 	 * any cluster of overrun errrors.
27190Sstevel@tonic-gate 	 */
27200Sstevel@tonic-gate 	if (async->async_hw_overrun) {
27210Sstevel@tonic-gate 		if (async->async_flags & ASYNC_ISOPEN) {
27220Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
27230Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
27240Sstevel@tonic-gate 			asyerror(CE_NOTE, "asy%d: silo overflow", instance);
27250Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
27260Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
27270Sstevel@tonic-gate 		}
27280Sstevel@tonic-gate 		async->async_hw_overrun = 0;
27290Sstevel@tonic-gate 	}
27300Sstevel@tonic-gate 	if (async->async_sw_overrun) {
27310Sstevel@tonic-gate 		if (async->async_flags & ASYNC_ISOPEN) {
27320Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
27330Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
27340Sstevel@tonic-gate 			asyerror(CE_NOTE, "asy%d: ring buffer overflow",
27350Sstevel@tonic-gate 				instance);
27360Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
27370Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
27380Sstevel@tonic-gate 		}
27390Sstevel@tonic-gate 		async->async_sw_overrun = 0;
27400Sstevel@tonic-gate 	}
27410Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
27420Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
27430Sstevel@tonic-gate 	mutex_enter(&asy_soft_lock);
27440Sstevel@tonic-gate 	if (asy->asy_flags & ASY_DOINGSOFT_RETRY) {
27450Sstevel@tonic-gate 		goto begin;
27460Sstevel@tonic-gate 	}
27470Sstevel@tonic-gate 	asy->asy_flags &= ~ASY_DOINGSOFT;
27480Sstevel@tonic-gate 	mutex_exit(&asy_soft_lock);
27490Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_softint: done\n", instance);
27500Sstevel@tonic-gate }
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate /*
27530Sstevel@tonic-gate  * Restart output on a line after a delay or break timer expired.
27540Sstevel@tonic-gate  */
27550Sstevel@tonic-gate static void
27560Sstevel@tonic-gate async_restart(void *arg)
27570Sstevel@tonic-gate {
27580Sstevel@tonic-gate 	struct asyncline *async = (struct asyncline *)arg;
27590Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
27600Sstevel@tonic-gate 	uchar_t lcr;
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 	/*
27630Sstevel@tonic-gate 	 * If break timer expired, turn off the break bit.
27640Sstevel@tonic-gate 	 */
27650Sstevel@tonic-gate #ifdef DEBUG
27660Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
27670Sstevel@tonic-gate 
27680Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_restart\n", instance);
27690Sstevel@tonic-gate #endif
27700Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
27710Sstevel@tonic-gate 	/*
27720Sstevel@tonic-gate 	 * If ASYNC_OUT_SUSPEND is also set, we don't really
27730Sstevel@tonic-gate 	 * clean the HW break, TIOCCBRK is responsible for this.
27740Sstevel@tonic-gate 	 */
27750Sstevel@tonic-gate 	if ((async->async_flags & ASYNC_BREAK) &&
27760Sstevel@tonic-gate 	    !(async->async_flags & ASYNC_OUT_SUSPEND)) {
27770Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
27781106Smrj 		lcr = ddi_get8(asy->asy_iohandle,
27790Sstevel@tonic-gate 			asy->asy_ioaddr + LCR);
27801106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
27810Sstevel@tonic-gate 			(lcr & ~SETBREAK));
27820Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
27830Sstevel@tonic-gate 	}
27840Sstevel@tonic-gate 	async->async_flags &= ~(ASYNC_DELAY|ASYNC_BREAK);
27850Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
27860Sstevel@tonic-gate 	async_start(async);
27870Sstevel@tonic-gate 
27880Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
27890Sstevel@tonic-gate }
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate static void
27920Sstevel@tonic-gate async_start(struct asyncline *async)
27930Sstevel@tonic-gate {
27940Sstevel@tonic-gate 	async_nstart(async, 0);
27950Sstevel@tonic-gate }
27960Sstevel@tonic-gate 
27970Sstevel@tonic-gate /*
27980Sstevel@tonic-gate  * Start output on a line, unless it's busy, frozen, or otherwise.
27990Sstevel@tonic-gate  */
28000Sstevel@tonic-gate /*ARGSUSED*/
28010Sstevel@tonic-gate static void
28020Sstevel@tonic-gate async_nstart(struct asyncline *async, int mode)
28030Sstevel@tonic-gate {
28040Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
28050Sstevel@tonic-gate 	int cc;
28060Sstevel@tonic-gate 	queue_t *q;
28070Sstevel@tonic-gate 	mblk_t *bp;
28080Sstevel@tonic-gate 	uchar_t *xmit_addr;
28090Sstevel@tonic-gate 	uchar_t	val;
28100Sstevel@tonic-gate 	int	fifo_len = 1;
28110Sstevel@tonic-gate 	boolean_t didsome;
28120Sstevel@tonic-gate 	mblk_t *nbp;
28130Sstevel@tonic-gate 
28140Sstevel@tonic-gate #ifdef DEBUG
28150Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
28160Sstevel@tonic-gate 
28170Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_nstart\n", instance);
28180Sstevel@tonic-gate #endif
28190Sstevel@tonic-gate 	if (asy->asy_use_fifo == FIFO_ON) {
28200Sstevel@tonic-gate 		fifo_len = asy->asy_fifo_buf; /* with FIFO buffers */
28210Sstevel@tonic-gate 		if (fifo_len > asy_max_tx_fifo)
28220Sstevel@tonic-gate 			fifo_len = asy_max_tx_fifo;
28230Sstevel@tonic-gate 	}
28240Sstevel@tonic-gate 
28250Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate 	/*
28280Sstevel@tonic-gate 	 * If the chip is busy (i.e., we're waiting for a break timeout
28290Sstevel@tonic-gate 	 * to expire, or for the current transmission to finish, or for
28300Sstevel@tonic-gate 	 * output to finish draining from chip), don't grab anything new.
28310Sstevel@tonic-gate 	 */
28320Sstevel@tonic-gate 	if (async->async_flags & (ASYNC_BREAK|ASYNC_BUSY)) {
28330Sstevel@tonic-gate 		DEBUGCONT2((mode? ASY_DEBUG_OUT : 0),
28340Sstevel@tonic-gate 			"async%d_nstart: start %s.\n",
28350Sstevel@tonic-gate 			instance,
28360Sstevel@tonic-gate 			async->async_flags & ASYNC_BREAK ? "break" : "busy");
28370Sstevel@tonic-gate 		return;
28380Sstevel@tonic-gate 	}
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 	/*
28410Sstevel@tonic-gate 	 * Check only pended sw input flow control.
28420Sstevel@tonic-gate 	 */
28430Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
28440Sstevel@tonic-gate 	if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
28450Sstevel@tonic-gate 		fifo_len--;
28460Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate 	/*
28490Sstevel@tonic-gate 	 * If we're waiting for a delay timeout to expire, don't grab
28500Sstevel@tonic-gate 	 * anything new.
28510Sstevel@tonic-gate 	 */
28520Sstevel@tonic-gate 	if (async->async_flags & ASYNC_DELAY) {
28530Sstevel@tonic-gate 		DEBUGCONT1((mode? ASY_DEBUG_OUT : 0),
28540Sstevel@tonic-gate 			"async%d_nstart: start ASYNC_DELAY.\n", instance);
28550Sstevel@tonic-gate 		return;
28560Sstevel@tonic-gate 	}
28570Sstevel@tonic-gate 
28580Sstevel@tonic-gate 	if ((q = async->async_ttycommon.t_writeq) == NULL) {
28590Sstevel@tonic-gate 		DEBUGCONT1((mode? ASY_DEBUG_OUT : 0),
28600Sstevel@tonic-gate 			"async%d_nstart: start writeq is null.\n", instance);
28610Sstevel@tonic-gate 		return;	/* not attached to a stream */
28620Sstevel@tonic-gate 	}
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 	for (;;) {
28650Sstevel@tonic-gate 		if ((bp = getq(q)) == NULL)
28660Sstevel@tonic-gate 			return;	/* no data to transmit */
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 		/*
28690Sstevel@tonic-gate 		 * We have a message block to work on.
28700Sstevel@tonic-gate 		 * Check whether it's a break, a delay, or an ioctl (the latter
28710Sstevel@tonic-gate 		 * occurs if the ioctl in question was waiting for the output
28720Sstevel@tonic-gate 		 * to drain).  If it's one of those, process it immediately.
28730Sstevel@tonic-gate 		 */
28740Sstevel@tonic-gate 		switch (bp->b_datap->db_type) {
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate 		case M_BREAK:
28770Sstevel@tonic-gate 			/*
28780Sstevel@tonic-gate 			 * Set the break bit, and arrange for "async_restart"
28790Sstevel@tonic-gate 			 * to be called in 1/4 second; it will turn the
28800Sstevel@tonic-gate 			 * break bit off, and call "async_start" to grab
28810Sstevel@tonic-gate 			 * the next message.
28820Sstevel@tonic-gate 			 */
28830Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
28841106Smrj 			val = ddi_get8(asy->asy_iohandle,
28850Sstevel@tonic-gate 				asy->asy_ioaddr + LCR);
28861106Smrj 			ddi_put8(asy->asy_iohandle,
28870Sstevel@tonic-gate 				asy->asy_ioaddr + LCR, (val | SETBREAK));
28880Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
28890Sstevel@tonic-gate 			async->async_flags |= ASYNC_BREAK;
28900Sstevel@tonic-gate 			(void) timeout(async_restart, (caddr_t)async,
28910Sstevel@tonic-gate 			    drv_usectohz(1000000)/4);
28920Sstevel@tonic-gate 			freemsg(bp);
28930Sstevel@tonic-gate 			return;	/* wait for this to finish */
28940Sstevel@tonic-gate 
28950Sstevel@tonic-gate 		case M_DELAY:
28960Sstevel@tonic-gate 			/*
28970Sstevel@tonic-gate 			 * Arrange for "async_restart" to be called when the
28980Sstevel@tonic-gate 			 * delay expires; it will turn ASYNC_DELAY off,
28990Sstevel@tonic-gate 			 * and call "async_start" to grab the next message.
29000Sstevel@tonic-gate 			 */
29010Sstevel@tonic-gate 			(void) timeout(async_restart, (caddr_t)async,
29020Sstevel@tonic-gate 			    (int)(*(unsigned char *)bp->b_rptr + 6));
29030Sstevel@tonic-gate 			async->async_flags |= ASYNC_DELAY;
29040Sstevel@tonic-gate 			freemsg(bp);
29050Sstevel@tonic-gate 			return;	/* wait for this to finish */
29060Sstevel@tonic-gate 
29070Sstevel@tonic-gate 		case M_IOCTL:
29080Sstevel@tonic-gate 			/*
29090Sstevel@tonic-gate 			 * This ioctl was waiting for the output ahead of
29100Sstevel@tonic-gate 			 * it to drain; obviously, it has.  Do it, and
29110Sstevel@tonic-gate 			 * then grab the next message after it.
29120Sstevel@tonic-gate 			 */
29130Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
29140Sstevel@tonic-gate 			async_ioctl(async, q, bp);
29150Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
29160Sstevel@tonic-gate 			continue;
29170Sstevel@tonic-gate 		}
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 		while (bp != NULL && (cc = bp->b_wptr - bp->b_rptr) == 0) {
29200Sstevel@tonic-gate 			nbp = bp->b_cont;
29210Sstevel@tonic-gate 			freeb(bp);
29220Sstevel@tonic-gate 			bp = nbp;
29230Sstevel@tonic-gate 		}
29240Sstevel@tonic-gate 		if (bp != NULL)
29250Sstevel@tonic-gate 			break;
29260Sstevel@tonic-gate 	}
29270Sstevel@tonic-gate 
29280Sstevel@tonic-gate 	/*
29290Sstevel@tonic-gate 	 * We have data to transmit.  If output is stopped, put
29300Sstevel@tonic-gate 	 * it back and try again later.
29310Sstevel@tonic-gate 	 */
29320Sstevel@tonic-gate 	if (async->async_flags & (ASYNC_HW_OUT_FLW | ASYNC_SW_OUT_FLW |
29330Sstevel@tonic-gate 	    ASYNC_STOPPED | ASYNC_OUT_SUSPEND)) {
29340Sstevel@tonic-gate 		(void) putbq(q, bp);
29350Sstevel@tonic-gate 		return;
29360Sstevel@tonic-gate 	}
29370Sstevel@tonic-gate 
29380Sstevel@tonic-gate 	async->async_xmitblk = bp;
29390Sstevel@tonic-gate 	xmit_addr = bp->b_rptr;
29400Sstevel@tonic-gate 	bp = bp->b_cont;
29410Sstevel@tonic-gate 	if (bp != NULL)
29420Sstevel@tonic-gate 		(void) putbq(q, bp);	/* not done with this message yet */
29430Sstevel@tonic-gate 
29440Sstevel@tonic-gate 	/*
29450Sstevel@tonic-gate 	 * In 5-bit mode, the high order bits are used
29460Sstevel@tonic-gate 	 * to indicate character sizes less than five,
29470Sstevel@tonic-gate 	 * so we need to explicitly mask before transmitting
29480Sstevel@tonic-gate 	 */
29490Sstevel@tonic-gate 	if ((async->async_ttycommon.t_cflag & CSIZE) == CS5) {
29500Sstevel@tonic-gate 		unsigned char *p = xmit_addr;
29510Sstevel@tonic-gate 		int cnt = cc;
29520Sstevel@tonic-gate 
29530Sstevel@tonic-gate 		while (cnt--)
29540Sstevel@tonic-gate 			*p++ &= (unsigned char) 0x1f;
29550Sstevel@tonic-gate 	}
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 	/*
29580Sstevel@tonic-gate 	 * Set up this block for pseudo-DMA.
29590Sstevel@tonic-gate 	 */
29600Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
29610Sstevel@tonic-gate 	/*
29620Sstevel@tonic-gate 	 * If the transmitter is ready, shove the first
29630Sstevel@tonic-gate 	 * character out.
29640Sstevel@tonic-gate 	 */
29650Sstevel@tonic-gate 	didsome = B_FALSE;
29660Sstevel@tonic-gate 	while (--fifo_len >= 0 && cc > 0) {
29671106Smrj 		if (!(ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) &
29680Sstevel@tonic-gate 		    XHRE))
29690Sstevel@tonic-gate 			break;
29701106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
29710Sstevel@tonic-gate 		    *xmit_addr++);
29720Sstevel@tonic-gate 		cc--;
29730Sstevel@tonic-gate 		didsome = B_TRUE;
29740Sstevel@tonic-gate 	}
29750Sstevel@tonic-gate 	async->async_optr = xmit_addr;
29760Sstevel@tonic-gate 	async->async_ocnt = cc;
29770Sstevel@tonic-gate 	if (didsome)
29780Sstevel@tonic-gate 		async->async_flags |= ASYNC_PROGRESS;
29790Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_BUSY,
29800Sstevel@tonic-gate 		"async%d_nstart: Set ASYNC_BUSY.  async_ocnt=%d\n",
29810Sstevel@tonic-gate 		instance,
29820Sstevel@tonic-gate 		async->async_ocnt);
29830Sstevel@tonic-gate 	async->async_flags |= ASYNC_BUSY;
29840Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
29850Sstevel@tonic-gate }
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate /*
29880Sstevel@tonic-gate  * Resume output by poking the transmitter.
29890Sstevel@tonic-gate  */
29900Sstevel@tonic-gate static void
29910Sstevel@tonic-gate async_resume(struct asyncline *async)
29920Sstevel@tonic-gate {
29930Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
29940Sstevel@tonic-gate #ifdef DEBUG
29950Sstevel@tonic-gate 	int instance;
29960Sstevel@tonic-gate #endif
29970Sstevel@tonic-gate 
29980Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
29990Sstevel@tonic-gate #ifdef DEBUG
30000Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
30010Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_resume\n", instance);
30020Sstevel@tonic-gate #endif
30030Sstevel@tonic-gate 
30041106Smrj 	if (ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) & XHRE) {
30050Sstevel@tonic-gate 		if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
30060Sstevel@tonic-gate 			return;
30070Sstevel@tonic-gate 		if (async->async_ocnt > 0 &&
30080Sstevel@tonic-gate 		    !(async->async_flags &
30090Sstevel@tonic-gate 		    (ASYNC_HW_OUT_FLW|ASYNC_SW_OUT_FLW|ASYNC_OUT_SUSPEND))) {
30101106Smrj 			ddi_put8(asy->asy_iohandle,
30110Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT, *async->async_optr++);
30120Sstevel@tonic-gate 			async->async_ocnt--;
30130Sstevel@tonic-gate 			async->async_flags |= ASYNC_PROGRESS;
30140Sstevel@tonic-gate 		}
30150Sstevel@tonic-gate 	}
30160Sstevel@tonic-gate }
30170Sstevel@tonic-gate 
30180Sstevel@tonic-gate /*
30190Sstevel@tonic-gate  * Hold the untimed break to last the minimum time.
30200Sstevel@tonic-gate  */
30210Sstevel@tonic-gate static void
30220Sstevel@tonic-gate async_hold_utbrk(void *arg)
30230Sstevel@tonic-gate {
30240Sstevel@tonic-gate 	struct asyncline *async = arg;
30250Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
30260Sstevel@tonic-gate 
30270Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
30280Sstevel@tonic-gate 	async->async_flags &= ~ASYNC_HOLD_UTBRK;
30290Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
30300Sstevel@tonic-gate 	async->async_utbrktid = 0;
30310Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
30320Sstevel@tonic-gate }
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate /*
30350Sstevel@tonic-gate  * Resume the untimed break.
30360Sstevel@tonic-gate  */
30370Sstevel@tonic-gate static void
30380Sstevel@tonic-gate async_resume_utbrk(struct asyncline *async)
30390Sstevel@tonic-gate {
30400Sstevel@tonic-gate 	uchar_t	val;
30410Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
30420Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 	/*
30450Sstevel@tonic-gate 	 * Because the wait time is very short,
30460Sstevel@tonic-gate 	 * so we use uninterruptably wait.
30470Sstevel@tonic-gate 	 */
30480Sstevel@tonic-gate 	while (async->async_flags & ASYNC_HOLD_UTBRK) {
30490Sstevel@tonic-gate 		cv_wait(&async->async_flags_cv, &asy->asy_excl);
30500Sstevel@tonic-gate 	}
30510Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
30520Sstevel@tonic-gate 	/*
30530Sstevel@tonic-gate 	 * Timed break and untimed break can exist simultaneously,
30540Sstevel@tonic-gate 	 * if ASYNC_BREAK is also set at here, we don't
30550Sstevel@tonic-gate 	 * really clean the HW break.
30560Sstevel@tonic-gate 	 */
30570Sstevel@tonic-gate 	if (!(async->async_flags & ASYNC_BREAK)) {
30581106Smrj 		val = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
30591106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
30600Sstevel@tonic-gate 		    (val & ~SETBREAK));
30610Sstevel@tonic-gate 	}
30620Sstevel@tonic-gate 	async->async_flags &= ~ASYNC_OUT_SUSPEND;
30630Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
30640Sstevel@tonic-gate 	if (async->async_ocnt > 0) {
30650Sstevel@tonic-gate 		async_resume(async);
30660Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
30670Sstevel@tonic-gate 	} else {
30680Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_BUSY;
30690Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
30700Sstevel@tonic-gate 		if (async->async_xmitblk != NULL) {
30710Sstevel@tonic-gate 			freeb(async->async_xmitblk);
30720Sstevel@tonic-gate 			async->async_xmitblk = NULL;
30730Sstevel@tonic-gate 		}
30740Sstevel@tonic-gate 		async_start(async);
30750Sstevel@tonic-gate 	}
30760Sstevel@tonic-gate }
30770Sstevel@tonic-gate 
30780Sstevel@tonic-gate /*
30790Sstevel@tonic-gate  * Process an "ioctl" message sent down to us.
30800Sstevel@tonic-gate  * Note that we don't need to get any locks until we are ready to access
30810Sstevel@tonic-gate  * the hardware.  Nothing we access until then is going to be altered
30820Sstevel@tonic-gate  * outside of the STREAMS framework, so we should be safe.
30830Sstevel@tonic-gate  */
30840Sstevel@tonic-gate int asydelay = 10000;
30850Sstevel@tonic-gate static void
30860Sstevel@tonic-gate async_ioctl(struct asyncline *async, queue_t *wq, mblk_t *mp)
30870Sstevel@tonic-gate {
30880Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
30890Sstevel@tonic-gate 	tty_common_t  *tp = &async->async_ttycommon;
30900Sstevel@tonic-gate 	struct iocblk *iocp;
30910Sstevel@tonic-gate 	unsigned datasize;
30920Sstevel@tonic-gate 	int error = 0;
30930Sstevel@tonic-gate 	uchar_t val;
30940Sstevel@tonic-gate 	mblk_t *datamp;
30950Sstevel@tonic-gate 	unsigned int index;
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate #ifdef DEBUG
30980Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_ioctl\n", instance);
31010Sstevel@tonic-gate #endif
31020Sstevel@tonic-gate 
31030Sstevel@tonic-gate 	if (tp->t_iocpending != NULL) {
31040Sstevel@tonic-gate 		/*
31050Sstevel@tonic-gate 		 * We were holding an "ioctl" response pending the
31060Sstevel@tonic-gate 		 * availability of an "mblk" to hold data to be passed up;
31070Sstevel@tonic-gate 		 * another "ioctl" came through, which means that "ioctl"
31080Sstevel@tonic-gate 		 * must have timed out or been aborted.
31090Sstevel@tonic-gate 		 */
31100Sstevel@tonic-gate 		freemsg(async->async_ttycommon.t_iocpending);
31110Sstevel@tonic-gate 		async->async_ttycommon.t_iocpending = NULL;
31120Sstevel@tonic-gate 	}
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
31150Sstevel@tonic-gate 
31160Sstevel@tonic-gate 	/*
31170Sstevel@tonic-gate 	 * For TIOCMGET and the PPS ioctls, do NOT call ttycommon_ioctl()
31180Sstevel@tonic-gate 	 * because this function frees up the message block (mp->b_cont) that
31190Sstevel@tonic-gate 	 * contains the user location where we pass back the results.
31200Sstevel@tonic-gate 	 *
31210Sstevel@tonic-gate 	 * Similarly, CONSOPENPOLLEDIO needs ioc_count, which ttycommon_ioctl
31220Sstevel@tonic-gate 	 * zaps.  We know that ttycommon_ioctl doesn't know any CONS*
31230Sstevel@tonic-gate 	 * ioctls, so keep the others safe too.
31240Sstevel@tonic-gate 	 */
31250Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_IOCTL, "async%d_ioctl: %s\n",
31260Sstevel@tonic-gate 		instance,
31270Sstevel@tonic-gate 		iocp->ioc_cmd == TIOCMGET ? "TIOCMGET" :
31280Sstevel@tonic-gate 		iocp->ioc_cmd == TIOCMSET ? "TIOCMSET" :
31290Sstevel@tonic-gate 		iocp->ioc_cmd == TIOCMBIS ? "TIOCMBIS" :
31300Sstevel@tonic-gate 		iocp->ioc_cmd == TIOCMBIC ? "TIOCMBIC" :
31310Sstevel@tonic-gate 					    "other");
31320Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
31330Sstevel@tonic-gate 	case TIOCMGET:
31340Sstevel@tonic-gate 	case TIOCGPPS:
31350Sstevel@tonic-gate 	case TIOCSPPS:
31360Sstevel@tonic-gate 	case TIOCGPPSEV:
31370Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
31380Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
31390Sstevel@tonic-gate 	case CONSSETABORTENABLE:
31400Sstevel@tonic-gate 	case CONSGETABORTENABLE:
31410Sstevel@tonic-gate 		error = -1; /* Do Nothing */
31420Sstevel@tonic-gate 		break;
31430Sstevel@tonic-gate 	default:
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate 		/*
31460Sstevel@tonic-gate 		 * The only way in which "ttycommon_ioctl" can fail is if the
31470Sstevel@tonic-gate 		 * "ioctl" requires a response containing data to be returned
31480Sstevel@tonic-gate 		 * to the user, and no mblk could be allocated for the data.
31490Sstevel@tonic-gate 		 * No such "ioctl" alters our state.  Thus, we always go ahead
31500Sstevel@tonic-gate 		 * and do any state-changes the "ioctl" calls for.  If we
31510Sstevel@tonic-gate 		 * couldn't allocate the data, "ttycommon_ioctl" has stashed
31520Sstevel@tonic-gate 		 * the "ioctl" away safely, so we just call "bufcall" to
31530Sstevel@tonic-gate 		 * request that we be called back when we stand a better
31540Sstevel@tonic-gate 		 * chance of allocating the data.
31550Sstevel@tonic-gate 		 */
31560Sstevel@tonic-gate 		if ((datasize = ttycommon_ioctl(tp, wq, mp, &error)) != 0) {
31570Sstevel@tonic-gate 			if (async->async_wbufcid)
31580Sstevel@tonic-gate 				unbufcall(async->async_wbufcid);
31590Sstevel@tonic-gate 			async->async_wbufcid = bufcall(datasize, BPRI_HI,
31600Sstevel@tonic-gate 			    (void (*)(void *)) async_reioctl,
31610Sstevel@tonic-gate 			    (void *)(intptr_t)async->async_common->asy_unit);
31620Sstevel@tonic-gate 			return;
31630Sstevel@tonic-gate 		}
31640Sstevel@tonic-gate 	}
31650Sstevel@tonic-gate 
31660Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
31670Sstevel@tonic-gate 
31680Sstevel@tonic-gate 	if (error == 0) {
31690Sstevel@tonic-gate 		/*
31700Sstevel@tonic-gate 		 * "ttycommon_ioctl" did most of the work; we just use the
31710Sstevel@tonic-gate 		 * data it set up.
31720Sstevel@tonic-gate 		 */
31730Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
31740Sstevel@tonic-gate 
31750Sstevel@tonic-gate 		case TCSETS:
31760Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
31770Sstevel@tonic-gate 			if (asy_baudok(asy))
31780Sstevel@tonic-gate 				asy_program(asy, ASY_NOINIT);
31790Sstevel@tonic-gate 			else
31800Sstevel@tonic-gate 				error = EINVAL;
31810Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
31820Sstevel@tonic-gate 			break;
31830Sstevel@tonic-gate 		case TCSETSF:
31840Sstevel@tonic-gate 		case TCSETSW:
31850Sstevel@tonic-gate 		case TCSETA:
31860Sstevel@tonic-gate 		case TCSETAW:
31870Sstevel@tonic-gate 		case TCSETAF:
31880Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
31890Sstevel@tonic-gate 			if (!asy_baudok(asy))
31900Sstevel@tonic-gate 				error = EINVAL;
31910Sstevel@tonic-gate 			else {
31920Sstevel@tonic-gate 				if (asy_isbusy(asy))
31930Sstevel@tonic-gate 					asy_waiteot(asy);
31940Sstevel@tonic-gate 				asy_program(asy, ASY_NOINIT);
31950Sstevel@tonic-gate 			}
31960Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
31970Sstevel@tonic-gate 			break;
31980Sstevel@tonic-gate 		}
31990Sstevel@tonic-gate 	} else if (error < 0) {
32000Sstevel@tonic-gate 		/*
32010Sstevel@tonic-gate 		 * "ttycommon_ioctl" didn't do anything; we process it here.
32020Sstevel@tonic-gate 		 */
32030Sstevel@tonic-gate 		error = 0;
32040Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 		case TIOCGPPS:
32070Sstevel@tonic-gate 			/*
32080Sstevel@tonic-gate 			 * Get PPS on/off.
32090Sstevel@tonic-gate 			 */
32100Sstevel@tonic-gate 			if (mp->b_cont != NULL)
32110Sstevel@tonic-gate 				freemsg(mp->b_cont);
32120Sstevel@tonic-gate 
32130Sstevel@tonic-gate 			mp->b_cont = allocb(sizeof (int), BPRI_HI);
32140Sstevel@tonic-gate 			if (mp->b_cont == NULL) {
32150Sstevel@tonic-gate 				error = ENOMEM;
32160Sstevel@tonic-gate 				break;
32170Sstevel@tonic-gate 			}
32180Sstevel@tonic-gate 			if (asy->asy_flags & ASY_PPS)
32190Sstevel@tonic-gate 				*(int *)mp->b_cont->b_wptr = 1;
32200Sstevel@tonic-gate 			else
32210Sstevel@tonic-gate 				*(int *)mp->b_cont->b_wptr = 0;
32220Sstevel@tonic-gate 			mp->b_cont->b_wptr += sizeof (int);
32230Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
32240Sstevel@tonic-gate 			iocp->ioc_count = sizeof (int);
32250Sstevel@tonic-gate 			break;
32260Sstevel@tonic-gate 
32270Sstevel@tonic-gate 		case TIOCSPPS:
32280Sstevel@tonic-gate 			/*
32290Sstevel@tonic-gate 			 * Set PPS on/off.
32300Sstevel@tonic-gate 			 */
32310Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
32320Sstevel@tonic-gate 			if (error != 0)
32330Sstevel@tonic-gate 				break;
32340Sstevel@tonic-gate 
32350Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
32360Sstevel@tonic-gate 			if (*(int *)mp->b_cont->b_rptr)
32370Sstevel@tonic-gate 				asy->asy_flags |= ASY_PPS;
32380Sstevel@tonic-gate 			else
32390Sstevel@tonic-gate 				asy->asy_flags &= ~ASY_PPS;
32400Sstevel@tonic-gate 			/* Reset edge sense */
32410Sstevel@tonic-gate 			asy->asy_flags &= ~ASY_PPS_EDGE;
32420Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
32430Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
32440Sstevel@tonic-gate 			break;
32450Sstevel@tonic-gate 
32460Sstevel@tonic-gate 		case TIOCGPPSEV:
32470Sstevel@tonic-gate 		{
32480Sstevel@tonic-gate 			/*
32490Sstevel@tonic-gate 			 * Get PPS event data.
32500Sstevel@tonic-gate 			 */
32510Sstevel@tonic-gate 			mblk_t *bp;
32520Sstevel@tonic-gate 			void *buf;
32530Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
32540Sstevel@tonic-gate 			struct ppsclockev32 p32;
32550Sstevel@tonic-gate #endif
32560Sstevel@tonic-gate 			struct ppsclockev ppsclockev;
32570Sstevel@tonic-gate 
32580Sstevel@tonic-gate 			if (mp->b_cont != NULL) {
32590Sstevel@tonic-gate 				freemsg(mp->b_cont);
32600Sstevel@tonic-gate 				mp->b_cont = NULL;
32610Sstevel@tonic-gate 			}
32620Sstevel@tonic-gate 
32630Sstevel@tonic-gate 			if ((asy->asy_flags & ASY_PPS) == 0) {
32640Sstevel@tonic-gate 				error = ENXIO;
32650Sstevel@tonic-gate 				break;
32660Sstevel@tonic-gate 			}
32670Sstevel@tonic-gate 
32680Sstevel@tonic-gate 			/* Protect from incomplete asy_ppsev */
32690Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
32700Sstevel@tonic-gate 			ppsclockev = asy_ppsev;
32710Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
32720Sstevel@tonic-gate 
32730Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
32740Sstevel@tonic-gate 			if ((iocp->ioc_flag & IOC_MODELS) != IOC_NATIVE) {
32750Sstevel@tonic-gate 				TIMEVAL_TO_TIMEVAL32(&p32.tv, &ppsclockev.tv);
32760Sstevel@tonic-gate 				p32.serial = ppsclockev.serial;
32770Sstevel@tonic-gate 				buf = &p32;
32780Sstevel@tonic-gate 				iocp->ioc_count = sizeof (struct ppsclockev32);
32790Sstevel@tonic-gate 			} else
32800Sstevel@tonic-gate #endif
32810Sstevel@tonic-gate 			{
32820Sstevel@tonic-gate 				buf = &ppsclockev;
32830Sstevel@tonic-gate 				iocp->ioc_count = sizeof (struct ppsclockev);
32840Sstevel@tonic-gate 			}
32850Sstevel@tonic-gate 
32860Sstevel@tonic-gate 			if ((bp = allocb(iocp->ioc_count, BPRI_HI)) == NULL) {
32870Sstevel@tonic-gate 				error = ENOMEM;
32880Sstevel@tonic-gate 				break;
32890Sstevel@tonic-gate 			}
32900Sstevel@tonic-gate 			mp->b_cont = bp;
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 			bcopy(buf, bp->b_wptr, iocp->ioc_count);
32930Sstevel@tonic-gate 			bp->b_wptr += iocp->ioc_count;
32940Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
32950Sstevel@tonic-gate 			break;
32960Sstevel@tonic-gate 		}
32970Sstevel@tonic-gate 
32980Sstevel@tonic-gate 		case TCSBRK:
32990Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
33000Sstevel@tonic-gate 			if (error != 0)
33010Sstevel@tonic-gate 				break;
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate 			if (*(int *)mp->b_cont->b_rptr == 0) {
33040Sstevel@tonic-gate 
33050Sstevel@tonic-gate 				/*
33060Sstevel@tonic-gate 				 * XXX Arrangements to ensure that a break
33070Sstevel@tonic-gate 				 * isn't in progress should be sufficient.
33080Sstevel@tonic-gate 				 * This ugly delay() is the only thing
33090Sstevel@tonic-gate 				 * that seems to work on the NCR Worldmark.
33100Sstevel@tonic-gate 				 * It should be replaced. Note that an
33110Sstevel@tonic-gate 				 * asy_waiteot() also does not work.
33120Sstevel@tonic-gate 				 */
33130Sstevel@tonic-gate 				if (asydelay)
33140Sstevel@tonic-gate 					delay(drv_usectohz(asydelay));
33150Sstevel@tonic-gate 
33160Sstevel@tonic-gate 				while (async->async_flags & ASYNC_BREAK) {
33170Sstevel@tonic-gate 					cv_wait(&async->async_flags_cv,
33180Sstevel@tonic-gate 					    &asy->asy_excl);
33190Sstevel@tonic-gate 				}
33200Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
33210Sstevel@tonic-gate 				/*
33220Sstevel@tonic-gate 				 * We loop until the TSR is empty and then
33230Sstevel@tonic-gate 				 * set the break.  ASYNC_BREAK has been set
33240Sstevel@tonic-gate 				 * to ensure that no characters are
33250Sstevel@tonic-gate 				 * transmitted while the TSR is being
33260Sstevel@tonic-gate 				 * flushed and SOUT is being used for the
33270Sstevel@tonic-gate 				 * break signal.
33280Sstevel@tonic-gate 				 *
33290Sstevel@tonic-gate 				 * The wait period is equal to
33300Sstevel@tonic-gate 				 * clock / (baud * 16) * 16 * 2.
33310Sstevel@tonic-gate 				 */
33320Sstevel@tonic-gate 				index = BAUDINDEX(
33330Sstevel@tonic-gate 					async->async_ttycommon.t_cflag);
33340Sstevel@tonic-gate 				async->async_flags |= ASYNC_BREAK;
33351106Smrj 				while ((ddi_get8(asy->asy_iohandle,
33360Sstevel@tonic-gate 				    asy->asy_ioaddr + LSR) & XSRE) == 0) {
33370Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
33380Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
33390Sstevel@tonic-gate 					drv_usecwait(
33400Sstevel@tonic-gate 						32*asyspdtab[index] & 0xfff);
33410Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
33420Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
33430Sstevel@tonic-gate 				}
33440Sstevel@tonic-gate 				/*
33450Sstevel@tonic-gate 				 * Arrange for "async_restart"
33460Sstevel@tonic-gate 				 * to be called in 1/4 second;
33470Sstevel@tonic-gate 				 * it will turn the break bit off, and call
33480Sstevel@tonic-gate 				 * "async_start" to grab the next message.
33490Sstevel@tonic-gate 				 */
33501106Smrj 				val = ddi_get8(asy->asy_iohandle,
33510Sstevel@tonic-gate 					asy->asy_ioaddr + LCR);
33521106Smrj 				ddi_put8(asy->asy_iohandle,
33530Sstevel@tonic-gate 					asy->asy_ioaddr + LCR,
33540Sstevel@tonic-gate 					(val | SETBREAK));
33550Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
33560Sstevel@tonic-gate 				(void) timeout(async_restart, (caddr_t)async,
33570Sstevel@tonic-gate 				    drv_usectohz(1000000)/4);
33580Sstevel@tonic-gate 			} else {
33590Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_OUT,
33600Sstevel@tonic-gate 					"async%d_ioctl: wait for flush.\n",
33610Sstevel@tonic-gate 					instance);
33620Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
33630Sstevel@tonic-gate 				asy_waiteot(asy);
33640Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
33650Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_OUT,
33660Sstevel@tonic-gate 					"async%d_ioctl: ldterm satisfied.\n",
33670Sstevel@tonic-gate 					instance);
33680Sstevel@tonic-gate 			}
33690Sstevel@tonic-gate 			break;
33700Sstevel@tonic-gate 
33710Sstevel@tonic-gate 		case TIOCSBRK:
33720Sstevel@tonic-gate 			if (!(async->async_flags & ASYNC_OUT_SUSPEND)) {
33730Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
33740Sstevel@tonic-gate 				async->async_flags |= ASYNC_OUT_SUSPEND;
33750Sstevel@tonic-gate 				async->async_flags |= ASYNC_HOLD_UTBRK;
33760Sstevel@tonic-gate 				index = BAUDINDEX(
33770Sstevel@tonic-gate 				    async->async_ttycommon.t_cflag);
33781106Smrj 				while ((ddi_get8(asy->asy_iohandle,
33790Sstevel@tonic-gate 				    asy->asy_ioaddr + LSR) & XSRE) == 0) {
33800Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
33810Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
33820Sstevel@tonic-gate 					drv_usecwait(
33830Sstevel@tonic-gate 					    32*asyspdtab[index] & 0xfff);
33840Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
33850Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
33860Sstevel@tonic-gate 				}
33871106Smrj 				val = ddi_get8(asy->asy_iohandle,
33880Sstevel@tonic-gate 				    asy->asy_ioaddr + LCR);
33891106Smrj 				ddi_put8(asy->asy_iohandle,
33900Sstevel@tonic-gate 				    asy->asy_ioaddr + LCR, (val | SETBREAK));
33910Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
33920Sstevel@tonic-gate 				/* wait for 100ms to hold BREAK */
33930Sstevel@tonic-gate 				async->async_utbrktid =
33940Sstevel@tonic-gate 				    timeout((void (*)())async_hold_utbrk,
33950Sstevel@tonic-gate 				    (caddr_t)async,
33960Sstevel@tonic-gate 				    drv_usectohz(asy_min_utbrk));
33970Sstevel@tonic-gate 			}
33980Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
33990Sstevel@tonic-gate 			break;
34000Sstevel@tonic-gate 
34010Sstevel@tonic-gate 		case TIOCCBRK:
34020Sstevel@tonic-gate 			if (async->async_flags & ASYNC_OUT_SUSPEND)
34030Sstevel@tonic-gate 				async_resume_utbrk(async);
34040Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
34050Sstevel@tonic-gate 			break;
34060Sstevel@tonic-gate 
34070Sstevel@tonic-gate 		case TIOCMSET:
34080Sstevel@tonic-gate 		case TIOCMBIS:
34090Sstevel@tonic-gate 		case TIOCMBIC:
34100Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT) {
34110Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
34120Sstevel@tonic-gate 					"non-transparent\n", instance);
34130Sstevel@tonic-gate 
34140Sstevel@tonic-gate 				error = miocpullup(mp, sizeof (int));
34150Sstevel@tonic-gate 				if (error != 0)
34160Sstevel@tonic-gate 					break;
34170Sstevel@tonic-gate 
34180Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
34190Sstevel@tonic-gate 				(void) asymctl(asy,
34200Sstevel@tonic-gate 					dmtoasy(*(int *)mp->b_cont->b_rptr),
34210Sstevel@tonic-gate 					iocp->ioc_cmd);
34220Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
34230Sstevel@tonic-gate 				iocp->ioc_error = 0;
34240Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
34250Sstevel@tonic-gate 			} else {
34260Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
34270Sstevel@tonic-gate 					"transparent\n", instance);
34280Sstevel@tonic-gate 				mcopyin(mp, NULL, sizeof (int), NULL);
34290Sstevel@tonic-gate 			}
34300Sstevel@tonic-gate 			break;
34310Sstevel@tonic-gate 
34320Sstevel@tonic-gate 		case TIOCMGET:
34330Sstevel@tonic-gate 			datamp = allocb(sizeof (int), BPRI_MED);
34340Sstevel@tonic-gate 			if (datamp == NULL) {
34350Sstevel@tonic-gate 				error = EAGAIN;
34360Sstevel@tonic-gate 				break;
34370Sstevel@tonic-gate 			}
34380Sstevel@tonic-gate 
34390Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
34400Sstevel@tonic-gate 			*(int *)datamp->b_rptr = asymctl(asy, 0, TIOCMGET);
34410Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
34420Sstevel@tonic-gate 
34430Sstevel@tonic-gate 			if (iocp->ioc_count == TRANSPARENT) {
34440Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
34450Sstevel@tonic-gate 					"transparent\n", instance);
34460Sstevel@tonic-gate 				mcopyout(mp, NULL, sizeof (int), NULL,
34470Sstevel@tonic-gate 					datamp);
34480Sstevel@tonic-gate 			} else {
34490Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
34500Sstevel@tonic-gate 					"non-transparent\n", instance);
34510Sstevel@tonic-gate 				mioc2ack(mp, datamp, sizeof (int), 0);
34520Sstevel@tonic-gate 			}
34530Sstevel@tonic-gate 			break;
34540Sstevel@tonic-gate 
34550Sstevel@tonic-gate 		case CONSOPENPOLLEDIO:
34560Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (struct cons_polledio *));
34570Sstevel@tonic-gate 			if (error != 0)
34580Sstevel@tonic-gate 				break;
34590Sstevel@tonic-gate 
34600Sstevel@tonic-gate 			*(struct cons_polledio **)mp->b_cont->b_rptr =
34610Sstevel@tonic-gate 				&asy->polledio;
34620Sstevel@tonic-gate 
34630Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
34640Sstevel@tonic-gate 			break;
34650Sstevel@tonic-gate 
34660Sstevel@tonic-gate 		case CONSCLOSEPOLLEDIO:
34670Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
34680Sstevel@tonic-gate 			iocp->ioc_error = 0;
34690Sstevel@tonic-gate 			iocp->ioc_rval = 0;
34700Sstevel@tonic-gate 			break;
34710Sstevel@tonic-gate 
34720Sstevel@tonic-gate 		case CONSSETABORTENABLE:
34730Sstevel@tonic-gate 			error = secpolicy_console(iocp->ioc_cr);
34740Sstevel@tonic-gate 			if (error != 0)
34750Sstevel@tonic-gate 				break;
34760Sstevel@tonic-gate 
34770Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT) {
34780Sstevel@tonic-gate 				error = EINVAL;
34790Sstevel@tonic-gate 				break;
34800Sstevel@tonic-gate 			}
34810Sstevel@tonic-gate 
34820Sstevel@tonic-gate 			if (*(intptr_t *)mp->b_cont->b_rptr)
34830Sstevel@tonic-gate 				asy->asy_flags |= ASY_CONSOLE;
34840Sstevel@tonic-gate 			else
34850Sstevel@tonic-gate 				asy->asy_flags &= ~ASY_CONSOLE;
34860Sstevel@tonic-gate 
34870Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
34880Sstevel@tonic-gate 			iocp->ioc_error = 0;
34890Sstevel@tonic-gate 			iocp->ioc_rval = 0;
34900Sstevel@tonic-gate 			break;
34910Sstevel@tonic-gate 
34920Sstevel@tonic-gate 		case CONSGETABORTENABLE:
34930Sstevel@tonic-gate 			/*CONSTANTCONDITION*/
34940Sstevel@tonic-gate 			ASSERT(sizeof (boolean_t) <= sizeof (boolean_t *));
34950Sstevel@tonic-gate 			/*
34960Sstevel@tonic-gate 			 * Store the return value right in the payload
34970Sstevel@tonic-gate 			 * we were passed.  Crude.
34980Sstevel@tonic-gate 			 */
34990Sstevel@tonic-gate 			mcopyout(mp, NULL, sizeof (boolean_t), NULL, NULL);
35000Sstevel@tonic-gate 			*(boolean_t *)mp->b_cont->b_rptr =
35010Sstevel@tonic-gate 				(asy->asy_flags & ASY_CONSOLE) != 0;
35020Sstevel@tonic-gate 			break;
35030Sstevel@tonic-gate 
35040Sstevel@tonic-gate 		default:
35050Sstevel@tonic-gate 			/*
35060Sstevel@tonic-gate 			 * If we don't understand it, it's an error.  NAK it.
35070Sstevel@tonic-gate 			 */
35080Sstevel@tonic-gate 			error = EINVAL;
35090Sstevel@tonic-gate 			break;
35100Sstevel@tonic-gate 		}
35110Sstevel@tonic-gate 	}
35120Sstevel@tonic-gate 	if (error != 0) {
35130Sstevel@tonic-gate 		iocp->ioc_error = error;
35140Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
35150Sstevel@tonic-gate 	}
35160Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
35170Sstevel@tonic-gate 	qreply(wq, mp);
35180Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_ioctl: done\n", instance);
35190Sstevel@tonic-gate }
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate static int
35220Sstevel@tonic-gate asyrsrv(queue_t *q)
35230Sstevel@tonic-gate {
35240Sstevel@tonic-gate 	mblk_t *bp;
35250Sstevel@tonic-gate 	struct asyncline *async;
35260Sstevel@tonic-gate 
35270Sstevel@tonic-gate 	async = (struct asyncline *)q->q_ptr;
35280Sstevel@tonic-gate 
35290Sstevel@tonic-gate 	while (canputnext(q) && (bp = getq(q)))
35300Sstevel@tonic-gate 		putnext(q, bp);
35310Sstevel@tonic-gate 	ASYSETSOFT(async->async_common);
35320Sstevel@tonic-gate 	async->async_polltid = 0;
35330Sstevel@tonic-gate 	return (0);
35340Sstevel@tonic-gate }
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate /*
35370Sstevel@tonic-gate  * Put procedure for write queue.
35380Sstevel@tonic-gate  * Respond to M_STOP, M_START, M_IOCTL, and M_FLUSH messages here;
35390Sstevel@tonic-gate  * set the flow control character for M_STOPI and M_STARTI messages;
35400Sstevel@tonic-gate  * queue up M_BREAK, M_DELAY, and M_DATA messages for processing
35410Sstevel@tonic-gate  * by the start routine, and then call the start routine; discard
35420Sstevel@tonic-gate  * everything else.  Note that this driver does not incorporate any
35430Sstevel@tonic-gate  * mechanism to negotiate to handle the canonicalization process.
35440Sstevel@tonic-gate  * It expects that these functions are handled in upper module(s),
35450Sstevel@tonic-gate  * as we do in ldterm.
35460Sstevel@tonic-gate  */
35470Sstevel@tonic-gate static int
35480Sstevel@tonic-gate asywput(queue_t *q, mblk_t *mp)
35490Sstevel@tonic-gate {
35500Sstevel@tonic-gate 	struct asyncline *async;
35510Sstevel@tonic-gate 	struct asycom *asy;
35520Sstevel@tonic-gate #ifdef DEBUG
35530Sstevel@tonic-gate 	int instance;
35540Sstevel@tonic-gate #endif
35550Sstevel@tonic-gate 	int error;
35560Sstevel@tonic-gate 
35570Sstevel@tonic-gate 	async = (struct asyncline *)q->q_ptr;
35580Sstevel@tonic-gate #ifdef DEBUG
35590Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
35600Sstevel@tonic-gate #endif
35610Sstevel@tonic-gate 	asy = async->async_common;
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
35640Sstevel@tonic-gate 
35650Sstevel@tonic-gate 	case M_STOP:
35660Sstevel@tonic-gate 		/*
35670Sstevel@tonic-gate 		 * Since we don't do real DMA, we can just let the
35680Sstevel@tonic-gate 		 * chip coast to a stop after applying the brakes.
35690Sstevel@tonic-gate 		 */
35700Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
35710Sstevel@tonic-gate 		async->async_flags |= ASYNC_STOPPED;
35720Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
35730Sstevel@tonic-gate 		freemsg(mp);
35740Sstevel@tonic-gate 		break;
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate 	case M_START:
35770Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
35780Sstevel@tonic-gate 		if (async->async_flags & ASYNC_STOPPED) {
35790Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_STOPPED;
35800Sstevel@tonic-gate 			/*
35810Sstevel@tonic-gate 			 * If an output operation is in progress,
35820Sstevel@tonic-gate 			 * resume it.  Otherwise, prod the start
35830Sstevel@tonic-gate 			 * routine.
35840Sstevel@tonic-gate 			 */
35850Sstevel@tonic-gate 			if (async->async_ocnt > 0) {
35860Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
35870Sstevel@tonic-gate 				async_resume(async);
35880Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
35890Sstevel@tonic-gate 			} else {
35900Sstevel@tonic-gate 				async_start(async);
35910Sstevel@tonic-gate 			}
35920Sstevel@tonic-gate 		}
35930Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
35940Sstevel@tonic-gate 		freemsg(mp);
35950Sstevel@tonic-gate 		break;
35960Sstevel@tonic-gate 
35970Sstevel@tonic-gate 	case M_IOCTL:
35980Sstevel@tonic-gate 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
35990Sstevel@tonic-gate 
36000Sstevel@tonic-gate 		case TCSBRK:
36010Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
36020Sstevel@tonic-gate 			if (error != 0) {
36030Sstevel@tonic-gate 				miocnak(q, mp, 0, error);
36040Sstevel@tonic-gate 				return (0);
36050Sstevel@tonic-gate 			}
36060Sstevel@tonic-gate 
36070Sstevel@tonic-gate 			if (*(int *)mp->b_cont->b_rptr != 0) {
36080Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_OUT,
36090Sstevel@tonic-gate 					"async%d_ioctl: flush request.\n",
36100Sstevel@tonic-gate 					instance);
36110Sstevel@tonic-gate 				(void) putq(q, mp);
36120Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl);
36130Sstevel@tonic-gate 
36140Sstevel@tonic-gate 				/*
36150Sstevel@tonic-gate 				 * If an TIOCSBRK is in progress,
36160Sstevel@tonic-gate 				 * clean it as TIOCCBRK does,
36170Sstevel@tonic-gate 				 * then kick off output.
36180Sstevel@tonic-gate 				 * If TIOCSBRK is not in progress,
36190Sstevel@tonic-gate 				 * just kick off output.
36200Sstevel@tonic-gate 				 */
36210Sstevel@tonic-gate 				async_resume_utbrk(async);
36220Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl);
36230Sstevel@tonic-gate 				break;
36240Sstevel@tonic-gate 			}
36250Sstevel@tonic-gate 			/*FALLTHROUGH*/
36260Sstevel@tonic-gate 		case TCSETSW:
36270Sstevel@tonic-gate 		case TCSETSF:
36280Sstevel@tonic-gate 		case TCSETAW:
36290Sstevel@tonic-gate 		case TCSETAF:
36300Sstevel@tonic-gate 			/*
36310Sstevel@tonic-gate 			 * The changes do not take effect until all
36320Sstevel@tonic-gate 			 * output queued before them is drained.
36330Sstevel@tonic-gate 			 * Put this message on the queue, so that
36340Sstevel@tonic-gate 			 * "async_start" will see it when it's done
36350Sstevel@tonic-gate 			 * with the output before it.  Poke the
36360Sstevel@tonic-gate 			 * start routine, just in case.
36370Sstevel@tonic-gate 			 */
36380Sstevel@tonic-gate 			(void) putq(q, mp);
36390Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
36400Sstevel@tonic-gate 
36410Sstevel@tonic-gate 			/*
36420Sstevel@tonic-gate 			 * If an TIOCSBRK is in progress,
36430Sstevel@tonic-gate 			 * clean it as TIOCCBRK does.
36440Sstevel@tonic-gate 			 * then kick off output.
36450Sstevel@tonic-gate 			 * If TIOCSBRK is not in progress,
36460Sstevel@tonic-gate 			 * just kick off output.
36470Sstevel@tonic-gate 			 */
36480Sstevel@tonic-gate 			async_resume_utbrk(async);
36490Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
36500Sstevel@tonic-gate 			break;
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 		default:
36530Sstevel@tonic-gate 			/*
36540Sstevel@tonic-gate 			 * Do it now.
36550Sstevel@tonic-gate 			 */
36560Sstevel@tonic-gate 			async_ioctl(async, q, mp);
36570Sstevel@tonic-gate 			break;
36580Sstevel@tonic-gate 		}
36590Sstevel@tonic-gate 		break;
36600Sstevel@tonic-gate 
36610Sstevel@tonic-gate 	case M_FLUSH:
36620Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
36630Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
36640Sstevel@tonic-gate 
36650Sstevel@tonic-gate 			/*
36660Sstevel@tonic-gate 			 * Abort any output in progress.
36670Sstevel@tonic-gate 			 */
36680Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
36690Sstevel@tonic-gate 			if (async->async_flags & ASYNC_BUSY) {
36700Sstevel@tonic-gate 			    DEBUGCONT1(ASY_DEBUG_BUSY, "asy%dwput: "
36710Sstevel@tonic-gate 				    "Clearing async_ocnt, "
36720Sstevel@tonic-gate 				    "leaving ASYNC_BUSY set\n",
36730Sstevel@tonic-gate 				    instance);
36740Sstevel@tonic-gate 				async->async_ocnt = 0;
36750Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_BUSY;
36760Sstevel@tonic-gate 			} /* if */
36770Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
36780Sstevel@tonic-gate 
36790Sstevel@tonic-gate 			/* Flush FIFO buffers */
36800Sstevel@tonic-gate 			if (asy->asy_use_fifo == FIFO_ON) {
36810Sstevel@tonic-gate 				asy_reset_fifo(asy, FIFOTXFLSH);
36820Sstevel@tonic-gate 			}
36830Sstevel@tonic-gate 
36840Sstevel@tonic-gate 			/*
36850Sstevel@tonic-gate 			 * Flush our write queue.
36860Sstevel@tonic-gate 			 */
36870Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
36880Sstevel@tonic-gate 			if (async->async_xmitblk != NULL) {
36890Sstevel@tonic-gate 				freeb(async->async_xmitblk);
36900Sstevel@tonic-gate 				async->async_xmitblk = NULL;
36910Sstevel@tonic-gate 			}
36920Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
36930Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;	/* it has been flushed */
36940Sstevel@tonic-gate 		}
36950Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
36960Sstevel@tonic-gate 			/* Flush FIFO buffers */
36970Sstevel@tonic-gate 			if (asy->asy_use_fifo == FIFO_ON) {
36980Sstevel@tonic-gate 				asy_reset_fifo(asy, FIFORXFLSH);
36990Sstevel@tonic-gate 			}
37000Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
37010Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
37020Sstevel@tonic-gate 		} else {
37030Sstevel@tonic-gate 			freemsg(mp);
37040Sstevel@tonic-gate 		}
37050Sstevel@tonic-gate 
37060Sstevel@tonic-gate 		/*
37070Sstevel@tonic-gate 		 * We must make sure we process messages that survive the
37080Sstevel@tonic-gate 		 * write-side flush.
37090Sstevel@tonic-gate 		 */
37100Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
37110Sstevel@tonic-gate 		async_start(async);
37120Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
37130Sstevel@tonic-gate 		break;
37140Sstevel@tonic-gate 
37150Sstevel@tonic-gate 	case M_BREAK:
37160Sstevel@tonic-gate 	case M_DELAY:
37170Sstevel@tonic-gate 	case M_DATA:
37180Sstevel@tonic-gate 		/*
37190Sstevel@tonic-gate 		 * Queue the message up to be transmitted,
37200Sstevel@tonic-gate 		 * and poke the start routine.
37210Sstevel@tonic-gate 		 */
37220Sstevel@tonic-gate 		(void) putq(q, mp);
37230Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
37240Sstevel@tonic-gate 		async_start(async);
37250Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
37260Sstevel@tonic-gate 		break;
37270Sstevel@tonic-gate 
37280Sstevel@tonic-gate 	case M_STOPI:
37290Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
37300Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
37310Sstevel@tonic-gate 		if (!(async->async_inflow_source & IN_FLOW_USER)) {
37320Sstevel@tonic-gate 			async_flowcontrol_hw_input(asy, FLOW_STOP,
37330Sstevel@tonic-gate 			    IN_FLOW_USER);
37340Sstevel@tonic-gate 			(void) async_flowcontrol_sw_input(asy, FLOW_STOP,
37350Sstevel@tonic-gate 			    IN_FLOW_USER);
37360Sstevel@tonic-gate 		}
37370Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
37380Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
37390Sstevel@tonic-gate 		freemsg(mp);
37400Sstevel@tonic-gate 		break;
37410Sstevel@tonic-gate 
37420Sstevel@tonic-gate 	case M_STARTI:
37430Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
37440Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
37450Sstevel@tonic-gate 		if (async->async_inflow_source & IN_FLOW_USER) {
37460Sstevel@tonic-gate 			async_flowcontrol_hw_input(asy, FLOW_START,
37470Sstevel@tonic-gate 			    IN_FLOW_USER);
37480Sstevel@tonic-gate 			(void) async_flowcontrol_sw_input(asy, FLOW_START,
37490Sstevel@tonic-gate 			    IN_FLOW_USER);
37500Sstevel@tonic-gate 		}
37510Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
37520Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
37530Sstevel@tonic-gate 		freemsg(mp);
37540Sstevel@tonic-gate 		break;
37550Sstevel@tonic-gate 
37560Sstevel@tonic-gate 	case M_CTL:
37570Sstevel@tonic-gate 		if (MBLKL(mp) >= sizeof (struct iocblk) &&
37580Sstevel@tonic-gate 		    ((struct iocblk *)mp->b_rptr)->ioc_cmd == MC_POSIXQUERY) {
37590Sstevel@tonic-gate 			((struct iocblk *)mp->b_rptr)->ioc_cmd = MC_HAS_POSIX;
37600Sstevel@tonic-gate 			qreply(q, mp);
37610Sstevel@tonic-gate 		} else {
37620Sstevel@tonic-gate 			/*
37630Sstevel@tonic-gate 			 * These MC_SERVICE type messages are used by upper
37640Sstevel@tonic-gate 			 * modules to tell this driver to send input up
37650Sstevel@tonic-gate 			 * immediately, or that it can wait for normal
37660Sstevel@tonic-gate 			 * processing that may or may not be done.  Sun
37670Sstevel@tonic-gate 			 * requires these for the mouse module.
37680Sstevel@tonic-gate 			 * (XXX - for x86?)
37690Sstevel@tonic-gate 			 */
37700Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
37710Sstevel@tonic-gate 			switch (*mp->b_rptr) {
37720Sstevel@tonic-gate 
37730Sstevel@tonic-gate 			case MC_SERVICEIMM:
37740Sstevel@tonic-gate 				async->async_flags |= ASYNC_SERVICEIMM;
37750Sstevel@tonic-gate 				break;
37760Sstevel@tonic-gate 
37770Sstevel@tonic-gate 			case MC_SERVICEDEF:
37780Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_SERVICEIMM;
37790Sstevel@tonic-gate 				break;
37800Sstevel@tonic-gate 			}
37810Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
37820Sstevel@tonic-gate 			freemsg(mp);
37830Sstevel@tonic-gate 		}
37840Sstevel@tonic-gate 		break;
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 	case M_IOCDATA:
37870Sstevel@tonic-gate 		async_iocdata(q, mp);
37880Sstevel@tonic-gate 		break;
37890Sstevel@tonic-gate 
37900Sstevel@tonic-gate 	default:
37910Sstevel@tonic-gate 		freemsg(mp);
37920Sstevel@tonic-gate 		break;
37930Sstevel@tonic-gate 	}
37940Sstevel@tonic-gate 	return (0);
37950Sstevel@tonic-gate }
37960Sstevel@tonic-gate 
37970Sstevel@tonic-gate /*
37980Sstevel@tonic-gate  * Retry an "ioctl", now that "bufcall" claims we may be able to allocate
37990Sstevel@tonic-gate  * the buffer we need.
38000Sstevel@tonic-gate  */
38010Sstevel@tonic-gate static void
38020Sstevel@tonic-gate async_reioctl(void *unit)
38030Sstevel@tonic-gate {
38040Sstevel@tonic-gate 	int instance = (uintptr_t)unit;
38050Sstevel@tonic-gate 	struct asyncline *async;
38060Sstevel@tonic-gate 	struct asycom *asy;
38070Sstevel@tonic-gate 	queue_t	*q;
38080Sstevel@tonic-gate 	mblk_t	*mp;
38090Sstevel@tonic-gate 
38100Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, instance);
38110Sstevel@tonic-gate 	ASSERT(asy != NULL);
38120Sstevel@tonic-gate 	async = asy->asy_priv;
38130Sstevel@tonic-gate 
38140Sstevel@tonic-gate 	/*
38150Sstevel@tonic-gate 	 * The bufcall is no longer pending.
38160Sstevel@tonic-gate 	 */
38170Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
38180Sstevel@tonic-gate 	async->async_wbufcid = 0;
38190Sstevel@tonic-gate 	if ((q = async->async_ttycommon.t_writeq) == NULL) {
38200Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
38210Sstevel@tonic-gate 		return;
38220Sstevel@tonic-gate 	}
38230Sstevel@tonic-gate 	if ((mp = async->async_ttycommon.t_iocpending) != NULL) {
38240Sstevel@tonic-gate 		/* not pending any more */
38250Sstevel@tonic-gate 		async->async_ttycommon.t_iocpending = NULL;
38260Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
38270Sstevel@tonic-gate 		async_ioctl(async, q, mp);
38280Sstevel@tonic-gate 	} else
38290Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
38300Sstevel@tonic-gate }
38310Sstevel@tonic-gate 
38320Sstevel@tonic-gate static void
38330Sstevel@tonic-gate async_iocdata(queue_t *q, mblk_t *mp)
38340Sstevel@tonic-gate {
38350Sstevel@tonic-gate 	struct asyncline	*async = (struct asyncline *)q->q_ptr;
38360Sstevel@tonic-gate 	struct asycom		*asy;
38370Sstevel@tonic-gate 	struct iocblk *ip;
38380Sstevel@tonic-gate 	struct copyresp *csp;
38390Sstevel@tonic-gate #ifdef DEBUG
38400Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
38410Sstevel@tonic-gate #endif
38420Sstevel@tonic-gate 
38430Sstevel@tonic-gate 	asy = async->async_common;
38440Sstevel@tonic-gate 	ip = (struct iocblk *)mp->b_rptr;
38450Sstevel@tonic-gate 	csp = (struct copyresp *)mp->b_rptr;
38460Sstevel@tonic-gate 
38470Sstevel@tonic-gate 	if (csp->cp_rval != 0) {
38480Sstevel@tonic-gate 		if (csp->cp_private)
38490Sstevel@tonic-gate 			freemsg(csp->cp_private);
38500Sstevel@tonic-gate 		freemsg(mp);
38510Sstevel@tonic-gate 		return;
38520Sstevel@tonic-gate 	}
38530Sstevel@tonic-gate 
38540Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
38550Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_MODEM, "async%d_iocdata: case %s\n",
38560Sstevel@tonic-gate 		instance,
38570Sstevel@tonic-gate 		csp->cp_cmd == TIOCMGET ? "TIOCMGET" :
38580Sstevel@tonic-gate 		csp->cp_cmd == TIOCMSET ? "TIOCMSET" :
38590Sstevel@tonic-gate 		csp->cp_cmd == TIOCMBIS ? "TIOCMBIS" :
38600Sstevel@tonic-gate 		    "TIOCMBIC");
38610Sstevel@tonic-gate 	switch (csp->cp_cmd) {
38620Sstevel@tonic-gate 
38630Sstevel@tonic-gate 	case TIOCMGET:
38640Sstevel@tonic-gate 		if (mp->b_cont) {
38650Sstevel@tonic-gate 			freemsg(mp->b_cont);
38660Sstevel@tonic-gate 			mp->b_cont = NULL;
38670Sstevel@tonic-gate 		}
38680Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
38690Sstevel@tonic-gate 		ip->ioc_error = 0;
38700Sstevel@tonic-gate 		ip->ioc_count = 0;
38710Sstevel@tonic-gate 		ip->ioc_rval = 0;
38720Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct iocblk);
38730Sstevel@tonic-gate 		break;
38740Sstevel@tonic-gate 
38750Sstevel@tonic-gate 	case TIOCMSET:
38760Sstevel@tonic-gate 	case TIOCMBIS:
38770Sstevel@tonic-gate 	case TIOCMBIC:
38780Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
38790Sstevel@tonic-gate 		(void) asymctl(asy,
38800Sstevel@tonic-gate 			dmtoasy(*(int *)mp->b_cont->b_rptr),
38810Sstevel@tonic-gate 			csp->cp_cmd);
38820Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
38830Sstevel@tonic-gate 		mioc2ack(mp, NULL, 0, 0);
38840Sstevel@tonic-gate 		break;
38850Sstevel@tonic-gate 
38860Sstevel@tonic-gate 	default:
38870Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
38880Sstevel@tonic-gate 		ip->ioc_error = EINVAL;
38890Sstevel@tonic-gate 		break;
38900Sstevel@tonic-gate 	}
38910Sstevel@tonic-gate 	qreply(q, mp);
38920Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
38930Sstevel@tonic-gate }
38940Sstevel@tonic-gate 
38950Sstevel@tonic-gate /*
38960Sstevel@tonic-gate  * debugger/console support routines.
38970Sstevel@tonic-gate  */
38980Sstevel@tonic-gate 
38990Sstevel@tonic-gate /*
39000Sstevel@tonic-gate  * put a character out
39010Sstevel@tonic-gate  * Do not use interrupts.  If char is LF, put out CR, LF.
39020Sstevel@tonic-gate  */
39030Sstevel@tonic-gate static void
39041762Slt200341 asyputchar(cons_polledio_arg_t arg, uchar_t c)
39050Sstevel@tonic-gate {
39060Sstevel@tonic-gate 	struct asycom *asy = (struct asycom *)arg;
39070Sstevel@tonic-gate 
39080Sstevel@tonic-gate 	if (c == '\n')
39090Sstevel@tonic-gate 		asyputchar(arg, '\r');
39100Sstevel@tonic-gate 
39111106Smrj 	while ((ddi_get8(asy->asy_iohandle,
39120Sstevel@tonic-gate 	    asy->asy_ioaddr + LSR) & XHRE) == 0) {
39130Sstevel@tonic-gate 		/* wait for xmit to finish */
39140Sstevel@tonic-gate 		drv_usecwait(10);
39150Sstevel@tonic-gate 	}
39160Sstevel@tonic-gate 
39170Sstevel@tonic-gate 	/* put the character out */
39181106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT, c);
39190Sstevel@tonic-gate }
39200Sstevel@tonic-gate 
39210Sstevel@tonic-gate /*
39220Sstevel@tonic-gate  * See if there's a character available. If no character is
39230Sstevel@tonic-gate  * available, return 0. Run in polled mode, no interrupts.
39240Sstevel@tonic-gate  */
39250Sstevel@tonic-gate static boolean_t
39261762Slt200341 asyischar(cons_polledio_arg_t arg)
39270Sstevel@tonic-gate {
39280Sstevel@tonic-gate 	struct asycom *asy = (struct asycom *)arg;
39290Sstevel@tonic-gate 
39301106Smrj 	return ((ddi_get8(asy->asy_iohandle,
39310Sstevel@tonic-gate 		asy->asy_ioaddr + LSR) & RCA) != 0);
39320Sstevel@tonic-gate }
39330Sstevel@tonic-gate 
39340Sstevel@tonic-gate /*
39350Sstevel@tonic-gate  * Get a character. Run in polled mode, no interrupts.
39360Sstevel@tonic-gate  */
39370Sstevel@tonic-gate static int
39381762Slt200341 asygetchar(cons_polledio_arg_t arg)
39390Sstevel@tonic-gate {
39400Sstevel@tonic-gate 	struct asycom *asy = (struct asycom *)arg;
39410Sstevel@tonic-gate 
39420Sstevel@tonic-gate 	while (!asyischar(arg))
39430Sstevel@tonic-gate 		drv_usecwait(10);
39441106Smrj 	return (ddi_get8(asy->asy_iohandle,
39450Sstevel@tonic-gate 		asy->asy_ioaddr + DAT));
39460Sstevel@tonic-gate }
39470Sstevel@tonic-gate 
39480Sstevel@tonic-gate /*
39490Sstevel@tonic-gate  * Set or get the modem control status.
39500Sstevel@tonic-gate  */
39510Sstevel@tonic-gate static int
39520Sstevel@tonic-gate asymctl(struct asycom *asy, int bits, int how)
39530Sstevel@tonic-gate {
39540Sstevel@tonic-gate 	int mcr_r, msr_r;
39550Sstevel@tonic-gate 	int instance = asy->asy_unit;
39560Sstevel@tonic-gate 
39570Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
39580Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
39590Sstevel@tonic-gate 
39600Sstevel@tonic-gate 	/* Read Modem Control Registers */
39611106Smrj 	mcr_r = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
39620Sstevel@tonic-gate 
39630Sstevel@tonic-gate 	switch (how) {
39640Sstevel@tonic-gate 
39650Sstevel@tonic-gate 	case TIOCMSET:
39660Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM,
39670Sstevel@tonic-gate 			"asy%dmctl: TIOCMSET, bits = %x\n", instance, bits);
39680Sstevel@tonic-gate 		mcr_r = bits;		/* Set bits	*/
39690Sstevel@tonic-gate 		break;
39700Sstevel@tonic-gate 
39710Sstevel@tonic-gate 	case TIOCMBIS:
39720Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM, "asy%dmctl: TIOCMBIS, bits = %x\n",
39730Sstevel@tonic-gate 			instance, bits);
39740Sstevel@tonic-gate 		mcr_r |= bits;		/* Mask in bits	*/
39750Sstevel@tonic-gate 		break;
39760Sstevel@tonic-gate 
39770Sstevel@tonic-gate 	case TIOCMBIC:
39780Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM, "asy%dmctl: TIOCMBIC, bits = %x\n",
39790Sstevel@tonic-gate 			instance, bits);
39800Sstevel@tonic-gate 		mcr_r &= ~bits;		/* Mask out bits */
39810Sstevel@tonic-gate 		break;
39820Sstevel@tonic-gate 
39830Sstevel@tonic-gate 	case TIOCMGET:
39840Sstevel@tonic-gate 		/* Read Modem Status Registers */
39850Sstevel@tonic-gate 		/*
39860Sstevel@tonic-gate 		 * If modem interrupts are enabled, we return the
39870Sstevel@tonic-gate 		 * saved value of msr. We read MSR only in async_msint()
39880Sstevel@tonic-gate 		 */
39891106Smrj 		if (ddi_get8(asy->asy_iohandle,
39900Sstevel@tonic-gate 		    asy->asy_ioaddr + ICR) & MIEN) {
39910Sstevel@tonic-gate 			msr_r = asy->asy_msr;
39920Sstevel@tonic-gate 			DEBUGCONT2(ASY_DEBUG_MODEM,
39930Sstevel@tonic-gate 				"asy%dmctl: TIOCMGET, read msr_r = %x\n",
39940Sstevel@tonic-gate 				instance, msr_r);
39950Sstevel@tonic-gate 		} else {
39961106Smrj 			msr_r = ddi_get8(asy->asy_iohandle,
39970Sstevel@tonic-gate 					asy->asy_ioaddr + MSR);
39980Sstevel@tonic-gate 			DEBUGCONT2(ASY_DEBUG_MODEM,
39990Sstevel@tonic-gate 				"asy%dmctl: TIOCMGET, read MSR = %x\n",
40000Sstevel@tonic-gate 				instance, msr_r);
40010Sstevel@tonic-gate 		}
40020Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM, "asy%dtodm: modem_lines = %x\n",
40030Sstevel@tonic-gate 			instance, asytodm(mcr_r, msr_r));
40040Sstevel@tonic-gate 		return (asytodm(mcr_r, msr_r));
40050Sstevel@tonic-gate 	}
40060Sstevel@tonic-gate 
40071106Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR, mcr_r);
40080Sstevel@tonic-gate 
40090Sstevel@tonic-gate 	return (mcr_r);
40100Sstevel@tonic-gate }
40110Sstevel@tonic-gate 
40120Sstevel@tonic-gate static int
40130Sstevel@tonic-gate asytodm(int mcr_r, int msr_r)
40140Sstevel@tonic-gate {
40150Sstevel@tonic-gate 	int b = 0;
40160Sstevel@tonic-gate 
40170Sstevel@tonic-gate 	/* MCR registers */
40180Sstevel@tonic-gate 	if (mcr_r & RTS)
40190Sstevel@tonic-gate 		b |= TIOCM_RTS;
40200Sstevel@tonic-gate 
40210Sstevel@tonic-gate 	if (mcr_r & DTR)
40220Sstevel@tonic-gate 		b |= TIOCM_DTR;
40230Sstevel@tonic-gate 
40240Sstevel@tonic-gate 	/* MSR registers */
40250Sstevel@tonic-gate 	if (msr_r & DCD)
40260Sstevel@tonic-gate 		b |= TIOCM_CAR;
40270Sstevel@tonic-gate 
40280Sstevel@tonic-gate 	if (msr_r & CTS)
40290Sstevel@tonic-gate 		b |= TIOCM_CTS;
40300Sstevel@tonic-gate 
40310Sstevel@tonic-gate 	if (msr_r & DSR)
40320Sstevel@tonic-gate 		b |= TIOCM_DSR;
40330Sstevel@tonic-gate 
40340Sstevel@tonic-gate 	if (msr_r & RI)
40350Sstevel@tonic-gate 		b |= TIOCM_RNG;
40360Sstevel@tonic-gate 	return (b);
40370Sstevel@tonic-gate }
40380Sstevel@tonic-gate 
40390Sstevel@tonic-gate static int
40400Sstevel@tonic-gate dmtoasy(int bits)
40410Sstevel@tonic-gate {
40420Sstevel@tonic-gate 	int b = 0;
40430Sstevel@tonic-gate 
40440Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_MODEM, "dmtoasy: bits = %x\n", bits);
40450Sstevel@tonic-gate #ifdef	CAN_NOT_SET	/* only DTR and RTS can be set */
40460Sstevel@tonic-gate 	if (bits & TIOCM_CAR)
40470Sstevel@tonic-gate 		b |= DCD;
40480Sstevel@tonic-gate 	if (bits & TIOCM_CTS)
40490Sstevel@tonic-gate 		b |= CTS;
40500Sstevel@tonic-gate 	if (bits & TIOCM_DSR)
40510Sstevel@tonic-gate 		b |= DSR;
40520Sstevel@tonic-gate 	if (bits & TIOCM_RNG)
40530Sstevel@tonic-gate 		b |= RI;
40540Sstevel@tonic-gate #endif
40550Sstevel@tonic-gate 
40560Sstevel@tonic-gate 	if (bits & TIOCM_RTS) {
40570Sstevel@tonic-gate 		DEBUGCONT0(ASY_DEBUG_MODEM, "dmtoasy: set b & RTS\n");
40580Sstevel@tonic-gate 		b |= RTS;
40590Sstevel@tonic-gate 	}
40600Sstevel@tonic-gate 	if (bits & TIOCM_DTR) {
40610Sstevel@tonic-gate 		DEBUGCONT0(ASY_DEBUG_MODEM, "dmtoasy: set b & DTR\n");
40620Sstevel@tonic-gate 		b |= DTR;
40630Sstevel@tonic-gate 	}
40640Sstevel@tonic-gate 
40650Sstevel@tonic-gate 	return (b);
40660Sstevel@tonic-gate }
40670Sstevel@tonic-gate 
40680Sstevel@tonic-gate static void
40690Sstevel@tonic-gate asyerror(int level, const char *fmt, ...)
40700Sstevel@tonic-gate {
40710Sstevel@tonic-gate 	va_list adx;
40720Sstevel@tonic-gate 	static	time_t	last;
40730Sstevel@tonic-gate 	static	const char *lastfmt;
40740Sstevel@tonic-gate 	time_t	now;
40750Sstevel@tonic-gate 
40760Sstevel@tonic-gate 	/*
40770Sstevel@tonic-gate 	 * Don't print the same error message too often.
40780Sstevel@tonic-gate 	 * Print the message only if we have not printed the
40790Sstevel@tonic-gate 	 * message within the last second.
40800Sstevel@tonic-gate 	 * Note: that fmt cannot be a pointer to a string
40810Sstevel@tonic-gate 	 * stored on the stack. The fmt pointer
40820Sstevel@tonic-gate 	 * must be in the data segment otherwise lastfmt would point
40830Sstevel@tonic-gate 	 * to non-sense.
40840Sstevel@tonic-gate 	 */
40850Sstevel@tonic-gate 	now = gethrestime_sec();
40860Sstevel@tonic-gate 	if (last == now && lastfmt == fmt)
40870Sstevel@tonic-gate 		return;
40880Sstevel@tonic-gate 
40890Sstevel@tonic-gate 	last = now;
40900Sstevel@tonic-gate 	lastfmt = fmt;
40910Sstevel@tonic-gate 
40920Sstevel@tonic-gate 	va_start(adx, fmt);
40930Sstevel@tonic-gate 	vcmn_err(level, fmt, adx);
40940Sstevel@tonic-gate 	va_end(adx);
40950Sstevel@tonic-gate }
40960Sstevel@tonic-gate 
40970Sstevel@tonic-gate /*
40980Sstevel@tonic-gate  * asy_parse_mode(dev_info_t *devi, struct asycom *asy)
40990Sstevel@tonic-gate  * The value of this property is in the form of "9600,8,n,1,-"
41000Sstevel@tonic-gate  * 1) speed: 9600, 4800, ...
41010Sstevel@tonic-gate  * 2) data bits
41020Sstevel@tonic-gate  * 3) parity: n(none), e(even), o(odd)
41030Sstevel@tonic-gate  * 4) stop bits
41040Sstevel@tonic-gate  * 5) handshake: -(none), h(hardware: rts/cts), s(software: xon/off)
41050Sstevel@tonic-gate  *
41060Sstevel@tonic-gate  * This parsing came from a SPARCstation eeprom.
41070Sstevel@tonic-gate  */
41080Sstevel@tonic-gate static void
41090Sstevel@tonic-gate asy_parse_mode(dev_info_t *devi, struct asycom *asy)
41100Sstevel@tonic-gate {
41110Sstevel@tonic-gate 	char		name[40];
41120Sstevel@tonic-gate 	char		val[40];
41130Sstevel@tonic-gate 	int		len;
41140Sstevel@tonic-gate 	int		ret;
41150Sstevel@tonic-gate 	char		*p;
41160Sstevel@tonic-gate 	char		*p1;
41170Sstevel@tonic-gate 
41180Sstevel@tonic-gate 	ASSERT(asy->asy_com_port != 0);
41190Sstevel@tonic-gate 
41200Sstevel@tonic-gate 	/*
41210Sstevel@tonic-gate 	 * Parse the ttyx-mode property
41220Sstevel@tonic-gate 	 */
41230Sstevel@tonic-gate 	(void) sprintf(name, "tty%c-mode", asy->asy_com_port + 'a' - 1);
41240Sstevel@tonic-gate 	len = sizeof (val);
41250Sstevel@tonic-gate 	ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
41260Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
41270Sstevel@tonic-gate 		(void) sprintf(name, "com%c-mode", asy->asy_com_port + '0');
41280Sstevel@tonic-gate 		len = sizeof (val);
41290Sstevel@tonic-gate 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
41300Sstevel@tonic-gate 	}
41310Sstevel@tonic-gate 
41320Sstevel@tonic-gate 	/* no property to parse */
41330Sstevel@tonic-gate 	asy->asy_cflag = 0;
41340Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS)
41350Sstevel@tonic-gate 		return;
41360Sstevel@tonic-gate 
41370Sstevel@tonic-gate 	p = val;
41380Sstevel@tonic-gate 	/* ---- baud rate ---- */
41390Sstevel@tonic-gate 	asy->asy_cflag = CREAD|B9600;		/* initial default */
41400Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0) {
41410Sstevel@tonic-gate 		*p1++ = '\0';
41420Sstevel@tonic-gate 	} else {
41430Sstevel@tonic-gate 		asy->asy_cflag |= BITS8;	/* add default bits */
41440Sstevel@tonic-gate 		return;
41450Sstevel@tonic-gate 	}
41460Sstevel@tonic-gate 
41470Sstevel@tonic-gate 	if (strcmp(p, "110") == 0)
41480Sstevel@tonic-gate 		asy->asy_bidx = B110;
41490Sstevel@tonic-gate 	else if (strcmp(p, "150") == 0)
41500Sstevel@tonic-gate 		asy->asy_bidx = B150;
41510Sstevel@tonic-gate 	else if (strcmp(p, "300") == 0)
41520Sstevel@tonic-gate 		asy->asy_bidx = B300;
41530Sstevel@tonic-gate 	else if (strcmp(p, "600") == 0)
41540Sstevel@tonic-gate 		asy->asy_bidx = B600;
41550Sstevel@tonic-gate 	else if (strcmp(p, "1200") == 0)
41560Sstevel@tonic-gate 		asy->asy_bidx = B1200;
41570Sstevel@tonic-gate 	else if (strcmp(p, "2400") == 0)
41580Sstevel@tonic-gate 		asy->asy_bidx = B2400;
41590Sstevel@tonic-gate 	else if (strcmp(p, "4800") == 0)
41600Sstevel@tonic-gate 		asy->asy_bidx = B4800;
41610Sstevel@tonic-gate 	else if (strcmp(p, "9600") == 0)
41620Sstevel@tonic-gate 		asy->asy_bidx = B9600;
41630Sstevel@tonic-gate 	else if (strcmp(p, "19200") == 0)
41640Sstevel@tonic-gate 		asy->asy_bidx = B19200;
41650Sstevel@tonic-gate 	else if (strcmp(p, "38400") == 0)
41660Sstevel@tonic-gate 		asy->asy_bidx = B38400;
41670Sstevel@tonic-gate 	else if (strcmp(p, "57600") == 0)
41680Sstevel@tonic-gate 		asy->asy_bidx = B57600;
41690Sstevel@tonic-gate 	else if (strcmp(p, "115200") == 0)
41700Sstevel@tonic-gate 		asy->asy_bidx = B115200;
41710Sstevel@tonic-gate 	else
41720Sstevel@tonic-gate 		asy->asy_bidx = B9600;
41730Sstevel@tonic-gate 
41740Sstevel@tonic-gate 	asy->asy_cflag &= ~CBAUD;
41750Sstevel@tonic-gate 	if (asy->asy_bidx > CBAUD) {	/* > 38400 uses the CBAUDEXT bit */
41760Sstevel@tonic-gate 		asy->asy_cflag |= CBAUDEXT;
41770Sstevel@tonic-gate 		asy->asy_cflag |= asy->asy_bidx - CBAUD - 1;
41780Sstevel@tonic-gate 	} else {
41790Sstevel@tonic-gate 		asy->asy_cflag |= asy->asy_bidx;
41800Sstevel@tonic-gate 	}
41810Sstevel@tonic-gate 
41820Sstevel@tonic-gate 	ASSERT(asy->asy_bidx == BAUDINDEX(asy->asy_cflag));
41830Sstevel@tonic-gate 
41840Sstevel@tonic-gate 	/* ---- Next item is data bits ---- */
41850Sstevel@tonic-gate 	p = p1;
41860Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0)  {
41870Sstevel@tonic-gate 		*p1++ = '\0';
41880Sstevel@tonic-gate 	} else {
41890Sstevel@tonic-gate 		asy->asy_cflag |= BITS8;	/* add default bits */
41900Sstevel@tonic-gate 		return;
41910Sstevel@tonic-gate 	}
41920Sstevel@tonic-gate 	switch (*p) {
41930Sstevel@tonic-gate 		default:
41940Sstevel@tonic-gate 		case '8':
41950Sstevel@tonic-gate 			asy->asy_cflag |= CS8;
41960Sstevel@tonic-gate 			asy->asy_lcr = BITS8;
41970Sstevel@tonic-gate 			break;
41980Sstevel@tonic-gate 		case '7':
41990Sstevel@tonic-gate 			asy->asy_cflag |= CS7;
42000Sstevel@tonic-gate 			asy->asy_lcr = BITS7;
42010Sstevel@tonic-gate 			break;
42020Sstevel@tonic-gate 		case '6':
42030Sstevel@tonic-gate 			asy->asy_cflag |= CS6;
42040Sstevel@tonic-gate 			asy->asy_lcr = BITS6;
42050Sstevel@tonic-gate 			break;
42060Sstevel@tonic-gate 		case '5':
42070Sstevel@tonic-gate 			/* LINTED: CS5 is currently zero (but might change) */
42080Sstevel@tonic-gate 			asy->asy_cflag |= CS5;
42090Sstevel@tonic-gate 			asy->asy_lcr = BITS5;
42100Sstevel@tonic-gate 			break;
42110Sstevel@tonic-gate 	}
42120Sstevel@tonic-gate 
42130Sstevel@tonic-gate 	/* ---- Parity info ---- */
42140Sstevel@tonic-gate 	p = p1;
42150Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0)  {
42160Sstevel@tonic-gate 		*p1++ = '\0';
42170Sstevel@tonic-gate 	} else {
42180Sstevel@tonic-gate 		return;
42190Sstevel@tonic-gate 	}
42200Sstevel@tonic-gate 	switch (*p)  {
42210Sstevel@tonic-gate 		default:
42220Sstevel@tonic-gate 		case 'n':
42230Sstevel@tonic-gate 			break;
42240Sstevel@tonic-gate 		case 'e':
42250Sstevel@tonic-gate 			asy->asy_cflag |= PARENB;
42260Sstevel@tonic-gate 			asy->asy_lcr |= PEN; break;
42270Sstevel@tonic-gate 		case 'o':
42280Sstevel@tonic-gate 			asy->asy_cflag |= PARENB|PARODD;
42290Sstevel@tonic-gate 			asy->asy_lcr |= PEN|EPS;
42300Sstevel@tonic-gate 			break;
42310Sstevel@tonic-gate 	}
42320Sstevel@tonic-gate 
42330Sstevel@tonic-gate 	/* ---- Find stop bits ---- */
42340Sstevel@tonic-gate 	p = p1;
42350Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0)  {
42360Sstevel@tonic-gate 		*p1++ = '\0';
42370Sstevel@tonic-gate 	} else {
42380Sstevel@tonic-gate 		return;
42390Sstevel@tonic-gate 	}
42400Sstevel@tonic-gate 	if (*p == '2') {
42410Sstevel@tonic-gate 		asy->asy_cflag |= CSTOPB;
42420Sstevel@tonic-gate 		asy->asy_lcr |= STB;
42430Sstevel@tonic-gate 	}
42440Sstevel@tonic-gate 
42450Sstevel@tonic-gate 	/* ---- handshake is next ---- */
42460Sstevel@tonic-gate 	p = p1;
42470Sstevel@tonic-gate 	if (p) {
42480Sstevel@tonic-gate 		if ((p1 = strchr(p, ',')) != 0)
42490Sstevel@tonic-gate 			*p1++ = '\0';
42500Sstevel@tonic-gate 
42510Sstevel@tonic-gate 		if (*p == 'h')
42520Sstevel@tonic-gate 			asy->asy_cflag |= CRTSCTS;
42530Sstevel@tonic-gate 		else if (*p == 's')
42540Sstevel@tonic-gate 			asy->asy_cflag |= CRTSXOFF;
42550Sstevel@tonic-gate 	}
42560Sstevel@tonic-gate }
42570Sstevel@tonic-gate 
42580Sstevel@tonic-gate /*
42590Sstevel@tonic-gate  * Check for abort character sequence
42600Sstevel@tonic-gate  */
42610Sstevel@tonic-gate static boolean_t
42620Sstevel@tonic-gate abort_charseq_recognize(uchar_t ch)
42630Sstevel@tonic-gate {
42640Sstevel@tonic-gate 	static int state = 0;
42650Sstevel@tonic-gate #define	CNTRL(c) ((c)&037)
42660Sstevel@tonic-gate 	static char sequence[] = { '\r', '~', CNTRL('b') };
42670Sstevel@tonic-gate 
42680Sstevel@tonic-gate 	if (ch == sequence[state]) {
42690Sstevel@tonic-gate 		if (++state >= sizeof (sequence)) {
42700Sstevel@tonic-gate 			state = 0;
42710Sstevel@tonic-gate 			return (B_TRUE);
42720Sstevel@tonic-gate 		}
42730Sstevel@tonic-gate 	} else {
42740Sstevel@tonic-gate 		state = (ch == sequence[0]) ? 1 : 0;
42750Sstevel@tonic-gate 	}
42760Sstevel@tonic-gate 	return (B_FALSE);
42770Sstevel@tonic-gate }
42780Sstevel@tonic-gate 
42790Sstevel@tonic-gate /*
42800Sstevel@tonic-gate  * Flow control functions
42810Sstevel@tonic-gate  */
42820Sstevel@tonic-gate /*
42830Sstevel@tonic-gate  * Software input flow control
42840Sstevel@tonic-gate  * This function can execute software input flow control sucessfully
42850Sstevel@tonic-gate  * at most of situations except that the line is in BREAK status
42860Sstevel@tonic-gate  * (timed and untimed break).
42870Sstevel@tonic-gate  * INPUT VALUE of onoff:
42880Sstevel@tonic-gate  *               FLOW_START means to send out a XON char
42890Sstevel@tonic-gate  *                          and clear SW input flow control flag.
42900Sstevel@tonic-gate  *               FLOW_STOP means to send out a XOFF char
42910Sstevel@tonic-gate  *                          and set SW input flow control flag.
42920Sstevel@tonic-gate  *               FLOW_CHECK means to check whether there is pending XON/XOFF
42930Sstevel@tonic-gate  *                          if it is true, send it out.
42940Sstevel@tonic-gate  * INPUT VALUE of type:
42950Sstevel@tonic-gate  *		 IN_FLOW_RINGBUFF means flow control is due to RING BUFFER
42960Sstevel@tonic-gate  *		 IN_FLOW_STREAMS means flow control is due to STREAMS
42970Sstevel@tonic-gate  *		 IN_FLOW_USER means flow control is due to user's commands
42980Sstevel@tonic-gate  * RETURN VALUE: B_FALSE means no flow control char is sent
42990Sstevel@tonic-gate  *               B_TRUE means one flow control char is sent
43000Sstevel@tonic-gate  */
43010Sstevel@tonic-gate static boolean_t
43020Sstevel@tonic-gate async_flowcontrol_sw_input(struct asycom *asy, async_flowc_action onoff,
43030Sstevel@tonic-gate     int type)
43040Sstevel@tonic-gate {
43050Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
43060Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
43070Sstevel@tonic-gate 	int rval = B_FALSE;
43080Sstevel@tonic-gate 
43090Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
43100Sstevel@tonic-gate 
43110Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_iflag & IXOFF))
43120Sstevel@tonic-gate 		return (rval);
43130Sstevel@tonic-gate 
43140Sstevel@tonic-gate 	/*
43150Sstevel@tonic-gate 	 * If we get this far, then we know IXOFF is set.
43160Sstevel@tonic-gate 	 */
43170Sstevel@tonic-gate 	switch (onoff) {
43180Sstevel@tonic-gate 	case FLOW_STOP:
43190Sstevel@tonic-gate 		async->async_inflow_source |= type;
43200Sstevel@tonic-gate 
43210Sstevel@tonic-gate 		/*
43220Sstevel@tonic-gate 		 * We'll send an XOFF character for each of up to
43230Sstevel@tonic-gate 		 * three different input flow control attempts to stop input.
43240Sstevel@tonic-gate 		 * If we already send out one XOFF, but FLOW_STOP comes again,
43250Sstevel@tonic-gate 		 * it seems that input flow control becomes more serious,
43260Sstevel@tonic-gate 		 * then send XOFF again.
43270Sstevel@tonic-gate 		 */
43280Sstevel@tonic-gate 		if (async->async_inflow_source & (IN_FLOW_RINGBUFF |
43290Sstevel@tonic-gate 		    IN_FLOW_STREAMS | IN_FLOW_USER))
43300Sstevel@tonic-gate 			async->async_flags |= ASYNC_SW_IN_FLOW |
43310Sstevel@tonic-gate 			    ASYNC_SW_IN_NEEDED;
43320Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_SFLOW, "async%d: input sflow stop, "
43330Sstevel@tonic-gate 		    "type = %x\n", instance, async->async_inflow_source);
43340Sstevel@tonic-gate 		break;
43350Sstevel@tonic-gate 	case FLOW_START:
43360Sstevel@tonic-gate 		async->async_inflow_source &= ~type;
43370Sstevel@tonic-gate 		if (async->async_inflow_source == 0) {
43380Sstevel@tonic-gate 			async->async_flags = (async->async_flags &
43390Sstevel@tonic-gate 			    ~ASYNC_SW_IN_FLOW) | ASYNC_SW_IN_NEEDED;
43400Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_SFLOW, "async%d: "
43410Sstevel@tonic-gate 			    "input sflow start\n", instance);
43420Sstevel@tonic-gate 		}
43430Sstevel@tonic-gate 		break;
43440Sstevel@tonic-gate 	default:
43450Sstevel@tonic-gate 		break;
43460Sstevel@tonic-gate 	}
43470Sstevel@tonic-gate 
43480Sstevel@tonic-gate 	if (((async->async_flags & (ASYNC_SW_IN_NEEDED | ASYNC_BREAK |
43490Sstevel@tonic-gate 	    ASYNC_OUT_SUSPEND)) == ASYNC_SW_IN_NEEDED) &&
43501106Smrj 	    (ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) & XHRE)) {
43510Sstevel@tonic-gate 		/*
43520Sstevel@tonic-gate 		 * If we get this far, then we know we need to send out
43530Sstevel@tonic-gate 		 * XON or XOFF char.
43540Sstevel@tonic-gate 		 */
43550Sstevel@tonic-gate 		async->async_flags = (async->async_flags &
43560Sstevel@tonic-gate 		    ~ASYNC_SW_IN_NEEDED) | ASYNC_BUSY;
43571106Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
43580Sstevel@tonic-gate 		    async->async_flags & ASYNC_SW_IN_FLOW ?
43590Sstevel@tonic-gate 		    async->async_stopc : async->async_startc);
43600Sstevel@tonic-gate 		rval = B_TRUE;
43610Sstevel@tonic-gate 	}
43620Sstevel@tonic-gate 	return (rval);
43630Sstevel@tonic-gate }
43640Sstevel@tonic-gate 
43650Sstevel@tonic-gate /*
43660Sstevel@tonic-gate  * Software output flow control
43670Sstevel@tonic-gate  * This function can be executed sucessfully at any situation.
43680Sstevel@tonic-gate  * It does not handle HW, and just change the SW output flow control flag.
43690Sstevel@tonic-gate  * INPUT VALUE of onoff:
43700Sstevel@tonic-gate  *                 FLOW_START means to clear SW output flow control flag,
43710Sstevel@tonic-gate  *			also combine with HW output flow control status to
43720Sstevel@tonic-gate  *			determine if we need to set ASYNC_OUT_FLW_RESUME.
43730Sstevel@tonic-gate  *                 FLOW_STOP means to set SW output flow control flag,
43740Sstevel@tonic-gate  *			also clear ASYNC_OUT_FLW_RESUME.
43750Sstevel@tonic-gate  */
43760Sstevel@tonic-gate static void
43770Sstevel@tonic-gate async_flowcontrol_sw_output(struct asycom *asy, async_flowc_action onoff)
43780Sstevel@tonic-gate {
43790Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
43800Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
43810Sstevel@tonic-gate 
43820Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
43830Sstevel@tonic-gate 
43840Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_iflag & IXON))
43850Sstevel@tonic-gate 		return;
43860Sstevel@tonic-gate 
43870Sstevel@tonic-gate 	switch (onoff) {
43880Sstevel@tonic-gate 	case FLOW_STOP:
43890Sstevel@tonic-gate 		async->async_flags |= ASYNC_SW_OUT_FLW;
43900Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
43910Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_SFLOW, "async%d: output sflow stop\n",
43920Sstevel@tonic-gate 		    instance);
43930Sstevel@tonic-gate 		break;
43940Sstevel@tonic-gate 	case FLOW_START:
43950Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_SW_OUT_FLW;
43960Sstevel@tonic-gate 		if (!(async->async_flags & ASYNC_HW_OUT_FLW))
43970Sstevel@tonic-gate 			async->async_flags |= ASYNC_OUT_FLW_RESUME;
43980Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_SFLOW, "async%d: output sflow start\n",
43990Sstevel@tonic-gate 		    instance);
44000Sstevel@tonic-gate 		break;
44010Sstevel@tonic-gate 	default:
44020Sstevel@tonic-gate 		break;
44030Sstevel@tonic-gate 	}
44040Sstevel@tonic-gate }
44050Sstevel@tonic-gate 
44060Sstevel@tonic-gate /*
44070Sstevel@tonic-gate  * Hardware input flow control
44080Sstevel@tonic-gate  * This function can be executed sucessfully at any situation.
44090Sstevel@tonic-gate  * It directly changes RTS depending on input parameter onoff.
44100Sstevel@tonic-gate  * INPUT VALUE of onoff:
44110Sstevel@tonic-gate  *       FLOW_START means to clear HW input flow control flag,
44120Sstevel@tonic-gate  *                  and pull up RTS if it is low.
44130Sstevel@tonic-gate  *       FLOW_STOP means to set HW input flow control flag,
44140Sstevel@tonic-gate  *                  and low RTS if it is high.
44150Sstevel@tonic-gate  * INPUT VALUE of type:
44160Sstevel@tonic-gate  *		 IN_FLOW_RINGBUFF means flow control is due to RING BUFFER
44170Sstevel@tonic-gate  *		 IN_FLOW_STREAMS means flow control is due to STREAMS
44180Sstevel@tonic-gate  *		 IN_FLOW_USER means flow control is due to user's commands
44190Sstevel@tonic-gate  */
44200Sstevel@tonic-gate static void
44210Sstevel@tonic-gate async_flowcontrol_hw_input(struct asycom *asy, async_flowc_action onoff,
44220Sstevel@tonic-gate     int type)
44230Sstevel@tonic-gate {
44240Sstevel@tonic-gate 	uchar_t	mcr;
44250Sstevel@tonic-gate 	uchar_t	flag;
44260Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
44270Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
44280Sstevel@tonic-gate 
44290Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
44300Sstevel@tonic-gate 
44310Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_cflag & CRTSXOFF))
44320Sstevel@tonic-gate 		return;
44330Sstevel@tonic-gate 
44340Sstevel@tonic-gate 	switch (onoff) {
44350Sstevel@tonic-gate 	case FLOW_STOP:
44360Sstevel@tonic-gate 		async->async_inflow_source |= type;
44370Sstevel@tonic-gate 		if (async->async_inflow_source & (IN_FLOW_RINGBUFF |
44380Sstevel@tonic-gate 		    IN_FLOW_STREAMS | IN_FLOW_USER))
44390Sstevel@tonic-gate 			async->async_flags |= ASYNC_HW_IN_FLOW;
44400Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_HFLOW, "async%d: input hflow stop, "
44410Sstevel@tonic-gate 		    "type = %x\n", instance, async->async_inflow_source);
44420Sstevel@tonic-gate 		break;
44430Sstevel@tonic-gate 	case FLOW_START:
44440Sstevel@tonic-gate 		async->async_inflow_source &= ~type;
44450Sstevel@tonic-gate 		if (async->async_inflow_source == 0) {
44460Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_HW_IN_FLOW;
44470Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_HFLOW, "async%d: "
44480Sstevel@tonic-gate 			    "input hflow start\n", instance);
44490Sstevel@tonic-gate 		}
44500Sstevel@tonic-gate 		break;
44510Sstevel@tonic-gate 	default:
44520Sstevel@tonic-gate 		break;
44530Sstevel@tonic-gate 	}
44541106Smrj 	mcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
44550Sstevel@tonic-gate 	flag = (async->async_flags & ASYNC_HW_IN_FLOW) ? 0 : RTS;
44560Sstevel@tonic-gate 
44570Sstevel@tonic-gate 	if (((mcr ^ flag) & RTS) != 0) {
44581106Smrj 		ddi_put8(asy->asy_iohandle,
44590Sstevel@tonic-gate 		    asy->asy_ioaddr + MCR, (mcr ^ RTS));
44600Sstevel@tonic-gate 	}
44610Sstevel@tonic-gate }
44620Sstevel@tonic-gate 
44630Sstevel@tonic-gate /*
44640Sstevel@tonic-gate  * Hardware output flow control
44650Sstevel@tonic-gate  * This function can execute HW output flow control sucessfully
44660Sstevel@tonic-gate  * at any situation.
44670Sstevel@tonic-gate  * It doesn't really change RTS, and just change
44680Sstevel@tonic-gate  * HW output flow control flag depending on CTS status.
44690Sstevel@tonic-gate  * INPUT VALUE of onoff:
44700Sstevel@tonic-gate  *                FLOW_START means to clear HW output flow control flag.
44710Sstevel@tonic-gate  *			also combine with SW output flow control status to
44720Sstevel@tonic-gate  *			determine if we need to set ASYNC_OUT_FLW_RESUME.
44730Sstevel@tonic-gate  *                FLOW_STOP means to set HW output flow control flag.
44740Sstevel@tonic-gate  *			also clear ASYNC_OUT_FLW_RESUME.
44750Sstevel@tonic-gate  */
44760Sstevel@tonic-gate static void
44770Sstevel@tonic-gate async_flowcontrol_hw_output(struct asycom *asy, async_flowc_action onoff)
44780Sstevel@tonic-gate {
44790Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
44800Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
44810Sstevel@tonic-gate 
44820Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
44830Sstevel@tonic-gate 
44840Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_cflag & CRTSCTS))
44850Sstevel@tonic-gate 		return;
44860Sstevel@tonic-gate 
44870Sstevel@tonic-gate 	switch (onoff) {
44880Sstevel@tonic-gate 	case FLOW_STOP:
44890Sstevel@tonic-gate 		async->async_flags |= ASYNC_HW_OUT_FLW;
44900Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
44910Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_HFLOW, "async%d: output hflow stop\n",
44920Sstevel@tonic-gate 		    instance);
44930Sstevel@tonic-gate 		break;
44940Sstevel@tonic-gate 	case FLOW_START:
44950Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_HW_OUT_FLW;
44960Sstevel@tonic-gate 		if (!(async->async_flags & ASYNC_SW_OUT_FLW))
44970Sstevel@tonic-gate 			async->async_flags |= ASYNC_OUT_FLW_RESUME;
44980Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_HFLOW, "async%d: output hflow start\n",
44990Sstevel@tonic-gate 		    instance);
45000Sstevel@tonic-gate 		break;
45010Sstevel@tonic-gate 	default:
45020Sstevel@tonic-gate 		break;
45030Sstevel@tonic-gate 	}
45040Sstevel@tonic-gate }
4505