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