xref: /illumos-gate/usr/src/uts/common/io/asy.c (revision 281f0747d41a786d6474ec71e7dc8dad1758a176)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54ab75253Smrj  * Common Development and Distribution License (the "License").
64ab75253Smrj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
214ab75253Smrj 
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
237c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
247c478bd9Sstevel@tonic-gate /*	  All Rights Reserved					*/
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
27*281f0747Slt200341  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * Serial I/O driver for 8250/16450/16550A/16650/16750 chips.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/param.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/signal.h>
407c478bd9Sstevel@tonic-gate #include <sys/stream.h>
417c478bd9Sstevel@tonic-gate #include <sys/termio.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/file.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
467c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
477c478bd9Sstevel@tonic-gate #include <sys/strtty.h>
487c478bd9Sstevel@tonic-gate #include <sys/debug.h>
497c478bd9Sstevel@tonic-gate #include <sys/kbio.h>
507c478bd9Sstevel@tonic-gate #include <sys/cred.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
537c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
547c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
557c478bd9Sstevel@tonic-gate #include <sys/cred.h>
567c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
577c478bd9Sstevel@tonic-gate #ifdef DEBUG
587c478bd9Sstevel@tonic-gate #include <sys/promif.h>
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
617c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
627c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
637c478bd9Sstevel@tonic-gate #include <sys/asy.h>
647c478bd9Sstevel@tonic-gate #include <sys/policy.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * set the RX FIFO trigger_level to half the RX FIFO size for now
687c478bd9Sstevel@tonic-gate  * we may want to make this configurable later.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate static	int asy_trig_level = FIFO_TRIG_8;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate int asy_drain_check = 15000000;		/* tunable: exit drain check time */
737c478bd9Sstevel@tonic-gate int asy_min_dtr_low = 500000;		/* tunable: minimum DTR down time */
747c478bd9Sstevel@tonic-gate int asy_min_utbrk = 100000;		/* tunable: minumum untimed brk time */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate int asymaxchip = ASY16750;	/* tunable: limit chip support we look for */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Just in case someone has a chip with broken loopback mode, we provide a
807c478bd9Sstevel@tonic-gate  * means to disable the loopback test. By default, we only loopback test
817c478bd9Sstevel@tonic-gate  * UARTs which look like they have FIFOs bigger than 16 bytes.
827c478bd9Sstevel@tonic-gate  * Set to 0 to suppress test, or to 2 to enable test on any size FIFO.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate int asy_fifo_test = 1;		/* tunable: set to 0, 1, or 2 */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Allow ability to switch off testing of the scratch register.
887c478bd9Sstevel@tonic-gate  * Some UART emulators might not have it. This will also disable the test
897c478bd9Sstevel@tonic-gate  * for Exar/Startech ST16C650, as that requires use of the SCR register.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate int asy_scr_test = 1;		/* tunable: set to 0 to disable SCR reg test */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * As we don't yet support on-chip flow control, it's a bad idea to put a
957c478bd9Sstevel@tonic-gate  * large number of characters in the TX FIFO, since if other end tells us
967c478bd9Sstevel@tonic-gate  * to stop transmitting, we can only stop filling the TX FIFO, but it will
977c478bd9Sstevel@tonic-gate  * still carry on draining by itself, so remote end still gets what's left
987c478bd9Sstevel@tonic-gate  * in the FIFO.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate int asy_max_tx_fifo = 16;	/* tunable: max fill of TX FIFO */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define	async_stopc	async_ttycommon.t_stopc
1037c478bd9Sstevel@tonic-gate #define	async_startc	async_ttycommon.t_startc
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #define	ASY_INIT	1
1067c478bd9Sstevel@tonic-gate #define	ASY_NOINIT	0
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /* enum value for sw and hw flow control action */
1097c478bd9Sstevel@tonic-gate typedef enum {
1107c478bd9Sstevel@tonic-gate 	FLOW_CHECK,
1117c478bd9Sstevel@tonic-gate 	FLOW_STOP,
1127c478bd9Sstevel@tonic-gate 	FLOW_START
1137c478bd9Sstevel@tonic-gate } async_flowc_action;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #ifdef DEBUG
1167c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_INIT	0x0001	/* Output msgs during driver initialization. */
1177c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_INPUT	0x0002	/* Report characters received during int. */
1187c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_EOT	0x0004	/* Output msgs when wait for xmit to finish. */
1197c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_CLOSE	0x0008	/* Output msgs when driver open/close called */
1207c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_HFLOW	0x0010	/* Output msgs when H/W flowcontrol is active */
1217c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_PROCS	0x0020	/* Output each proc name as it is entered. */
1227c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_STATE	0x0040	/* Output value of Interrupt Service Reg. */
1237c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_INTR	0x0080	/* Output value of Interrupt Service Reg. */
1247c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_OUT	0x0100	/* Output msgs about output events. */
1257c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_BUSY	0x0200	/* Output msgs when xmit is enabled/disabled */
1267c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_MODEM	0x0400	/* Output msgs about modem status & control. */
1277c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_MODM2	0x0800	/* Output msgs about modem status & control. */
1287c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_IOCTL	0x1000	/* Output msgs about ioctl messages. */
1297c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_CHIP	0x2000	/* Output msgs about chip identification. */
1307c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_SFLOW	0x4000	/* Output msgs when S/W flowcontrol is active */
1317c478bd9Sstevel@tonic-gate #define	ASY_DEBUG(x) (debug & (x))
1327c478bd9Sstevel@tonic-gate static	int debug  = 0;
1337c478bd9Sstevel@tonic-gate #else
1347c478bd9Sstevel@tonic-gate #define	ASY_DEBUG(x) B_FALSE
1357c478bd9Sstevel@tonic-gate #endif
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /* pnpISA compressed device ids */
1387c478bd9Sstevel@tonic-gate #define	pnpMTS0219 0xb6930219	/* Multitech MT5634ZTX modem */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * PPS (Pulse Per Second) support.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate void ddi_hardpps();
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * This is protected by the asy_excl_hi of the port on which PPS event
1467c478bd9Sstevel@tonic-gate  * handling is enabled.  Note that only one port should have this enabled at
1477c478bd9Sstevel@tonic-gate  * any one time.  Enabling PPS handling on multiple ports will result in
1487c478bd9Sstevel@tonic-gate  * unpredictable (but benign) results.
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate static struct ppsclockev asy_ppsev;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate #ifdef PPSCLOCKLED
1537c478bd9Sstevel@tonic-gate /* XXX Use these to observe PPS latencies and jitter on a scope */
1547c478bd9Sstevel@tonic-gate #define	LED_ON
1557c478bd9Sstevel@tonic-gate #define	LED_OFF
1567c478bd9Sstevel@tonic-gate #else
1577c478bd9Sstevel@tonic-gate #define	LED_ON
1587c478bd9Sstevel@tonic-gate #define	LED_OFF
1597c478bd9Sstevel@tonic-gate #endif
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static	int max_asy_instance = -1;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static	uint_t	asysoftintr(caddr_t intarg);
1647c478bd9Sstevel@tonic-gate static	uint_t	asyintr(caddr_t argasy);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate static boolean_t abort_charseq_recognize(uchar_t ch);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /* The async interrupt entry points */
1697c478bd9Sstevel@tonic-gate static void	async_txint(struct asycom *asy);
1707c478bd9Sstevel@tonic-gate static void	async_rxint(struct asycom *asy, uchar_t lsr);
1717c478bd9Sstevel@tonic-gate static void	async_msint(struct asycom *asy);
1727c478bd9Sstevel@tonic-gate static void	async_softint(struct asycom *asy);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate static void	async_ioctl(struct asyncline *async, queue_t *q, mblk_t *mp);
1757c478bd9Sstevel@tonic-gate static void	async_reioctl(void *unit);
1767c478bd9Sstevel@tonic-gate static void	async_iocdata(queue_t *q, mblk_t *mp);
1777c478bd9Sstevel@tonic-gate static void	async_restart(void *arg);
1787c478bd9Sstevel@tonic-gate static void	async_start(struct asyncline *async);
1797c478bd9Sstevel@tonic-gate static void	async_nstart(struct asyncline *async, int mode);
1807c478bd9Sstevel@tonic-gate static void	async_resume(struct asyncline *async);
1817c478bd9Sstevel@tonic-gate static void	asy_program(struct asycom *asy, int mode);
1827c478bd9Sstevel@tonic-gate static void	asyinit(struct asycom *asy);
1837c478bd9Sstevel@tonic-gate static void	asy_waiteot(struct asycom *asy);
184*281f0747Slt200341 static void	asyputchar(cons_polledio_arg_t, uchar_t c);
185*281f0747Slt200341 static int	asygetchar(cons_polledio_arg_t);
186*281f0747Slt200341 static boolean_t	asyischar(cons_polledio_arg_t);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate static int	asymctl(struct asycom *, int, int);
1897c478bd9Sstevel@tonic-gate static int	asytodm(int, int);
1907c478bd9Sstevel@tonic-gate static int	dmtoasy(int);
1917c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
1927c478bd9Sstevel@tonic-gate static void	asyerror(int level, const char *fmt, ...) __KPRINTFLIKE(2);
1937c478bd9Sstevel@tonic-gate static void	asy_parse_mode(dev_info_t *devi, struct asycom *asy);
1947c478bd9Sstevel@tonic-gate static void	asy_soft_state_free(struct asycom *);
1957c478bd9Sstevel@tonic-gate static char	*asy_hw_name(struct asycom *asy);
1967c478bd9Sstevel@tonic-gate static void	async_hold_utbrk(void *arg);
1977c478bd9Sstevel@tonic-gate static void	async_resume_utbrk(struct asyncline *async);
1987c478bd9Sstevel@tonic-gate static void	async_dtr_free(struct asyncline *async);
1997c478bd9Sstevel@tonic-gate static int	asy_identify_chip(dev_info_t *devi, struct asycom *asy);
2007c478bd9Sstevel@tonic-gate static void	asy_reset_fifo(struct asycom *asy, uchar_t flags);
2017c478bd9Sstevel@tonic-gate static int	asy_getproperty(dev_info_t *devi, struct asycom *asy,
2027c478bd9Sstevel@tonic-gate 		    const char *property);
2037c478bd9Sstevel@tonic-gate static boolean_t	async_flowcontrol_sw_input(struct asycom *asy,
2047c478bd9Sstevel@tonic-gate 			    async_flowc_action onoff, int type);
2057c478bd9Sstevel@tonic-gate static void	async_flowcontrol_sw_output(struct asycom *asy,
2067c478bd9Sstevel@tonic-gate 		    async_flowc_action onoff);
2077c478bd9Sstevel@tonic-gate static void	async_flowcontrol_hw_input(struct asycom *asy,
2087c478bd9Sstevel@tonic-gate 		    async_flowc_action onoff, int type);
2097c478bd9Sstevel@tonic-gate static void	async_flowcontrol_hw_output(struct asycom *asy,
2107c478bd9Sstevel@tonic-gate 		    async_flowc_action onoff);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate #define	GET_PROP(devi, pname, pflag, pval, plen) \
2137c478bd9Sstevel@tonic-gate 		(ddi_prop_op(DDI_DEV_T_ANY, (devi), PROP_LEN_AND_VAL_BUF, \
2147c478bd9Sstevel@tonic-gate 		(pflag), (pname), (caddr_t)(pval), (plen)))
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate static ddi_iblock_cookie_t asy_soft_iblock;
2177c478bd9Sstevel@tonic-gate ddi_softintr_t asy_softintr_id;
2187c478bd9Sstevel@tonic-gate static	int asy_addedsoft = 0;
2197c478bd9Sstevel@tonic-gate int	asysoftpend;	/* soft interrupt pending */
2207c478bd9Sstevel@tonic-gate kmutex_t asy_soft_lock;	/* lock protecting asysoftpend */
2217c478bd9Sstevel@tonic-gate kmutex_t asy_glob_lock; /* lock protecting global data manipulation */
2227c478bd9Sstevel@tonic-gate void *asy_soft_state;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /* Standard COM port I/O addresses */
2257c478bd9Sstevel@tonic-gate static const int standard_com_ports[] = {
2267c478bd9Sstevel@tonic-gate 	COM1_IOADDR, COM2_IOADDR, COM3_IOADDR, COM4_IOADDR
2277c478bd9Sstevel@tonic-gate };
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static int *com_ports;
2307c478bd9Sstevel@tonic-gate static uint_t num_com_ports;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate  * Baud rate table. Indexed by #defines found in sys/termios.h
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate ushort_t asyspdtab[] = {
2367c478bd9Sstevel@tonic-gate 	0,	/* 0 baud rate */
2377c478bd9Sstevel@tonic-gate 	0x900,	/* 50 baud rate */
2387c478bd9Sstevel@tonic-gate 	0x600,	/* 75 baud rate */
2397c478bd9Sstevel@tonic-gate 	0x417,	/* 110 baud rate (%0.026) */
2407c478bd9Sstevel@tonic-gate 	0x359,	/* 134 baud rate (%0.058) */
2417c478bd9Sstevel@tonic-gate 	0x300,	/* 150 baud rate */
2427c478bd9Sstevel@tonic-gate 	0x240,	/* 200 baud rate */
2437c478bd9Sstevel@tonic-gate 	0x180,	/* 300 baud rate */
2447c478bd9Sstevel@tonic-gate 	0x0c0,	/* 600 baud rate */
2457c478bd9Sstevel@tonic-gate 	0x060,	/* 1200 baud rate */
2467c478bd9Sstevel@tonic-gate 	0x040,	/* 1800 baud rate */
2477c478bd9Sstevel@tonic-gate 	0x030,	/* 2400 baud rate */
2487c478bd9Sstevel@tonic-gate 	0x018,	/* 4800 baud rate */
2497c478bd9Sstevel@tonic-gate 	0x00c,	/* 9600 baud rate */
2507c478bd9Sstevel@tonic-gate 	0x006,	/* 19200 baud rate */
2517c478bd9Sstevel@tonic-gate 	0x003,	/* 38400 baud rate */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	0x002,	/* 57600 baud rate */
2547c478bd9Sstevel@tonic-gate 	0x0,	/* 76800 baud rate not supported */
2557c478bd9Sstevel@tonic-gate 	0x001,	/* 115200 baud rate */
2567c478bd9Sstevel@tonic-gate 	0x0,	/* 153600 baud rate not supported */
2577c478bd9Sstevel@tonic-gate 	0x0,	/* 0x8002 (SMC chip) 230400 baud rate not supported */
2587c478bd9Sstevel@tonic-gate 	0x0,	/* 307200 baud rate not supported */
2597c478bd9Sstevel@tonic-gate 	0x0,	/* 0x8001 (SMC chip) 460800 baud rate not supported */
2607c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2617c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2627c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2637c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2647c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2657c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2667c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2677c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2687c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2697c478bd9Sstevel@tonic-gate };
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate static int asyrsrv(queue_t *q);
2727c478bd9Sstevel@tonic-gate static int asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr);
2737c478bd9Sstevel@tonic-gate static int asyclose(queue_t *q, int flag, cred_t *credp);
2747c478bd9Sstevel@tonic-gate static int asywput(queue_t *q, mblk_t *mp);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate struct module_info asy_info = {
2777c478bd9Sstevel@tonic-gate 	0,
2787c478bd9Sstevel@tonic-gate 	"asy",
2797c478bd9Sstevel@tonic-gate 	0,
2807c478bd9Sstevel@tonic-gate 	INFPSZ,
2817c478bd9Sstevel@tonic-gate 	4096,
2827c478bd9Sstevel@tonic-gate 	128
2837c478bd9Sstevel@tonic-gate };
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate static struct qinit asy_rint = {
2867c478bd9Sstevel@tonic-gate 	putq,
2877c478bd9Sstevel@tonic-gate 	asyrsrv,
2887c478bd9Sstevel@tonic-gate 	asyopen,
2897c478bd9Sstevel@tonic-gate 	asyclose,
2907c478bd9Sstevel@tonic-gate 	NULL,
2917c478bd9Sstevel@tonic-gate 	&asy_info,
2927c478bd9Sstevel@tonic-gate 	NULL
2937c478bd9Sstevel@tonic-gate };
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate static struct qinit asy_wint = {
2967c478bd9Sstevel@tonic-gate 	asywput,
2977c478bd9Sstevel@tonic-gate 	NULL,
2987c478bd9Sstevel@tonic-gate 	NULL,
2997c478bd9Sstevel@tonic-gate 	NULL,
3007c478bd9Sstevel@tonic-gate 	NULL,
3017c478bd9Sstevel@tonic-gate 	&asy_info,
3027c478bd9Sstevel@tonic-gate 	NULL
3037c478bd9Sstevel@tonic-gate };
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate struct streamtab asy_str_info = {
3067c478bd9Sstevel@tonic-gate 	&asy_rint,
3077c478bd9Sstevel@tonic-gate 	&asy_wint,
3087c478bd9Sstevel@tonic-gate 	NULL,
3097c478bd9Sstevel@tonic-gate 	NULL
3107c478bd9Sstevel@tonic-gate };
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate static int asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
3137c478bd9Sstevel@tonic-gate 		void **result);
3147c478bd9Sstevel@tonic-gate static int asyprobe(dev_info_t *);
3157c478bd9Sstevel@tonic-gate static int asyattach(dev_info_t *, ddi_attach_cmd_t);
3167c478bd9Sstevel@tonic-gate static int asydetach(dev_info_t *, ddi_detach_cmd_t);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate static 	struct cb_ops cb_asy_ops = {
3197c478bd9Sstevel@tonic-gate 	nodev,			/* cb_open */
3207c478bd9Sstevel@tonic-gate 	nodev,			/* cb_close */
3217c478bd9Sstevel@tonic-gate 	nodev,			/* cb_strategy */
3227c478bd9Sstevel@tonic-gate 	nodev,			/* cb_print */
3237c478bd9Sstevel@tonic-gate 	nodev,			/* cb_dump */
3247c478bd9Sstevel@tonic-gate 	nodev,			/* cb_read */
3257c478bd9Sstevel@tonic-gate 	nodev,			/* cb_write */
3267c478bd9Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
3277c478bd9Sstevel@tonic-gate 	nodev,			/* cb_devmap */
3287c478bd9Sstevel@tonic-gate 	nodev,			/* cb_mmap */
3297c478bd9Sstevel@tonic-gate 	nodev,			/* cb_segmap */
3307c478bd9Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
3317c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
3327c478bd9Sstevel@tonic-gate 	&asy_str_info,		/* cb_stream */
3337c478bd9Sstevel@tonic-gate 	D_MP			/* cb_flag */
3347c478bd9Sstevel@tonic-gate };
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate struct dev_ops asy_ops = {
3377c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
3387c478bd9Sstevel@tonic-gate 	0,			/* devo_refcnt */
3397c478bd9Sstevel@tonic-gate 	asyinfo,		/* devo_getinfo */
3407c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_identify */
3417c478bd9Sstevel@tonic-gate 	asyprobe,		/* devo_probe */
3427c478bd9Sstevel@tonic-gate 	asyattach,		/* devo_attach */
3437c478bd9Sstevel@tonic-gate 	asydetach,		/* devo_detach */
3447c478bd9Sstevel@tonic-gate 	nodev,			/* devo_reset */
3457c478bd9Sstevel@tonic-gate 	&cb_asy_ops,		/* devo_cb_ops */
3467c478bd9Sstevel@tonic-gate };
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
3497c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a driver */
3507c478bd9Sstevel@tonic-gate 	"ASY driver %I%",
3517c478bd9Sstevel@tonic-gate 	&asy_ops,	/* driver ops */
3527c478bd9Sstevel@tonic-gate };
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
3557c478bd9Sstevel@tonic-gate 	MODREV_1,
3567c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
3577c478bd9Sstevel@tonic-gate 	NULL
3587c478bd9Sstevel@tonic-gate };
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate int
3617c478bd9Sstevel@tonic-gate _init(void)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	int i;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	i = ddi_soft_state_init(&asy_soft_state, sizeof (struct asycom), 2);
3667c478bd9Sstevel@tonic-gate 	if (i == 0) {
3677c478bd9Sstevel@tonic-gate 		mutex_init(&asy_glob_lock, NULL, MUTEX_DRIVER, NULL);
3687c478bd9Sstevel@tonic-gate 		if ((i = mod_install(&modlinkage)) != 0) {
3697c478bd9Sstevel@tonic-gate 			mutex_destroy(&asy_glob_lock);
3707c478bd9Sstevel@tonic-gate 			ddi_soft_state_fini(&asy_soft_state);
3717c478bd9Sstevel@tonic-gate 		} else {
3727c478bd9Sstevel@tonic-gate 			DEBUGCONT2(ASY_DEBUG_INIT, "%s, debug = %x\n",
3737c478bd9Sstevel@tonic-gate 			    modldrv.drv_linkinfo, debug);
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 	return (i);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate int
3807c478bd9Sstevel@tonic-gate _fini(void)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	int i;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if ((i = mod_remove(&modlinkage)) == 0) {
3857c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_INIT, "%s unloading\n",
3867c478bd9Sstevel@tonic-gate 		    modldrv.drv_linkinfo);
3877c478bd9Sstevel@tonic-gate 		ASSERT(max_asy_instance == -1);
3887c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy_glob_lock);
3897c478bd9Sstevel@tonic-gate 		if (asy_addedsoft)
3907c478bd9Sstevel@tonic-gate 			ddi_remove_softintr(asy_softintr_id);
3917c478bd9Sstevel@tonic-gate 		asy_addedsoft = 0;
3927c478bd9Sstevel@tonic-gate 		/* free "motherboard-serial-ports" property if allocated */
3937c478bd9Sstevel@tonic-gate 		if (com_ports != NULL && com_ports != (int *)standard_com_ports)
3947c478bd9Sstevel@tonic-gate 		    ddi_prop_free(com_ports);
3957c478bd9Sstevel@tonic-gate 		com_ports = NULL;
3967c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy_soft_lock);
3977c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&asy_soft_state);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 	return (i);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate int
4037c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate static int
4097c478bd9Sstevel@tonic-gate asydetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	int instance;
4127c478bd9Sstevel@tonic-gate 	struct asycom *asy;
4137c478bd9Sstevel@tonic-gate 	struct asyncline *async;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
4167c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(devi);	/* find out which unit */
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, instance);
4217c478bd9Sstevel@tonic-gate 	if (asy == NULL)
4227c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4237c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	DEBUGNOTE2(ASY_DEBUG_INIT, "asy%d: %s shutdown.",
4267c478bd9Sstevel@tonic-gate 	    instance, asy_hw_name(asy));
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/* cancel DTR hold timeout */
4297c478bd9Sstevel@tonic-gate 	if (async->async_dtrtid != 0) {
4307c478bd9Sstevel@tonic-gate 		(void) untimeout(async->async_dtrtid);
4317c478bd9Sstevel@tonic-gate 		async->async_dtrtid = 0;
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/* remove all minor device node(s) for this device */
4357c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	mutex_destroy(&asy->asy_excl);
4387c478bd9Sstevel@tonic-gate 	mutex_destroy(&asy->asy_excl_hi);
4397c478bd9Sstevel@tonic-gate 	cv_destroy(&async->async_flags_cv);
4407c478bd9Sstevel@tonic-gate 	ddi_remove_intr(devi, 0, asy->asy_iblock);
4417c478bd9Sstevel@tonic-gate 	ddi_regs_map_free(&asy->asy_iohandle);
4427c478bd9Sstevel@tonic-gate 	asy_soft_state_free(asy);
4437c478bd9Sstevel@tonic-gate 	DEBUGNOTE1(ASY_DEBUG_INIT, "asy%d: shutdown complete", instance);
4447c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate /*
4487c478bd9Sstevel@tonic-gate  * asyprobe
4497c478bd9Sstevel@tonic-gate  * We don't bother probing for the hardware, as since Solaris 2.6, device
4507c478bd9Sstevel@tonic-gate  * nodes are only created for auto-detected hardware or nodes explicitly
4517c478bd9Sstevel@tonic-gate  * created by the user, e.g. via the DCA. However, we should check the
4527c478bd9Sstevel@tonic-gate  * device node is at least vaguely usable, i.e. we have a block of 8 i/o
4537c478bd9Sstevel@tonic-gate  * ports. This prevents attempting to attach to bogus serial ports which
4547c478bd9Sstevel@tonic-gate  * some BIOSs still partially report when they are disabled in the BIOS.
4557c478bd9Sstevel@tonic-gate  */
4567c478bd9Sstevel@tonic-gate static int
4577c478bd9Sstevel@tonic-gate asyprobe(dev_info_t *devi)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	int instance;
4607c478bd9Sstevel@tonic-gate 	int ret = DDI_PROBE_FAILURE;
4617c478bd9Sstevel@tonic-gate 	int regnum;
4627c478bd9Sstevel@tonic-gate 	int reglen, nregs;
4637c478bd9Sstevel@tonic-gate 	struct reglist {
4647c478bd9Sstevel@tonic-gate 		uint_t bustype;
4657c478bd9Sstevel@tonic-gate 		int base;
4667c478bd9Sstevel@tonic-gate 		int size;
4677c478bd9Sstevel@tonic-gate 	} *reglist = NULL;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(devi);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/* Retrieve "reg" property */
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
4747c478bd9Sstevel@tonic-gate 	    "reg", (caddr_t)&reglist, &reglen) != DDI_PROP_SUCCESS) {
4757c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "asyprobe: \"reg\" property not found "
4767c478bd9Sstevel@tonic-gate 		    "in devices property list");
4777c478bd9Sstevel@tonic-gate 		goto probedone;
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/* find I/O bus register property */
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	nregs = reglen / sizeof (struct reglist);
4837c478bd9Sstevel@tonic-gate 	for (regnum = 0; regnum < nregs; regnum++) {
4847c478bd9Sstevel@tonic-gate 		if (reglist[regnum].bustype == 1)
4857c478bd9Sstevel@tonic-gate 			break;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 	if (regnum >= nregs) {
4887c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_INIT,
4897c478bd9Sstevel@tonic-gate 		    "asy%dprobe: No I/O register property", instance);
4907c478bd9Sstevel@tonic-gate 		goto probedone;
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (reglist[regnum].size < 8) {	/* not enough registers for a UART */
4947c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_INIT,
4957c478bd9Sstevel@tonic-gate 		    "asy%dprobe: Invalid I/O register property", instance);
4967c478bd9Sstevel@tonic-gate 		goto probedone;
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	ret = DDI_PROBE_DONTCARE;	/* OK, looks like it might be usable */
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate probedone:
5027c478bd9Sstevel@tonic-gate 	if (reglist != NULL)
5037c478bd9Sstevel@tonic-gate 		kmem_free(reglist, reglen);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_INIT, "asy%dprobe: ret=%s\n", instance,
5067c478bd9Sstevel@tonic-gate 	    ret == DDI_PROBE_DONTCARE ? "DDI_PROBE_DONTCARE" :
5077c478bd9Sstevel@tonic-gate 	    "DDI_PROBE_FAILURE");
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	return (ret);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate static int
5137c478bd9Sstevel@tonic-gate asyattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	int instance;
5167c478bd9Sstevel@tonic-gate 	int mcr;
5177c478bd9Sstevel@tonic-gate 	int ret;
5187c478bd9Sstevel@tonic-gate 	int regnum = 0;
5197c478bd9Sstevel@tonic-gate 	int i;
5207c478bd9Sstevel@tonic-gate 	struct asycom *asy;
5217c478bd9Sstevel@tonic-gate 	char name[40];
5227c478bd9Sstevel@tonic-gate 	int status;
5237c478bd9Sstevel@tonic-gate 	static ddi_device_acc_attr_t ioattr = {
5247c478bd9Sstevel@tonic-gate 		DDI_DEVICE_ATTR_V0,
5257c478bd9Sstevel@tonic-gate 		DDI_NEVERSWAP_ACC,
5267c478bd9Sstevel@tonic-gate 		DDI_STRICTORDER_ACC,
5277c478bd9Sstevel@tonic-gate 	};
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
5307c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(devi);	/* find out which unit */
5337c478bd9Sstevel@tonic-gate 	ret = ddi_soft_state_zalloc(asy_soft_state, instance);
5347c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
5357c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5367c478bd9Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, instance);
5377c478bd9Sstevel@tonic-gate 	ASSERT(asy != NULL);	/* can't fail - we only just allocated it */
5387c478bd9Sstevel@tonic-gate 	asy->asy_unit = instance;
5397c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
5407c478bd9Sstevel@tonic-gate 	if (instance > max_asy_instance)
5417c478bd9Sstevel@tonic-gate 		max_asy_instance = instance;
5427c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/*CSTYLED*/
5457c478bd9Sstevel@tonic-gate 	{
5467c478bd9Sstevel@tonic-gate 		int reglen, nregs;
5477c478bd9Sstevel@tonic-gate 		int i;
5487c478bd9Sstevel@tonic-gate 		struct {
5497c478bd9Sstevel@tonic-gate 			uint_t bustype;
5507c478bd9Sstevel@tonic-gate 			int base;
5517c478bd9Sstevel@tonic-gate 			int size;
5527c478bd9Sstevel@tonic-gate 		} *reglist;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 		/* new probe */
5557c478bd9Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
5567c478bd9Sstevel@tonic-gate 		    "reg", (caddr_t)&reglist, &reglen) != DDI_PROP_SUCCESS) {
5577c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "asyattach: reg property not found "
5587c478bd9Sstevel@tonic-gate 				"in devices property list");
5597c478bd9Sstevel@tonic-gate 			asy_soft_state_free(asy);
5607c478bd9Sstevel@tonic-gate 			return (DDI_PROBE_FAILURE);
5617c478bd9Sstevel@tonic-gate 		}
5627c478bd9Sstevel@tonic-gate 		regnum = -1;
5637c478bd9Sstevel@tonic-gate 		nregs = reglen / sizeof (*reglist);
5647c478bd9Sstevel@tonic-gate 		for (i = 0; i < nregs; i++) {
5657c478bd9Sstevel@tonic-gate 			switch (reglist[i].bustype) {
5667c478bd9Sstevel@tonic-gate 			case 1:			/* I/O bus reg property */
5677c478bd9Sstevel@tonic-gate 				if (regnum == -1) /* only use the first one */
5687c478bd9Sstevel@tonic-gate 					regnum = i;
5697c478bd9Sstevel@tonic-gate 				break;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 			case pnpMTS0219:	/* Multitech MT5634ZTX modem */
5727c478bd9Sstevel@tonic-gate 				/* Venus chipset can't do loopback test */
5737c478bd9Sstevel@tonic-gate 				asy->asy_flags2 |= ASY2_NO_LOOPBACK;
5747c478bd9Sstevel@tonic-gate 				break;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 			default:
5777c478bd9Sstevel@tonic-gate 				break;
5787c478bd9Sstevel@tonic-gate 			}
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 		kmem_free(reglist, reglen);
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (regnum < 0 ||
5847c478bd9Sstevel@tonic-gate 	    ddi_regs_map_setup(devi, regnum, (caddr_t *)&asy->asy_ioaddr,
5857c478bd9Sstevel@tonic-gate 	    (offset_t)0, (offset_t)0, &ioattr, &asy->asy_iohandle)
5867c478bd9Sstevel@tonic-gate 	    != DDI_SUCCESS) {
5877c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "asy%d: could not map UART registers @ %p",
5887c478bd9Sstevel@tonic-gate 		    instance, (void *)asy->asy_ioaddr);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		asy_soft_state_free(asy);
5917c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_INIT, "asy%dattach: UART @ %p\n",
5957c478bd9Sstevel@tonic-gate 	    instance, (void *)asy->asy_ioaddr);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
5987c478bd9Sstevel@tonic-gate 	if (com_ports == NULL) {	/* need to initialize com_ports */
5997c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, devi, 0,
6007c478bd9Sstevel@tonic-gate 		    "motherboard-serial-ports", &com_ports, &num_com_ports) !=
6017c478bd9Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
6027c478bd9Sstevel@tonic-gate 			/* Use our built-in COM[1234] values */
6037c478bd9Sstevel@tonic-gate 			com_ports = (int *)standard_com_ports;
6047c478bd9Sstevel@tonic-gate 			num_com_ports = sizeof (standard_com_ports) /
6057c478bd9Sstevel@tonic-gate 			    sizeof (standard_com_ports[0]);
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 		if (num_com_ports > 10) {
6087c478bd9Sstevel@tonic-gate 			/* We run out of single digits for device properties */
6097c478bd9Sstevel@tonic-gate 			num_com_ports = 10;
6107c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
6117c478bd9Sstevel@tonic-gate 			    "More than %d motherboard-serial-ports",
6127c478bd9Sstevel@tonic-gate 			    num_com_ports);
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	/*
6187c478bd9Sstevel@tonic-gate 	 * Lookup the i/o address to see if this is a standard COM port
6197c478bd9Sstevel@tonic-gate 	 * in which case we assign it the correct tty[a-d] to match the
6207c478bd9Sstevel@tonic-gate 	 * COM port number, or some other i/o address in which case it
6217c478bd9Sstevel@tonic-gate 	 * will be assigned /dev/term/[0123...] in some rather arbitrary
6227c478bd9Sstevel@tonic-gate 	 * fashion.
6237c478bd9Sstevel@tonic-gate 	 */
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_com_ports; i++) {
6267c478bd9Sstevel@tonic-gate 		if (asy->asy_ioaddr == (uint8_t *)(uintptr_t)com_ports[i]) {
6277c478bd9Sstevel@tonic-gate 			asy->asy_com_port = i + 1;
6287c478bd9Sstevel@tonic-gate 			break;
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/*
6337c478bd9Sstevel@tonic-gate 	 * It appears that there was async hardware that on reset
6347c478bd9Sstevel@tonic-gate 	 * did not clear ICR.  Hence when we get to
6357c478bd9Sstevel@tonic-gate 	 * ddi_get_iblock_cookie below, this hardware would cause
6367c478bd9Sstevel@tonic-gate 	 * the system to hang if there was input available.
6377c478bd9Sstevel@tonic-gate 	 */
6387c478bd9Sstevel@tonic-gate 
6394ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0x00);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/* establish default usage */
6427c478bd9Sstevel@tonic-gate 	asy->asy_mcr |= RTS|DTR;		/* do use RTS/DTR after open */
6437c478bd9Sstevel@tonic-gate 	asy->asy_lcr = STOP1|BITS8;		/* default to 1 stop 8 bits */
6447c478bd9Sstevel@tonic-gate 	asy->asy_bidx = B9600;			/* default to 9600  */
6457c478bd9Sstevel@tonic-gate #ifdef DEBUG
6467c478bd9Sstevel@tonic-gate 	asy->asy_msint_cnt = 0;			/* # of times in async_msint */
6477c478bd9Sstevel@tonic-gate #endif
6487c478bd9Sstevel@tonic-gate 	mcr = 0;				/* don't enable until open */
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (asy->asy_com_port != 0) {
6517c478bd9Sstevel@tonic-gate 		/*
6527c478bd9Sstevel@tonic-gate 		 * For motherboard ports, emulate tty eeprom properties.
6537c478bd9Sstevel@tonic-gate 		 * Actually, we can't tell if a port is motherboard or not,
6547c478bd9Sstevel@tonic-gate 		 * so for "motherboard ports", read standard DOS COM ports.
6557c478bd9Sstevel@tonic-gate 		 */
6567c478bd9Sstevel@tonic-gate 		switch (asy_getproperty(devi, asy, "ignore-cd")) {
6577c478bd9Sstevel@tonic-gate 		case 0:				/* *-ignore-cd=False */
6587c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
6597c478bd9Sstevel@tonic-gate 			    "asy%dattach: clear ASY_IGNORE_CD\n", instance);
6607c478bd9Sstevel@tonic-gate 			asy->asy_flags &= ~ASY_IGNORE_CD; /* wait for cd */
6617c478bd9Sstevel@tonic-gate 			break;
6627c478bd9Sstevel@tonic-gate 		case 1:				/* *-ignore-cd=True */
6637c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
6647c478bd9Sstevel@tonic-gate 		default:			/* *-ignore-cd not defined */
6657c478bd9Sstevel@tonic-gate 			/*
6667c478bd9Sstevel@tonic-gate 			 * We set rather silly defaults of soft carrier on
6677c478bd9Sstevel@tonic-gate 			 * and DTR/RTS raised here because it might be that
6687c478bd9Sstevel@tonic-gate 			 * one of the motherboard ports is the system console.
6697c478bd9Sstevel@tonic-gate 			 */
6707c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
6717c478bd9Sstevel@tonic-gate 			    "asy%dattach: set ASY_IGNORE_CD, set RTS & DTR\n",
6727c478bd9Sstevel@tonic-gate 			    instance);
6737c478bd9Sstevel@tonic-gate 			mcr = asy->asy_mcr;		/* rts/dtr on */
6747c478bd9Sstevel@tonic-gate 			asy->asy_flags |= ASY_IGNORE_CD;	/* ignore cd */
6757c478bd9Sstevel@tonic-gate 			break;
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		/* Property for not raising DTR/RTS */
6797c478bd9Sstevel@tonic-gate 		switch (asy_getproperty(devi, asy, "rts-dtr-off")) {
6807c478bd9Sstevel@tonic-gate 		case 0:				/* *-rts-dtr-off=False */
6817c478bd9Sstevel@tonic-gate 			asy->asy_flags |= ASY_RTS_DTR_OFF;	/* OFF */
6827c478bd9Sstevel@tonic-gate 			mcr = asy->asy_mcr;		/* rts/dtr on */
6837c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM, "asy%dattach: "
6847c478bd9Sstevel@tonic-gate 			    "ASY_RTS_DTR_OFF set and DTR & RTS set\n",
6857c478bd9Sstevel@tonic-gate 			    instance);
6867c478bd9Sstevel@tonic-gate 			break;
6877c478bd9Sstevel@tonic-gate 		case 1:				/* *-rts-dtr-off=True */
6887c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
6897c478bd9Sstevel@tonic-gate 		default:			/* *-rts-dtr-off undefined */
6907c478bd9Sstevel@tonic-gate 			break;
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		/* Parse property for tty modes */
6947c478bd9Sstevel@tonic-gate 		asy_parse_mode(devi, asy);
6957c478bd9Sstevel@tonic-gate 	} else {
6967c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_MODEM,
6977c478bd9Sstevel@tonic-gate 		    "asy%dattach: clear ASY_IGNORE_CD, clear RTS & DTR\n",
6987c478bd9Sstevel@tonic-gate 		    instance);
6997c478bd9Sstevel@tonic-gate 		asy->asy_flags &= ~ASY_IGNORE_CD;	/* wait for cd */
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	/*
7037c478bd9Sstevel@tonic-gate 	 * Initialize the port with default settings.
7047c478bd9Sstevel@tonic-gate 	 */
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	asy->asy_fifo_buf = 1;
7077c478bd9Sstevel@tonic-gate 	asy->asy_use_fifo = FIFO_OFF;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	/*
7107c478bd9Sstevel@tonic-gate 	 * Get icookie for mutexes initialization
7117c478bd9Sstevel@tonic-gate 	 */
7127c478bd9Sstevel@tonic-gate 	if ((ddi_get_iblock_cookie(devi, 0, &asy->asy_iblock) !=
7137c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS) ||
7147c478bd9Sstevel@tonic-gate 	    (ddi_get_soft_iblock_cookie(devi, DDI_SOFTINT_MED,
7157c478bd9Sstevel@tonic-gate 	    &asy_soft_iblock) != DDI_SUCCESS)) {
7167c478bd9Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
7177c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
7187c478bd9Sstevel@tonic-gate 		    "asy%d: could not hook interrupt for UART @ %p\n",
7197c478bd9Sstevel@tonic-gate 		    instance, (void *)asy->asy_ioaddr);
7207c478bd9Sstevel@tonic-gate 		asy_soft_state_free(asy);
7217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	/*
7257c478bd9Sstevel@tonic-gate 	 * Initialize mutexes before accessing the hardware
7267c478bd9Sstevel@tonic-gate 	 */
7277c478bd9Sstevel@tonic-gate 	mutex_init(&asy->asy_excl, NULL, MUTEX_DRIVER, asy_soft_iblock);
7287c478bd9Sstevel@tonic-gate 	mutex_init(&asy->asy_excl_hi, NULL, MUTEX_DRIVER,
7297c478bd9Sstevel@tonic-gate 		(void *)asy->asy_iblock);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
7327c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (asy_identify_chip(devi, asy) != DDI_SUCCESS) {
7357c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
7367c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
7377c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
7387c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
7397c478bd9Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
7407c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "Cannot identify UART chip at %p\n",
7417c478bd9Sstevel@tonic-gate 		    (void *)asy->asy_ioaddr);
7427c478bd9Sstevel@tonic-gate 		asy_soft_state_free(asy);
7437c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	/* disable all interrupts */
7474ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0);
7487c478bd9Sstevel@tonic-gate 	/* select baud rate generator */
7494ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, DLAB);
7507c478bd9Sstevel@tonic-gate 	/* Set the baud rate to 9600 */
7514ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + (DAT+DLL),
7527c478bd9Sstevel@tonic-gate 		asyspdtab[asy->asy_bidx] & 0xff);
7534ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + (DAT+DLH),
7547c478bd9Sstevel@tonic-gate 		(asyspdtab[asy->asy_bidx] >> 8) & 0xff);
7554ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
7567c478bd9Sstevel@tonic-gate 		asy->asy_lcr);
7574ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR, mcr);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
7607c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	/*
7637c478bd9Sstevel@tonic-gate 	 * Set up the other components of the asycom structure for this port.
7647c478bd9Sstevel@tonic-gate 	 */
7657c478bd9Sstevel@tonic-gate 	asy->asy_dip = devi;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
7687c478bd9Sstevel@tonic-gate 	if (asy_addedsoft == 0) { /* install the soft interrupt handler */
7697c478bd9Sstevel@tonic-gate 		if (ddi_add_softintr(devi, DDI_SOFTINT_MED,
7707c478bd9Sstevel@tonic-gate 		    &asy_softintr_id, NULL, 0, asysoftintr,
7717c478bd9Sstevel@tonic-gate 		    (caddr_t)0) != DDI_SUCCESS) {
7727c478bd9Sstevel@tonic-gate 			mutex_destroy(&asy->asy_excl);
7737c478bd9Sstevel@tonic-gate 			mutex_destroy(&asy->asy_excl_hi);
7747c478bd9Sstevel@tonic-gate 			ddi_regs_map_free(&asy->asy_iohandle);
7757c478bd9Sstevel@tonic-gate 			mutex_exit(&asy_glob_lock);
7767c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
7777c478bd9Sstevel@tonic-gate 				"Can not set soft interrupt for ASY driver\n");
7787c478bd9Sstevel@tonic-gate 			asy_soft_state_free(asy);
7797c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
7807c478bd9Sstevel@tonic-gate 		}
7817c478bd9Sstevel@tonic-gate 		mutex_init(&asy_soft_lock, NULL, MUTEX_DRIVER,
7827c478bd9Sstevel@tonic-gate 			(void *)asy->asy_iblock);
7837c478bd9Sstevel@tonic-gate 		asy_addedsoft++;
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
7887c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	/*
7917c478bd9Sstevel@tonic-gate 	 * Install interrupt handler for this device.
7927c478bd9Sstevel@tonic-gate 	 */
7937c478bd9Sstevel@tonic-gate 	if (ddi_add_intr(devi, 0, NULL, 0, asyintr,
7947c478bd9Sstevel@tonic-gate 	    (caddr_t)asy) != DDI_SUCCESS) {
7957c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
7967c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
7977c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
7987c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
7997c478bd9Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
8007c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
8017c478bd9Sstevel@tonic-gate 			"Can not set device interrupt for ASY driver\n");
8027c478bd9Sstevel@tonic-gate 		asy_soft_state_free(asy);
8037c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
8077c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	asyinit(asy);	/* initialize the asyncline structure */
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/* create minor device nodes for this device */
8127c478bd9Sstevel@tonic-gate 	if (asy->asy_com_port != 0) {
8137c478bd9Sstevel@tonic-gate 		/*
8147c478bd9Sstevel@tonic-gate 		 * For DOS COM ports, add letter suffix so
8157c478bd9Sstevel@tonic-gate 		 * devfsadm can create correct link names.
8167c478bd9Sstevel@tonic-gate 		 */
8177c478bd9Sstevel@tonic-gate 		name[0] = asy->asy_com_port + 'a' - 1;
8187c478bd9Sstevel@tonic-gate 		name[1] = '\0';
8197c478bd9Sstevel@tonic-gate 	} else {
8207c478bd9Sstevel@tonic-gate 		/*
8217c478bd9Sstevel@tonic-gate 		 * ISA port which isn't a standard DOS COM
8227c478bd9Sstevel@tonic-gate 		 * port needs no further qualification.
8237c478bd9Sstevel@tonic-gate 		 */
8247c478bd9Sstevel@tonic-gate 		name[0] = '\0';
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 	status = ddi_create_minor_node(devi, name, S_IFCHR, instance,
8277c478bd9Sstevel@tonic-gate 	    asy->asy_com_port != 0 ? DDI_NT_SERIAL_MB : DDI_NT_SERIAL, NULL);
8287c478bd9Sstevel@tonic-gate 	if (status == DDI_SUCCESS) {
8297c478bd9Sstevel@tonic-gate 		(void) strcat(name, ",cu");
8307c478bd9Sstevel@tonic-gate 		status = ddi_create_minor_node(devi, name, S_IFCHR,
8317c478bd9Sstevel@tonic-gate 		    OUTLINE | instance,
8327c478bd9Sstevel@tonic-gate 		    asy->asy_com_port != 0 ? DDI_NT_SERIAL_MB_DO :
8337c478bd9Sstevel@tonic-gate 		    DDI_NT_SERIAL_DO, NULL);
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
8377c478bd9Sstevel@tonic-gate 		struct asyncline *async = asy->asy_priv;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
8407c478bd9Sstevel@tonic-gate 		ddi_remove_intr(devi, 0, asy->asy_iblock);
8417c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
8427c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
8437c478bd9Sstevel@tonic-gate 		cv_destroy(&async->async_flags_cv);
8447c478bd9Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
8457c478bd9Sstevel@tonic-gate 		asy_soft_state_free(asy);
8467c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	/*
8507c478bd9Sstevel@tonic-gate 	 * Fill in the polled I/O structure.
8517c478bd9Sstevel@tonic-gate 	 */
8527c478bd9Sstevel@tonic-gate 	asy->polledio.cons_polledio_version = CONSPOLLEDIO_V0;
853*281f0747Slt200341 	asy->polledio.cons_polledio_argument = (cons_polledio_arg_t)asy;
8547c478bd9Sstevel@tonic-gate 	asy->polledio.cons_polledio_putchar = asyputchar;
8557c478bd9Sstevel@tonic-gate 	asy->polledio.cons_polledio_getchar = asygetchar;
8567c478bd9Sstevel@tonic-gate 	asy->polledio.cons_polledio_ischar = asyischar;
8577c478bd9Sstevel@tonic-gate 	asy->polledio.cons_polledio_enter = NULL;
8587c478bd9Sstevel@tonic-gate 	asy->polledio.cons_polledio_exit = NULL;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
8617c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_INIT, "asy%dattach: done\n", instance);
8627c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8667c478bd9Sstevel@tonic-gate static int
8677c478bd9Sstevel@tonic-gate asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
8687c478bd9Sstevel@tonic-gate 	void **result)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	dev_t dev = (dev_t)arg;
8717c478bd9Sstevel@tonic-gate 	int instance, error;
8727c478bd9Sstevel@tonic-gate 	struct asycom *asy;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	instance = UNIT(dev);
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	switch (infocmd) {
8777c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
87870796907Scth 		asy = ddi_get_soft_state(asy_soft_state, instance);
87970796907Scth 		if ((asy == NULL) || (asy->asy_dip == NULL))
8807c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
8817c478bd9Sstevel@tonic-gate 		else {
8827c478bd9Sstevel@tonic-gate 			*result = (void *) asy->asy_dip;
8837c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
8847c478bd9Sstevel@tonic-gate 		}
8857c478bd9Sstevel@tonic-gate 		break;
8867c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
8877c478bd9Sstevel@tonic-gate 		*result = (void *)(intptr_t)instance;
8887c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
8897c478bd9Sstevel@tonic-gate 		break;
8907c478bd9Sstevel@tonic-gate 	default:
8917c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 	return (error);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate /* asy_getproperty -- walk through all name variants until we find a match */
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate static int
8997c478bd9Sstevel@tonic-gate asy_getproperty(dev_info_t *devi, struct asycom *asy, const char *property)
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 	int len;
9027c478bd9Sstevel@tonic-gate 	int ret;
9037c478bd9Sstevel@tonic-gate 	char letter = asy->asy_com_port + 'a' - 1;	/* for ttya */
9047c478bd9Sstevel@tonic-gate 	char number = asy->asy_com_port + '0';		/* for COM1 */
9057c478bd9Sstevel@tonic-gate 	char val[40];
9067c478bd9Sstevel@tonic-gate 	char name[40];
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/* Property for ignoring DCD */
9097c478bd9Sstevel@tonic-gate 	(void) sprintf(name, "tty%c-%s", letter, property);
9107c478bd9Sstevel@tonic-gate 	len = sizeof (val);
9117c478bd9Sstevel@tonic-gate 	ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
9127c478bd9Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
9137c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "com%c-%s", number, property);
9147c478bd9Sstevel@tonic-gate 		len = sizeof (val);
9157c478bd9Sstevel@tonic-gate 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val,
9167c478bd9Sstevel@tonic-gate 				&len);
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
9197c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "tty0%c-%s", number, property);
9207c478bd9Sstevel@tonic-gate 		len = sizeof (val);
9217c478bd9Sstevel@tonic-gate 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val,
9227c478bd9Sstevel@tonic-gate 				&len);
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
9257c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "port-%c-%s", letter, property);
9267c478bd9Sstevel@tonic-gate 		len = sizeof (val);
9277c478bd9Sstevel@tonic-gate 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val,
9287c478bd9Sstevel@tonic-gate 				&len);
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS)
9317c478bd9Sstevel@tonic-gate 		return (-1);		/* property non-existant */
9327c478bd9Sstevel@tonic-gate 	if (val[0] == 'f' || val[0] == 'F' || val[0] == '0')
9337c478bd9Sstevel@tonic-gate 		return (0);		/* property false/0 */
9347c478bd9Sstevel@tonic-gate 	return (1);			/* property true/!0 */
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /* asy_soft_state_free - local wrapper for ddi_soft_state_free(9F) */
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate static void
9407c478bd9Sstevel@tonic-gate asy_soft_state_free(struct asycom *asy)
9417c478bd9Sstevel@tonic-gate {
9427c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
9437c478bd9Sstevel@tonic-gate 	/* If we were the max_asy_instance, work out new value */
9447c478bd9Sstevel@tonic-gate 	if (asy->asy_unit == max_asy_instance) {
9457c478bd9Sstevel@tonic-gate 		while (--max_asy_instance >= 0) {
9467c478bd9Sstevel@tonic-gate 			if (ddi_get_soft_state(asy_soft_state,
9477c478bd9Sstevel@tonic-gate 			    max_asy_instance) != NULL)
9487c478bd9Sstevel@tonic-gate 				break;
9497c478bd9Sstevel@tonic-gate 		}
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if (asy->asy_priv != NULL) {
9547c478bd9Sstevel@tonic-gate 		kmem_free(asy->asy_priv, sizeof (struct asyncline));
9557c478bd9Sstevel@tonic-gate 		asy->asy_priv = NULL;
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(asy_soft_state, asy->asy_unit);
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate static char *
9617c478bd9Sstevel@tonic-gate asy_hw_name(struct asycom *asy)
9627c478bd9Sstevel@tonic-gate {
9637c478bd9Sstevel@tonic-gate 	switch (asy->asy_hwtype) {
9647c478bd9Sstevel@tonic-gate 	case ASY8250A:
9657c478bd9Sstevel@tonic-gate 		return ("8250A/16450");
9667c478bd9Sstevel@tonic-gate 	case ASY16550:
9677c478bd9Sstevel@tonic-gate 		return ("16550");
9687c478bd9Sstevel@tonic-gate 	case ASY16550A:
9697c478bd9Sstevel@tonic-gate 		return ("16550A");
9707c478bd9Sstevel@tonic-gate 	case ASY16650:
9717c478bd9Sstevel@tonic-gate 		return ("16650");
9727c478bd9Sstevel@tonic-gate 	case ASY16750:
9737c478bd9Sstevel@tonic-gate 		return ("16750");
9747c478bd9Sstevel@tonic-gate 	default:
9757c478bd9Sstevel@tonic-gate 		DEBUGNOTE2(ASY_DEBUG_INIT,
9767c478bd9Sstevel@tonic-gate 		    "asy%d: asy_hw_name: unknown asy_hwtype: %d",
9777c478bd9Sstevel@tonic-gate 		    asy->asy_unit, asy->asy_hwtype);
9787c478bd9Sstevel@tonic-gate 		return ("?");
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate static int
9837c478bd9Sstevel@tonic-gate asy_identify_chip(dev_info_t *devi, struct asycom *asy)
9847c478bd9Sstevel@tonic-gate {
9857c478bd9Sstevel@tonic-gate 	int ret;
9867c478bd9Sstevel@tonic-gate 	int mcr;
9877c478bd9Sstevel@tonic-gate 	dev_t dev;
9887c478bd9Sstevel@tonic-gate 	uint_t hwtype;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if (asy_scr_test) {
9917c478bd9Sstevel@tonic-gate 		/* Check scratch register works. */
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 		/* write to scratch register */
9944ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + SCR, SCRTEST);
9957c478bd9Sstevel@tonic-gate 		/* make sure that pattern doesn't just linger on the bus */
9964ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + FIFOR, 0x00);
9977c478bd9Sstevel@tonic-gate 		/* read data back from scratch register */
9984ab75253Smrj 		ret = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + SCR);
9997c478bd9Sstevel@tonic-gate 		if (ret != SCRTEST) {
10007c478bd9Sstevel@tonic-gate 			/*
10017c478bd9Sstevel@tonic-gate 			 * Scratch register not working.
10027c478bd9Sstevel@tonic-gate 			 * Probably not an async chip.
10037c478bd9Sstevel@tonic-gate 			 * 8250 and 8250B don't have scratch registers,
10047c478bd9Sstevel@tonic-gate 			 * but only worked in ancient PC XT's anyway.
10057c478bd9Sstevel@tonic-gate 			 */
10067c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "asy%d: UART @ %p "
10077c478bd9Sstevel@tonic-gate 			    "scratch register: expected 0x5a, got 0x%02x\n",
10087c478bd9Sstevel@tonic-gate 			    asy->asy_unit, (void *)asy->asy_ioaddr, ret);
10097c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
10107c478bd9Sstevel@tonic-gate 		}
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 	/*
10137c478bd9Sstevel@tonic-gate 	 * Use 16550 fifo reset sequence specified in NS application
10147c478bd9Sstevel@tonic-gate 	 * note. Disable fifos until chip is initialized.
10157c478bd9Sstevel@tonic-gate 	 */
10164ab75253Smrj 	ddi_put8(asy->asy_iohandle,
10177c478bd9Sstevel@tonic-gate 	    asy->asy_ioaddr + FIFOR, 0x00);	/* clear */
10184ab75253Smrj 	ddi_put8(asy->asy_iohandle,
10197c478bd9Sstevel@tonic-gate 	    asy->asy_ioaddr + FIFOR, FIFO_ON);	/* enable */
10204ab75253Smrj 	ddi_put8(asy->asy_iohandle,
10217c478bd9Sstevel@tonic-gate 	    asy->asy_ioaddr + FIFOR, FIFO_ON | FIFORXFLSH);
10227c478bd9Sstevel@tonic-gate 						/* reset */
10237c478bd9Sstevel@tonic-gate 	if (asymaxchip >= ASY16650 && asy_scr_test) {
10247c478bd9Sstevel@tonic-gate 		/*
10257c478bd9Sstevel@tonic-gate 		 * Reset 16650 enhanced regs also, in case we have one of these
10267c478bd9Sstevel@tonic-gate 		 */
10274ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
10287c478bd9Sstevel@tonic-gate 		    EFRACCESS);
10294ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + EFR,
10307c478bd9Sstevel@tonic-gate 		    0);
10314ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
10327c478bd9Sstevel@tonic-gate 		    STOP1|BITS8);
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	/*
10367c478bd9Sstevel@tonic-gate 	 * See what sort of FIFO we have.
10377c478bd9Sstevel@tonic-gate 	 * Try enabling it and see what chip makes of this.
10387c478bd9Sstevel@tonic-gate 	 */
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	asy->asy_fifor = 0;
10417c478bd9Sstevel@tonic-gate 	asy->asy_hwtype = asymaxchip; /* just for asy_reset_fifo() */
10427c478bd9Sstevel@tonic-gate 	if (asymaxchip >= ASY16550A)
10437c478bd9Sstevel@tonic-gate 		asy->asy_fifor |=
10447c478bd9Sstevel@tonic-gate 		    FIFO_ON | FIFODMA | (asy_trig_level & 0xff);
10457c478bd9Sstevel@tonic-gate 	if (asymaxchip >= ASY16650)
10467c478bd9Sstevel@tonic-gate 		asy->asy_fifor |= FIFOEXTRA1 | FIFOEXTRA2;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	asy_reset_fifo(asy, FIFOTXFLSH | FIFORXFLSH);
10497c478bd9Sstevel@tonic-gate 
10504ab75253Smrj 	mcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
10514ab75253Smrj 	ret = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + ISR);
10527c478bd9Sstevel@tonic-gate 	DEBUGCONT4(ASY_DEBUG_CHIP,
10537c478bd9Sstevel@tonic-gate 	    "asy%d: probe fifo FIFOR=0x%02x ISR=0x%02x MCR=0x%02x\n",
10547c478bd9Sstevel@tonic-gate 	    asy->asy_unit, asy->asy_fifor | FIFOTXFLSH | FIFORXFLSH,
10557c478bd9Sstevel@tonic-gate 	    ret, mcr);
10567c478bd9Sstevel@tonic-gate 	switch (ret & 0xf0) {
10577c478bd9Sstevel@tonic-gate 	case 0x40:
10587c478bd9Sstevel@tonic-gate 		hwtype = ASY16550; /* 16550 with broken FIFO */
10597c478bd9Sstevel@tonic-gate 		asy->asy_fifor = 0;
10607c478bd9Sstevel@tonic-gate 		break;
10617c478bd9Sstevel@tonic-gate 	case 0xc0:
10627c478bd9Sstevel@tonic-gate 		hwtype = ASY16550A;
10637c478bd9Sstevel@tonic-gate 		asy->asy_fifo_buf = 16;
10647c478bd9Sstevel@tonic-gate 		asy->asy_use_fifo = FIFO_ON;
10657c478bd9Sstevel@tonic-gate 		asy->asy_fifor &= ~(FIFOEXTRA1 | FIFOEXTRA2);
10667c478bd9Sstevel@tonic-gate 		break;
10677c478bd9Sstevel@tonic-gate 	case 0xe0:
10687c478bd9Sstevel@tonic-gate 		hwtype = ASY16650;
10697c478bd9Sstevel@tonic-gate 		asy->asy_fifo_buf = 32;
10707c478bd9Sstevel@tonic-gate 		asy->asy_use_fifo = FIFO_ON;
10717c478bd9Sstevel@tonic-gate 		asy->asy_fifor &= ~(FIFOEXTRA1);
10727c478bd9Sstevel@tonic-gate 		break;
10737c478bd9Sstevel@tonic-gate 	case 0xf0:
10747c478bd9Sstevel@tonic-gate 		/*
10757c478bd9Sstevel@tonic-gate 		 * Note we get 0xff if chip didn't return us anything,
10767c478bd9Sstevel@tonic-gate 		 * e.g. if there's no chip there.
10777c478bd9Sstevel@tonic-gate 		 */
10787c478bd9Sstevel@tonic-gate 		if (ret == 0xff) {
10797c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "asy%d: UART @ %p "
10807c478bd9Sstevel@tonic-gate 			    "interrupt register: got 0xff\n",
10817c478bd9Sstevel@tonic-gate 			    asy->asy_unit, (void *)asy->asy_ioaddr);
10827c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
10857c478bd9Sstevel@tonic-gate 	case 0xd0:
10867c478bd9Sstevel@tonic-gate 		hwtype = ASY16750;
10877c478bd9Sstevel@tonic-gate 		asy->asy_fifo_buf = 64;
10887c478bd9Sstevel@tonic-gate 		asy->asy_use_fifo = FIFO_ON;
10897c478bd9Sstevel@tonic-gate 		break;
10907c478bd9Sstevel@tonic-gate 	default:
10917c478bd9Sstevel@tonic-gate 		hwtype = ASY8250A; /* No FIFO */
10927c478bd9Sstevel@tonic-gate 		asy->asy_fifor = 0;
10937c478bd9Sstevel@tonic-gate 	}
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	if (hwtype > asymaxchip) {
10967c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "asy%d: UART @ %p "
10977c478bd9Sstevel@tonic-gate 		    "unexpected probe result: "
10987c478bd9Sstevel@tonic-gate 		    "FIFOR=0x%02x ISR=0x%02x MCR=0x%02x\n",
10997c478bd9Sstevel@tonic-gate 		    asy->asy_unit, (void *)asy->asy_ioaddr,
11007c478bd9Sstevel@tonic-gate 		    asy->asy_fifor | FIFOTXFLSH | FIFORXFLSH, ret, mcr);
11017c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	/*
11057c478bd9Sstevel@tonic-gate 	 * Now reset the FIFO operation appropriate for the chip type.
11067c478bd9Sstevel@tonic-gate 	 * Note we must call asy_reset_fifo() before any possible
11077c478bd9Sstevel@tonic-gate 	 * downgrade of the asy->asy_hwtype, or it may not disable
11087c478bd9Sstevel@tonic-gate 	 * the more advanced features we specifically want downgraded.
11097c478bd9Sstevel@tonic-gate 	 */
11107c478bd9Sstevel@tonic-gate 	asy_reset_fifo(asy, 0);
11117c478bd9Sstevel@tonic-gate 	asy->asy_hwtype = hwtype;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	/*
11147c478bd9Sstevel@tonic-gate 	 * Check for Exar/Startech ST16C650, which will still look like a
11157c478bd9Sstevel@tonic-gate 	 * 16550A until we enable its enhanced mode.
11167c478bd9Sstevel@tonic-gate 	 */
11177c478bd9Sstevel@tonic-gate 	if (asy->asy_hwtype == ASY16550A && asymaxchip >= ASY16650 &&
11187c478bd9Sstevel@tonic-gate 	    asy_scr_test) {
11197c478bd9Sstevel@tonic-gate 		/* Enable enhanced mode register access */
11204ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
11217c478bd9Sstevel@tonic-gate 		    EFRACCESS);
11227c478bd9Sstevel@tonic-gate 		/* zero scratch register (not scratch register if enhanced) */
11234ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + SCR, 0);
11247c478bd9Sstevel@tonic-gate 		/* Disable enhanced mode register access */
11254ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
11267c478bd9Sstevel@tonic-gate 		    STOP1|BITS8);
11277c478bd9Sstevel@tonic-gate 		/* read back scratch register */
11284ab75253Smrj 		ret = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + SCR);
11297c478bd9Sstevel@tonic-gate 		if (ret == SCRTEST) {
11307c478bd9Sstevel@tonic-gate 			/* looks like we have an ST16650 -- enable it */
11314ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
11327c478bd9Sstevel@tonic-gate 			    EFRACCESS);
11334ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + EFR,
11347c478bd9Sstevel@tonic-gate 			    ENHENABLE);
11354ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
11367c478bd9Sstevel@tonic-gate 			    STOP1|BITS8);
11377c478bd9Sstevel@tonic-gate 			asy->asy_hwtype = ASY16650;
11387c478bd9Sstevel@tonic-gate 			asy->asy_fifo_buf = 32;
11397c478bd9Sstevel@tonic-gate 			asy->asy_fifor |= 0x10; /* 24 byte txfifo trigger */
11407c478bd9Sstevel@tonic-gate 			asy_reset_fifo(asy, 0);
11417c478bd9Sstevel@tonic-gate 		}
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	/*
11457c478bd9Sstevel@tonic-gate 	 * If we think we might have a FIFO larger than 16 characters,
11467c478bd9Sstevel@tonic-gate 	 * measure FIFO size and check it against expected.
11477c478bd9Sstevel@tonic-gate 	 */
11487c478bd9Sstevel@tonic-gate 	if (asy_fifo_test > 0 &&
11497c478bd9Sstevel@tonic-gate 	    !(asy->asy_flags2 & ASY2_NO_LOOPBACK) &&
11507c478bd9Sstevel@tonic-gate 	    (asy->asy_fifo_buf > 16 ||
11517c478bd9Sstevel@tonic-gate 	    (asy_fifo_test > 1 && asy->asy_use_fifo == FIFO_ON) ||
11527c478bd9Sstevel@tonic-gate 	    ASY_DEBUG(ASY_DEBUG_CHIP))) {
11537c478bd9Sstevel@tonic-gate 		int i;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 		/* Set baud rate to 57600 (fairly arbitrary choice) */
11564ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
11577c478bd9Sstevel@tonic-gate 		    DLAB);
11584ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
11597c478bd9Sstevel@tonic-gate 		    asyspdtab[B57600] & 0xff);
11604ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR,
11617c478bd9Sstevel@tonic-gate 		    (asyspdtab[B57600] >> 8) & 0xff);
11627c478bd9Sstevel@tonic-gate 		/* Set 8 bits, 1 stop bit */
11634ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
11647c478bd9Sstevel@tonic-gate 		    STOP1|BITS8);
11657c478bd9Sstevel@tonic-gate 		/* Set loopback mode */
11664ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
11677c478bd9Sstevel@tonic-gate 		    DTR | RTS | ASY_LOOP | OUT1 | OUT2);
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 		/* Overfill fifo */
11707c478bd9Sstevel@tonic-gate 		for (i = 0; i < asy->asy_fifo_buf * 2; i++) {
11714ab75253Smrj 			ddi_put8(asy->asy_iohandle,
11727c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT, i);
11737c478bd9Sstevel@tonic-gate 		}
11747c478bd9Sstevel@tonic-gate 		/*
11757c478bd9Sstevel@tonic-gate 		 * Now there's an interesting question here about which
11767c478bd9Sstevel@tonic-gate 		 * FIFO we're testing the size of, RX or TX. We just
11777c478bd9Sstevel@tonic-gate 		 * filled the TX FIFO much faster than it can empty,
11787c478bd9Sstevel@tonic-gate 		 * although it is possible one or two characters may
11797c478bd9Sstevel@tonic-gate 		 * have gone from it to the TX shift register.
11807c478bd9Sstevel@tonic-gate 		 * We wait for enough time for all the characters to
11817c478bd9Sstevel@tonic-gate 		 * move into the RX FIFO and any excess characters to
11827c478bd9Sstevel@tonic-gate 		 * have been lost, and then read all the RX FIFO. So
11837c478bd9Sstevel@tonic-gate 		 * the answer we finally get will be the size which is
11847c478bd9Sstevel@tonic-gate 		 * the MIN(RX FIFO,(TX FIFO + 1 or 2)). The critical
11857c478bd9Sstevel@tonic-gate 		 * one is actually the TX FIFO, because if we overfill
11867c478bd9Sstevel@tonic-gate 		 * it in normal operation, the excess characters are
11877c478bd9Sstevel@tonic-gate 		 * lost with no warning.
11887c478bd9Sstevel@tonic-gate 		 */
11897c478bd9Sstevel@tonic-gate 		/*
11907c478bd9Sstevel@tonic-gate 		 * Wait for characters to move into RX FIFO.
11917c478bd9Sstevel@tonic-gate 		 * In theory, 200 * asy->asy_fifo_buf * 2 should be
11927c478bd9Sstevel@tonic-gate 		 * enough. However, in practice it isn't always, so we
11937c478bd9Sstevel@tonic-gate 		 * increase to 400 so some slow 16550A's finish, and we
11947c478bd9Sstevel@tonic-gate 		 * increase to 3 so we spot more characters coming back
11957c478bd9Sstevel@tonic-gate 		 * than we sent, in case that should ever happen.
11967c478bd9Sstevel@tonic-gate 		 */
11977c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(400 * asy->asy_fifo_buf * 3));
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 		/* Now see how many characters we can read back */
12007c478bd9Sstevel@tonic-gate 		for (i = 0; i < asy->asy_fifo_buf * 3; i++) {
12014ab75253Smrj 			ret = ddi_get8(asy->asy_iohandle,
12027c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + LSR);
12037c478bd9Sstevel@tonic-gate 			if (!(ret & RCA))
12047c478bd9Sstevel@tonic-gate 				break;	/* FIFO emptied */
12054ab75253Smrj 			(void) ddi_get8(asy->asy_iohandle,
12067c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT); /* lose another */
12077c478bd9Sstevel@tonic-gate 		}
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 		DEBUGCONT3(ASY_DEBUG_CHIP,
12107c478bd9Sstevel@tonic-gate 		    "asy%d FIFO size: expected=%d, measured=%d\n",
12117c478bd9Sstevel@tonic-gate 		    asy->asy_unit, asy->asy_fifo_buf, i);
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 		hwtype = asy->asy_hwtype;
12147c478bd9Sstevel@tonic-gate 		if (i < asy->asy_fifo_buf) {
12157c478bd9Sstevel@tonic-gate 			/*
12167c478bd9Sstevel@tonic-gate 			 * FIFO is somewhat smaller than we anticipated.
12177c478bd9Sstevel@tonic-gate 			 * If we have 16 characters usable, then this
12187c478bd9Sstevel@tonic-gate 			 * UART will probably work well enough in
12197c478bd9Sstevel@tonic-gate 			 * 16550A mode. If less than 16 characters,
12207c478bd9Sstevel@tonic-gate 			 * then we'd better not use it at all.
12217c478bd9Sstevel@tonic-gate 			 * UARTs with busted FIFOs do crop up.
12227c478bd9Sstevel@tonic-gate 			 */
12237c478bd9Sstevel@tonic-gate 			if (i >= 16 && asy->asy_fifo_buf >= 16) {
12247c478bd9Sstevel@tonic-gate 				/* fall back to a 16550A */
12257c478bd9Sstevel@tonic-gate 				hwtype = ASY16550A;
12267c478bd9Sstevel@tonic-gate 				asy->asy_fifo_buf = 16;
12277c478bd9Sstevel@tonic-gate 				asy->asy_fifor &= ~(FIFOEXTRA1 | FIFOEXTRA2);
12287c478bd9Sstevel@tonic-gate 			} else {
12297c478bd9Sstevel@tonic-gate 				/* fall back to no FIFO at all */
12307c478bd9Sstevel@tonic-gate 				hwtype = ASY16550;
12317c478bd9Sstevel@tonic-gate 				asy->asy_fifo_buf = 1;
12327c478bd9Sstevel@tonic-gate 				asy->asy_use_fifo = FIFO_OFF;
12337c478bd9Sstevel@tonic-gate 				asy->asy_fifor &=
12347c478bd9Sstevel@tonic-gate 				    ~(FIFO_ON | FIFOEXTRA1 | FIFOEXTRA2);
12357c478bd9Sstevel@tonic-gate 			}
12367c478bd9Sstevel@tonic-gate 		}
12377c478bd9Sstevel@tonic-gate 		/*
12387c478bd9Sstevel@tonic-gate 		 * We will need to reprogram the FIFO if we changed
12397c478bd9Sstevel@tonic-gate 		 * our mind about how to drive it above, and in any
12407c478bd9Sstevel@tonic-gate 		 * case, it would be a good idea to flush any garbage
12417c478bd9Sstevel@tonic-gate 		 * out incase the loopback test left anything behind.
12427c478bd9Sstevel@tonic-gate 		 * Again as earlier above, we must call asy_reset_fifo()
12437c478bd9Sstevel@tonic-gate 		 * before any possible downgrade of asy->asy_hwtype.
12447c478bd9Sstevel@tonic-gate 		 */
12457c478bd9Sstevel@tonic-gate 		if (asy->asy_hwtype >= ASY16650 && hwtype < ASY16650) {
12467c478bd9Sstevel@tonic-gate 			/* Disable 16650 enhanced mode */
12474ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
12487c478bd9Sstevel@tonic-gate 			    EFRACCESS);
12494ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + EFR,
12507c478bd9Sstevel@tonic-gate 			    0);
12514ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
12527c478bd9Sstevel@tonic-gate 			    STOP1|BITS8);
12537c478bd9Sstevel@tonic-gate 		}
12547c478bd9Sstevel@tonic-gate 		asy_reset_fifo(asy, FIFOTXFLSH | FIFORXFLSH);
12557c478bd9Sstevel@tonic-gate 		asy->asy_hwtype = hwtype;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 		/* Clear loopback mode and restore DTR/RTS */
12584ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR, mcr);
12597c478bd9Sstevel@tonic-gate 	}
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	DEBUGNOTE3(ASY_DEBUG_CHIP, "asy%d %s @ %p",
12627c478bd9Sstevel@tonic-gate 	    asy->asy_unit, asy_hw_name(asy), (void *)asy->asy_ioaddr);
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	/* Make UART type visible in device tree for prtconf, etc */
12657c478bd9Sstevel@tonic-gate 	dev = makedevice(DDI_MAJOR_T_UNKNOWN, asy->asy_unit);
12667c478bd9Sstevel@tonic-gate 	(void) ddi_prop_update_string(dev, devi, "uart", asy_hw_name(asy));
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	if (asy->asy_hwtype == ASY16550)	/* for broken 16550's, */
12697c478bd9Sstevel@tonic-gate 		asy->asy_hwtype = ASY8250A;	/* drive them as 8250A */
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate /*
12757c478bd9Sstevel@tonic-gate  * asyinit() initializes the TTY protocol-private data for this channel
12767c478bd9Sstevel@tonic-gate  * before enabling the interrupts.
12777c478bd9Sstevel@tonic-gate  */
12787c478bd9Sstevel@tonic-gate static void
12797c478bd9Sstevel@tonic-gate asyinit(struct asycom *asy)
12807c478bd9Sstevel@tonic-gate {
12817c478bd9Sstevel@tonic-gate 	struct asyncline *async;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	asy->asy_priv = kmem_zalloc(sizeof (struct asyncline), KM_SLEEP);
12847c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
12857c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
12867c478bd9Sstevel@tonic-gate 	async->async_common = asy;
12877c478bd9Sstevel@tonic-gate 	cv_init(&async->async_flags_cv, NULL, CV_DRIVER, NULL);
12887c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
12927c478bd9Sstevel@tonic-gate static int
12937c478bd9Sstevel@tonic-gate asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr)
12947c478bd9Sstevel@tonic-gate {
12957c478bd9Sstevel@tonic-gate 	struct asycom	*asy;
12967c478bd9Sstevel@tonic-gate 	struct asyncline *async;
12977c478bd9Sstevel@tonic-gate 	int		mcr;
12987c478bd9Sstevel@tonic-gate 	int		unit;
12997c478bd9Sstevel@tonic-gate 	int 		len;
13007c478bd9Sstevel@tonic-gate 	struct termios 	*termiosp;
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	unit = UNIT(*dev);
13037c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_CLOSE, "asy%dopen\n", unit);
13047c478bd9Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, unit);
13057c478bd9Sstevel@tonic-gate 	if (asy == NULL)
13067c478bd9Sstevel@tonic-gate 		return (ENXIO);		/* unit not configured */
13077c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
13087c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate again:
13117c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	/*
13147c478bd9Sstevel@tonic-gate 	 * Block waiting for carrier to come up, unless this is a no-delay open.
13157c478bd9Sstevel@tonic-gate 	 */
13167c478bd9Sstevel@tonic-gate 	if (!(async->async_flags & ASYNC_ISOPEN)) {
13177c478bd9Sstevel@tonic-gate 		/*
13187c478bd9Sstevel@tonic-gate 		 * Set the default termios settings (cflag).
13197c478bd9Sstevel@tonic-gate 		 * Others are set in ldterm.
13207c478bd9Sstevel@tonic-gate 		 */
13217c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_root_node(),
13247c478bd9Sstevel@tonic-gate 		    0, "ttymodes",
13257c478bd9Sstevel@tonic-gate 		    (caddr_t)&termiosp, &len) == DDI_PROP_SUCCESS &&
13267c478bd9Sstevel@tonic-gate 		    len == sizeof (struct termios)) {
13277c478bd9Sstevel@tonic-gate 			async->async_ttycommon.t_cflag = termiosp->c_cflag;
13287c478bd9Sstevel@tonic-gate 			kmem_free(termiosp, len);
13297c478bd9Sstevel@tonic-gate 		} else
13307c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
13317c478bd9Sstevel@tonic-gate 				"asy: couldn't get ttymodes property!");
13327c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 		/* eeprom mode support - respect properties */
13357c478bd9Sstevel@tonic-gate 		if (asy->asy_cflag)
13367c478bd9Sstevel@tonic-gate 			async->async_ttycommon.t_cflag = asy->asy_cflag;
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_iflag = 0;
13397c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_iocpending = NULL;
13407c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_row = 0;
13417c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_col = 0;
13427c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_xpixel = 0;
13437c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_ypixel = 0;
13447c478bd9Sstevel@tonic-gate 		async->async_dev = *dev;
13457c478bd9Sstevel@tonic-gate 		async->async_wbufcid = 0;
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 		async->async_startc = CSTART;
13487c478bd9Sstevel@tonic-gate 		async->async_stopc = CSTOP;
13497c478bd9Sstevel@tonic-gate 		asy_program(asy, ASY_INIT);
13507c478bd9Sstevel@tonic-gate 	} else if ((async->async_ttycommon.t_flags & TS_XCLUDE) &&
13517c478bd9Sstevel@tonic-gate 						secpolicy_excl_open(cr) != 0) {
13527c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
13537c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
13547c478bd9Sstevel@tonic-gate 		return (EBUSY);
13557c478bd9Sstevel@tonic-gate 	} else if ((*dev & OUTLINE) && !(async->async_flags & ASYNC_OUT)) {
13567c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
13577c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
13587c478bd9Sstevel@tonic-gate 		return (EBUSY);
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	if (*dev & OUTLINE)
13627c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_OUT;
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	/* Raise DTR on every open, but delay if it was just lowered. */
13657c478bd9Sstevel@tonic-gate 	while (async->async_flags & ASYNC_DTR_DELAY) {
13667c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_MODEM,
13677c478bd9Sstevel@tonic-gate 		    "asy%dopen: waiting for the ASYNC_DTR_DELAY to be clear\n",
13687c478bd9Sstevel@tonic-gate 		    unit);
13697c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
13707c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&async->async_flags_cv,
13717c478bd9Sstevel@tonic-gate 		    &asy->asy_excl) == 0) {
13727c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
13737c478bd9Sstevel@tonic-gate 			    "asy%dopen: interrupted by signal, exiting\n",
13747c478bd9Sstevel@tonic-gate 			    unit);
13757c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
13767c478bd9Sstevel@tonic-gate 			return (EINTR);
13777c478bd9Sstevel@tonic-gate 		}
13787c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
13797c478bd9Sstevel@tonic-gate 	}
13807c478bd9Sstevel@tonic-gate 
13814ab75253Smrj 	mcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
13824ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
13837c478bd9Sstevel@tonic-gate 		mcr|(asy->asy_mcr&DTR));
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	DEBUGCONT3(ASY_DEBUG_INIT,
13867c478bd9Sstevel@tonic-gate 		"asy%dopen: \"Raise DTR on every open\": make mcr = %x, "
13877c478bd9Sstevel@tonic-gate 		"make TS_SOFTCAR = %s\n",
13887c478bd9Sstevel@tonic-gate 		unit, mcr|(asy->asy_mcr&DTR),
13897c478bd9Sstevel@tonic-gate 		(asy->asy_flags & ASY_IGNORE_CD) ? "ON" : "OFF");
13907c478bd9Sstevel@tonic-gate 	if (asy->asy_flags & ASY_IGNORE_CD) {
13917c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_MODEM,
13927c478bd9Sstevel@tonic-gate 			"asy%dopen: ASY_IGNORE_CD set, set TS_SOFTCAR\n",
13937c478bd9Sstevel@tonic-gate 			unit);
13947c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_flags |= TS_SOFTCAR;
13957c478bd9Sstevel@tonic-gate 	}
13967c478bd9Sstevel@tonic-gate 	else
13977c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_flags &= ~TS_SOFTCAR;
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	/*
14007c478bd9Sstevel@tonic-gate 	 * Check carrier.
14017c478bd9Sstevel@tonic-gate 	 */
14024ab75253Smrj 	asy->asy_msr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MSR);
14037c478bd9Sstevel@tonic-gate 	DEBUGCONT3(ASY_DEBUG_INIT, "asy%dopen: TS_SOFTCAR is %s, "
14047c478bd9Sstevel@tonic-gate 		"MSR & DCD is %s\n",
14057c478bd9Sstevel@tonic-gate 		unit,
14067c478bd9Sstevel@tonic-gate 		(async->async_ttycommon.t_flags & TS_SOFTCAR) ? "set" : "clear",
14077c478bd9Sstevel@tonic-gate 		(asy->asy_msr & DCD) ? "set" : "clear");
14087c478bd9Sstevel@tonic-gate 	if (asy->asy_msr & DCD)
14097c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_CARR_ON;
14107c478bd9Sstevel@tonic-gate 	else
14117c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_CARR_ON;
14127c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	/*
14157c478bd9Sstevel@tonic-gate 	 * If FNDELAY and FNONBLOCK are clear, block until carrier up.
14167c478bd9Sstevel@tonic-gate 	 * Quit on interrupt.
14177c478bd9Sstevel@tonic-gate 	 */
14187c478bd9Sstevel@tonic-gate 	if (!(flag & (FNDELAY|FNONBLOCK)) &&
14197c478bd9Sstevel@tonic-gate 	    !(async->async_ttycommon.t_cflag & CLOCAL)) {
14207c478bd9Sstevel@tonic-gate 		if ((!(async->async_flags & (ASYNC_CARR_ON|ASYNC_OUT)) &&
14217c478bd9Sstevel@tonic-gate 		    !(async->async_ttycommon.t_flags & TS_SOFTCAR)) ||
14227c478bd9Sstevel@tonic-gate 		    ((async->async_flags & ASYNC_OUT) &&
14237c478bd9Sstevel@tonic-gate 		    !(*dev & OUTLINE))) {
14247c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_WOPEN;
14257c478bd9Sstevel@tonic-gate 			if (cv_wait_sig(&async->async_flags_cv,
14267c478bd9Sstevel@tonic-gate 			    &asy->asy_excl) == B_FALSE) {
14277c478bd9Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_WOPEN;
14287c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl);
14297c478bd9Sstevel@tonic-gate 				return (EINTR);
14307c478bd9Sstevel@tonic-gate 			}
14317c478bd9Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_WOPEN;
14327c478bd9Sstevel@tonic-gate 			goto again;
14337c478bd9Sstevel@tonic-gate 		}
14347c478bd9Sstevel@tonic-gate 	} else if ((async->async_flags & ASYNC_OUT) && !(*dev & OUTLINE)) {
14357c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
14367c478bd9Sstevel@tonic-gate 		return (EBUSY);
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	async->async_ttycommon.t_readq = rq;
14407c478bd9Sstevel@tonic-gate 	async->async_ttycommon.t_writeq = WR(rq);
14417c478bd9Sstevel@tonic-gate 	rq->q_ptr = WR(rq)->q_ptr = (caddr_t)async;
14427c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
14437c478bd9Sstevel@tonic-gate 	/*
14447c478bd9Sstevel@tonic-gate 	 * Caution here -- qprocson sets the pointers that are used by canput
14457c478bd9Sstevel@tonic-gate 	 * called by async_softint.  ASYNC_ISOPEN must *not* be set until those
14467c478bd9Sstevel@tonic-gate 	 * pointers are valid.
14477c478bd9Sstevel@tonic-gate 	 */
14487c478bd9Sstevel@tonic-gate 	qprocson(rq);
14497c478bd9Sstevel@tonic-gate 	async->async_flags |= ASYNC_ISOPEN;
14507c478bd9Sstevel@tonic-gate 	async->async_polltid = 0;
14517c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_INIT, "asy%dopen: done\n", unit);
14527c478bd9Sstevel@tonic-gate 	return (0);
14537c478bd9Sstevel@tonic-gate }
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate static void
14567c478bd9Sstevel@tonic-gate async_progress_check(void *arg)
14577c478bd9Sstevel@tonic-gate {
14587c478bd9Sstevel@tonic-gate 	struct asyncline *async = arg;
14597c478bd9Sstevel@tonic-gate 	struct asycom	 *asy = async->async_common;
14607c478bd9Sstevel@tonic-gate 	mblk_t *bp;
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	/*
14637c478bd9Sstevel@tonic-gate 	 * We define "progress" as either waiting on a timed break or delay, or
14647c478bd9Sstevel@tonic-gate 	 * having had at least one transmitter interrupt.  If none of these are
14657c478bd9Sstevel@tonic-gate 	 * true, then just terminate the output and wake up that close thread.
14667c478bd9Sstevel@tonic-gate 	 */
14677c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
14687c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
14697c478bd9Sstevel@tonic-gate 	if (!(async->async_flags & (ASYNC_BREAK|ASYNC_DELAY|ASYNC_PROGRESS))) {
14707c478bd9Sstevel@tonic-gate 		async->async_ocnt = 0;
14717c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_BUSY;
14727c478bd9Sstevel@tonic-gate 		async->async_timer = 0;
14737c478bd9Sstevel@tonic-gate 		bp = async->async_xmitblk;
14747c478bd9Sstevel@tonic-gate 		async->async_xmitblk = NULL;
14757c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
14767c478bd9Sstevel@tonic-gate 		if (bp != NULL)
14777c478bd9Sstevel@tonic-gate 			freeb(bp);
14787c478bd9Sstevel@tonic-gate 		/*
14797c478bd9Sstevel@tonic-gate 		 * Since this timer is running, we know that we're in exit(2).
14807c478bd9Sstevel@tonic-gate 		 * That means that the user can't possibly be waiting on any
14817c478bd9Sstevel@tonic-gate 		 * valid ioctl(2) completion anymore, and we should just flush
14827c478bd9Sstevel@tonic-gate 		 * everything.
14837c478bd9Sstevel@tonic-gate 		 */
14847c478bd9Sstevel@tonic-gate 		flushq(async->async_ttycommon.t_writeq, FLUSHALL);
14857c478bd9Sstevel@tonic-gate 		cv_broadcast(&async->async_flags_cv);
14867c478bd9Sstevel@tonic-gate 	} else {
14877c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_PROGRESS;
14887c478bd9Sstevel@tonic-gate 		async->async_timer = timeout(async_progress_check, async,
14897c478bd9Sstevel@tonic-gate 		    drv_usectohz(asy_drain_check));
14907c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
14917c478bd9Sstevel@tonic-gate 	}
14927c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
14937c478bd9Sstevel@tonic-gate }
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate /*
14967c478bd9Sstevel@tonic-gate  * Release DTR so that asyopen() can raise it.
14977c478bd9Sstevel@tonic-gate  */
14987c478bd9Sstevel@tonic-gate static void
14997c478bd9Sstevel@tonic-gate async_dtr_free(struct asyncline *async)
15007c478bd9Sstevel@tonic-gate {
15017c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_MODEM,
15047c478bd9Sstevel@tonic-gate 	    "async_dtr_free, clearing ASYNC_DTR_DELAY\n");
15057c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
15067c478bd9Sstevel@tonic-gate 	async->async_flags &= ~ASYNC_DTR_DELAY;
15077c478bd9Sstevel@tonic-gate 	async->async_dtrtid = 0;
15087c478bd9Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
15097c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
15107c478bd9Sstevel@tonic-gate }
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate /*
15137c478bd9Sstevel@tonic-gate  * Close routine.
15147c478bd9Sstevel@tonic-gate  */
15157c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
15167c478bd9Sstevel@tonic-gate static int
15177c478bd9Sstevel@tonic-gate asyclose(queue_t *q, int flag, cred_t *credp)
15187c478bd9Sstevel@tonic-gate {
15197c478bd9Sstevel@tonic-gate 	struct asyncline *async;
15207c478bd9Sstevel@tonic-gate 	struct asycom	 *asy;
15217c478bd9Sstevel@tonic-gate 	int icr, lcr;
15227c478bd9Sstevel@tonic-gate #ifdef DEBUG
15237c478bd9Sstevel@tonic-gate 	int instance;
15247c478bd9Sstevel@tonic-gate #endif
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	async = (struct asyncline *)q->q_ptr;
15277c478bd9Sstevel@tonic-gate 	ASSERT(async != NULL);
15287c478bd9Sstevel@tonic-gate #ifdef DEBUG
15297c478bd9Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
15307c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_CLOSE, "asy%dclose\n", instance);
15317c478bd9Sstevel@tonic-gate #endif
15327c478bd9Sstevel@tonic-gate 	asy = async->async_common;
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
15357c478bd9Sstevel@tonic-gate 	async->async_flags |= ASYNC_CLOSING;
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	/*
15387c478bd9Sstevel@tonic-gate 	 * Turn off PPS handling early to avoid events occuring during
15397c478bd9Sstevel@tonic-gate 	 * close.  Also reset the DCD edge monitoring bit.
15407c478bd9Sstevel@tonic-gate 	 */
15417c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
15427c478bd9Sstevel@tonic-gate 	asy->asy_flags &= ~(ASY_PPS | ASY_PPS_EDGE);
15437c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	/*
15467c478bd9Sstevel@tonic-gate 	 * There are two flavors of break -- timed (M_BREAK or TCSBRK) and
15477c478bd9Sstevel@tonic-gate 	 * untimed (TIOCSBRK).  For the timed case, these are enqueued on our
15487c478bd9Sstevel@tonic-gate 	 * write queue and there's a timer running, so we don't have to worry
15497c478bd9Sstevel@tonic-gate 	 * about them.  For the untimed case, though, the user obviously made a
15507c478bd9Sstevel@tonic-gate 	 * mistake, because these are handled immediately.  We'll terminate the
15517c478bd9Sstevel@tonic-gate 	 * break now and honor his implicit request by discarding the rest of
15527c478bd9Sstevel@tonic-gate 	 * the data.
15537c478bd9Sstevel@tonic-gate 	 */
15547c478bd9Sstevel@tonic-gate 	if (async->async_flags & ASYNC_OUT_SUSPEND) {
15557c478bd9Sstevel@tonic-gate 		if (async->async_utbrktid != 0) {
15567c478bd9Sstevel@tonic-gate 			(void) untimeout(async->async_utbrktid);
15577c478bd9Sstevel@tonic-gate 			async->async_utbrktid = 0;
15587c478bd9Sstevel@tonic-gate 		}
15597c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
15604ab75253Smrj 		lcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
15614ab75253Smrj 		ddi_put8(asy->asy_iohandle,
15627c478bd9Sstevel@tonic-gate 		    asy->asy_ioaddr + LCR, (lcr & ~SETBREAK));
15637c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
15647c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_SUSPEND;
15657c478bd9Sstevel@tonic-gate 		goto nodrain;
15667c478bd9Sstevel@tonic-gate 	}
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	/*
15697c478bd9Sstevel@tonic-gate 	 * If the user told us not to delay the close ("non-blocking"), then
15707c478bd9Sstevel@tonic-gate 	 * don't bother trying to drain.
15717c478bd9Sstevel@tonic-gate 	 *
15727c478bd9Sstevel@tonic-gate 	 * If the user did M_STOP (ASYNC_STOPPED), there's no hope of ever
15737c478bd9Sstevel@tonic-gate 	 * getting an M_START (since these messages aren't enqueued), and the
15747c478bd9Sstevel@tonic-gate 	 * only other way to clear the stop condition is by loss of DCD, which
15757c478bd9Sstevel@tonic-gate 	 * would discard the queue data.  Thus, we drop the output data if
15767c478bd9Sstevel@tonic-gate 	 * ASYNC_STOPPED is set.
15777c478bd9Sstevel@tonic-gate 	 */
15787c478bd9Sstevel@tonic-gate 	if ((flag & (FNDELAY|FNONBLOCK)) ||
15797c478bd9Sstevel@tonic-gate 	    (async->async_flags & ASYNC_STOPPED)) {
15807c478bd9Sstevel@tonic-gate 		goto nodrain;
15817c478bd9Sstevel@tonic-gate 	}
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	/*
15847c478bd9Sstevel@tonic-gate 	 * If there's any pending output, then we have to try to drain it.
15857c478bd9Sstevel@tonic-gate 	 * There are two main cases to be handled:
15867c478bd9Sstevel@tonic-gate 	 *	- called by close(2): need to drain until done or until
15877c478bd9Sstevel@tonic-gate 	 *	  a signal is received.  No timeout.
15887c478bd9Sstevel@tonic-gate 	 *	- called by exit(2): need to drain while making progress
15897c478bd9Sstevel@tonic-gate 	 *	  or until a timeout occurs.  No signals.
15907c478bd9Sstevel@tonic-gate 	 *
15917c478bd9Sstevel@tonic-gate 	 * If we can't rely on receiving a signal to get us out of a hung
15927c478bd9Sstevel@tonic-gate 	 * session, then we have to use a timer.  In this case, we set a timer
15937c478bd9Sstevel@tonic-gate 	 * to check for progress in sending the output data -- all that we ask
15947c478bd9Sstevel@tonic-gate 	 * (at each interval) is that there's been some progress made.  Since
15957c478bd9Sstevel@tonic-gate 	 * the interrupt routine grabs buffers from the write queue, we can't
15967c478bd9Sstevel@tonic-gate 	 * trust changes in async_ocnt.  Instead, we use a progress flag.
15977c478bd9Sstevel@tonic-gate 	 *
15987c478bd9Sstevel@tonic-gate 	 * Note that loss of carrier will cause the output queue to be flushed,
15997c478bd9Sstevel@tonic-gate 	 * and we'll wake up again and finish normally.
16007c478bd9Sstevel@tonic-gate 	 */
16017c478bd9Sstevel@tonic-gate 	if (!ddi_can_receive_sig() && asy_drain_check != 0) {
16027c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_PROGRESS;
16037c478bd9Sstevel@tonic-gate 		async->async_timer = timeout(async_progress_check, async,
16047c478bd9Sstevel@tonic-gate 		    drv_usectohz(asy_drain_check));
16057c478bd9Sstevel@tonic-gate 	}
16067c478bd9Sstevel@tonic-gate 	while (async->async_ocnt > 0 ||
16077c478bd9Sstevel@tonic-gate 	    async->async_ttycommon.t_writeq->q_first != NULL ||
16087c478bd9Sstevel@tonic-gate 	    (async->async_flags & (ASYNC_BUSY|ASYNC_BREAK|ASYNC_DELAY))) {
16097c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&async->async_flags_cv, &asy->asy_excl) == 0)
16107c478bd9Sstevel@tonic-gate 			break;
16117c478bd9Sstevel@tonic-gate 	}
16127c478bd9Sstevel@tonic-gate 	if (async->async_timer != 0) {
16137c478bd9Sstevel@tonic-gate 		(void) untimeout(async->async_timer);
16147c478bd9Sstevel@tonic-gate 		async->async_timer = 0;
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate nodrain:
16187c478bd9Sstevel@tonic-gate 	async->async_ocnt = 0;
16197c478bd9Sstevel@tonic-gate 	if (async->async_xmitblk != NULL)
16207c478bd9Sstevel@tonic-gate 		freeb(async->async_xmitblk);
16217c478bd9Sstevel@tonic-gate 	async->async_xmitblk = NULL;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	/*
16247c478bd9Sstevel@tonic-gate 	 * If line has HUPCL set or is incompletely opened fix up the modem
16257c478bd9Sstevel@tonic-gate 	 * lines.
16267c478bd9Sstevel@tonic-gate 	 */
16277c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_MODEM,
16287c478bd9Sstevel@tonic-gate 		"asy%dclose: next check HUPCL flag\n", instance);
16297c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
16307c478bd9Sstevel@tonic-gate 	if ((async->async_ttycommon.t_cflag & HUPCL) ||
16317c478bd9Sstevel@tonic-gate 	    (async->async_flags & ASYNC_WOPEN)) {
16327c478bd9Sstevel@tonic-gate 		DEBUGCONT3(ASY_DEBUG_MODEM,
16337c478bd9Sstevel@tonic-gate 			"asy%dclose: HUPCL flag = %x, ASYNC_WOPEN flag = %x\n",
16347c478bd9Sstevel@tonic-gate 			instance,
16357c478bd9Sstevel@tonic-gate 			async->async_ttycommon.t_cflag & HUPCL,
16367c478bd9Sstevel@tonic-gate 			async->async_ttycommon.t_cflag & ASYNC_WOPEN);
16377c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_DTR_DELAY;
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 		/* turn off DTR, RTS but NOT interrupt to 386 */
16407c478bd9Sstevel@tonic-gate 		if (asy->asy_flags & (ASY_IGNORE_CD|ASY_RTS_DTR_OFF)) {
16417c478bd9Sstevel@tonic-gate 			DEBUGCONT3(ASY_DEBUG_MODEM,
16427c478bd9Sstevel@tonic-gate 				"asy%dclose: ASY_IGNORE_CD flag = %x, "
16437c478bd9Sstevel@tonic-gate 				"ASY_RTS_DTR_OFF flag = %x\n",
16447c478bd9Sstevel@tonic-gate 				instance,
16457c478bd9Sstevel@tonic-gate 				asy->asy_flags & ASY_IGNORE_CD,
16467c478bd9Sstevel@tonic-gate 				asy->asy_flags & ASY_RTS_DTR_OFF);
16474ab75253Smrj 			ddi_put8(asy->asy_iohandle,
16487c478bd9Sstevel@tonic-gate 				asy->asy_ioaddr + MCR, asy->asy_mcr|OUT2);
16497c478bd9Sstevel@tonic-gate 		} else {
16507c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
16517c478bd9Sstevel@tonic-gate 			    "asy%dclose: Dropping DTR and RTS\n", instance);
16524ab75253Smrj 			ddi_put8(asy->asy_iohandle,
16537c478bd9Sstevel@tonic-gate 				asy->asy_ioaddr + MCR, OUT2);
16547c478bd9Sstevel@tonic-gate 		}
16557c478bd9Sstevel@tonic-gate 		async->async_dtrtid =
16567c478bd9Sstevel@tonic-gate 		    timeout((void (*)())async_dtr_free,
16577c478bd9Sstevel@tonic-gate 		    (caddr_t)async, drv_usectohz(asy_min_dtr_low));
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 	/*
16607c478bd9Sstevel@tonic-gate 	 * If nobody's using it now, turn off receiver interrupts.
16617c478bd9Sstevel@tonic-gate 	 */
16627c478bd9Sstevel@tonic-gate 	if ((async->async_flags & (ASYNC_WOPEN|ASYNC_ISOPEN)) == 0) {
16634ab75253Smrj 		icr = ddi_get8(asy->asy_iohandle,
16647c478bd9Sstevel@tonic-gate 			asy->asy_ioaddr + ICR);
16654ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR,
16667c478bd9Sstevel@tonic-gate 			(icr & ~RIEN));
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
16697c478bd9Sstevel@tonic-gate out:
16707c478bd9Sstevel@tonic-gate 	ttycommon_close(&async->async_ttycommon);
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	/*
16737c478bd9Sstevel@tonic-gate 	 * Cancel outstanding "bufcall" request.
16747c478bd9Sstevel@tonic-gate 	 */
16757c478bd9Sstevel@tonic-gate 	if (async->async_wbufcid != 0) {
16767c478bd9Sstevel@tonic-gate 		unbufcall(async->async_wbufcid);
16777c478bd9Sstevel@tonic-gate 		async->async_wbufcid = 0;
16787c478bd9Sstevel@tonic-gate 	}
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	/* Note that qprocsoff can't be done until after interrupts are off */
16817c478bd9Sstevel@tonic-gate 	qprocsoff(q);
16827c478bd9Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
16837c478bd9Sstevel@tonic-gate 	async->async_ttycommon.t_readq = NULL;
16847c478bd9Sstevel@tonic-gate 	async->async_ttycommon.t_writeq = NULL;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	/*
16877c478bd9Sstevel@tonic-gate 	 * Clear out device state, except persistant device property flags.
16887c478bd9Sstevel@tonic-gate 	 */
16897c478bd9Sstevel@tonic-gate 	async->async_flags &= (ASYNC_DTR_DELAY|ASY_RTS_DTR_OFF);
16907c478bd9Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
16917c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_CLOSE, "asy%dclose: done\n", instance);
16947c478bd9Sstevel@tonic-gate 	return (0);
16957c478bd9Sstevel@tonic-gate }
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate static boolean_t
16987c478bd9Sstevel@tonic-gate asy_isbusy(struct asycom *asy)
16997c478bd9Sstevel@tonic-gate {
17007c478bd9Sstevel@tonic-gate 	struct asyncline *async;
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_EOT, "asy_isbusy\n");
17037c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
17047c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
17057c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
17067c478bd9Sstevel@tonic-gate 	return ((async->async_ocnt > 0) ||
17074ab75253Smrj 		((ddi_get8(asy->asy_iohandle,
17087c478bd9Sstevel@tonic-gate 		    asy->asy_ioaddr + LSR) & (XSRE|XHRE)) == 0));
17097c478bd9Sstevel@tonic-gate }
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate static void
17127c478bd9Sstevel@tonic-gate asy_waiteot(struct asycom *asy)
17137c478bd9Sstevel@tonic-gate {
17147c478bd9Sstevel@tonic-gate 	/*
17157c478bd9Sstevel@tonic-gate 	 * Wait for the current transmission block and the
17167c478bd9Sstevel@tonic-gate 	 * current fifo data to transmit. Once this is done
17177c478bd9Sstevel@tonic-gate 	 * we may go on.
17187c478bd9Sstevel@tonic-gate 	 */
17197c478bd9Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_EOT, "asy_waiteot\n");
17207c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
17217c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
17227c478bd9Sstevel@tonic-gate 	while (asy_isbusy(asy)) {
17237c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
17247c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
17257c478bd9Sstevel@tonic-gate 		drv_usecwait(10000);		/* wait .01 */
17267c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
17277c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate }
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate /* asy_reset_fifo -- flush fifos and [re]program fifo control register */
17327c478bd9Sstevel@tonic-gate static void
17337c478bd9Sstevel@tonic-gate asy_reset_fifo(struct asycom *asy, uchar_t flush)
17347c478bd9Sstevel@tonic-gate {
17357c478bd9Sstevel@tonic-gate 	uchar_t lcr;
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	/* On a 16750, we have to set DLAB in order to set FIFOEXTRA. */
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	if (asy->asy_hwtype >= ASY16750) {
17404ab75253Smrj 		lcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
17414ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
17427c478bd9Sstevel@tonic-gate 		    lcr | DLAB);
17437c478bd9Sstevel@tonic-gate 	}
17447c478bd9Sstevel@tonic-gate 
17454ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + FIFOR,
17467c478bd9Sstevel@tonic-gate 	    asy->asy_fifor | flush);
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	/* Clear DLAB */
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	if (asy->asy_hwtype >= ASY16750) {
17514ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, lcr);
17527c478bd9Sstevel@tonic-gate 	}
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate /*
17567c478bd9Sstevel@tonic-gate  * Program the ASY port. Most of the async operation is based on the values
17577c478bd9Sstevel@tonic-gate  * of 'c_iflag' and 'c_cflag'.
17587c478bd9Sstevel@tonic-gate  */
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate #define	BAUDINDEX(cflg)	(((cflg) & CBAUDEXT) ? \
17617c478bd9Sstevel@tonic-gate 			(((cflg) & CBAUD) + CBAUD + 1) : ((cflg) & CBAUD))
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate static void
17647c478bd9Sstevel@tonic-gate asy_program(struct asycom *asy, int mode)
17657c478bd9Sstevel@tonic-gate {
17667c478bd9Sstevel@tonic-gate 	struct asyncline *async;
17677c478bd9Sstevel@tonic-gate 	int baudrate, c_flag;
17687c478bd9Sstevel@tonic-gate 	int icr, lcr;
17697c478bd9Sstevel@tonic-gate 	int flush_reg;
17707c478bd9Sstevel@tonic-gate 	int ocflags;
17717c478bd9Sstevel@tonic-gate #ifdef DEBUG
17727c478bd9Sstevel@tonic-gate 	int instance;
17737c478bd9Sstevel@tonic-gate #endif
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
17767c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
17797c478bd9Sstevel@tonic-gate #ifdef DEBUG
17807c478bd9Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
17817c478bd9Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_PROCS,
17827c478bd9Sstevel@tonic-gate 		"asy%d_program: mode = 0x%08X, enter\n", instance, mode);
17837c478bd9Sstevel@tonic-gate #endif
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	baudrate = BAUDINDEX(async->async_ttycommon.t_cflag);
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	async->async_ttycommon.t_cflag &= ~(CIBAUD);
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	if (baudrate > CBAUD) {
17907c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_cflag |= CIBAUDEXT;
17917c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_cflag |=
17927c478bd9Sstevel@tonic-gate 			(((baudrate - CBAUD - 1) << IBSHIFT) & CIBAUD);
17937c478bd9Sstevel@tonic-gate 	} else {
17947c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_cflag &= ~CIBAUDEXT;
17957c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_cflag |=
17967c478bd9Sstevel@tonic-gate 			((baudrate << IBSHIFT) & CIBAUD);
17977c478bd9Sstevel@tonic-gate 	}
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	c_flag = async->async_ttycommon.t_cflag &
18007c478bd9Sstevel@tonic-gate 		(CLOCAL|CREAD|CSTOPB|CSIZE|PARENB|PARODD|CBAUD|CBAUDEXT);
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	/* disable interrupts */
18034ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0);
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	ocflags = asy->asy_ocflag;
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	/* flush/reset the status registers */
18084ab75253Smrj 	(void) ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + ISR);
18094ab75253Smrj 	(void) ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR);
18104ab75253Smrj 	asy->asy_msr = flush_reg = ddi_get8(asy->asy_iohandle,
18117c478bd9Sstevel@tonic-gate 					asy->asy_ioaddr + MSR);
18127c478bd9Sstevel@tonic-gate 	/*
18137c478bd9Sstevel@tonic-gate 	 * The device is programmed in the open sequence, if we
18147c478bd9Sstevel@tonic-gate 	 * have to hardware handshake, then this is a good time
18157c478bd9Sstevel@tonic-gate 	 * to check if the device can receive any data.
18167c478bd9Sstevel@tonic-gate 	 */
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	if ((CRTSCTS & async->async_ttycommon.t_cflag) && !(flush_reg & CTS)) {
18197c478bd9Sstevel@tonic-gate 		async_flowcontrol_hw_output(asy, FLOW_STOP);
18207c478bd9Sstevel@tonic-gate 	} else {
18217c478bd9Sstevel@tonic-gate 		/*
18227c478bd9Sstevel@tonic-gate 		 * We can not use async_flowcontrol_hw_output(asy, FLOW_START)
18237c478bd9Sstevel@tonic-gate 		 * here, because if CRTSCTS is clear, we need clear
18247c478bd9Sstevel@tonic-gate 		 * ASYNC_HW_OUT_FLW bit.
18257c478bd9Sstevel@tonic-gate 		 */
18267c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_HW_OUT_FLW;
18277c478bd9Sstevel@tonic-gate 	}
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	/*
18307c478bd9Sstevel@tonic-gate 	 * If IXON is not set, clear ASYNC_SW_OUT_FLW;
18317c478bd9Sstevel@tonic-gate 	 * If IXON is set, no matter what IXON flag is before this
18327c478bd9Sstevel@tonic-gate 	 * function call to asy_program,
18337c478bd9Sstevel@tonic-gate 	 * we will use the old ASYNC_SW_OUT_FLW status.
18347c478bd9Sstevel@tonic-gate 	 * Because of handling IXON in the driver, we also should re-calculate
18357c478bd9Sstevel@tonic-gate 	 * the value of ASYNC_OUT_FLW_RESUME bit, but in fact,
18367c478bd9Sstevel@tonic-gate 	 * the TCSET* commands which call asy_program
18377c478bd9Sstevel@tonic-gate 	 * are put into the write queue, so there is no output needed to
18387c478bd9Sstevel@tonic-gate 	 * be resumed at this point.
18397c478bd9Sstevel@tonic-gate 	 */
18407c478bd9Sstevel@tonic-gate 	if (!(IXON & async->async_ttycommon.t_iflag))
18417c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_SW_OUT_FLW;
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	/* manually flush receive buffer or fifo (workaround for buggy fifos) */
18447c478bd9Sstevel@tonic-gate 	if (mode == ASY_INIT)
18457c478bd9Sstevel@tonic-gate 		if (asy->asy_use_fifo == FIFO_ON) {
18467c478bd9Sstevel@tonic-gate 			for (flush_reg = asy->asy_fifo_buf; flush_reg-- > 0; ) {
18474ab75253Smrj 				(void) ddi_get8(asy->asy_iohandle,
18487c478bd9Sstevel@tonic-gate 						asy->asy_ioaddr + DAT);
18497c478bd9Sstevel@tonic-gate 			}
18507c478bd9Sstevel@tonic-gate 		} else {
18514ab75253Smrj 			flush_reg = ddi_get8(asy->asy_iohandle,
18527c478bd9Sstevel@tonic-gate 					asy->asy_ioaddr + DAT);
18537c478bd9Sstevel@tonic-gate 		}
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	if (ocflags != (c_flag & ~CLOCAL) || mode == ASY_INIT) {
18567c478bd9Sstevel@tonic-gate 		/* Set line control */
18574ab75253Smrj 		lcr = ddi_get8(asy->asy_iohandle,
18587c478bd9Sstevel@tonic-gate 			asy->asy_ioaddr + LCR);
18597c478bd9Sstevel@tonic-gate 		lcr &= ~(WLS0|WLS1|STB|PEN|EPS);
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 		if (c_flag & CSTOPB)
18627c478bd9Sstevel@tonic-gate 			lcr |= STB;	/* 2 stop bits */
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 		if (c_flag & PARENB)
18657c478bd9Sstevel@tonic-gate 			lcr |= PEN;
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 		if ((c_flag & PARODD) == 0)
18687c478bd9Sstevel@tonic-gate 			lcr |= EPS;
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 		switch (c_flag & CSIZE) {
18717c478bd9Sstevel@tonic-gate 		case CS5:
18727c478bd9Sstevel@tonic-gate 			lcr |= BITS5;
18737c478bd9Sstevel@tonic-gate 			break;
18747c478bd9Sstevel@tonic-gate 		case CS6:
18757c478bd9Sstevel@tonic-gate 			lcr |= BITS6;
18767c478bd9Sstevel@tonic-gate 			break;
18777c478bd9Sstevel@tonic-gate 		case CS7:
18787c478bd9Sstevel@tonic-gate 			lcr |= BITS7;
18797c478bd9Sstevel@tonic-gate 			break;
18807c478bd9Sstevel@tonic-gate 		case CS8:
18817c478bd9Sstevel@tonic-gate 			lcr |= BITS8;
18827c478bd9Sstevel@tonic-gate 			break;
18837c478bd9Sstevel@tonic-gate 		}
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 		/* set the baud rate, unless it is "0" */
18864ab75253Smrj 		ddi_put8(asy->asy_iohandle,
18877c478bd9Sstevel@tonic-gate 			asy->asy_ioaddr + LCR, DLAB);
18887c478bd9Sstevel@tonic-gate 		if (baudrate != 0) {
18894ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
18907c478bd9Sstevel@tonic-gate 				asyspdtab[baudrate] & 0xff);
18914ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR,
18927c478bd9Sstevel@tonic-gate 				(asyspdtab[baudrate] >> 8) & 0xff);
18937c478bd9Sstevel@tonic-gate 		}
18947c478bd9Sstevel@tonic-gate 		/* set the line control modes */
18954ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, lcr);
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 		/*
18987c478bd9Sstevel@tonic-gate 		 * If we have a FIFO buffer, enable/flush
18997c478bd9Sstevel@tonic-gate 		 * at intialize time, flush if transitioning from
19007c478bd9Sstevel@tonic-gate 		 * CREAD off to CREAD on.
19017c478bd9Sstevel@tonic-gate 		 */
19027c478bd9Sstevel@tonic-gate 		if ((ocflags & CREAD) == 0 && (c_flag & CREAD) ||
19037c478bd9Sstevel@tonic-gate 		    mode == ASY_INIT)
19047c478bd9Sstevel@tonic-gate 			if (asy->asy_use_fifo == FIFO_ON)
19057c478bd9Sstevel@tonic-gate 				asy_reset_fifo(asy, FIFORXFLSH);
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 		/* remember the new cflags */
19087c478bd9Sstevel@tonic-gate 		asy->asy_ocflag = c_flag & ~CLOCAL;
19097c478bd9Sstevel@tonic-gate 	}
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	if (baudrate == 0)
19124ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
19137c478bd9Sstevel@tonic-gate 			(asy->asy_mcr & RTS) | OUT2);
19147c478bd9Sstevel@tonic-gate 	else
19154ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
19167c478bd9Sstevel@tonic-gate 			asy->asy_mcr | OUT2);
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	/*
19197c478bd9Sstevel@tonic-gate 	 * Call the modem status interrupt handler to check for the carrier
19207c478bd9Sstevel@tonic-gate 	 * in case CLOCAL was turned off after the carrier came on.
19217c478bd9Sstevel@tonic-gate 	 * (Note: Modem status interrupt is not enabled if CLOCAL is ON.)
19227c478bd9Sstevel@tonic-gate 	 */
19237c478bd9Sstevel@tonic-gate 	async_msint(asy);
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	/* Set interrupt control */
19267c478bd9Sstevel@tonic-gate 	DEBUGCONT3(ASY_DEBUG_MODM2,
19277c478bd9Sstevel@tonic-gate 		"asy%d_program: c_flag & CLOCAL = %x t_cflag & CRTSCTS = %x\n",
19287c478bd9Sstevel@tonic-gate 		instance,
19297c478bd9Sstevel@tonic-gate 		c_flag & CLOCAL,
19307c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_cflag & CRTSCTS);
19317c478bd9Sstevel@tonic-gate 	if ((c_flag & CLOCAL) && !(async->async_ttycommon.t_cflag & CRTSCTS))
19327c478bd9Sstevel@tonic-gate 		/*
19337c478bd9Sstevel@tonic-gate 		 * direct-wired line ignores DCD, so we don't enable modem
19347c478bd9Sstevel@tonic-gate 		 * status interrupts.
19357c478bd9Sstevel@tonic-gate 		 */
19367c478bd9Sstevel@tonic-gate 		icr = (TIEN | SIEN);
19377c478bd9Sstevel@tonic-gate 	else
19387c478bd9Sstevel@tonic-gate 		icr = (TIEN | SIEN | MIEN);
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	if (c_flag & CREAD)
19417c478bd9Sstevel@tonic-gate 		icr |= RIEN;
19427c478bd9Sstevel@tonic-gate 
19434ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, icr);
19447c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "asy%d_program: done\n", instance);
19457c478bd9Sstevel@tonic-gate }
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate static boolean_t
19487c478bd9Sstevel@tonic-gate asy_baudok(struct asycom *asy)
19497c478bd9Sstevel@tonic-gate {
19507c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
19517c478bd9Sstevel@tonic-gate 	int baudrate;
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	baudrate = BAUDINDEX(async->async_ttycommon.t_cflag);
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	if (baudrate >= sizeof (asyspdtab)/sizeof (*asyspdtab))
19577c478bd9Sstevel@tonic-gate 		return (0);
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	return (baudrate == 0 || asyspdtab[baudrate]);
19607c478bd9Sstevel@tonic-gate }
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate /*
19637c478bd9Sstevel@tonic-gate  * asyintr() is the High Level Interrupt Handler.
19647c478bd9Sstevel@tonic-gate  *
19657c478bd9Sstevel@tonic-gate  * There are four different interrupt types indexed by ISR register values:
19667c478bd9Sstevel@tonic-gate  *		0: modem
19677c478bd9Sstevel@tonic-gate  *		1: Tx holding register is empty, ready for next char
19687c478bd9Sstevel@tonic-gate  *		2: Rx register now holds a char to be picked up
19697c478bd9Sstevel@tonic-gate  *		3: error or break on line
19707c478bd9Sstevel@tonic-gate  * This routine checks the Bit 0 (interrupt-not-pending) to determine if
19717c478bd9Sstevel@tonic-gate  * the interrupt is from this port.
19727c478bd9Sstevel@tonic-gate  */
19737c478bd9Sstevel@tonic-gate uint_t
19747c478bd9Sstevel@tonic-gate asyintr(caddr_t argasy)
19757c478bd9Sstevel@tonic-gate {
19767c478bd9Sstevel@tonic-gate 	struct asycom		*asy = (struct asycom *)argasy;
19777c478bd9Sstevel@tonic-gate 	struct asyncline	*async;
19787c478bd9Sstevel@tonic-gate 	int			ret_status = DDI_INTR_UNCLAIMED;
19797c478bd9Sstevel@tonic-gate 	uchar_t			interrupt_id, lsr;
19807c478bd9Sstevel@tonic-gate 
19814ab75253Smrj 	interrupt_id = ddi_get8(asy->asy_iohandle,
19827c478bd9Sstevel@tonic-gate 				asy->asy_ioaddr + ISR) & 0x0F;
19837c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
19847c478bd9Sstevel@tonic-gate 	if ((async == NULL) || asy_addedsoft == 0 ||
19857c478bd9Sstevel@tonic-gate 		!(async->async_flags & (ASYNC_ISOPEN|ASYNC_WOPEN))) {
19867c478bd9Sstevel@tonic-gate 		if (interrupt_id & NOINTERRUPT)
19877c478bd9Sstevel@tonic-gate 			return (DDI_INTR_UNCLAIMED);
19887c478bd9Sstevel@tonic-gate 		else {
19897c478bd9Sstevel@tonic-gate 			/*
19907c478bd9Sstevel@tonic-gate 			 * reset the device by:
19917c478bd9Sstevel@tonic-gate 			 *	reading line status
19927c478bd9Sstevel@tonic-gate 			 *	reading any data from data status register
19937c478bd9Sstevel@tonic-gate 			 *	reading modem status
19947c478bd9Sstevel@tonic-gate 			 */
19954ab75253Smrj 			(void) ddi_get8(asy->asy_iohandle,
19967c478bd9Sstevel@tonic-gate 					asy->asy_ioaddr + LSR);
19974ab75253Smrj 			(void) ddi_get8(asy->asy_iohandle,
19987c478bd9Sstevel@tonic-gate 					asy->asy_ioaddr + DAT);
19994ab75253Smrj 			asy->asy_msr = ddi_get8(asy->asy_iohandle,
20007c478bd9Sstevel@tonic-gate 						asy->asy_ioaddr + MSR);
20017c478bd9Sstevel@tonic-gate 			return (DDI_INTR_CLAIMED);
20027c478bd9Sstevel@tonic-gate 		}
20037c478bd9Sstevel@tonic-gate 	}
20047c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
20057c478bd9Sstevel@tonic-gate 	/*
20067c478bd9Sstevel@tonic-gate 	 * We will loop until the interrupt line is pulled low. asy
20077c478bd9Sstevel@tonic-gate 	 * interrupt is edge triggered.
20087c478bd9Sstevel@tonic-gate 	 */
20097c478bd9Sstevel@tonic-gate 	/* CSTYLED */
20104ab75253Smrj 	for (;; interrupt_id = (ddi_get8(asy->asy_iohandle,
20117c478bd9Sstevel@tonic-gate 					asy->asy_ioaddr + ISR) & 0x0F)) {
20127c478bd9Sstevel@tonic-gate 		if (interrupt_id & NOINTERRUPT)
20137c478bd9Sstevel@tonic-gate 			break;
20147c478bd9Sstevel@tonic-gate 		ret_status = DDI_INTR_CLAIMED;
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_INTR,
20177c478bd9Sstevel@tonic-gate 			"asyintr: interrupt_id = 0x%d\n", interrupt_id);
20184ab75253Smrj 		lsr = ddi_get8(asy->asy_iohandle,
20197c478bd9Sstevel@tonic-gate 			asy->asy_ioaddr + LSR);
20207c478bd9Sstevel@tonic-gate 		switch (interrupt_id) {
20217c478bd9Sstevel@tonic-gate 		case RxRDY:
20227c478bd9Sstevel@tonic-gate 		case RSTATUS:
20237c478bd9Sstevel@tonic-gate 		case FFTMOUT:
20247c478bd9Sstevel@tonic-gate 			/* receiver interrupt or receiver errors */
20257c478bd9Sstevel@tonic-gate 			async_rxint(asy, lsr);
20267c478bd9Sstevel@tonic-gate 			break;
20277c478bd9Sstevel@tonic-gate 		case TxRDY:
20287c478bd9Sstevel@tonic-gate 			/* transmit interrupt */
20297c478bd9Sstevel@tonic-gate 			async_txint(asy);
20307c478bd9Sstevel@tonic-gate 			continue;
20317c478bd9Sstevel@tonic-gate 		case MSTATUS:
20327c478bd9Sstevel@tonic-gate 			/* modem status interrupt */
20337c478bd9Sstevel@tonic-gate 			async_msint(asy);
20347c478bd9Sstevel@tonic-gate 			break;
20357c478bd9Sstevel@tonic-gate 		}
20367c478bd9Sstevel@tonic-gate 		if ((lsr & XHRE) && (async->async_flags & ASYNC_BUSY) &&
20377c478bd9Sstevel@tonic-gate 		    (async->async_ocnt > 0))
20387c478bd9Sstevel@tonic-gate 			async_txint(asy);
20397c478bd9Sstevel@tonic-gate 	}
20407c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
20417c478bd9Sstevel@tonic-gate 	return (ret_status);
20427c478bd9Sstevel@tonic-gate }
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate /*
20457c478bd9Sstevel@tonic-gate  * Transmitter interrupt service routine.
20467c478bd9Sstevel@tonic-gate  * If there is more data to transmit in the current pseudo-DMA block,
20477c478bd9Sstevel@tonic-gate  * send the next character if output is not stopped or draining.
20487c478bd9Sstevel@tonic-gate  * Otherwise, queue up a soft interrupt.
20497c478bd9Sstevel@tonic-gate  *
20507c478bd9Sstevel@tonic-gate  * XXX -  Needs review for HW FIFOs.
20517c478bd9Sstevel@tonic-gate  */
20527c478bd9Sstevel@tonic-gate static void
20537c478bd9Sstevel@tonic-gate async_txint(struct asycom *asy)
20547c478bd9Sstevel@tonic-gate {
20557c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
20567c478bd9Sstevel@tonic-gate 	int		fifo_len;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	/*
20597c478bd9Sstevel@tonic-gate 	 * If ASYNC_BREAK or ASYNC_OUT_SUSPEND has been set, return to
20607c478bd9Sstevel@tonic-gate 	 * asyintr()'s context to claim the interrupt without performing
20617c478bd9Sstevel@tonic-gate 	 * any action. No character will be loaded into FIFO/THR until
20627c478bd9Sstevel@tonic-gate 	 * timed or untimed break is removed
20637c478bd9Sstevel@tonic-gate 	 */
20647c478bd9Sstevel@tonic-gate 	if (async->async_flags & (ASYNC_BREAK|ASYNC_OUT_SUSPEND))
20657c478bd9Sstevel@tonic-gate 		return;
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	fifo_len = asy->asy_fifo_buf; /* with FIFO buffers */
20687c478bd9Sstevel@tonic-gate 	if (fifo_len > asy_max_tx_fifo)
20697c478bd9Sstevel@tonic-gate 		fifo_len = asy_max_tx_fifo;
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
20727c478bd9Sstevel@tonic-gate 		fifo_len--;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	if (async->async_ocnt > 0 && fifo_len > 0 &&
20757c478bd9Sstevel@tonic-gate 	    !(async->async_flags &
20767c478bd9Sstevel@tonic-gate 	    (ASYNC_HW_OUT_FLW|ASYNC_SW_OUT_FLW|ASYNC_STOPPED))) {
20777c478bd9Sstevel@tonic-gate 		while (fifo_len-- > 0 && async->async_ocnt-- > 0) {
20784ab75253Smrj 			ddi_put8(asy->asy_iohandle,
20797c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT, *async->async_optr++);
20807c478bd9Sstevel@tonic-gate 		}
20817c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_PROGRESS;
20827c478bd9Sstevel@tonic-gate 	}
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	if (fifo_len <= 0)
20857c478bd9Sstevel@tonic-gate 		return;
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	ASYSETSOFT(asy);
20887c478bd9Sstevel@tonic-gate }
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate /*
20917c478bd9Sstevel@tonic-gate  * Interrupt on port: handle PPS event.  This function is only called
20927c478bd9Sstevel@tonic-gate  * for a port on which PPS event handling has been enabled.
20937c478bd9Sstevel@tonic-gate  */
20947c478bd9Sstevel@tonic-gate static void
20957c478bd9Sstevel@tonic-gate asy_ppsevent(struct asycom *asy, int msr)
20967c478bd9Sstevel@tonic-gate {
20977c478bd9Sstevel@tonic-gate 	if (asy->asy_flags & ASY_PPS_EDGE) {
20987c478bd9Sstevel@tonic-gate 		/* Have seen leading edge, now look for and record drop */
20997c478bd9Sstevel@tonic-gate 		if ((msr & DCD) == 0)
21007c478bd9Sstevel@tonic-gate 			asy->asy_flags &= ~ASY_PPS_EDGE;
21017c478bd9Sstevel@tonic-gate 		/*
21027c478bd9Sstevel@tonic-gate 		 * Waiting for leading edge, look for rise; stamp event and
21037c478bd9Sstevel@tonic-gate 		 * calibrate kernel clock.
21047c478bd9Sstevel@tonic-gate 		 */
21057c478bd9Sstevel@tonic-gate 	} else if (msr & DCD) {
21067c478bd9Sstevel@tonic-gate 			/*
21077c478bd9Sstevel@tonic-gate 			 * This code captures a timestamp at the designated
21087c478bd9Sstevel@tonic-gate 			 * transition of the PPS signal (DCD asserted).  The
21097c478bd9Sstevel@tonic-gate 			 * code provides a pointer to the timestamp, as well
21107c478bd9Sstevel@tonic-gate 			 * as the hardware counter value at the capture.
21117c478bd9Sstevel@tonic-gate 			 *
21127c478bd9Sstevel@tonic-gate 			 * Note: the kernel has nano based time values while
21137c478bd9Sstevel@tonic-gate 			 * NTP requires micro based, an in-line fast algorithm
21147c478bd9Sstevel@tonic-gate 			 * to convert nsec to usec is used here -- see hrt2ts()
21157c478bd9Sstevel@tonic-gate 			 * in common/os/timers.c for a full description.
21167c478bd9Sstevel@tonic-gate 			 */
21177c478bd9Sstevel@tonic-gate 			struct timeval *tvp = &asy_ppsev.tv;
21187c478bd9Sstevel@tonic-gate 			timestruc_t ts;
21197c478bd9Sstevel@tonic-gate 			long nsec, usec;
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 			asy->asy_flags |= ASY_PPS_EDGE;
21227c478bd9Sstevel@tonic-gate 			LED_OFF;
21237c478bd9Sstevel@tonic-gate 			gethrestime(&ts);
21247c478bd9Sstevel@tonic-gate 			LED_ON;
21257c478bd9Sstevel@tonic-gate 			nsec = ts.tv_nsec;
21267c478bd9Sstevel@tonic-gate 			usec = nsec + (nsec >> 2);
21277c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 1);
21287c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 2);
21297c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 4);
21307c478bd9Sstevel@tonic-gate 			usec = nsec - (usec >> 3);
21317c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 2);
21327c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 3);
21337c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 4);
21347c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 1);
21357c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 6);
21367c478bd9Sstevel@tonic-gate 			tvp->tv_usec = usec >> 10;
21377c478bd9Sstevel@tonic-gate 			tvp->tv_sec = ts.tv_sec;
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 			++asy_ppsev.serial;
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 			/*
21427c478bd9Sstevel@tonic-gate 			 * Because the kernel keeps a high-resolution time,
21437c478bd9Sstevel@tonic-gate 			 * pass the current highres timestamp in tvp and zero
21447c478bd9Sstevel@tonic-gate 			 * in usec.
21457c478bd9Sstevel@tonic-gate 			 */
21467c478bd9Sstevel@tonic-gate 			ddi_hardpps(tvp, 0);
21477c478bd9Sstevel@tonic-gate 	}
21487c478bd9Sstevel@tonic-gate }
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate /*
21517c478bd9Sstevel@tonic-gate  * Receiver interrupt: RxRDY interrupt, FIFO timeout interrupt or receive
21527c478bd9Sstevel@tonic-gate  * error interrupt.
21537c478bd9Sstevel@tonic-gate  * Try to put the character into the circular buffer for this line; if it
21547c478bd9Sstevel@tonic-gate  * overflows, indicate a circular buffer overrun. If this port is always
21557c478bd9Sstevel@tonic-gate  * to be serviced immediately, or the character is a STOP character, or
21567c478bd9Sstevel@tonic-gate  * more than 15 characters have arrived, queue up a soft interrupt to
21577c478bd9Sstevel@tonic-gate  * drain the circular buffer.
21587c478bd9Sstevel@tonic-gate  * XXX - needs review for hw FIFOs support.
21597c478bd9Sstevel@tonic-gate  */
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate static void
21627c478bd9Sstevel@tonic-gate async_rxint(struct asycom *asy, uchar_t lsr)
21637c478bd9Sstevel@tonic-gate {
21647c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
21657c478bd9Sstevel@tonic-gate 	uchar_t c;
21667c478bd9Sstevel@tonic-gate 	uint_t s, needsoft = 0;
21677c478bd9Sstevel@tonic-gate 	tty_common_t *tp;
21687c478bd9Sstevel@tonic-gate 	int looplim = asy->asy_fifo_buf * 2;
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	tp = &async->async_ttycommon;
21717c478bd9Sstevel@tonic-gate 	if (!(tp->t_cflag & CREAD)) {
21727c478bd9Sstevel@tonic-gate 		while (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
21734ab75253Smrj 			(void) (ddi_get8(asy->asy_iohandle,
21747c478bd9Sstevel@tonic-gate 					asy->asy_ioaddr + DAT) & 0xff);
21754ab75253Smrj 			lsr = ddi_get8(asy->asy_iohandle,
21767c478bd9Sstevel@tonic-gate 					asy->asy_ioaddr + LSR);
21777c478bd9Sstevel@tonic-gate 			if (looplim-- < 0)		/* limit loop */
21787c478bd9Sstevel@tonic-gate 				break;
21797c478bd9Sstevel@tonic-gate 		}
21807c478bd9Sstevel@tonic-gate 		return; /* line is not open for read? */
21817c478bd9Sstevel@tonic-gate 	}
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	while (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
21847c478bd9Sstevel@tonic-gate 		c = 0;
21857c478bd9Sstevel@tonic-gate 		s = 0;				/* reset error status */
21867c478bd9Sstevel@tonic-gate 		if (lsr & RCA) {
21874ab75253Smrj 			c = ddi_get8(asy->asy_iohandle,
21887c478bd9Sstevel@tonic-gate 				asy->asy_ioaddr + DAT) & 0xff;
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 			/*
21917c478bd9Sstevel@tonic-gate 			 * We handle XON/XOFF char if IXON is set,
21927c478bd9Sstevel@tonic-gate 			 * but if received char is _POSIX_VDISABLE,
21937c478bd9Sstevel@tonic-gate 			 * we left it to the up level module.
21947c478bd9Sstevel@tonic-gate 			 */
21957c478bd9Sstevel@tonic-gate 			if (tp->t_iflag & IXON) {
21967c478bd9Sstevel@tonic-gate 				if ((c == async->async_stopc) &&
21977c478bd9Sstevel@tonic-gate 				    (c != _POSIX_VDISABLE)) {
21987c478bd9Sstevel@tonic-gate 					async_flowcontrol_sw_output(asy,
21997c478bd9Sstevel@tonic-gate 					    FLOW_STOP);
22007c478bd9Sstevel@tonic-gate 					goto check_looplim;
22017c478bd9Sstevel@tonic-gate 				} else if ((c == async->async_startc) &&
22027c478bd9Sstevel@tonic-gate 				    (c != _POSIX_VDISABLE)) {
22037c478bd9Sstevel@tonic-gate 					async_flowcontrol_sw_output(asy,
22047c478bd9Sstevel@tonic-gate 					    FLOW_START);
22057c478bd9Sstevel@tonic-gate 					needsoft = 1;
22067c478bd9Sstevel@tonic-gate 					goto check_looplim;
22077c478bd9Sstevel@tonic-gate 				}
22087c478bd9Sstevel@tonic-gate 				if ((tp->t_iflag & IXANY) &&
22097c478bd9Sstevel@tonic-gate 				    (async->async_flags & ASYNC_SW_OUT_FLW)) {
22107c478bd9Sstevel@tonic-gate 					async_flowcontrol_sw_output(asy,
22117c478bd9Sstevel@tonic-gate 					    FLOW_START);
22127c478bd9Sstevel@tonic-gate 					needsoft = 1;
22137c478bd9Sstevel@tonic-gate 				}
22147c478bd9Sstevel@tonic-gate 			}
22157c478bd9Sstevel@tonic-gate 		}
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 		/*
22187c478bd9Sstevel@tonic-gate 		 * Check for character break sequence
22197c478bd9Sstevel@tonic-gate 		 */
22207c478bd9Sstevel@tonic-gate 		if ((abort_enable == KIOCABORTALTERNATE) &&
22217c478bd9Sstevel@tonic-gate 		    (asy->asy_flags & ASY_CONSOLE)) {
22227c478bd9Sstevel@tonic-gate 			if (abort_charseq_recognize(c))
22237c478bd9Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
22247c478bd9Sstevel@tonic-gate 		}
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 		/* Handle framing errors */
22277c478bd9Sstevel@tonic-gate 		if (lsr & (PARERR|FRMERR|BRKDET|OVRRUN)) {
22287c478bd9Sstevel@tonic-gate 			if (lsr & PARERR) {
22297c478bd9Sstevel@tonic-gate 				if (tp->t_iflag & INPCK) /* parity enabled */
22307c478bd9Sstevel@tonic-gate 					s |= PERROR;
22317c478bd9Sstevel@tonic-gate 			}
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 			if (lsr & (FRMERR|BRKDET))
22347c478bd9Sstevel@tonic-gate 				s |= FRERROR;
22357c478bd9Sstevel@tonic-gate 			if (lsr & OVRRUN) {
22367c478bd9Sstevel@tonic-gate 				async->async_hw_overrun = 1;
22377c478bd9Sstevel@tonic-gate 				s |= OVERRUN;
22387c478bd9Sstevel@tonic-gate 			}
22397c478bd9Sstevel@tonic-gate 		}
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 		if (s == 0)
22427c478bd9Sstevel@tonic-gate 			if ((tp->t_iflag & PARMRK) &&
22437c478bd9Sstevel@tonic-gate 			    !(tp->t_iflag & (IGNPAR|ISTRIP)) &&
22447c478bd9Sstevel@tonic-gate 			    (c == 0377))
22457c478bd9Sstevel@tonic-gate 				if (RING_POK(async, 2)) {
22467c478bd9Sstevel@tonic-gate 					RING_PUT(async, 0377);
22477c478bd9Sstevel@tonic-gate 					RING_PUT(async, c);
22487c478bd9Sstevel@tonic-gate 				} else
22497c478bd9Sstevel@tonic-gate 					async->async_sw_overrun = 1;
22507c478bd9Sstevel@tonic-gate 			else
22517c478bd9Sstevel@tonic-gate 				if (RING_POK(async, 1))
22527c478bd9Sstevel@tonic-gate 					RING_PUT(async, c);
22537c478bd9Sstevel@tonic-gate 				else
22547c478bd9Sstevel@tonic-gate 					async->async_sw_overrun = 1;
22557c478bd9Sstevel@tonic-gate 		else
22567c478bd9Sstevel@tonic-gate 			if (s & FRERROR) /* Handle framing errors */
22577c478bd9Sstevel@tonic-gate 				if (c == 0)
22587c478bd9Sstevel@tonic-gate 					if ((asy->asy_flags & ASY_CONSOLE) &&
22597c478bd9Sstevel@tonic-gate 					    (abort_enable !=
22607c478bd9Sstevel@tonic-gate 					    KIOCABORTALTERNATE))
22617c478bd9Sstevel@tonic-gate 						abort_sequence_enter((char *)0);
22627c478bd9Sstevel@tonic-gate 					else
22637c478bd9Sstevel@tonic-gate 						async->async_break++;
22647c478bd9Sstevel@tonic-gate 				else
22657c478bd9Sstevel@tonic-gate 					if (RING_POK(async, 1))
22667c478bd9Sstevel@tonic-gate 						RING_MARK(async, c, s);
22677c478bd9Sstevel@tonic-gate 					else
22687c478bd9Sstevel@tonic-gate 						async->async_sw_overrun = 1;
22697c478bd9Sstevel@tonic-gate 			else /* Parity errors are handled by ldterm */
22707c478bd9Sstevel@tonic-gate 				if (RING_POK(async, 1))
22717c478bd9Sstevel@tonic-gate 					RING_MARK(async, c, s);
22727c478bd9Sstevel@tonic-gate 				else
22737c478bd9Sstevel@tonic-gate 					async->async_sw_overrun = 1;
22747c478bd9Sstevel@tonic-gate check_looplim:
22754ab75253Smrj 		lsr = ddi_get8(asy->asy_iohandle,
22767c478bd9Sstevel@tonic-gate 			asy->asy_ioaddr + LSR);
22777c478bd9Sstevel@tonic-gate 		if (looplim-- < 0)		/* limit loop */
22787c478bd9Sstevel@tonic-gate 			break;
22797c478bd9Sstevel@tonic-gate 	}
22807c478bd9Sstevel@tonic-gate 	if ((RING_CNT(async) > (RINGSIZE * 3)/4) &&
22817c478bd9Sstevel@tonic-gate 	    !(async->async_inflow_source & IN_FLOW_RINGBUFF)) {
22827c478bd9Sstevel@tonic-gate 		async_flowcontrol_hw_input(asy, FLOW_STOP, IN_FLOW_RINGBUFF);
22837c478bd9Sstevel@tonic-gate 		(void) async_flowcontrol_sw_input(asy, FLOW_STOP,
22847c478bd9Sstevel@tonic-gate 		    IN_FLOW_RINGBUFF);
22857c478bd9Sstevel@tonic-gate 	}
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 	if ((async->async_flags & ASYNC_SERVICEIMM) || needsoft ||
22887c478bd9Sstevel@tonic-gate 	    (RING_FRAC(async)) || (async->async_polltid == 0))
22897c478bd9Sstevel@tonic-gate 		ASYSETSOFT(asy);	/* need a soft interrupt */
22907c478bd9Sstevel@tonic-gate }
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate /*
22937c478bd9Sstevel@tonic-gate  * Modem status interrupt.
22947c478bd9Sstevel@tonic-gate  *
22957c478bd9Sstevel@tonic-gate  * (Note: It is assumed that the MSR hasn't been read by asyintr().)
22967c478bd9Sstevel@tonic-gate  */
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate static void
22997c478bd9Sstevel@tonic-gate async_msint(struct asycom *asy)
23007c478bd9Sstevel@tonic-gate {
23017c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
23027c478bd9Sstevel@tonic-gate 	int msr, t_cflag = async->async_ttycommon.t_cflag;
23037c478bd9Sstevel@tonic-gate #ifdef DEBUG
23047c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
23057c478bd9Sstevel@tonic-gate #endif
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate async_msint_retry:
23087c478bd9Sstevel@tonic-gate 	/* this resets the interrupt */
23094ab75253Smrj 	msr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MSR);
23107c478bd9Sstevel@tonic-gate 	DEBUGCONT10(ASY_DEBUG_STATE,
23117c478bd9Sstevel@tonic-gate 		"async%d_msint call #%d:\n"
23127c478bd9Sstevel@tonic-gate 		"   transition: %3s %3s %3s %3s\n"
23137c478bd9Sstevel@tonic-gate 		"current state: %3s %3s %3s %3s\n",
23147c478bd9Sstevel@tonic-gate 		instance,
23157c478bd9Sstevel@tonic-gate 		++(asy->asy_msint_cnt),
23167c478bd9Sstevel@tonic-gate 		(msr & DCTS) ? "DCTS" : "    ",
23177c478bd9Sstevel@tonic-gate 		(msr & DDSR) ? "DDSR" : "    ",
23187c478bd9Sstevel@tonic-gate 		(msr & DRI)  ? "DRI " : "    ",
23197c478bd9Sstevel@tonic-gate 		(msr & DDCD) ? "DDCD" : "    ",
23207c478bd9Sstevel@tonic-gate 		(msr & CTS)  ? "CTS " : "    ",
23217c478bd9Sstevel@tonic-gate 		(msr & DSR)  ? "DSR " : "    ",
23227c478bd9Sstevel@tonic-gate 		(msr & RI)   ? "RI  " : "    ",
23237c478bd9Sstevel@tonic-gate 		(msr & DCD)  ? "DCD " : "    ");
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	/* If CTS status is changed, do H/W output flow control */
23267c478bd9Sstevel@tonic-gate 	if ((t_cflag & CRTSCTS) && (((asy->asy_msr ^ msr) & CTS) != 0))
23277c478bd9Sstevel@tonic-gate 		async_flowcontrol_hw_output(asy,
23287c478bd9Sstevel@tonic-gate 		    msr & CTS ? FLOW_START : FLOW_STOP);
23297c478bd9Sstevel@tonic-gate 	/*
23307c478bd9Sstevel@tonic-gate 	 * Reading MSR resets the interrupt, we save the
23317c478bd9Sstevel@tonic-gate 	 * value of msr so that other functions could examine MSR by
23327c478bd9Sstevel@tonic-gate 	 * looking at asy_msr.
23337c478bd9Sstevel@tonic-gate 	 */
23347c478bd9Sstevel@tonic-gate 	asy->asy_msr = (uchar_t)msr;
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	/* Handle PPS event */
23377c478bd9Sstevel@tonic-gate 	if (asy->asy_flags & ASY_PPS)
23387c478bd9Sstevel@tonic-gate 		asy_ppsevent(asy, msr);
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 	async->async_ext++;
23417c478bd9Sstevel@tonic-gate 	ASYSETSOFT(asy);
23427c478bd9Sstevel@tonic-gate 	/*
23437c478bd9Sstevel@tonic-gate 	 * We will make sure that the modem status presented to us
23447c478bd9Sstevel@tonic-gate 	 * during the previous read has not changed. If the chip samples
23457c478bd9Sstevel@tonic-gate 	 * the modem status on the falling edge of the interrupt line,
23467c478bd9Sstevel@tonic-gate 	 * and uses this state as the base for detecting change of modem
23477c478bd9Sstevel@tonic-gate 	 * status, we would miss a change of modem status event that occured
23487c478bd9Sstevel@tonic-gate 	 * after we initiated a read MSR operation.
23497c478bd9Sstevel@tonic-gate 	 */
23504ab75253Smrj 	msr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MSR);
23517c478bd9Sstevel@tonic-gate 	if (STATES(msr) != STATES(asy->asy_msr))
23527c478bd9Sstevel@tonic-gate 		goto	async_msint_retry;
23537c478bd9Sstevel@tonic-gate }
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate /*
23567c478bd9Sstevel@tonic-gate  * Handle a second-stage interrupt.
23577c478bd9Sstevel@tonic-gate  */
23587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23597c478bd9Sstevel@tonic-gate uint_t
23607c478bd9Sstevel@tonic-gate asysoftintr(caddr_t intarg)
23617c478bd9Sstevel@tonic-gate {
23627c478bd9Sstevel@tonic-gate 	struct asycom *asy;
23637c478bd9Sstevel@tonic-gate 	int rv;
23647c478bd9Sstevel@tonic-gate 	int instance;
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 	/*
23677c478bd9Sstevel@tonic-gate 	 * Test and clear soft interrupt.
23687c478bd9Sstevel@tonic-gate 	 */
23697c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_soft_lock);
23707c478bd9Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_PROCS, "asysoftintr: enter\n");
23717c478bd9Sstevel@tonic-gate 	rv = asysoftpend;
23727c478bd9Sstevel@tonic-gate 	if (rv != 0)
23737c478bd9Sstevel@tonic-gate 		asysoftpend = 0;
23747c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_soft_lock);
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 	if (rv) {
23777c478bd9Sstevel@tonic-gate 		/*
23787c478bd9Sstevel@tonic-gate 		 * Note - we can optimize the loop by remembering the last
23797c478bd9Sstevel@tonic-gate 		 * device that requested soft interrupt
23807c478bd9Sstevel@tonic-gate 		 */
23817c478bd9Sstevel@tonic-gate 		for (instance = 0; instance <= max_asy_instance; instance++) {
23827c478bd9Sstevel@tonic-gate 			asy = ddi_get_soft_state(asy_soft_state, instance);
23837c478bd9Sstevel@tonic-gate 			if (asy == NULL || asy->asy_priv == NULL)
23847c478bd9Sstevel@tonic-gate 				continue;
23857c478bd9Sstevel@tonic-gate 			mutex_enter(&asy_soft_lock);
23867c478bd9Sstevel@tonic-gate 			if (asy->asy_flags & ASY_NEEDSOFT) {
23877c478bd9Sstevel@tonic-gate 				asy->asy_flags &= ~ASY_NEEDSOFT;
23887c478bd9Sstevel@tonic-gate 				mutex_exit(&asy_soft_lock);
23897c478bd9Sstevel@tonic-gate 				async_softint(asy);
23907c478bd9Sstevel@tonic-gate 			} else
23917c478bd9Sstevel@tonic-gate 				mutex_exit(&asy_soft_lock);
23927c478bd9Sstevel@tonic-gate 		}
23937c478bd9Sstevel@tonic-gate 	}
23947c478bd9Sstevel@tonic-gate 	return (rv ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED);
23957c478bd9Sstevel@tonic-gate }
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate /*
23987c478bd9Sstevel@tonic-gate  * Handle a software interrupt.
23997c478bd9Sstevel@tonic-gate  */
24007c478bd9Sstevel@tonic-gate static void
24017c478bd9Sstevel@tonic-gate async_softint(struct asycom *asy)
24027c478bd9Sstevel@tonic-gate {
24037c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
24047c478bd9Sstevel@tonic-gate 	short	cc;
24057c478bd9Sstevel@tonic-gate 	mblk_t	*bp;
24067c478bd9Sstevel@tonic-gate 	queue_t	*q;
24077c478bd9Sstevel@tonic-gate 	uchar_t	val;
24087c478bd9Sstevel@tonic-gate 	uchar_t	c;
24097c478bd9Sstevel@tonic-gate 	tty_common_t	*tp;
24107c478bd9Sstevel@tonic-gate 	int nb;
24117c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_softint\n", instance);
24147c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_soft_lock);
24157c478bd9Sstevel@tonic-gate 	if (asy->asy_flags & ASY_DOINGSOFT) {
24167c478bd9Sstevel@tonic-gate 		asy->asy_flags |= ASY_DOINGSOFT_RETRY;
24177c478bd9Sstevel@tonic-gate 		mutex_exit(&asy_soft_lock);
24187c478bd9Sstevel@tonic-gate 		return;
24197c478bd9Sstevel@tonic-gate 	}
24207c478bd9Sstevel@tonic-gate 	asy->asy_flags |= ASY_DOINGSOFT;
24217c478bd9Sstevel@tonic-gate begin:
24227c478bd9Sstevel@tonic-gate 	asy->asy_flags &= ~ASY_DOINGSOFT_RETRY;
24237c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_soft_lock);
24247c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
24257c478bd9Sstevel@tonic-gate 	tp = &async->async_ttycommon;
24267c478bd9Sstevel@tonic-gate 	q = tp->t_readq;
24277c478bd9Sstevel@tonic-gate 	if (async->async_flags & ASYNC_OUT_FLW_RESUME) {
24287c478bd9Sstevel@tonic-gate 		if (async->async_ocnt > 0) {
24297c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
24307c478bd9Sstevel@tonic-gate 			async_resume(async);
24317c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
24327c478bd9Sstevel@tonic-gate 		} else {
24337c478bd9Sstevel@tonic-gate 			if (async->async_xmitblk)
24347c478bd9Sstevel@tonic-gate 				freeb(async->async_xmitblk);
24357c478bd9Sstevel@tonic-gate 			async->async_xmitblk = NULL;
24367c478bd9Sstevel@tonic-gate 			async_start(async);
24377c478bd9Sstevel@tonic-gate 		}
24387c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
24397c478bd9Sstevel@tonic-gate 	}
24407c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
24417c478bd9Sstevel@tonic-gate 	if (async->async_ext) {
24427c478bd9Sstevel@tonic-gate 		async->async_ext = 0;
24437c478bd9Sstevel@tonic-gate 		/* check for carrier up */
24447c478bd9Sstevel@tonic-gate 		DEBUGCONT3(ASY_DEBUG_MODM2,
24457c478bd9Sstevel@tonic-gate 			"async%d_softint: asy_msr & DCD = %x, "
24467c478bd9Sstevel@tonic-gate 			"tp->t_flags & TS_SOFTCAR = %x\n",
24477c478bd9Sstevel@tonic-gate 			instance,
24487c478bd9Sstevel@tonic-gate 			asy->asy_msr & DCD,
24497c478bd9Sstevel@tonic-gate 			tp->t_flags & TS_SOFTCAR);
24507c478bd9Sstevel@tonic-gate 		if (asy->asy_msr & DCD) {
24517c478bd9Sstevel@tonic-gate 			/* carrier present */
24527c478bd9Sstevel@tonic-gate 			if ((async->async_flags & ASYNC_CARR_ON) == 0) {
24537c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_MODM2,
24547c478bd9Sstevel@tonic-gate 					"async%d_softint: set ASYNC_CARR_ON\n",
24557c478bd9Sstevel@tonic-gate 					instance);
24567c478bd9Sstevel@tonic-gate 				async->async_flags |= ASYNC_CARR_ON;
24577c478bd9Sstevel@tonic-gate 				if (async->async_flags & ASYNC_ISOPEN) {
24587c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
24597c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
24607c478bd9Sstevel@tonic-gate 					(void) putctl(q, M_UNHANGUP);
24617c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
24627c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
24637c478bd9Sstevel@tonic-gate 				}
24647c478bd9Sstevel@tonic-gate 				cv_broadcast(&async->async_flags_cv);
24657c478bd9Sstevel@tonic-gate 			}
24667c478bd9Sstevel@tonic-gate 		} else {
24677c478bd9Sstevel@tonic-gate 			if ((async->async_flags & ASYNC_CARR_ON) &&
24687c478bd9Sstevel@tonic-gate 			    !(tp->t_cflag & CLOCAL) &&
24697c478bd9Sstevel@tonic-gate 			    !(tp->t_flags & TS_SOFTCAR)) {
24707c478bd9Sstevel@tonic-gate 				int flushflag;
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_MODEM,
24737c478bd9Sstevel@tonic-gate 					"async%d_softint: carrier dropped, "
24747c478bd9Sstevel@tonic-gate 					"so drop DTR\n",
24757c478bd9Sstevel@tonic-gate 					instance);
24767c478bd9Sstevel@tonic-gate 				/*
24777c478bd9Sstevel@tonic-gate 				 * Carrier went away.
24787c478bd9Sstevel@tonic-gate 				 * Drop DTR, abort any output in
24797c478bd9Sstevel@tonic-gate 				 * progress, indicate that output is
24807c478bd9Sstevel@tonic-gate 				 * not stopped, and send a hangup
24817c478bd9Sstevel@tonic-gate 				 * notification upstream.
24827c478bd9Sstevel@tonic-gate 				 */
24834ab75253Smrj 				val = ddi_get8(asy->asy_iohandle,
24847c478bd9Sstevel@tonic-gate 					asy->asy_ioaddr + MCR);
24854ab75253Smrj 				ddi_put8(asy->asy_iohandle,
24867c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + MCR, (val & ~DTR));
24877c478bd9Sstevel@tonic-gate 				if (async->async_flags & ASYNC_BUSY) {
24887c478bd9Sstevel@tonic-gate 				    DEBUGCONT0(ASY_DEBUG_BUSY,
24897c478bd9Sstevel@tonic-gate 					    "async_softint: "
24907c478bd9Sstevel@tonic-gate 					    "Carrier dropped.  "
24917c478bd9Sstevel@tonic-gate 					    "Clearing async_ocnt\n");
24927c478bd9Sstevel@tonic-gate 				    async->async_ocnt = 0;
24937c478bd9Sstevel@tonic-gate 				}	/* if */
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_STOPPED;
24967c478bd9Sstevel@tonic-gate 				if (async->async_flags & ASYNC_ISOPEN) {
24977c478bd9Sstevel@tonic-gate 				    mutex_exit(&asy->asy_excl_hi);
24987c478bd9Sstevel@tonic-gate 				    mutex_exit(&asy->asy_excl);
24997c478bd9Sstevel@tonic-gate 				    (void) putctl(q, M_HANGUP);
25007c478bd9Sstevel@tonic-gate 				    mutex_enter(&asy->asy_excl);
25017c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_MODEM,
25027c478bd9Sstevel@tonic-gate 					"async%d_softint: "
25037c478bd9Sstevel@tonic-gate 					"putctl(q, M_HANGUP)\n",
25047c478bd9Sstevel@tonic-gate 					instance);
25057c478bd9Sstevel@tonic-gate 				/*
25067c478bd9Sstevel@tonic-gate 				 * Flush FIFO buffers
25077c478bd9Sstevel@tonic-gate 				 * Any data left in there is invalid now
25087c478bd9Sstevel@tonic-gate 				 */
25097c478bd9Sstevel@tonic-gate 				if (asy->asy_use_fifo == FIFO_ON)
25107c478bd9Sstevel@tonic-gate 					asy_reset_fifo(asy, FIFOTXFLSH);
25117c478bd9Sstevel@tonic-gate 				/*
25127c478bd9Sstevel@tonic-gate 				 * Flush our write queue if we have one.
25137c478bd9Sstevel@tonic-gate 				 *
25147c478bd9Sstevel@tonic-gate 				 * If we're in the midst of close, then flush
25157c478bd9Sstevel@tonic-gate 				 * everything.  Don't leave stale ioctls lying
25167c478bd9Sstevel@tonic-gate 				 * about.
25177c478bd9Sstevel@tonic-gate 				 */
25187c478bd9Sstevel@tonic-gate 				flushflag = (async->async_flags &
25197c478bd9Sstevel@tonic-gate 				    ASYNC_CLOSING) ? FLUSHALL : FLUSHDATA;
25207c478bd9Sstevel@tonic-gate 				flushq(tp->t_writeq, flushflag);
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 				bp = async->async_xmitblk; /* active msg */
25237c478bd9Sstevel@tonic-gate 				if (bp != NULL) {
25247c478bd9Sstevel@tonic-gate 					freeb(bp);
25257c478bd9Sstevel@tonic-gate 					async->async_xmitblk = NULL;
25267c478bd9Sstevel@tonic-gate 				}
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
25297c478bd9Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_BUSY;
25307c478bd9Sstevel@tonic-gate 				/*
25317c478bd9Sstevel@tonic-gate 				 * This message warns of Carrier loss
25327c478bd9Sstevel@tonic-gate 				 * with data left to transmit can hang the
25337c478bd9Sstevel@tonic-gate 				 * system.
25347c478bd9Sstevel@tonic-gate 				 */
25357c478bd9Sstevel@tonic-gate 				DEBUGCONT0(ASY_DEBUG_MODEM,
25367c478bd9Sstevel@tonic-gate 					"async_softint: Flushing to "
25377c478bd9Sstevel@tonic-gate 					"prevent HUPCL hanging\n");
25387c478bd9Sstevel@tonic-gate 				}	/* if (ASYNC_ISOPEN) */
25397c478bd9Sstevel@tonic-gate 			}	/* if (ASYNC_CARR_ON && CLOCAL) */
25407c478bd9Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_CARR_ON;
25417c478bd9Sstevel@tonic-gate 			cv_broadcast(&async->async_flags_cv);
25427c478bd9Sstevel@tonic-gate 		}	/* else */
25437c478bd9Sstevel@tonic-gate 	}	/* if (async->async_ext) */
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 	/*
25487c478bd9Sstevel@tonic-gate 	 * If data has been added to the circular buffer, remove
25497c478bd9Sstevel@tonic-gate 	 * it from the buffer, and send it up the stream if there's
25507c478bd9Sstevel@tonic-gate 	 * somebody listening. Try to do it 16 bytes at a time. If we
25517c478bd9Sstevel@tonic-gate 	 * have more than 16 bytes to move, move 16 byte chunks and
25527c478bd9Sstevel@tonic-gate 	 * leave the rest for next time around (maybe it will grow).
25537c478bd9Sstevel@tonic-gate 	 */
25547c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
25557c478bd9Sstevel@tonic-gate 	if (!(async->async_flags & ASYNC_ISOPEN)) {
25567c478bd9Sstevel@tonic-gate 		RING_INIT(async);
25577c478bd9Sstevel@tonic-gate 		goto rv;
25587c478bd9Sstevel@tonic-gate 	}
25597c478bd9Sstevel@tonic-gate 	if ((cc = RING_CNT(async)) <= 0)
25607c478bd9Sstevel@tonic-gate 		goto rv;
25617c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	if (!canput(q)) {
25647c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
25657c478bd9Sstevel@tonic-gate 		if (!(async->async_inflow_source & IN_FLOW_STREAMS)) {
25667c478bd9Sstevel@tonic-gate 			async_flowcontrol_hw_input(asy, FLOW_STOP,
25677c478bd9Sstevel@tonic-gate 			    IN_FLOW_STREAMS);
25687c478bd9Sstevel@tonic-gate 			(void) async_flowcontrol_sw_input(asy, FLOW_STOP,
25697c478bd9Sstevel@tonic-gate 			    IN_FLOW_STREAMS);
25707c478bd9Sstevel@tonic-gate 		}
25717c478bd9Sstevel@tonic-gate 		goto rv;
25727c478bd9Sstevel@tonic-gate 	}
25737c478bd9Sstevel@tonic-gate 	if (async->async_inflow_source & IN_FLOW_STREAMS) {
25747c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
25757c478bd9Sstevel@tonic-gate 		async_flowcontrol_hw_input(asy, FLOW_START,
25767c478bd9Sstevel@tonic-gate 		    IN_FLOW_STREAMS);
25777c478bd9Sstevel@tonic-gate 		(void) async_flowcontrol_sw_input(asy, FLOW_START,
25787c478bd9Sstevel@tonic-gate 		    IN_FLOW_STREAMS);
25797c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
25807c478bd9Sstevel@tonic-gate 	}
25817c478bd9Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_INPUT,
25827c478bd9Sstevel@tonic-gate 		"async%d_softint: %d char(s) in queue.\n", instance, cc);
25837c478bd9Sstevel@tonic-gate 	if (!(bp = allocb(cc, BPRI_MED))) {
25847c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
25857c478bd9Sstevel@tonic-gate 		ttycommon_qfull(&async->async_ttycommon, q);
25867c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
25877c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
25887c478bd9Sstevel@tonic-gate 		goto rv;
25897c478bd9Sstevel@tonic-gate 	}
25907c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
25917c478bd9Sstevel@tonic-gate 	do {
25927c478bd9Sstevel@tonic-gate 		if (RING_ERR(async, S_ERRORS)) {
25937c478bd9Sstevel@tonic-gate 			RING_UNMARK(async);
25947c478bd9Sstevel@tonic-gate 			c = RING_GET(async);
25957c478bd9Sstevel@tonic-gate 			break;
25967c478bd9Sstevel@tonic-gate 		} else
25977c478bd9Sstevel@tonic-gate 			*bp->b_wptr++ = RING_GET(async);
25987c478bd9Sstevel@tonic-gate 	} while (--cc);
25997c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
26007c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
26017c478bd9Sstevel@tonic-gate 	if (bp->b_wptr > bp->b_rptr) {
26027c478bd9Sstevel@tonic-gate 			if (!canput(q)) {
26037c478bd9Sstevel@tonic-gate 				asyerror(CE_NOTE, "asy%d: local queue full",
26047c478bd9Sstevel@tonic-gate 					instance);
26057c478bd9Sstevel@tonic-gate 				freemsg(bp);
26067c478bd9Sstevel@tonic-gate 			} else
26077c478bd9Sstevel@tonic-gate 				(void) putq(q, bp);
26087c478bd9Sstevel@tonic-gate 	} else
26097c478bd9Sstevel@tonic-gate 		freemsg(bp);
26107c478bd9Sstevel@tonic-gate 	/*
26117c478bd9Sstevel@tonic-gate 	 * If we have a parity error, then send
26127c478bd9Sstevel@tonic-gate 	 * up an M_BREAK with the "bad"
26137c478bd9Sstevel@tonic-gate 	 * character as an argument. Let ldterm
26147c478bd9Sstevel@tonic-gate 	 * figure out what to do with the error.
26157c478bd9Sstevel@tonic-gate 	 */
26167c478bd9Sstevel@tonic-gate 	if (cc) {
26177c478bd9Sstevel@tonic-gate 		(void) putctl1(q, M_BREAK, c);
26187c478bd9Sstevel@tonic-gate 		ASYSETSOFT(async->async_common);	/* finish cc chars */
26197c478bd9Sstevel@tonic-gate 	}
26207c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
26217c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
26227c478bd9Sstevel@tonic-gate rv:
26237c478bd9Sstevel@tonic-gate 	if ((RING_CNT(async) < (RINGSIZE/4)) &&
26247c478bd9Sstevel@tonic-gate 	    (async->async_inflow_source & IN_FLOW_RINGBUFF)) {
26257c478bd9Sstevel@tonic-gate 		async_flowcontrol_hw_input(asy, FLOW_START, IN_FLOW_RINGBUFF);
26267c478bd9Sstevel@tonic-gate 		(void) async_flowcontrol_sw_input(asy, FLOW_START,
26277c478bd9Sstevel@tonic-gate 		    IN_FLOW_RINGBUFF);
26287c478bd9Sstevel@tonic-gate 	}
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 	/*
26317c478bd9Sstevel@tonic-gate 	 * If a transmission has finished, indicate that it's finished,
26327c478bd9Sstevel@tonic-gate 	 * and start that line up again.
26337c478bd9Sstevel@tonic-gate 	 */
26347c478bd9Sstevel@tonic-gate 	if (async->async_break > 0) {
26357c478bd9Sstevel@tonic-gate 		nb = async->async_break;
26367c478bd9Sstevel@tonic-gate 		async->async_break = 0;
26377c478bd9Sstevel@tonic-gate 		if (async->async_flags & ASYNC_ISOPEN) {
26387c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
26397c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
26407c478bd9Sstevel@tonic-gate 			for (; nb > 0; nb--)
26417c478bd9Sstevel@tonic-gate 				(void) putctl(q, M_BREAK);
26427c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
26437c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
26447c478bd9Sstevel@tonic-gate 		}
26457c478bd9Sstevel@tonic-gate 	}
26467c478bd9Sstevel@tonic-gate 	if (async->async_ocnt <= 0 && (async->async_flags & ASYNC_BUSY)) {
26477c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_BUSY,
26487c478bd9Sstevel@tonic-gate 		    "async%d_softint: Clearing ASYNC_BUSY.  async_ocnt=%d\n",
26497c478bd9Sstevel@tonic-gate 		    instance,
26507c478bd9Sstevel@tonic-gate 		    async->async_ocnt);
26517c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_BUSY;
26527c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
26537c478bd9Sstevel@tonic-gate 		if (async->async_xmitblk)
26547c478bd9Sstevel@tonic-gate 			freeb(async->async_xmitblk);
26557c478bd9Sstevel@tonic-gate 		async->async_xmitblk = NULL;
26567c478bd9Sstevel@tonic-gate 		async_start(async);
26577c478bd9Sstevel@tonic-gate 		/*
26587c478bd9Sstevel@tonic-gate 		 * If the flag isn't set after doing the async_start above, we
26597c478bd9Sstevel@tonic-gate 		 * may have finished all the queued output.  Signal any thread
26607c478bd9Sstevel@tonic-gate 		 * stuck in close.
26617c478bd9Sstevel@tonic-gate 		 */
26627c478bd9Sstevel@tonic-gate 		if (!(async->async_flags & ASYNC_BUSY))
26637c478bd9Sstevel@tonic-gate 			cv_broadcast(&async->async_flags_cv);
26647c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
26657c478bd9Sstevel@tonic-gate 	}
26667c478bd9Sstevel@tonic-gate 	/*
26677c478bd9Sstevel@tonic-gate 	 * A note about these overrun bits: all they do is *tell* someone
26687c478bd9Sstevel@tonic-gate 	 * about an error- They do not track multiple errors. In fact,
26697c478bd9Sstevel@tonic-gate 	 * you could consider them latched register bits if you like.
26707c478bd9Sstevel@tonic-gate 	 * We are only interested in printing the error message once for
26717c478bd9Sstevel@tonic-gate 	 * any cluster of overrun errrors.
26727c478bd9Sstevel@tonic-gate 	 */
26737c478bd9Sstevel@tonic-gate 	if (async->async_hw_overrun) {
26747c478bd9Sstevel@tonic-gate 		if (async->async_flags & ASYNC_ISOPEN) {
26757c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
26767c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
26777c478bd9Sstevel@tonic-gate 			asyerror(CE_NOTE, "asy%d: silo overflow", instance);
26787c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
26797c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
26807c478bd9Sstevel@tonic-gate 		}
26817c478bd9Sstevel@tonic-gate 		async->async_hw_overrun = 0;
26827c478bd9Sstevel@tonic-gate 	}
26837c478bd9Sstevel@tonic-gate 	if (async->async_sw_overrun) {
26847c478bd9Sstevel@tonic-gate 		if (async->async_flags & ASYNC_ISOPEN) {
26857c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
26867c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
26877c478bd9Sstevel@tonic-gate 			asyerror(CE_NOTE, "asy%d: ring buffer overflow",
26887c478bd9Sstevel@tonic-gate 				instance);
26897c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
26907c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
26917c478bd9Sstevel@tonic-gate 		}
26927c478bd9Sstevel@tonic-gate 		async->async_sw_overrun = 0;
26937c478bd9Sstevel@tonic-gate 	}
26947c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
26957c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
26967c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_soft_lock);
26977c478bd9Sstevel@tonic-gate 	if (asy->asy_flags & ASY_DOINGSOFT_RETRY) {
26987c478bd9Sstevel@tonic-gate 		goto begin;
26997c478bd9Sstevel@tonic-gate 	}
27007c478bd9Sstevel@tonic-gate 	asy->asy_flags &= ~ASY_DOINGSOFT;
27017c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_soft_lock);
27027c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_softint: done\n", instance);
27037c478bd9Sstevel@tonic-gate }
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate /*
27067c478bd9Sstevel@tonic-gate  * Restart output on a line after a delay or break timer expired.
27077c478bd9Sstevel@tonic-gate  */
27087c478bd9Sstevel@tonic-gate static void
27097c478bd9Sstevel@tonic-gate async_restart(void *arg)
27107c478bd9Sstevel@tonic-gate {
27117c478bd9Sstevel@tonic-gate 	struct asyncline *async = (struct asyncline *)arg;
27127c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
27137c478bd9Sstevel@tonic-gate 	uchar_t lcr;
27147c478bd9Sstevel@tonic-gate 
27157c478bd9Sstevel@tonic-gate 	/*
27167c478bd9Sstevel@tonic-gate 	 * If break timer expired, turn off the break bit.
27177c478bd9Sstevel@tonic-gate 	 */
27187c478bd9Sstevel@tonic-gate #ifdef DEBUG
27197c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_restart\n", instance);
27227c478bd9Sstevel@tonic-gate #endif
27237c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
27247c478bd9Sstevel@tonic-gate 	/*
27257c478bd9Sstevel@tonic-gate 	 * If ASYNC_OUT_SUSPEND is also set, we don't really
27267c478bd9Sstevel@tonic-gate 	 * clean the HW break, TIOCCBRK is responsible for this.
27277c478bd9Sstevel@tonic-gate 	 */
27287c478bd9Sstevel@tonic-gate 	if ((async->async_flags & ASYNC_BREAK) &&
27297c478bd9Sstevel@tonic-gate 	    !(async->async_flags & ASYNC_OUT_SUSPEND)) {
27307c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
27314ab75253Smrj 		lcr = ddi_get8(asy->asy_iohandle,
27327c478bd9Sstevel@tonic-gate 			asy->asy_ioaddr + LCR);
27334ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
27347c478bd9Sstevel@tonic-gate 			(lcr & ~SETBREAK));
27357c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
27367c478bd9Sstevel@tonic-gate 	}
27377c478bd9Sstevel@tonic-gate 	async->async_flags &= ~(ASYNC_DELAY|ASYNC_BREAK);
27387c478bd9Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
27397c478bd9Sstevel@tonic-gate 	async_start(async);
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
27427c478bd9Sstevel@tonic-gate }
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate static void
27457c478bd9Sstevel@tonic-gate async_start(struct asyncline *async)
27467c478bd9Sstevel@tonic-gate {
27477c478bd9Sstevel@tonic-gate 	async_nstart(async, 0);
27487c478bd9Sstevel@tonic-gate }
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate /*
27517c478bd9Sstevel@tonic-gate  * Start output on a line, unless it's busy, frozen, or otherwise.
27527c478bd9Sstevel@tonic-gate  */
27537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
27547c478bd9Sstevel@tonic-gate static void
27557c478bd9Sstevel@tonic-gate async_nstart(struct asyncline *async, int mode)
27567c478bd9Sstevel@tonic-gate {
27577c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
27587c478bd9Sstevel@tonic-gate 	int cc;
27597c478bd9Sstevel@tonic-gate 	queue_t *q;
27607c478bd9Sstevel@tonic-gate 	mblk_t *bp;
27617c478bd9Sstevel@tonic-gate 	uchar_t *xmit_addr;
27627c478bd9Sstevel@tonic-gate 	uchar_t	val;
27637c478bd9Sstevel@tonic-gate 	int	fifo_len = 1;
27647c478bd9Sstevel@tonic-gate 	boolean_t didsome;
27657c478bd9Sstevel@tonic-gate 	mblk_t *nbp;
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate #ifdef DEBUG
27687c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_nstart\n", instance);
27717c478bd9Sstevel@tonic-gate #endif
27727c478bd9Sstevel@tonic-gate 	if (asy->asy_use_fifo == FIFO_ON) {
27737c478bd9Sstevel@tonic-gate 		fifo_len = asy->asy_fifo_buf; /* with FIFO buffers */
27747c478bd9Sstevel@tonic-gate 		if (fifo_len > asy_max_tx_fifo)
27757c478bd9Sstevel@tonic-gate 			fifo_len = asy_max_tx_fifo;
27767c478bd9Sstevel@tonic-gate 	}
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	/*
27817c478bd9Sstevel@tonic-gate 	 * If the chip is busy (i.e., we're waiting for a break timeout
27827c478bd9Sstevel@tonic-gate 	 * to expire, or for the current transmission to finish, or for
27837c478bd9Sstevel@tonic-gate 	 * output to finish draining from chip), don't grab anything new.
27847c478bd9Sstevel@tonic-gate 	 */
27857c478bd9Sstevel@tonic-gate 	if (async->async_flags & (ASYNC_BREAK|ASYNC_BUSY)) {
27867c478bd9Sstevel@tonic-gate 		DEBUGCONT2((mode? ASY_DEBUG_OUT : 0),
27877c478bd9Sstevel@tonic-gate 			"async%d_nstart: start %s.\n",
27887c478bd9Sstevel@tonic-gate 			instance,
27897c478bd9Sstevel@tonic-gate 			async->async_flags & ASYNC_BREAK ? "break" : "busy");
27907c478bd9Sstevel@tonic-gate 		return;
27917c478bd9Sstevel@tonic-gate 	}
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	/*
27947c478bd9Sstevel@tonic-gate 	 * Check only pended sw input flow control.
27957c478bd9Sstevel@tonic-gate 	 */
27967c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
27977c478bd9Sstevel@tonic-gate 	if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
27987c478bd9Sstevel@tonic-gate 		fifo_len--;
27997c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 	/*
28027c478bd9Sstevel@tonic-gate 	 * If we're waiting for a delay timeout to expire, don't grab
28037c478bd9Sstevel@tonic-gate 	 * anything new.
28047c478bd9Sstevel@tonic-gate 	 */
28057c478bd9Sstevel@tonic-gate 	if (async->async_flags & ASYNC_DELAY) {
28067c478bd9Sstevel@tonic-gate 		DEBUGCONT1((mode? ASY_DEBUG_OUT : 0),
28077c478bd9Sstevel@tonic-gate 			"async%d_nstart: start ASYNC_DELAY.\n", instance);
28087c478bd9Sstevel@tonic-gate 		return;
28097c478bd9Sstevel@tonic-gate 	}
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	if ((q = async->async_ttycommon.t_writeq) == NULL) {
28127c478bd9Sstevel@tonic-gate 		DEBUGCONT1((mode? ASY_DEBUG_OUT : 0),
28137c478bd9Sstevel@tonic-gate 			"async%d_nstart: start writeq is null.\n", instance);
28147c478bd9Sstevel@tonic-gate 		return;	/* not attached to a stream */
28157c478bd9Sstevel@tonic-gate 	}
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 	for (;;) {
28187c478bd9Sstevel@tonic-gate 		if ((bp = getq(q)) == NULL)
28197c478bd9Sstevel@tonic-gate 			return;	/* no data to transmit */
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 		/*
28227c478bd9Sstevel@tonic-gate 		 * We have a message block to work on.
28237c478bd9Sstevel@tonic-gate 		 * Check whether it's a break, a delay, or an ioctl (the latter
28247c478bd9Sstevel@tonic-gate 		 * occurs if the ioctl in question was waiting for the output
28257c478bd9Sstevel@tonic-gate 		 * to drain).  If it's one of those, process it immediately.
28267c478bd9Sstevel@tonic-gate 		 */
28277c478bd9Sstevel@tonic-gate 		switch (bp->b_datap->db_type) {
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate 		case M_BREAK:
28307c478bd9Sstevel@tonic-gate 			/*
28317c478bd9Sstevel@tonic-gate 			 * Set the break bit, and arrange for "async_restart"
28327c478bd9Sstevel@tonic-gate 			 * to be called in 1/4 second; it will turn the
28337c478bd9Sstevel@tonic-gate 			 * break bit off, and call "async_start" to grab
28347c478bd9Sstevel@tonic-gate 			 * the next message.
28357c478bd9Sstevel@tonic-gate 			 */
28367c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
28374ab75253Smrj 			val = ddi_get8(asy->asy_iohandle,
28387c478bd9Sstevel@tonic-gate 				asy->asy_ioaddr + LCR);
28394ab75253Smrj 			ddi_put8(asy->asy_iohandle,
28407c478bd9Sstevel@tonic-gate 				asy->asy_ioaddr + LCR, (val | SETBREAK));
28417c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
28427c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_BREAK;
28437c478bd9Sstevel@tonic-gate 			(void) timeout(async_restart, (caddr_t)async,
28447c478bd9Sstevel@tonic-gate 			    drv_usectohz(1000000)/4);
28457c478bd9Sstevel@tonic-gate 			freemsg(bp);
28467c478bd9Sstevel@tonic-gate 			return;	/* wait for this to finish */
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 		case M_DELAY:
28497c478bd9Sstevel@tonic-gate 			/*
28507c478bd9Sstevel@tonic-gate 			 * Arrange for "async_restart" to be called when the
28517c478bd9Sstevel@tonic-gate 			 * delay expires; it will turn ASYNC_DELAY off,
28527c478bd9Sstevel@tonic-gate 			 * and call "async_start" to grab the next message.
28537c478bd9Sstevel@tonic-gate 			 */
28547c478bd9Sstevel@tonic-gate 			(void) timeout(async_restart, (caddr_t)async,
28557c478bd9Sstevel@tonic-gate 			    (int)(*(unsigned char *)bp->b_rptr + 6));
28567c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_DELAY;
28577c478bd9Sstevel@tonic-gate 			freemsg(bp);
28587c478bd9Sstevel@tonic-gate 			return;	/* wait for this to finish */
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 		case M_IOCTL:
28617c478bd9Sstevel@tonic-gate 			/*
28627c478bd9Sstevel@tonic-gate 			 * This ioctl was waiting for the output ahead of
28637c478bd9Sstevel@tonic-gate 			 * it to drain; obviously, it has.  Do it, and
28647c478bd9Sstevel@tonic-gate 			 * then grab the next message after it.
28657c478bd9Sstevel@tonic-gate 			 */
28667c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
28677c478bd9Sstevel@tonic-gate 			async_ioctl(async, q, bp);
28687c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
28697c478bd9Sstevel@tonic-gate 			continue;
28707c478bd9Sstevel@tonic-gate 		}
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 		while (bp != NULL && (cc = bp->b_wptr - bp->b_rptr) == 0) {
28737c478bd9Sstevel@tonic-gate 			nbp = bp->b_cont;
28747c478bd9Sstevel@tonic-gate 			freeb(bp);
28757c478bd9Sstevel@tonic-gate 			bp = nbp;
28767c478bd9Sstevel@tonic-gate 		}
28777c478bd9Sstevel@tonic-gate 		if (bp != NULL)
28787c478bd9Sstevel@tonic-gate 			break;
28797c478bd9Sstevel@tonic-gate 	}
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate 	/*
28827c478bd9Sstevel@tonic-gate 	 * We have data to transmit.  If output is stopped, put
28837c478bd9Sstevel@tonic-gate 	 * it back and try again later.
28847c478bd9Sstevel@tonic-gate 	 */
28857c478bd9Sstevel@tonic-gate 	if (async->async_flags & (ASYNC_HW_OUT_FLW | ASYNC_SW_OUT_FLW |
28867c478bd9Sstevel@tonic-gate 	    ASYNC_STOPPED | ASYNC_OUT_SUSPEND)) {
28877c478bd9Sstevel@tonic-gate 		(void) putbq(q, bp);
28887c478bd9Sstevel@tonic-gate 		return;
28897c478bd9Sstevel@tonic-gate 	}
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 	async->async_xmitblk = bp;
28927c478bd9Sstevel@tonic-gate 	xmit_addr = bp->b_rptr;
28937c478bd9Sstevel@tonic-gate 	bp = bp->b_cont;
28947c478bd9Sstevel@tonic-gate 	if (bp != NULL)
28957c478bd9Sstevel@tonic-gate 		(void) putbq(q, bp);	/* not done with this message yet */
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 	/*
28987c478bd9Sstevel@tonic-gate 	 * In 5-bit mode, the high order bits are used
28997c478bd9Sstevel@tonic-gate 	 * to indicate character sizes less than five,
29007c478bd9Sstevel@tonic-gate 	 * so we need to explicitly mask before transmitting
29017c478bd9Sstevel@tonic-gate 	 */
29027c478bd9Sstevel@tonic-gate 	if ((async->async_ttycommon.t_cflag & CSIZE) == CS5) {
29037c478bd9Sstevel@tonic-gate 		unsigned char *p = xmit_addr;
29047c478bd9Sstevel@tonic-gate 		int cnt = cc;
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 		while (cnt--)
29077c478bd9Sstevel@tonic-gate 			*p++ &= (unsigned char) 0x1f;
29087c478bd9Sstevel@tonic-gate 	}
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 	/*
29117c478bd9Sstevel@tonic-gate 	 * Set up this block for pseudo-DMA.
29127c478bd9Sstevel@tonic-gate 	 */
29137c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
29147c478bd9Sstevel@tonic-gate 	/*
29157c478bd9Sstevel@tonic-gate 	 * If the transmitter is ready, shove the first
29167c478bd9Sstevel@tonic-gate 	 * character out.
29177c478bd9Sstevel@tonic-gate 	 */
29187c478bd9Sstevel@tonic-gate 	didsome = B_FALSE;
29197c478bd9Sstevel@tonic-gate 	while (--fifo_len >= 0 && cc > 0) {
29204ab75253Smrj 		if (!(ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) &
29217c478bd9Sstevel@tonic-gate 		    XHRE))
29227c478bd9Sstevel@tonic-gate 			break;
29234ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
29247c478bd9Sstevel@tonic-gate 		    *xmit_addr++);
29257c478bd9Sstevel@tonic-gate 		cc--;
29267c478bd9Sstevel@tonic-gate 		didsome = B_TRUE;
29277c478bd9Sstevel@tonic-gate 	}
29287c478bd9Sstevel@tonic-gate 	async->async_optr = xmit_addr;
29297c478bd9Sstevel@tonic-gate 	async->async_ocnt = cc;
29307c478bd9Sstevel@tonic-gate 	if (didsome)
29317c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_PROGRESS;
29327c478bd9Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_BUSY,
29337c478bd9Sstevel@tonic-gate 		"async%d_nstart: Set ASYNC_BUSY.  async_ocnt=%d\n",
29347c478bd9Sstevel@tonic-gate 		instance,
29357c478bd9Sstevel@tonic-gate 		async->async_ocnt);
29367c478bd9Sstevel@tonic-gate 	async->async_flags |= ASYNC_BUSY;
29377c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
29387c478bd9Sstevel@tonic-gate }
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate /*
29417c478bd9Sstevel@tonic-gate  * Resume output by poking the transmitter.
29427c478bd9Sstevel@tonic-gate  */
29437c478bd9Sstevel@tonic-gate static void
29447c478bd9Sstevel@tonic-gate async_resume(struct asyncline *async)
29457c478bd9Sstevel@tonic-gate {
29467c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
29477c478bd9Sstevel@tonic-gate #ifdef DEBUG
29487c478bd9Sstevel@tonic-gate 	int instance;
29497c478bd9Sstevel@tonic-gate #endif
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
29527c478bd9Sstevel@tonic-gate #ifdef DEBUG
29537c478bd9Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
29547c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_resume\n", instance);
29557c478bd9Sstevel@tonic-gate #endif
29567c478bd9Sstevel@tonic-gate 
29574ab75253Smrj 	if (ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) & XHRE) {
29587c478bd9Sstevel@tonic-gate 		if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
29597c478bd9Sstevel@tonic-gate 			return;
29607c478bd9Sstevel@tonic-gate 		if (async->async_ocnt > 0 &&
29617c478bd9Sstevel@tonic-gate 		    !(async->async_flags &
29627c478bd9Sstevel@tonic-gate 		    (ASYNC_HW_OUT_FLW|ASYNC_SW_OUT_FLW|ASYNC_OUT_SUSPEND))) {
29634ab75253Smrj 			ddi_put8(asy->asy_iohandle,
29647c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT, *async->async_optr++);
29657c478bd9Sstevel@tonic-gate 			async->async_ocnt--;
29667c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_PROGRESS;
29677c478bd9Sstevel@tonic-gate 		}
29687c478bd9Sstevel@tonic-gate 	}
29697c478bd9Sstevel@tonic-gate }
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate /*
29727c478bd9Sstevel@tonic-gate  * Hold the untimed break to last the minimum time.
29737c478bd9Sstevel@tonic-gate  */
29747c478bd9Sstevel@tonic-gate static void
29757c478bd9Sstevel@tonic-gate async_hold_utbrk(void *arg)
29767c478bd9Sstevel@tonic-gate {
29777c478bd9Sstevel@tonic-gate 	struct asyncline *async = arg;
29787c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
29797c478bd9Sstevel@tonic-gate 
29807c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
29817c478bd9Sstevel@tonic-gate 	async->async_flags &= ~ASYNC_HOLD_UTBRK;
29827c478bd9Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
29837c478bd9Sstevel@tonic-gate 	async->async_utbrktid = 0;
29847c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
29857c478bd9Sstevel@tonic-gate }
29867c478bd9Sstevel@tonic-gate 
29877c478bd9Sstevel@tonic-gate /*
29887c478bd9Sstevel@tonic-gate  * Resume the untimed break.
29897c478bd9Sstevel@tonic-gate  */
29907c478bd9Sstevel@tonic-gate static void
29917c478bd9Sstevel@tonic-gate async_resume_utbrk(struct asyncline *async)
29927c478bd9Sstevel@tonic-gate {
29937c478bd9Sstevel@tonic-gate 	uchar_t	val;
29947c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
29957c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate 	/*
29987c478bd9Sstevel@tonic-gate 	 * Because the wait time is very short,
29997c478bd9Sstevel@tonic-gate 	 * so we use uninterruptably wait.
30007c478bd9Sstevel@tonic-gate 	 */
30017c478bd9Sstevel@tonic-gate 	while (async->async_flags & ASYNC_HOLD_UTBRK) {
30027c478bd9Sstevel@tonic-gate 		cv_wait(&async->async_flags_cv, &asy->asy_excl);
30037c478bd9Sstevel@tonic-gate 	}
30047c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
30057c478bd9Sstevel@tonic-gate 	/*
30067c478bd9Sstevel@tonic-gate 	 * Timed break and untimed break can exist simultaneously,
30077c478bd9Sstevel@tonic-gate 	 * if ASYNC_BREAK is also set at here, we don't
30087c478bd9Sstevel@tonic-gate 	 * really clean the HW break.
30097c478bd9Sstevel@tonic-gate 	 */
30107c478bd9Sstevel@tonic-gate 	if (!(async->async_flags & ASYNC_BREAK)) {
30114ab75253Smrj 		val = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
30124ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
30137c478bd9Sstevel@tonic-gate 		    (val & ~SETBREAK));
30147c478bd9Sstevel@tonic-gate 	}
30157c478bd9Sstevel@tonic-gate 	async->async_flags &= ~ASYNC_OUT_SUSPEND;
30167c478bd9Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
30177c478bd9Sstevel@tonic-gate 	if (async->async_ocnt > 0) {
30187c478bd9Sstevel@tonic-gate 		async_resume(async);
30197c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
30207c478bd9Sstevel@tonic-gate 	} else {
30217c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_BUSY;
30227c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
30237c478bd9Sstevel@tonic-gate 		if (async->async_xmitblk != NULL) {
30247c478bd9Sstevel@tonic-gate 			freeb(async->async_xmitblk);
30257c478bd9Sstevel@tonic-gate 			async->async_xmitblk = NULL;
30267c478bd9Sstevel@tonic-gate 		}
30277c478bd9Sstevel@tonic-gate 		async_start(async);
30287c478bd9Sstevel@tonic-gate 	}
30297c478bd9Sstevel@tonic-gate }
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate /*
30327c478bd9Sstevel@tonic-gate  * Process an "ioctl" message sent down to us.
30337c478bd9Sstevel@tonic-gate  * Note that we don't need to get any locks until we are ready to access
30347c478bd9Sstevel@tonic-gate  * the hardware.  Nothing we access until then is going to be altered
30357c478bd9Sstevel@tonic-gate  * outside of the STREAMS framework, so we should be safe.
30367c478bd9Sstevel@tonic-gate  */
30377c478bd9Sstevel@tonic-gate int asydelay = 10000;
30387c478bd9Sstevel@tonic-gate static void
30397c478bd9Sstevel@tonic-gate async_ioctl(struct asyncline *async, queue_t *wq, mblk_t *mp)
30407c478bd9Sstevel@tonic-gate {
30417c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
30427c478bd9Sstevel@tonic-gate 	tty_common_t  *tp = &async->async_ttycommon;
30437c478bd9Sstevel@tonic-gate 	struct iocblk *iocp;
30447c478bd9Sstevel@tonic-gate 	unsigned datasize;
30457c478bd9Sstevel@tonic-gate 	int error = 0;
30467c478bd9Sstevel@tonic-gate 	uchar_t val;
30477c478bd9Sstevel@tonic-gate 	mblk_t *datamp;
30487c478bd9Sstevel@tonic-gate 	unsigned int index;
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate #ifdef DEBUG
30517c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_ioctl\n", instance);
30547c478bd9Sstevel@tonic-gate #endif
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 	if (tp->t_iocpending != NULL) {
30577c478bd9Sstevel@tonic-gate 		/*
30587c478bd9Sstevel@tonic-gate 		 * We were holding an "ioctl" response pending the
30597c478bd9Sstevel@tonic-gate 		 * availability of an "mblk" to hold data to be passed up;
30607c478bd9Sstevel@tonic-gate 		 * another "ioctl" came through, which means that "ioctl"
30617c478bd9Sstevel@tonic-gate 		 * must have timed out or been aborted.
30627c478bd9Sstevel@tonic-gate 		 */
30637c478bd9Sstevel@tonic-gate 		freemsg(async->async_ttycommon.t_iocpending);
30647c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_iocpending = NULL;
30657c478bd9Sstevel@tonic-gate 	}
30667c478bd9Sstevel@tonic-gate 
30677c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate 	/*
30707c478bd9Sstevel@tonic-gate 	 * For TIOCMGET and the PPS ioctls, do NOT call ttycommon_ioctl()
30717c478bd9Sstevel@tonic-gate 	 * because this function frees up the message block (mp->b_cont) that
30727c478bd9Sstevel@tonic-gate 	 * contains the user location where we pass back the results.
30737c478bd9Sstevel@tonic-gate 	 *
30747c478bd9Sstevel@tonic-gate 	 * Similarly, CONSOPENPOLLEDIO needs ioc_count, which ttycommon_ioctl
30757c478bd9Sstevel@tonic-gate 	 * zaps.  We know that ttycommon_ioctl doesn't know any CONS*
30767c478bd9Sstevel@tonic-gate 	 * ioctls, so keep the others safe too.
30777c478bd9Sstevel@tonic-gate 	 */
30787c478bd9Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_IOCTL, "async%d_ioctl: %s\n",
30797c478bd9Sstevel@tonic-gate 		instance,
30807c478bd9Sstevel@tonic-gate 		iocp->ioc_cmd == TIOCMGET ? "TIOCMGET" :
30817c478bd9Sstevel@tonic-gate 		iocp->ioc_cmd == TIOCMSET ? "TIOCMSET" :
30827c478bd9Sstevel@tonic-gate 		iocp->ioc_cmd == TIOCMBIS ? "TIOCMBIS" :
30837c478bd9Sstevel@tonic-gate 		iocp->ioc_cmd == TIOCMBIC ? "TIOCMBIC" :
30847c478bd9Sstevel@tonic-gate 					    "other");
30857c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
30867c478bd9Sstevel@tonic-gate 	case TIOCMGET:
30877c478bd9Sstevel@tonic-gate 	case TIOCGPPS:
30887c478bd9Sstevel@tonic-gate 	case TIOCSPPS:
30897c478bd9Sstevel@tonic-gate 	case TIOCGPPSEV:
30907c478bd9Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
30917c478bd9Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
30927c478bd9Sstevel@tonic-gate 	case CONSSETABORTENABLE:
30937c478bd9Sstevel@tonic-gate 	case CONSGETABORTENABLE:
30947c478bd9Sstevel@tonic-gate 		error = -1; /* Do Nothing */
30957c478bd9Sstevel@tonic-gate 		break;
30967c478bd9Sstevel@tonic-gate 	default:
30977c478bd9Sstevel@tonic-gate 
30987c478bd9Sstevel@tonic-gate 		/*
30997c478bd9Sstevel@tonic-gate 		 * The only way in which "ttycommon_ioctl" can fail is if the
31007c478bd9Sstevel@tonic-gate 		 * "ioctl" requires a response containing data to be returned
31017c478bd9Sstevel@tonic-gate 		 * to the user, and no mblk could be allocated for the data.
31027c478bd9Sstevel@tonic-gate 		 * No such "ioctl" alters our state.  Thus, we always go ahead
31037c478bd9Sstevel@tonic-gate 		 * and do any state-changes the "ioctl" calls for.  If we
31047c478bd9Sstevel@tonic-gate 		 * couldn't allocate the data, "ttycommon_ioctl" has stashed
31057c478bd9Sstevel@tonic-gate 		 * the "ioctl" away safely, so we just call "bufcall" to
31067c478bd9Sstevel@tonic-gate 		 * request that we be called back when we stand a better
31077c478bd9Sstevel@tonic-gate 		 * chance of allocating the data.
31087c478bd9Sstevel@tonic-gate 		 */
31097c478bd9Sstevel@tonic-gate 		if ((datasize = ttycommon_ioctl(tp, wq, mp, &error)) != 0) {
31107c478bd9Sstevel@tonic-gate 			if (async->async_wbufcid)
31117c478bd9Sstevel@tonic-gate 				unbufcall(async->async_wbufcid);
31127c478bd9Sstevel@tonic-gate 			async->async_wbufcid = bufcall(datasize, BPRI_HI,
31137c478bd9Sstevel@tonic-gate 			    (void (*)(void *)) async_reioctl,
31147c478bd9Sstevel@tonic-gate 			    (void *)(intptr_t)async->async_common->asy_unit);
31157c478bd9Sstevel@tonic-gate 			return;
31167c478bd9Sstevel@tonic-gate 		}
31177c478bd9Sstevel@tonic-gate 	}
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
31207c478bd9Sstevel@tonic-gate 
31217c478bd9Sstevel@tonic-gate 	if (error == 0) {
31227c478bd9Sstevel@tonic-gate 		/*
31237c478bd9Sstevel@tonic-gate 		 * "ttycommon_ioctl" did most of the work; we just use the
31247c478bd9Sstevel@tonic-gate 		 * data it set up.
31257c478bd9Sstevel@tonic-gate 		 */
31267c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
31277c478bd9Sstevel@tonic-gate 
31287c478bd9Sstevel@tonic-gate 		case TCSETS:
31297c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
31307c478bd9Sstevel@tonic-gate 			if (asy_baudok(asy))
31317c478bd9Sstevel@tonic-gate 				asy_program(asy, ASY_NOINIT);
31327c478bd9Sstevel@tonic-gate 			else
31337c478bd9Sstevel@tonic-gate 				error = EINVAL;
31347c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
31357c478bd9Sstevel@tonic-gate 			break;
31367c478bd9Sstevel@tonic-gate 		case TCSETSF:
31377c478bd9Sstevel@tonic-gate 		case TCSETSW:
31387c478bd9Sstevel@tonic-gate 		case TCSETA:
31397c478bd9Sstevel@tonic-gate 		case TCSETAW:
31407c478bd9Sstevel@tonic-gate 		case TCSETAF:
31417c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
31427c478bd9Sstevel@tonic-gate 			if (!asy_baudok(asy))
31437c478bd9Sstevel@tonic-gate 				error = EINVAL;
31447c478bd9Sstevel@tonic-gate 			else {
31457c478bd9Sstevel@tonic-gate 				if (asy_isbusy(asy))
31467c478bd9Sstevel@tonic-gate 					asy_waiteot(asy);
31477c478bd9Sstevel@tonic-gate 				asy_program(asy, ASY_NOINIT);
31487c478bd9Sstevel@tonic-gate 			}
31497c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
31507c478bd9Sstevel@tonic-gate 			break;
31517c478bd9Sstevel@tonic-gate 		}
31527c478bd9Sstevel@tonic-gate 	} else if (error < 0) {
31537c478bd9Sstevel@tonic-gate 		/*
31547c478bd9Sstevel@tonic-gate 		 * "ttycommon_ioctl" didn't do anything; we process it here.
31557c478bd9Sstevel@tonic-gate 		 */
31567c478bd9Sstevel@tonic-gate 		error = 0;
31577c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 		case TIOCGPPS:
31607c478bd9Sstevel@tonic-gate 			/*
31617c478bd9Sstevel@tonic-gate 			 * Get PPS on/off.
31627c478bd9Sstevel@tonic-gate 			 */
31637c478bd9Sstevel@tonic-gate 			if (mp->b_cont != NULL)
31647c478bd9Sstevel@tonic-gate 				freemsg(mp->b_cont);
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate 			mp->b_cont = allocb(sizeof (int), BPRI_HI);
31677c478bd9Sstevel@tonic-gate 			if (mp->b_cont == NULL) {
31687c478bd9Sstevel@tonic-gate 				error = ENOMEM;
31697c478bd9Sstevel@tonic-gate 				break;
31707c478bd9Sstevel@tonic-gate 			}
31717c478bd9Sstevel@tonic-gate 			if (asy->asy_flags & ASY_PPS)
31727c478bd9Sstevel@tonic-gate 				*(int *)mp->b_cont->b_wptr = 1;
31737c478bd9Sstevel@tonic-gate 			else
31747c478bd9Sstevel@tonic-gate 				*(int *)mp->b_cont->b_wptr = 0;
31757c478bd9Sstevel@tonic-gate 			mp->b_cont->b_wptr += sizeof (int);
31767c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
31777c478bd9Sstevel@tonic-gate 			iocp->ioc_count = sizeof (int);
31787c478bd9Sstevel@tonic-gate 			break;
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 		case TIOCSPPS:
31817c478bd9Sstevel@tonic-gate 			/*
31827c478bd9Sstevel@tonic-gate 			 * Set PPS on/off.
31837c478bd9Sstevel@tonic-gate 			 */
31847c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
31857c478bd9Sstevel@tonic-gate 			if (error != 0)
31867c478bd9Sstevel@tonic-gate 				break;
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
31897c478bd9Sstevel@tonic-gate 			if (*(int *)mp->b_cont->b_rptr)
31907c478bd9Sstevel@tonic-gate 				asy->asy_flags |= ASY_PPS;
31917c478bd9Sstevel@tonic-gate 			else
31927c478bd9Sstevel@tonic-gate 				asy->asy_flags &= ~ASY_PPS;
31937c478bd9Sstevel@tonic-gate 			/* Reset edge sense */
31947c478bd9Sstevel@tonic-gate 			asy->asy_flags &= ~ASY_PPS_EDGE;
31957c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
31967c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
31977c478bd9Sstevel@tonic-gate 			break;
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 		case TIOCGPPSEV:
32007c478bd9Sstevel@tonic-gate 		{
32017c478bd9Sstevel@tonic-gate 			/*
32027c478bd9Sstevel@tonic-gate 			 * Get PPS event data.
32037c478bd9Sstevel@tonic-gate 			 */
32047c478bd9Sstevel@tonic-gate 			mblk_t *bp;
32057c478bd9Sstevel@tonic-gate 			void *buf;
32067c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
32077c478bd9Sstevel@tonic-gate 			struct ppsclockev32 p32;
32087c478bd9Sstevel@tonic-gate #endif
32097c478bd9Sstevel@tonic-gate 			struct ppsclockev ppsclockev;
32107c478bd9Sstevel@tonic-gate 
32117c478bd9Sstevel@tonic-gate 			if (mp->b_cont != NULL) {
32127c478bd9Sstevel@tonic-gate 				freemsg(mp->b_cont);
32137c478bd9Sstevel@tonic-gate 				mp->b_cont = NULL;
32147c478bd9Sstevel@tonic-gate 			}
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 			if ((asy->asy_flags & ASY_PPS) == 0) {
32177c478bd9Sstevel@tonic-gate 				error = ENXIO;
32187c478bd9Sstevel@tonic-gate 				break;
32197c478bd9Sstevel@tonic-gate 			}
32207c478bd9Sstevel@tonic-gate 
32217c478bd9Sstevel@tonic-gate 			/* Protect from incomplete asy_ppsev */
32227c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
32237c478bd9Sstevel@tonic-gate 			ppsclockev = asy_ppsev;
32247c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
32257c478bd9Sstevel@tonic-gate 
32267c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
32277c478bd9Sstevel@tonic-gate 			if ((iocp->ioc_flag & IOC_MODELS) != IOC_NATIVE) {
32287c478bd9Sstevel@tonic-gate 				TIMEVAL_TO_TIMEVAL32(&p32.tv, &ppsclockev.tv);
32297c478bd9Sstevel@tonic-gate 				p32.serial = ppsclockev.serial;
32307c478bd9Sstevel@tonic-gate 				buf = &p32;
32317c478bd9Sstevel@tonic-gate 				iocp->ioc_count = sizeof (struct ppsclockev32);
32327c478bd9Sstevel@tonic-gate 			} else
32337c478bd9Sstevel@tonic-gate #endif
32347c478bd9Sstevel@tonic-gate 			{
32357c478bd9Sstevel@tonic-gate 				buf = &ppsclockev;
32367c478bd9Sstevel@tonic-gate 				iocp->ioc_count = sizeof (struct ppsclockev);
32377c478bd9Sstevel@tonic-gate 			}
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate 			if ((bp = allocb(iocp->ioc_count, BPRI_HI)) == NULL) {
32407c478bd9Sstevel@tonic-gate 				error = ENOMEM;
32417c478bd9Sstevel@tonic-gate 				break;
32427c478bd9Sstevel@tonic-gate 			}
32437c478bd9Sstevel@tonic-gate 			mp->b_cont = bp;
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate 			bcopy(buf, bp->b_wptr, iocp->ioc_count);
32467c478bd9Sstevel@tonic-gate 			bp->b_wptr += iocp->ioc_count;
32477c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
32487c478bd9Sstevel@tonic-gate 			break;
32497c478bd9Sstevel@tonic-gate 		}
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate 		case TCSBRK:
32527c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
32537c478bd9Sstevel@tonic-gate 			if (error != 0)
32547c478bd9Sstevel@tonic-gate 				break;
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate 			if (*(int *)mp->b_cont->b_rptr == 0) {
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate 				/*
32597c478bd9Sstevel@tonic-gate 				 * XXX Arrangements to ensure that a break
32607c478bd9Sstevel@tonic-gate 				 * isn't in progress should be sufficient.
32617c478bd9Sstevel@tonic-gate 				 * This ugly delay() is the only thing
32627c478bd9Sstevel@tonic-gate 				 * that seems to work on the NCR Worldmark.
32637c478bd9Sstevel@tonic-gate 				 * It should be replaced. Note that an
32647c478bd9Sstevel@tonic-gate 				 * asy_waiteot() also does not work.
32657c478bd9Sstevel@tonic-gate 				 */
32667c478bd9Sstevel@tonic-gate 				if (asydelay)
32677c478bd9Sstevel@tonic-gate 					delay(drv_usectohz(asydelay));
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 				while (async->async_flags & ASYNC_BREAK) {
32707c478bd9Sstevel@tonic-gate 					cv_wait(&async->async_flags_cv,
32717c478bd9Sstevel@tonic-gate 					    &asy->asy_excl);
32727c478bd9Sstevel@tonic-gate 				}
32737c478bd9Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
32747c478bd9Sstevel@tonic-gate 				/*
32757c478bd9Sstevel@tonic-gate 				 * We loop until the TSR is empty and then
32767c478bd9Sstevel@tonic-gate 				 * set the break.  ASYNC_BREAK has been set
32777c478bd9Sstevel@tonic-gate 				 * to ensure that no characters are
32787c478bd9Sstevel@tonic-gate 				 * transmitted while the TSR is being
32797c478bd9Sstevel@tonic-gate 				 * flushed and SOUT is being used for the
32807c478bd9Sstevel@tonic-gate 				 * break signal.
32817c478bd9Sstevel@tonic-gate 				 *
32827c478bd9Sstevel@tonic-gate 				 * The wait period is equal to
32837c478bd9Sstevel@tonic-gate 				 * clock / (baud * 16) * 16 * 2.
32847c478bd9Sstevel@tonic-gate 				 */
32857c478bd9Sstevel@tonic-gate 				index = BAUDINDEX(
32867c478bd9Sstevel@tonic-gate 					async->async_ttycommon.t_cflag);
32877c478bd9Sstevel@tonic-gate 				async->async_flags |= ASYNC_BREAK;
32884ab75253Smrj 				while ((ddi_get8(asy->asy_iohandle,
32897c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + LSR) & XSRE) == 0) {
32907c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
32917c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
32927c478bd9Sstevel@tonic-gate 					drv_usecwait(
32937c478bd9Sstevel@tonic-gate 						32*asyspdtab[index] & 0xfff);
32947c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
32957c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
32967c478bd9Sstevel@tonic-gate 				}
32977c478bd9Sstevel@tonic-gate 				/*
32987c478bd9Sstevel@tonic-gate 				 * Arrange for "async_restart"
32997c478bd9Sstevel@tonic-gate 				 * to be called in 1/4 second;
33007c478bd9Sstevel@tonic-gate 				 * it will turn the break bit off, and call
33017c478bd9Sstevel@tonic-gate 				 * "async_start" to grab the next message.
33027c478bd9Sstevel@tonic-gate 				 */
33034ab75253Smrj 				val = ddi_get8(asy->asy_iohandle,
33047c478bd9Sstevel@tonic-gate 					asy->asy_ioaddr + LCR);
33054ab75253Smrj 				ddi_put8(asy->asy_iohandle,
33067c478bd9Sstevel@tonic-gate 					asy->asy_ioaddr + LCR,
33077c478bd9Sstevel@tonic-gate 					(val | SETBREAK));
33087c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
33097c478bd9Sstevel@tonic-gate 				(void) timeout(async_restart, (caddr_t)async,
33107c478bd9Sstevel@tonic-gate 				    drv_usectohz(1000000)/4);
33117c478bd9Sstevel@tonic-gate 			} else {
33127c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_OUT,
33137c478bd9Sstevel@tonic-gate 					"async%d_ioctl: wait for flush.\n",
33147c478bd9Sstevel@tonic-gate 					instance);
33157c478bd9Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
33167c478bd9Sstevel@tonic-gate 				asy_waiteot(asy);
33177c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
33187c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_OUT,
33197c478bd9Sstevel@tonic-gate 					"async%d_ioctl: ldterm satisfied.\n",
33207c478bd9Sstevel@tonic-gate 					instance);
33217c478bd9Sstevel@tonic-gate 			}
33227c478bd9Sstevel@tonic-gate 			break;
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate 		case TIOCSBRK:
33257c478bd9Sstevel@tonic-gate 			if (!(async->async_flags & ASYNC_OUT_SUSPEND)) {
33267c478bd9Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
33277c478bd9Sstevel@tonic-gate 				async->async_flags |= ASYNC_OUT_SUSPEND;
33287c478bd9Sstevel@tonic-gate 				async->async_flags |= ASYNC_HOLD_UTBRK;
33297c478bd9Sstevel@tonic-gate 				index = BAUDINDEX(
33307c478bd9Sstevel@tonic-gate 				    async->async_ttycommon.t_cflag);
33314ab75253Smrj 				while ((ddi_get8(asy->asy_iohandle,
33327c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + LSR) & XSRE) == 0) {
33337c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
33347c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
33357c478bd9Sstevel@tonic-gate 					drv_usecwait(
33367c478bd9Sstevel@tonic-gate 					    32*asyspdtab[index] & 0xfff);
33377c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
33387c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
33397c478bd9Sstevel@tonic-gate 				}
33404ab75253Smrj 				val = ddi_get8(asy->asy_iohandle,
33417c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + LCR);
33424ab75253Smrj 				ddi_put8(asy->asy_iohandle,
33437c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + LCR, (val | SETBREAK));
33447c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
33457c478bd9Sstevel@tonic-gate 				/* wait for 100ms to hold BREAK */
33467c478bd9Sstevel@tonic-gate 				async->async_utbrktid =
33477c478bd9Sstevel@tonic-gate 				    timeout((void (*)())async_hold_utbrk,
33487c478bd9Sstevel@tonic-gate 				    (caddr_t)async,
33497c478bd9Sstevel@tonic-gate 				    drv_usectohz(asy_min_utbrk));
33507c478bd9Sstevel@tonic-gate 			}
33517c478bd9Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
33527c478bd9Sstevel@tonic-gate 			break;
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate 		case TIOCCBRK:
33557c478bd9Sstevel@tonic-gate 			if (async->async_flags & ASYNC_OUT_SUSPEND)
33567c478bd9Sstevel@tonic-gate 				async_resume_utbrk(async);
33577c478bd9Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
33587c478bd9Sstevel@tonic-gate 			break;
33597c478bd9Sstevel@tonic-gate 
33607c478bd9Sstevel@tonic-gate 		case TIOCMSET:
33617c478bd9Sstevel@tonic-gate 		case TIOCMBIS:
33627c478bd9Sstevel@tonic-gate 		case TIOCMBIC:
33637c478bd9Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT) {
33647c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
33657c478bd9Sstevel@tonic-gate 					"non-transparent\n", instance);
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate 				error = miocpullup(mp, sizeof (int));
33687c478bd9Sstevel@tonic-gate 				if (error != 0)
33697c478bd9Sstevel@tonic-gate 					break;
33707c478bd9Sstevel@tonic-gate 
33717c478bd9Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
33727c478bd9Sstevel@tonic-gate 				(void) asymctl(asy,
33737c478bd9Sstevel@tonic-gate 					dmtoasy(*(int *)mp->b_cont->b_rptr),
33747c478bd9Sstevel@tonic-gate 					iocp->ioc_cmd);
33757c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
33767c478bd9Sstevel@tonic-gate 				iocp->ioc_error = 0;
33777c478bd9Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
33787c478bd9Sstevel@tonic-gate 			} else {
33797c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
33807c478bd9Sstevel@tonic-gate 					"transparent\n", instance);
33817c478bd9Sstevel@tonic-gate 				mcopyin(mp, NULL, sizeof (int), NULL);
33827c478bd9Sstevel@tonic-gate 			}
33837c478bd9Sstevel@tonic-gate 			break;
33847c478bd9Sstevel@tonic-gate 
33857c478bd9Sstevel@tonic-gate 		case TIOCMGET:
33867c478bd9Sstevel@tonic-gate 			datamp = allocb(sizeof (int), BPRI_MED);
33877c478bd9Sstevel@tonic-gate 			if (datamp == NULL) {
33887c478bd9Sstevel@tonic-gate 				error = EAGAIN;
33897c478bd9Sstevel@tonic-gate 				break;
33907c478bd9Sstevel@tonic-gate 			}
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
33937c478bd9Sstevel@tonic-gate 			*(int *)datamp->b_rptr = asymctl(asy, 0, TIOCMGET);
33947c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
33957c478bd9Sstevel@tonic-gate 
33967c478bd9Sstevel@tonic-gate 			if (iocp->ioc_count == TRANSPARENT) {
33977c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
33987c478bd9Sstevel@tonic-gate 					"transparent\n", instance);
33997c478bd9Sstevel@tonic-gate 				mcopyout(mp, NULL, sizeof (int), NULL,
34007c478bd9Sstevel@tonic-gate 					datamp);
34017c478bd9Sstevel@tonic-gate 			} else {
34027c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
34037c478bd9Sstevel@tonic-gate 					"non-transparent\n", instance);
34047c478bd9Sstevel@tonic-gate 				mioc2ack(mp, datamp, sizeof (int), 0);
34057c478bd9Sstevel@tonic-gate 			}
34067c478bd9Sstevel@tonic-gate 			break;
34077c478bd9Sstevel@tonic-gate 
34087c478bd9Sstevel@tonic-gate 		case CONSOPENPOLLEDIO:
34097c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (struct cons_polledio *));
34107c478bd9Sstevel@tonic-gate 			if (error != 0)
34117c478bd9Sstevel@tonic-gate 				break;
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate 			*(struct cons_polledio **)mp->b_cont->b_rptr =
34147c478bd9Sstevel@tonic-gate 				&asy->polledio;
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
34177c478bd9Sstevel@tonic-gate 			break;
34187c478bd9Sstevel@tonic-gate 
34197c478bd9Sstevel@tonic-gate 		case CONSCLOSEPOLLEDIO:
34207c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
34217c478bd9Sstevel@tonic-gate 			iocp->ioc_error = 0;
34227c478bd9Sstevel@tonic-gate 			iocp->ioc_rval = 0;
34237c478bd9Sstevel@tonic-gate 			break;
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate 		case CONSSETABORTENABLE:
34267c478bd9Sstevel@tonic-gate 			error = secpolicy_console(iocp->ioc_cr);
34277c478bd9Sstevel@tonic-gate 			if (error != 0)
34287c478bd9Sstevel@tonic-gate 				break;
34297c478bd9Sstevel@tonic-gate 
34307c478bd9Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT) {
34317c478bd9Sstevel@tonic-gate 				error = EINVAL;
34327c478bd9Sstevel@tonic-gate 				break;
34337c478bd9Sstevel@tonic-gate 			}
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 			if (*(intptr_t *)mp->b_cont->b_rptr)
34367c478bd9Sstevel@tonic-gate 				asy->asy_flags |= ASY_CONSOLE;
34377c478bd9Sstevel@tonic-gate 			else
34387c478bd9Sstevel@tonic-gate 				asy->asy_flags &= ~ASY_CONSOLE;
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
34417c478bd9Sstevel@tonic-gate 			iocp->ioc_error = 0;
34427c478bd9Sstevel@tonic-gate 			iocp->ioc_rval = 0;
34437c478bd9Sstevel@tonic-gate 			break;
34447c478bd9Sstevel@tonic-gate 
34457c478bd9Sstevel@tonic-gate 		case CONSGETABORTENABLE:
34467c478bd9Sstevel@tonic-gate 			/*CONSTANTCONDITION*/
34477c478bd9Sstevel@tonic-gate 			ASSERT(sizeof (boolean_t) <= sizeof (boolean_t *));
34487c478bd9Sstevel@tonic-gate 			/*
34497c478bd9Sstevel@tonic-gate 			 * Store the return value right in the payload
34507c478bd9Sstevel@tonic-gate 			 * we were passed.  Crude.
34517c478bd9Sstevel@tonic-gate 			 */
34527c478bd9Sstevel@tonic-gate 			mcopyout(mp, NULL, sizeof (boolean_t), NULL, NULL);
34537c478bd9Sstevel@tonic-gate 			*(boolean_t *)mp->b_cont->b_rptr =
34547c478bd9Sstevel@tonic-gate 				(asy->asy_flags & ASY_CONSOLE) != 0;
34557c478bd9Sstevel@tonic-gate 			break;
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate 		default:
34587c478bd9Sstevel@tonic-gate 			/*
34597c478bd9Sstevel@tonic-gate 			 * If we don't understand it, it's an error.  NAK it.
34607c478bd9Sstevel@tonic-gate 			 */
34617c478bd9Sstevel@tonic-gate 			error = EINVAL;
34627c478bd9Sstevel@tonic-gate 			break;
34637c478bd9Sstevel@tonic-gate 		}
34647c478bd9Sstevel@tonic-gate 	}
34657c478bd9Sstevel@tonic-gate 	if (error != 0) {
34667c478bd9Sstevel@tonic-gate 		iocp->ioc_error = error;
34677c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
34687c478bd9Sstevel@tonic-gate 	}
34697c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
34707c478bd9Sstevel@tonic-gate 	qreply(wq, mp);
34717c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_ioctl: done\n", instance);
34727c478bd9Sstevel@tonic-gate }
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate static int
34757c478bd9Sstevel@tonic-gate asyrsrv(queue_t *q)
34767c478bd9Sstevel@tonic-gate {
34777c478bd9Sstevel@tonic-gate 	mblk_t *bp;
34787c478bd9Sstevel@tonic-gate 	struct asyncline *async;
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 	async = (struct asyncline *)q->q_ptr;
34817c478bd9Sstevel@tonic-gate 
34827c478bd9Sstevel@tonic-gate 	while (canputnext(q) && (bp = getq(q)))
34837c478bd9Sstevel@tonic-gate 		putnext(q, bp);
34847c478bd9Sstevel@tonic-gate 	ASYSETSOFT(async->async_common);
34857c478bd9Sstevel@tonic-gate 	async->async_polltid = 0;
34867c478bd9Sstevel@tonic-gate 	return (0);
34877c478bd9Sstevel@tonic-gate }
34887c478bd9Sstevel@tonic-gate 
34897c478bd9Sstevel@tonic-gate /*
34907c478bd9Sstevel@tonic-gate  * Put procedure for write queue.
34917c478bd9Sstevel@tonic-gate  * Respond to M_STOP, M_START, M_IOCTL, and M_FLUSH messages here;
34927c478bd9Sstevel@tonic-gate  * set the flow control character for M_STOPI and M_STARTI messages;
34937c478bd9Sstevel@tonic-gate  * queue up M_BREAK, M_DELAY, and M_DATA messages for processing
34947c478bd9Sstevel@tonic-gate  * by the start routine, and then call the start routine; discard
34957c478bd9Sstevel@tonic-gate  * everything else.  Note that this driver does not incorporate any
34967c478bd9Sstevel@tonic-gate  * mechanism to negotiate to handle the canonicalization process.
34977c478bd9Sstevel@tonic-gate  * It expects that these functions are handled in upper module(s),
34987c478bd9Sstevel@tonic-gate  * as we do in ldterm.
34997c478bd9Sstevel@tonic-gate  */
35007c478bd9Sstevel@tonic-gate static int
35017c478bd9Sstevel@tonic-gate asywput(queue_t *q, mblk_t *mp)
35027c478bd9Sstevel@tonic-gate {
35037c478bd9Sstevel@tonic-gate 	struct asyncline *async;
35047c478bd9Sstevel@tonic-gate 	struct asycom *asy;
35057c478bd9Sstevel@tonic-gate #ifdef DEBUG
35067c478bd9Sstevel@tonic-gate 	int instance;
35077c478bd9Sstevel@tonic-gate #endif
35087c478bd9Sstevel@tonic-gate 	int error;
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 	async = (struct asyncline *)q->q_ptr;
35117c478bd9Sstevel@tonic-gate #ifdef DEBUG
35127c478bd9Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
35137c478bd9Sstevel@tonic-gate #endif
35147c478bd9Sstevel@tonic-gate 	asy = async->async_common;
35157c478bd9Sstevel@tonic-gate 
35167c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
35177c478bd9Sstevel@tonic-gate 
35187c478bd9Sstevel@tonic-gate 	case M_STOP:
35197c478bd9Sstevel@tonic-gate 		/*
35207c478bd9Sstevel@tonic-gate 		 * Since we don't do real DMA, we can just let the
35217c478bd9Sstevel@tonic-gate 		 * chip coast to a stop after applying the brakes.
35227c478bd9Sstevel@tonic-gate 		 */
35237c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
35247c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_STOPPED;
35257c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
35267c478bd9Sstevel@tonic-gate 		freemsg(mp);
35277c478bd9Sstevel@tonic-gate 		break;
35287c478bd9Sstevel@tonic-gate 
35297c478bd9Sstevel@tonic-gate 	case M_START:
35307c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
35317c478bd9Sstevel@tonic-gate 		if (async->async_flags & ASYNC_STOPPED) {
35327c478bd9Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_STOPPED;
35337c478bd9Sstevel@tonic-gate 			/*
35347c478bd9Sstevel@tonic-gate 			 * If an output operation is in progress,
35357c478bd9Sstevel@tonic-gate 			 * resume it.  Otherwise, prod the start
35367c478bd9Sstevel@tonic-gate 			 * routine.
35377c478bd9Sstevel@tonic-gate 			 */
35387c478bd9Sstevel@tonic-gate 			if (async->async_ocnt > 0) {
35397c478bd9Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
35407c478bd9Sstevel@tonic-gate 				async_resume(async);
35417c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
35427c478bd9Sstevel@tonic-gate 			} else {
35437c478bd9Sstevel@tonic-gate 				async_start(async);
35447c478bd9Sstevel@tonic-gate 			}
35457c478bd9Sstevel@tonic-gate 		}
35467c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
35477c478bd9Sstevel@tonic-gate 		freemsg(mp);
35487c478bd9Sstevel@tonic-gate 		break;
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 	case M_IOCTL:
35517c478bd9Sstevel@tonic-gate 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
35527c478bd9Sstevel@tonic-gate 
35537c478bd9Sstevel@tonic-gate 		case TCSBRK:
35547c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
35557c478bd9Sstevel@tonic-gate 			if (error != 0) {
35567c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, error);
35577c478bd9Sstevel@tonic-gate 				return (0);
35587c478bd9Sstevel@tonic-gate 			}
35597c478bd9Sstevel@tonic-gate 
35607c478bd9Sstevel@tonic-gate 			if (*(int *)mp->b_cont->b_rptr != 0) {
35617c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_OUT,
35627c478bd9Sstevel@tonic-gate 					"async%d_ioctl: flush request.\n",
35637c478bd9Sstevel@tonic-gate 					instance);
35647c478bd9Sstevel@tonic-gate 				(void) putq(q, mp);
35657c478bd9Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl);
35667c478bd9Sstevel@tonic-gate 
35677c478bd9Sstevel@tonic-gate 				/*
35687c478bd9Sstevel@tonic-gate 				 * If an TIOCSBRK is in progress,
35697c478bd9Sstevel@tonic-gate 				 * clean it as TIOCCBRK does,
35707c478bd9Sstevel@tonic-gate 				 * then kick off output.
35717c478bd9Sstevel@tonic-gate 				 * If TIOCSBRK is not in progress,
35727c478bd9Sstevel@tonic-gate 				 * just kick off output.
35737c478bd9Sstevel@tonic-gate 				 */
35747c478bd9Sstevel@tonic-gate 				async_resume_utbrk(async);
35757c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl);
35767c478bd9Sstevel@tonic-gate 				break;
35777c478bd9Sstevel@tonic-gate 			}
35787c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
35797c478bd9Sstevel@tonic-gate 		case TCSETSW:
35807c478bd9Sstevel@tonic-gate 		case TCSETSF:
35817c478bd9Sstevel@tonic-gate 		case TCSETAW:
35827c478bd9Sstevel@tonic-gate 		case TCSETAF:
35837c478bd9Sstevel@tonic-gate 			/*
35847c478bd9Sstevel@tonic-gate 			 * The changes do not take effect until all
35857c478bd9Sstevel@tonic-gate 			 * output queued before them is drained.
35867c478bd9Sstevel@tonic-gate 			 * Put this message on the queue, so that
35877c478bd9Sstevel@tonic-gate 			 * "async_start" will see it when it's done
35887c478bd9Sstevel@tonic-gate 			 * with the output before it.  Poke the
35897c478bd9Sstevel@tonic-gate 			 * start routine, just in case.
35907c478bd9Sstevel@tonic-gate 			 */
35917c478bd9Sstevel@tonic-gate 			(void) putq(q, mp);
35927c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate 			/*
35957c478bd9Sstevel@tonic-gate 			 * If an TIOCSBRK is in progress,
35967c478bd9Sstevel@tonic-gate 			 * clean it as TIOCCBRK does.
35977c478bd9Sstevel@tonic-gate 			 * then kick off output.
35987c478bd9Sstevel@tonic-gate 			 * If TIOCSBRK is not in progress,
35997c478bd9Sstevel@tonic-gate 			 * just kick off output.
36007c478bd9Sstevel@tonic-gate 			 */
36017c478bd9Sstevel@tonic-gate 			async_resume_utbrk(async);
36027c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
36037c478bd9Sstevel@tonic-gate 			break;
36047c478bd9Sstevel@tonic-gate 
36057c478bd9Sstevel@tonic-gate 		default:
36067c478bd9Sstevel@tonic-gate 			/*
36077c478bd9Sstevel@tonic-gate 			 * Do it now.
36087c478bd9Sstevel@tonic-gate 			 */
36097c478bd9Sstevel@tonic-gate 			async_ioctl(async, q, mp);
36107c478bd9Sstevel@tonic-gate 			break;
36117c478bd9Sstevel@tonic-gate 		}
36127c478bd9Sstevel@tonic-gate 		break;
36137c478bd9Sstevel@tonic-gate 
36147c478bd9Sstevel@tonic-gate 	case M_FLUSH:
36157c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
36167c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
36177c478bd9Sstevel@tonic-gate 
36187c478bd9Sstevel@tonic-gate 			/*
36197c478bd9Sstevel@tonic-gate 			 * Abort any output in progress.
36207c478bd9Sstevel@tonic-gate 			 */
36217c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
36227c478bd9Sstevel@tonic-gate 			if (async->async_flags & ASYNC_BUSY) {
36237c478bd9Sstevel@tonic-gate 			    DEBUGCONT1(ASY_DEBUG_BUSY, "asy%dwput: "
36247c478bd9Sstevel@tonic-gate 				    "Clearing async_ocnt, "
36257c478bd9Sstevel@tonic-gate 				    "leaving ASYNC_BUSY set\n",
36267c478bd9Sstevel@tonic-gate 				    instance);
36277c478bd9Sstevel@tonic-gate 				async->async_ocnt = 0;
36287c478bd9Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_BUSY;
36297c478bd9Sstevel@tonic-gate 			} /* if */
36307c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
36317c478bd9Sstevel@tonic-gate 
36327c478bd9Sstevel@tonic-gate 			/* Flush FIFO buffers */
36337c478bd9Sstevel@tonic-gate 			if (asy->asy_use_fifo == FIFO_ON) {
36347c478bd9Sstevel@tonic-gate 				asy_reset_fifo(asy, FIFOTXFLSH);
36357c478bd9Sstevel@tonic-gate 			}
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 			/*
36387c478bd9Sstevel@tonic-gate 			 * Flush our write queue.
36397c478bd9Sstevel@tonic-gate 			 */
36407c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
36417c478bd9Sstevel@tonic-gate 			if (async->async_xmitblk != NULL) {
36427c478bd9Sstevel@tonic-gate 				freeb(async->async_xmitblk);
36437c478bd9Sstevel@tonic-gate 				async->async_xmitblk = NULL;
36447c478bd9Sstevel@tonic-gate 			}
36457c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
36467c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;	/* it has been flushed */
36477c478bd9Sstevel@tonic-gate 		}
36487c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
36497c478bd9Sstevel@tonic-gate 			/* Flush FIFO buffers */
36507c478bd9Sstevel@tonic-gate 			if (asy->asy_use_fifo == FIFO_ON) {
36517c478bd9Sstevel@tonic-gate 				asy_reset_fifo(asy, FIFORXFLSH);
36527c478bd9Sstevel@tonic-gate 			}
36537c478bd9Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
36547c478bd9Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
36557c478bd9Sstevel@tonic-gate 		} else {
36567c478bd9Sstevel@tonic-gate 			freemsg(mp);
36577c478bd9Sstevel@tonic-gate 		}
36587c478bd9Sstevel@tonic-gate 
36597c478bd9Sstevel@tonic-gate 		/*
36607c478bd9Sstevel@tonic-gate 		 * We must make sure we process messages that survive the
36617c478bd9Sstevel@tonic-gate 		 * write-side flush.
36627c478bd9Sstevel@tonic-gate 		 */
36637c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
36647c478bd9Sstevel@tonic-gate 		async_start(async);
36657c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
36667c478bd9Sstevel@tonic-gate 		break;
36677c478bd9Sstevel@tonic-gate 
36687c478bd9Sstevel@tonic-gate 	case M_BREAK:
36697c478bd9Sstevel@tonic-gate 	case M_DELAY:
36707c478bd9Sstevel@tonic-gate 	case M_DATA:
36717c478bd9Sstevel@tonic-gate 		/*
36727c478bd9Sstevel@tonic-gate 		 * Queue the message up to be transmitted,
36737c478bd9Sstevel@tonic-gate 		 * and poke the start routine.
36747c478bd9Sstevel@tonic-gate 		 */
36757c478bd9Sstevel@tonic-gate 		(void) putq(q, mp);
36767c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
36777c478bd9Sstevel@tonic-gate 		async_start(async);
36787c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
36797c478bd9Sstevel@tonic-gate 		break;
36807c478bd9Sstevel@tonic-gate 
36817c478bd9Sstevel@tonic-gate 	case M_STOPI:
36827c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
36837c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
36847c478bd9Sstevel@tonic-gate 		if (!(async->async_inflow_source & IN_FLOW_USER)) {
36857c478bd9Sstevel@tonic-gate 			async_flowcontrol_hw_input(asy, FLOW_STOP,
36867c478bd9Sstevel@tonic-gate 			    IN_FLOW_USER);
36877c478bd9Sstevel@tonic-gate 			(void) async_flowcontrol_sw_input(asy, FLOW_STOP,
36887c478bd9Sstevel@tonic-gate 			    IN_FLOW_USER);
36897c478bd9Sstevel@tonic-gate 		}
36907c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
36917c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
36927c478bd9Sstevel@tonic-gate 		freemsg(mp);
36937c478bd9Sstevel@tonic-gate 		break;
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate 	case M_STARTI:
36967c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
36977c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
36987c478bd9Sstevel@tonic-gate 		if (async->async_inflow_source & IN_FLOW_USER) {
36997c478bd9Sstevel@tonic-gate 			async_flowcontrol_hw_input(asy, FLOW_START,
37007c478bd9Sstevel@tonic-gate 			    IN_FLOW_USER);
37017c478bd9Sstevel@tonic-gate 			(void) async_flowcontrol_sw_input(asy, FLOW_START,
37027c478bd9Sstevel@tonic-gate 			    IN_FLOW_USER);
37037c478bd9Sstevel@tonic-gate 		}
37047c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
37057c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
37067c478bd9Sstevel@tonic-gate 		freemsg(mp);
37077c478bd9Sstevel@tonic-gate 		break;
37087c478bd9Sstevel@tonic-gate 
37097c478bd9Sstevel@tonic-gate 	case M_CTL:
37107c478bd9Sstevel@tonic-gate 		if (MBLKL(mp) >= sizeof (struct iocblk) &&
37117c478bd9Sstevel@tonic-gate 		    ((struct iocblk *)mp->b_rptr)->ioc_cmd == MC_POSIXQUERY) {
37127c478bd9Sstevel@tonic-gate 			((struct iocblk *)mp->b_rptr)->ioc_cmd = MC_HAS_POSIX;
37137c478bd9Sstevel@tonic-gate 			qreply(q, mp);
37147c478bd9Sstevel@tonic-gate 		} else {
37157c478bd9Sstevel@tonic-gate 			/*
37167c478bd9Sstevel@tonic-gate 			 * These MC_SERVICE type messages are used by upper
37177c478bd9Sstevel@tonic-gate 			 * modules to tell this driver to send input up
37187c478bd9Sstevel@tonic-gate 			 * immediately, or that it can wait for normal
37197c478bd9Sstevel@tonic-gate 			 * processing that may or may not be done.  Sun
37207c478bd9Sstevel@tonic-gate 			 * requires these for the mouse module.
37217c478bd9Sstevel@tonic-gate 			 * (XXX - for x86?)
37227c478bd9Sstevel@tonic-gate 			 */
37237c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
37247c478bd9Sstevel@tonic-gate 			switch (*mp->b_rptr) {
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate 			case MC_SERVICEIMM:
37277c478bd9Sstevel@tonic-gate 				async->async_flags |= ASYNC_SERVICEIMM;
37287c478bd9Sstevel@tonic-gate 				break;
37297c478bd9Sstevel@tonic-gate 
37307c478bd9Sstevel@tonic-gate 			case MC_SERVICEDEF:
37317c478bd9Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_SERVICEIMM;
37327c478bd9Sstevel@tonic-gate 				break;
37337c478bd9Sstevel@tonic-gate 			}
37347c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
37357c478bd9Sstevel@tonic-gate 			freemsg(mp);
37367c478bd9Sstevel@tonic-gate 		}
37377c478bd9Sstevel@tonic-gate 		break;
37387c478bd9Sstevel@tonic-gate 
37397c478bd9Sstevel@tonic-gate 	case M_IOCDATA:
37407c478bd9Sstevel@tonic-gate 		async_iocdata(q, mp);
37417c478bd9Sstevel@tonic-gate 		break;
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate 	default:
37447c478bd9Sstevel@tonic-gate 		freemsg(mp);
37457c478bd9Sstevel@tonic-gate 		break;
37467c478bd9Sstevel@tonic-gate 	}
37477c478bd9Sstevel@tonic-gate 	return (0);
37487c478bd9Sstevel@tonic-gate }
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate /*
37517c478bd9Sstevel@tonic-gate  * Retry an "ioctl", now that "bufcall" claims we may be able to allocate
37527c478bd9Sstevel@tonic-gate  * the buffer we need.
37537c478bd9Sstevel@tonic-gate  */
37547c478bd9Sstevel@tonic-gate static void
37557c478bd9Sstevel@tonic-gate async_reioctl(void *unit)
37567c478bd9Sstevel@tonic-gate {
37577c478bd9Sstevel@tonic-gate 	int instance = (uintptr_t)unit;
37587c478bd9Sstevel@tonic-gate 	struct asyncline *async;
37597c478bd9Sstevel@tonic-gate 	struct asycom *asy;
37607c478bd9Sstevel@tonic-gate 	queue_t	*q;
37617c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
37627c478bd9Sstevel@tonic-gate 
37637c478bd9Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, instance);
37647c478bd9Sstevel@tonic-gate 	ASSERT(asy != NULL);
37657c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
37667c478bd9Sstevel@tonic-gate 
37677c478bd9Sstevel@tonic-gate 	/*
37687c478bd9Sstevel@tonic-gate 	 * The bufcall is no longer pending.
37697c478bd9Sstevel@tonic-gate 	 */
37707c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
37717c478bd9Sstevel@tonic-gate 	async->async_wbufcid = 0;
37727c478bd9Sstevel@tonic-gate 	if ((q = async->async_ttycommon.t_writeq) == NULL) {
37737c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
37747c478bd9Sstevel@tonic-gate 		return;
37757c478bd9Sstevel@tonic-gate 	}
37767c478bd9Sstevel@tonic-gate 	if ((mp = async->async_ttycommon.t_iocpending) != NULL) {
37777c478bd9Sstevel@tonic-gate 		/* not pending any more */
37787c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_iocpending = NULL;
37797c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
37807c478bd9Sstevel@tonic-gate 		async_ioctl(async, q, mp);
37817c478bd9Sstevel@tonic-gate 	} else
37827c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
37837c478bd9Sstevel@tonic-gate }
37847c478bd9Sstevel@tonic-gate 
37857c478bd9Sstevel@tonic-gate static void
37867c478bd9Sstevel@tonic-gate async_iocdata(queue_t *q, mblk_t *mp)
37877c478bd9Sstevel@tonic-gate {
37887c478bd9Sstevel@tonic-gate 	struct asyncline	*async = (struct asyncline *)q->q_ptr;
37897c478bd9Sstevel@tonic-gate 	struct asycom		*asy;
37907c478bd9Sstevel@tonic-gate 	struct iocblk *ip;
37917c478bd9Sstevel@tonic-gate 	struct copyresp *csp;
37927c478bd9Sstevel@tonic-gate #ifdef DEBUG
37937c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
37947c478bd9Sstevel@tonic-gate #endif
37957c478bd9Sstevel@tonic-gate 
37967c478bd9Sstevel@tonic-gate 	asy = async->async_common;
37977c478bd9Sstevel@tonic-gate 	ip = (struct iocblk *)mp->b_rptr;
37987c478bd9Sstevel@tonic-gate 	csp = (struct copyresp *)mp->b_rptr;
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 	if (csp->cp_rval != 0) {
38017c478bd9Sstevel@tonic-gate 		if (csp->cp_private)
38027c478bd9Sstevel@tonic-gate 			freemsg(csp->cp_private);
38037c478bd9Sstevel@tonic-gate 		freemsg(mp);
38047c478bd9Sstevel@tonic-gate 		return;
38057c478bd9Sstevel@tonic-gate 	}
38067c478bd9Sstevel@tonic-gate 
38077c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
38087c478bd9Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_MODEM, "async%d_iocdata: case %s\n",
38097c478bd9Sstevel@tonic-gate 		instance,
38107c478bd9Sstevel@tonic-gate 		csp->cp_cmd == TIOCMGET ? "TIOCMGET" :
38117c478bd9Sstevel@tonic-gate 		csp->cp_cmd == TIOCMSET ? "TIOCMSET" :
38127c478bd9Sstevel@tonic-gate 		csp->cp_cmd == TIOCMBIS ? "TIOCMBIS" :
38137c478bd9Sstevel@tonic-gate 		    "TIOCMBIC");
38147c478bd9Sstevel@tonic-gate 	switch (csp->cp_cmd) {
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate 	case TIOCMGET:
38177c478bd9Sstevel@tonic-gate 		if (mp->b_cont) {
38187c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
38197c478bd9Sstevel@tonic-gate 			mp->b_cont = NULL;
38207c478bd9Sstevel@tonic-gate 		}
38217c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
38227c478bd9Sstevel@tonic-gate 		ip->ioc_error = 0;
38237c478bd9Sstevel@tonic-gate 		ip->ioc_count = 0;
38247c478bd9Sstevel@tonic-gate 		ip->ioc_rval = 0;
38257c478bd9Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct iocblk);
38267c478bd9Sstevel@tonic-gate 		break;
38277c478bd9Sstevel@tonic-gate 
38287c478bd9Sstevel@tonic-gate 	case TIOCMSET:
38297c478bd9Sstevel@tonic-gate 	case TIOCMBIS:
38307c478bd9Sstevel@tonic-gate 	case TIOCMBIC:
38317c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
38327c478bd9Sstevel@tonic-gate 		(void) asymctl(asy,
38337c478bd9Sstevel@tonic-gate 			dmtoasy(*(int *)mp->b_cont->b_rptr),
38347c478bd9Sstevel@tonic-gate 			csp->cp_cmd);
38357c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
38367c478bd9Sstevel@tonic-gate 		mioc2ack(mp, NULL, 0, 0);
38377c478bd9Sstevel@tonic-gate 		break;
38387c478bd9Sstevel@tonic-gate 
38397c478bd9Sstevel@tonic-gate 	default:
38407c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
38417c478bd9Sstevel@tonic-gate 		ip->ioc_error = EINVAL;
38427c478bd9Sstevel@tonic-gate 		break;
38437c478bd9Sstevel@tonic-gate 	}
38447c478bd9Sstevel@tonic-gate 	qreply(q, mp);
38457c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
38467c478bd9Sstevel@tonic-gate }
38477c478bd9Sstevel@tonic-gate 
38487c478bd9Sstevel@tonic-gate /*
38497c478bd9Sstevel@tonic-gate  * debugger/console support routines.
38507c478bd9Sstevel@tonic-gate  */
38517c478bd9Sstevel@tonic-gate 
38527c478bd9Sstevel@tonic-gate /*
38537c478bd9Sstevel@tonic-gate  * put a character out
38547c478bd9Sstevel@tonic-gate  * Do not use interrupts.  If char is LF, put out CR, LF.
38557c478bd9Sstevel@tonic-gate  */
38567c478bd9Sstevel@tonic-gate static void
3857*281f0747Slt200341 asyputchar(cons_polledio_arg_t arg, uchar_t c)
38587c478bd9Sstevel@tonic-gate {
38597c478bd9Sstevel@tonic-gate 	struct asycom *asy = (struct asycom *)arg;
38607c478bd9Sstevel@tonic-gate 
38617c478bd9Sstevel@tonic-gate 	if (c == '\n')
38627c478bd9Sstevel@tonic-gate 		asyputchar(arg, '\r');
38637c478bd9Sstevel@tonic-gate 
38644ab75253Smrj 	while ((ddi_get8(asy->asy_iohandle,
38657c478bd9Sstevel@tonic-gate 	    asy->asy_ioaddr + LSR) & XHRE) == 0) {
38667c478bd9Sstevel@tonic-gate 		/* wait for xmit to finish */
38677c478bd9Sstevel@tonic-gate 		drv_usecwait(10);
38687c478bd9Sstevel@tonic-gate 	}
38697c478bd9Sstevel@tonic-gate 
38707c478bd9Sstevel@tonic-gate 	/* put the character out */
38714ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT, c);
38727c478bd9Sstevel@tonic-gate }
38737c478bd9Sstevel@tonic-gate 
38747c478bd9Sstevel@tonic-gate /*
38757c478bd9Sstevel@tonic-gate  * See if there's a character available. If no character is
38767c478bd9Sstevel@tonic-gate  * available, return 0. Run in polled mode, no interrupts.
38777c478bd9Sstevel@tonic-gate  */
38787c478bd9Sstevel@tonic-gate static boolean_t
3879*281f0747Slt200341 asyischar(cons_polledio_arg_t arg)
38807c478bd9Sstevel@tonic-gate {
38817c478bd9Sstevel@tonic-gate 	struct asycom *asy = (struct asycom *)arg;
38827c478bd9Sstevel@tonic-gate 
38834ab75253Smrj 	return ((ddi_get8(asy->asy_iohandle,
38847c478bd9Sstevel@tonic-gate 		asy->asy_ioaddr + LSR) & RCA) != 0);
38857c478bd9Sstevel@tonic-gate }
38867c478bd9Sstevel@tonic-gate 
38877c478bd9Sstevel@tonic-gate /*
38887c478bd9Sstevel@tonic-gate  * Get a character. Run in polled mode, no interrupts.
38897c478bd9Sstevel@tonic-gate  */
38907c478bd9Sstevel@tonic-gate static int
3891*281f0747Slt200341 asygetchar(cons_polledio_arg_t arg)
38927c478bd9Sstevel@tonic-gate {
38937c478bd9Sstevel@tonic-gate 	struct asycom *asy = (struct asycom *)arg;
38947c478bd9Sstevel@tonic-gate 
38957c478bd9Sstevel@tonic-gate 	while (!asyischar(arg))
38967c478bd9Sstevel@tonic-gate 		drv_usecwait(10);
38974ab75253Smrj 	return (ddi_get8(asy->asy_iohandle,
38987c478bd9Sstevel@tonic-gate 		asy->asy_ioaddr + DAT));
38997c478bd9Sstevel@tonic-gate }
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate /*
39027c478bd9Sstevel@tonic-gate  * Set or get the modem control status.
39037c478bd9Sstevel@tonic-gate  */
39047c478bd9Sstevel@tonic-gate static int
39057c478bd9Sstevel@tonic-gate asymctl(struct asycom *asy, int bits, int how)
39067c478bd9Sstevel@tonic-gate {
39077c478bd9Sstevel@tonic-gate 	int mcr_r, msr_r;
39087c478bd9Sstevel@tonic-gate 	int instance = asy->asy_unit;
39097c478bd9Sstevel@tonic-gate 
39107c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
39117c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
39127c478bd9Sstevel@tonic-gate 
39137c478bd9Sstevel@tonic-gate 	/* Read Modem Control Registers */
39144ab75253Smrj 	mcr_r = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate 	switch (how) {
39177c478bd9Sstevel@tonic-gate 
39187c478bd9Sstevel@tonic-gate 	case TIOCMSET:
39197c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM,
39207c478bd9Sstevel@tonic-gate 			"asy%dmctl: TIOCMSET, bits = %x\n", instance, bits);
39217c478bd9Sstevel@tonic-gate 		mcr_r = bits;		/* Set bits	*/
39227c478bd9Sstevel@tonic-gate 		break;
39237c478bd9Sstevel@tonic-gate 
39247c478bd9Sstevel@tonic-gate 	case TIOCMBIS:
39257c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM, "asy%dmctl: TIOCMBIS, bits = %x\n",
39267c478bd9Sstevel@tonic-gate 			instance, bits);
39277c478bd9Sstevel@tonic-gate 		mcr_r |= bits;		/* Mask in bits	*/
39287c478bd9Sstevel@tonic-gate 		break;
39297c478bd9Sstevel@tonic-gate 
39307c478bd9Sstevel@tonic-gate 	case TIOCMBIC:
39317c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM, "asy%dmctl: TIOCMBIC, bits = %x\n",
39327c478bd9Sstevel@tonic-gate 			instance, bits);
39337c478bd9Sstevel@tonic-gate 		mcr_r &= ~bits;		/* Mask out bits */
39347c478bd9Sstevel@tonic-gate 		break;
39357c478bd9Sstevel@tonic-gate 
39367c478bd9Sstevel@tonic-gate 	case TIOCMGET:
39377c478bd9Sstevel@tonic-gate 		/* Read Modem Status Registers */
39387c478bd9Sstevel@tonic-gate 		/*
39397c478bd9Sstevel@tonic-gate 		 * If modem interrupts are enabled, we return the
39407c478bd9Sstevel@tonic-gate 		 * saved value of msr. We read MSR only in async_msint()
39417c478bd9Sstevel@tonic-gate 		 */
39424ab75253Smrj 		if (ddi_get8(asy->asy_iohandle,
39437c478bd9Sstevel@tonic-gate 		    asy->asy_ioaddr + ICR) & MIEN) {
39447c478bd9Sstevel@tonic-gate 			msr_r = asy->asy_msr;
39457c478bd9Sstevel@tonic-gate 			DEBUGCONT2(ASY_DEBUG_MODEM,
39467c478bd9Sstevel@tonic-gate 				"asy%dmctl: TIOCMGET, read msr_r = %x\n",
39477c478bd9Sstevel@tonic-gate 				instance, msr_r);
39487c478bd9Sstevel@tonic-gate 		} else {
39494ab75253Smrj 			msr_r = ddi_get8(asy->asy_iohandle,
39507c478bd9Sstevel@tonic-gate 					asy->asy_ioaddr + MSR);
39517c478bd9Sstevel@tonic-gate 			DEBUGCONT2(ASY_DEBUG_MODEM,
39527c478bd9Sstevel@tonic-gate 				"asy%dmctl: TIOCMGET, read MSR = %x\n",
39537c478bd9Sstevel@tonic-gate 				instance, msr_r);
39547c478bd9Sstevel@tonic-gate 		}
39557c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM, "asy%dtodm: modem_lines = %x\n",
39567c478bd9Sstevel@tonic-gate 			instance, asytodm(mcr_r, msr_r));
39577c478bd9Sstevel@tonic-gate 		return (asytodm(mcr_r, msr_r));
39587c478bd9Sstevel@tonic-gate 	}
39597c478bd9Sstevel@tonic-gate 
39604ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR, mcr_r);
39617c478bd9Sstevel@tonic-gate 
39627c478bd9Sstevel@tonic-gate 	return (mcr_r);
39637c478bd9Sstevel@tonic-gate }
39647c478bd9Sstevel@tonic-gate 
39657c478bd9Sstevel@tonic-gate static int
39667c478bd9Sstevel@tonic-gate asytodm(int mcr_r, int msr_r)
39677c478bd9Sstevel@tonic-gate {
39687c478bd9Sstevel@tonic-gate 	int b = 0;
39697c478bd9Sstevel@tonic-gate 
39707c478bd9Sstevel@tonic-gate 	/* MCR registers */
39717c478bd9Sstevel@tonic-gate 	if (mcr_r & RTS)
39727c478bd9Sstevel@tonic-gate 		b |= TIOCM_RTS;
39737c478bd9Sstevel@tonic-gate 
39747c478bd9Sstevel@tonic-gate 	if (mcr_r & DTR)
39757c478bd9Sstevel@tonic-gate 		b |= TIOCM_DTR;
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate 	/* MSR registers */
39787c478bd9Sstevel@tonic-gate 	if (msr_r & DCD)
39797c478bd9Sstevel@tonic-gate 		b |= TIOCM_CAR;
39807c478bd9Sstevel@tonic-gate 
39817c478bd9Sstevel@tonic-gate 	if (msr_r & CTS)
39827c478bd9Sstevel@tonic-gate 		b |= TIOCM_CTS;
39837c478bd9Sstevel@tonic-gate 
39847c478bd9Sstevel@tonic-gate 	if (msr_r & DSR)
39857c478bd9Sstevel@tonic-gate 		b |= TIOCM_DSR;
39867c478bd9Sstevel@tonic-gate 
39877c478bd9Sstevel@tonic-gate 	if (msr_r & RI)
39887c478bd9Sstevel@tonic-gate 		b |= TIOCM_RNG;
39897c478bd9Sstevel@tonic-gate 	return (b);
39907c478bd9Sstevel@tonic-gate }
39917c478bd9Sstevel@tonic-gate 
39927c478bd9Sstevel@tonic-gate static int
39937c478bd9Sstevel@tonic-gate dmtoasy(int bits)
39947c478bd9Sstevel@tonic-gate {
39957c478bd9Sstevel@tonic-gate 	int b = 0;
39967c478bd9Sstevel@tonic-gate 
39977c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_MODEM, "dmtoasy: bits = %x\n", bits);
39987c478bd9Sstevel@tonic-gate #ifdef	CAN_NOT_SET	/* only DTR and RTS can be set */
39997c478bd9Sstevel@tonic-gate 	if (bits & TIOCM_CAR)
40007c478bd9Sstevel@tonic-gate 		b |= DCD;
40017c478bd9Sstevel@tonic-gate 	if (bits & TIOCM_CTS)
40027c478bd9Sstevel@tonic-gate 		b |= CTS;
40037c478bd9Sstevel@tonic-gate 	if (bits & TIOCM_DSR)
40047c478bd9Sstevel@tonic-gate 		b |= DSR;
40057c478bd9Sstevel@tonic-gate 	if (bits & TIOCM_RNG)
40067c478bd9Sstevel@tonic-gate 		b |= RI;
40077c478bd9Sstevel@tonic-gate #endif
40087c478bd9Sstevel@tonic-gate 
40097c478bd9Sstevel@tonic-gate 	if (bits & TIOCM_RTS) {
40107c478bd9Sstevel@tonic-gate 		DEBUGCONT0(ASY_DEBUG_MODEM, "dmtoasy: set b & RTS\n");
40117c478bd9Sstevel@tonic-gate 		b |= RTS;
40127c478bd9Sstevel@tonic-gate 	}
40137c478bd9Sstevel@tonic-gate 	if (bits & TIOCM_DTR) {
40147c478bd9Sstevel@tonic-gate 		DEBUGCONT0(ASY_DEBUG_MODEM, "dmtoasy: set b & DTR\n");
40157c478bd9Sstevel@tonic-gate 		b |= DTR;
40167c478bd9Sstevel@tonic-gate 	}
40177c478bd9Sstevel@tonic-gate 
40187c478bd9Sstevel@tonic-gate 	return (b);
40197c478bd9Sstevel@tonic-gate }
40207c478bd9Sstevel@tonic-gate 
40217c478bd9Sstevel@tonic-gate static void
40227c478bd9Sstevel@tonic-gate asyerror(int level, const char *fmt, ...)
40237c478bd9Sstevel@tonic-gate {
40247c478bd9Sstevel@tonic-gate 	va_list adx;
40257c478bd9Sstevel@tonic-gate 	static	time_t	last;
40267c478bd9Sstevel@tonic-gate 	static	const char *lastfmt;
40277c478bd9Sstevel@tonic-gate 	time_t	now;
40287c478bd9Sstevel@tonic-gate 
40297c478bd9Sstevel@tonic-gate 	/*
40307c478bd9Sstevel@tonic-gate 	 * Don't print the same error message too often.
40317c478bd9Sstevel@tonic-gate 	 * Print the message only if we have not printed the
40327c478bd9Sstevel@tonic-gate 	 * message within the last second.
40337c478bd9Sstevel@tonic-gate 	 * Note: that fmt cannot be a pointer to a string
40347c478bd9Sstevel@tonic-gate 	 * stored on the stack. The fmt pointer
40357c478bd9Sstevel@tonic-gate 	 * must be in the data segment otherwise lastfmt would point
40367c478bd9Sstevel@tonic-gate 	 * to non-sense.
40377c478bd9Sstevel@tonic-gate 	 */
40387c478bd9Sstevel@tonic-gate 	now = gethrestime_sec();
40397c478bd9Sstevel@tonic-gate 	if (last == now && lastfmt == fmt)
40407c478bd9Sstevel@tonic-gate 		return;
40417c478bd9Sstevel@tonic-gate 
40427c478bd9Sstevel@tonic-gate 	last = now;
40437c478bd9Sstevel@tonic-gate 	lastfmt = fmt;
40447c478bd9Sstevel@tonic-gate 
40457c478bd9Sstevel@tonic-gate 	va_start(adx, fmt);
40467c478bd9Sstevel@tonic-gate 	vcmn_err(level, fmt, adx);
40477c478bd9Sstevel@tonic-gate 	va_end(adx);
40487c478bd9Sstevel@tonic-gate }
40497c478bd9Sstevel@tonic-gate 
40507c478bd9Sstevel@tonic-gate /*
40517c478bd9Sstevel@tonic-gate  * asy_parse_mode(dev_info_t *devi, struct asycom *asy)
40527c478bd9Sstevel@tonic-gate  * The value of this property is in the form of "9600,8,n,1,-"
40537c478bd9Sstevel@tonic-gate  * 1) speed: 9600, 4800, ...
40547c478bd9Sstevel@tonic-gate  * 2) data bits
40557c478bd9Sstevel@tonic-gate  * 3) parity: n(none), e(even), o(odd)
40567c478bd9Sstevel@tonic-gate  * 4) stop bits
40577c478bd9Sstevel@tonic-gate  * 5) handshake: -(none), h(hardware: rts/cts), s(software: xon/off)
40587c478bd9Sstevel@tonic-gate  *
40597c478bd9Sstevel@tonic-gate  * This parsing came from a SPARCstation eeprom.
40607c478bd9Sstevel@tonic-gate  */
40617c478bd9Sstevel@tonic-gate static void
40627c478bd9Sstevel@tonic-gate asy_parse_mode(dev_info_t *devi, struct asycom *asy)
40637c478bd9Sstevel@tonic-gate {
40647c478bd9Sstevel@tonic-gate 	char		name[40];
40657c478bd9Sstevel@tonic-gate 	char		val[40];
40667c478bd9Sstevel@tonic-gate 	int		len;
40677c478bd9Sstevel@tonic-gate 	int		ret;
40687c478bd9Sstevel@tonic-gate 	char		*p;
40697c478bd9Sstevel@tonic-gate 	char		*p1;
40707c478bd9Sstevel@tonic-gate 
40717c478bd9Sstevel@tonic-gate 	ASSERT(asy->asy_com_port != 0);
40727c478bd9Sstevel@tonic-gate 
40737c478bd9Sstevel@tonic-gate 	/*
40747c478bd9Sstevel@tonic-gate 	 * Parse the ttyx-mode property
40757c478bd9Sstevel@tonic-gate 	 */
40767c478bd9Sstevel@tonic-gate 	(void) sprintf(name, "tty%c-mode", asy->asy_com_port + 'a' - 1);
40777c478bd9Sstevel@tonic-gate 	len = sizeof (val);
40787c478bd9Sstevel@tonic-gate 	ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
40797c478bd9Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
40807c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "com%c-mode", asy->asy_com_port + '0');
40817c478bd9Sstevel@tonic-gate 		len = sizeof (val);
40827c478bd9Sstevel@tonic-gate 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
40837c478bd9Sstevel@tonic-gate 	}
40847c478bd9Sstevel@tonic-gate 
40857c478bd9Sstevel@tonic-gate 	/* no property to parse */
40867c478bd9Sstevel@tonic-gate 	asy->asy_cflag = 0;
40877c478bd9Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS)
40887c478bd9Sstevel@tonic-gate 		return;
40897c478bd9Sstevel@tonic-gate 
40907c478bd9Sstevel@tonic-gate 	p = val;
40917c478bd9Sstevel@tonic-gate 	/* ---- baud rate ---- */
40927c478bd9Sstevel@tonic-gate 	asy->asy_cflag = CREAD|B9600;		/* initial default */
40937c478bd9Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0) {
40947c478bd9Sstevel@tonic-gate 		*p1++ = '\0';
40957c478bd9Sstevel@tonic-gate 	} else {
40967c478bd9Sstevel@tonic-gate 		asy->asy_cflag |= BITS8;	/* add default bits */
40977c478bd9Sstevel@tonic-gate 		return;
40987c478bd9Sstevel@tonic-gate 	}
40997c478bd9Sstevel@tonic-gate 
41007c478bd9Sstevel@tonic-gate 	if (strcmp(p, "110") == 0)
41017c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B110;
41027c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "150") == 0)
41037c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B150;
41047c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "300") == 0)
41057c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B300;
41067c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "600") == 0)
41077c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B600;
41087c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "1200") == 0)
41097c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B1200;
41107c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "2400") == 0)
41117c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B2400;
41127c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "4800") == 0)
41137c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B4800;
41147c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "9600") == 0)
41157c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B9600;
41167c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "19200") == 0)
41177c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B19200;
41187c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "38400") == 0)
41197c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B38400;
41207c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "57600") == 0)
41217c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B57600;
41227c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "115200") == 0)
41237c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B115200;
41247c478bd9Sstevel@tonic-gate 	else
41257c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B9600;
41267c478bd9Sstevel@tonic-gate 
41277c478bd9Sstevel@tonic-gate 	asy->asy_cflag &= ~CBAUD;
41287c478bd9Sstevel@tonic-gate 	if (asy->asy_bidx > CBAUD) {	/* > 38400 uses the CBAUDEXT bit */
41297c478bd9Sstevel@tonic-gate 		asy->asy_cflag |= CBAUDEXT;
41307c478bd9Sstevel@tonic-gate 		asy->asy_cflag |= asy->asy_bidx - CBAUD - 1;
41317c478bd9Sstevel@tonic-gate 	} else {
41327c478bd9Sstevel@tonic-gate 		asy->asy_cflag |= asy->asy_bidx;
41337c478bd9Sstevel@tonic-gate 	}
41347c478bd9Sstevel@tonic-gate 
41357c478bd9Sstevel@tonic-gate 	ASSERT(asy->asy_bidx == BAUDINDEX(asy->asy_cflag));
41367c478bd9Sstevel@tonic-gate 
41377c478bd9Sstevel@tonic-gate 	/* ---- Next item is data bits ---- */
41387c478bd9Sstevel@tonic-gate 	p = p1;
41397c478bd9Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0)  {
41407c478bd9Sstevel@tonic-gate 		*p1++ = '\0';
41417c478bd9Sstevel@tonic-gate 	} else {
41427c478bd9Sstevel@tonic-gate 		asy->asy_cflag |= BITS8;	/* add default bits */
41437c478bd9Sstevel@tonic-gate 		return;
41447c478bd9Sstevel@tonic-gate 	}
41457c478bd9Sstevel@tonic-gate 	switch (*p) {
41467c478bd9Sstevel@tonic-gate 		default:
41477c478bd9Sstevel@tonic-gate 		case '8':
41487c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= CS8;
41497c478bd9Sstevel@tonic-gate 			asy->asy_lcr = BITS8;
41507c478bd9Sstevel@tonic-gate 			break;
41517c478bd9Sstevel@tonic-gate 		case '7':
41527c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= CS7;
41537c478bd9Sstevel@tonic-gate 			asy->asy_lcr = BITS7;
41547c478bd9Sstevel@tonic-gate 			break;
41557c478bd9Sstevel@tonic-gate 		case '6':
41567c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= CS6;
41577c478bd9Sstevel@tonic-gate 			asy->asy_lcr = BITS6;
41587c478bd9Sstevel@tonic-gate 			break;
41597c478bd9Sstevel@tonic-gate 		case '5':
41607c478bd9Sstevel@tonic-gate 			/* LINTED: CS5 is currently zero (but might change) */
41617c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= CS5;
41627c478bd9Sstevel@tonic-gate 			asy->asy_lcr = BITS5;
41637c478bd9Sstevel@tonic-gate 			break;
41647c478bd9Sstevel@tonic-gate 	}
41657c478bd9Sstevel@tonic-gate 
41667c478bd9Sstevel@tonic-gate 	/* ---- Parity info ---- */
41677c478bd9Sstevel@tonic-gate 	p = p1;
41687c478bd9Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0)  {
41697c478bd9Sstevel@tonic-gate 		*p1++ = '\0';
41707c478bd9Sstevel@tonic-gate 	} else {
41717c478bd9Sstevel@tonic-gate 		return;
41727c478bd9Sstevel@tonic-gate 	}
41737c478bd9Sstevel@tonic-gate 	switch (*p)  {
41747c478bd9Sstevel@tonic-gate 		default:
41757c478bd9Sstevel@tonic-gate 		case 'n':
41767c478bd9Sstevel@tonic-gate 			break;
41777c478bd9Sstevel@tonic-gate 		case 'e':
41787c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= PARENB;
41797c478bd9Sstevel@tonic-gate 			asy->asy_lcr |= PEN; break;
41807c478bd9Sstevel@tonic-gate 		case 'o':
41817c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= PARENB|PARODD;
41827c478bd9Sstevel@tonic-gate 			asy->asy_lcr |= PEN|EPS;
41837c478bd9Sstevel@tonic-gate 			break;
41847c478bd9Sstevel@tonic-gate 	}
41857c478bd9Sstevel@tonic-gate 
41867c478bd9Sstevel@tonic-gate 	/* ---- Find stop bits ---- */
41877c478bd9Sstevel@tonic-gate 	p = p1;
41887c478bd9Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0)  {
41897c478bd9Sstevel@tonic-gate 		*p1++ = '\0';
41907c478bd9Sstevel@tonic-gate 	} else {
41917c478bd9Sstevel@tonic-gate 		return;
41927c478bd9Sstevel@tonic-gate 	}
41937c478bd9Sstevel@tonic-gate 	if (*p == '2') {
41947c478bd9Sstevel@tonic-gate 		asy->asy_cflag |= CSTOPB;
41957c478bd9Sstevel@tonic-gate 		asy->asy_lcr |= STB;
41967c478bd9Sstevel@tonic-gate 	}
41977c478bd9Sstevel@tonic-gate 
41987c478bd9Sstevel@tonic-gate 	/* ---- handshake is next ---- */
41997c478bd9Sstevel@tonic-gate 	p = p1;
42007c478bd9Sstevel@tonic-gate 	if (p) {
42017c478bd9Sstevel@tonic-gate 		if ((p1 = strchr(p, ',')) != 0)
42027c478bd9Sstevel@tonic-gate 			*p1++ = '\0';
42037c478bd9Sstevel@tonic-gate 
42047c478bd9Sstevel@tonic-gate 		if (*p == 'h')
42057c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= CRTSCTS;
42067c478bd9Sstevel@tonic-gate 		else if (*p == 's')
42077c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= CRTSXOFF;
42087c478bd9Sstevel@tonic-gate 	}
42097c478bd9Sstevel@tonic-gate }
42107c478bd9Sstevel@tonic-gate 
42117c478bd9Sstevel@tonic-gate /*
42127c478bd9Sstevel@tonic-gate  * Check for abort character sequence
42137c478bd9Sstevel@tonic-gate  */
42147c478bd9Sstevel@tonic-gate static boolean_t
42157c478bd9Sstevel@tonic-gate abort_charseq_recognize(uchar_t ch)
42167c478bd9Sstevel@tonic-gate {
42177c478bd9Sstevel@tonic-gate 	static int state = 0;
42187c478bd9Sstevel@tonic-gate #define	CNTRL(c) ((c)&037)
42197c478bd9Sstevel@tonic-gate 	static char sequence[] = { '\r', '~', CNTRL('b') };
42207c478bd9Sstevel@tonic-gate 
42217c478bd9Sstevel@tonic-gate 	if (ch == sequence[state]) {
42227c478bd9Sstevel@tonic-gate 		if (++state >= sizeof (sequence)) {
42237c478bd9Sstevel@tonic-gate 			state = 0;
42247c478bd9Sstevel@tonic-gate 			return (B_TRUE);
42257c478bd9Sstevel@tonic-gate 		}
42267c478bd9Sstevel@tonic-gate 	} else {
42277c478bd9Sstevel@tonic-gate 		state = (ch == sequence[0]) ? 1 : 0;
42287c478bd9Sstevel@tonic-gate 	}
42297c478bd9Sstevel@tonic-gate 	return (B_FALSE);
42307c478bd9Sstevel@tonic-gate }
42317c478bd9Sstevel@tonic-gate 
42327c478bd9Sstevel@tonic-gate /*
42337c478bd9Sstevel@tonic-gate  * Flow control functions
42347c478bd9Sstevel@tonic-gate  */
42357c478bd9Sstevel@tonic-gate /*
42367c478bd9Sstevel@tonic-gate  * Software input flow control
42377c478bd9Sstevel@tonic-gate  * This function can execute software input flow control sucessfully
42387c478bd9Sstevel@tonic-gate  * at most of situations except that the line is in BREAK status
42397c478bd9Sstevel@tonic-gate  * (timed and untimed break).
42407c478bd9Sstevel@tonic-gate  * INPUT VALUE of onoff:
42417c478bd9Sstevel@tonic-gate  *               FLOW_START means to send out a XON char
42427c478bd9Sstevel@tonic-gate  *                          and clear SW input flow control flag.
42437c478bd9Sstevel@tonic-gate  *               FLOW_STOP means to send out a XOFF char
42447c478bd9Sstevel@tonic-gate  *                          and set SW input flow control flag.
42457c478bd9Sstevel@tonic-gate  *               FLOW_CHECK means to check whether there is pending XON/XOFF
42467c478bd9Sstevel@tonic-gate  *                          if it is true, send it out.
42477c478bd9Sstevel@tonic-gate  * INPUT VALUE of type:
42487c478bd9Sstevel@tonic-gate  *		 IN_FLOW_RINGBUFF means flow control is due to RING BUFFER
42497c478bd9Sstevel@tonic-gate  *		 IN_FLOW_STREAMS means flow control is due to STREAMS
42507c478bd9Sstevel@tonic-gate  *		 IN_FLOW_USER means flow control is due to user's commands
42517c478bd9Sstevel@tonic-gate  * RETURN VALUE: B_FALSE means no flow control char is sent
42527c478bd9Sstevel@tonic-gate  *               B_TRUE means one flow control char is sent
42537c478bd9Sstevel@tonic-gate  */
42547c478bd9Sstevel@tonic-gate static boolean_t
42557c478bd9Sstevel@tonic-gate async_flowcontrol_sw_input(struct asycom *asy, async_flowc_action onoff,
42567c478bd9Sstevel@tonic-gate     int type)
42577c478bd9Sstevel@tonic-gate {
42587c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
42597c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
42607c478bd9Sstevel@tonic-gate 	int rval = B_FALSE;
42617c478bd9Sstevel@tonic-gate 
42627c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
42637c478bd9Sstevel@tonic-gate 
42647c478bd9Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_iflag & IXOFF))
42657c478bd9Sstevel@tonic-gate 		return (rval);
42667c478bd9Sstevel@tonic-gate 
42677c478bd9Sstevel@tonic-gate 	/*
42687c478bd9Sstevel@tonic-gate 	 * If we get this far, then we know IXOFF is set.
42697c478bd9Sstevel@tonic-gate 	 */
42707c478bd9Sstevel@tonic-gate 	switch (onoff) {
42717c478bd9Sstevel@tonic-gate 	case FLOW_STOP:
42727c478bd9Sstevel@tonic-gate 		async->async_inflow_source |= type;
42737c478bd9Sstevel@tonic-gate 
42747c478bd9Sstevel@tonic-gate 		/*
42757c478bd9Sstevel@tonic-gate 		 * We'll send an XOFF character for each of up to
42767c478bd9Sstevel@tonic-gate 		 * three different input flow control attempts to stop input.
42777c478bd9Sstevel@tonic-gate 		 * If we already send out one XOFF, but FLOW_STOP comes again,
42787c478bd9Sstevel@tonic-gate 		 * it seems that input flow control becomes more serious,
42797c478bd9Sstevel@tonic-gate 		 * then send XOFF again.
42807c478bd9Sstevel@tonic-gate 		 */
42817c478bd9Sstevel@tonic-gate 		if (async->async_inflow_source & (IN_FLOW_RINGBUFF |
42827c478bd9Sstevel@tonic-gate 		    IN_FLOW_STREAMS | IN_FLOW_USER))
42837c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_SW_IN_FLOW |
42847c478bd9Sstevel@tonic-gate 			    ASYNC_SW_IN_NEEDED;
42857c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_SFLOW, "async%d: input sflow stop, "
42867c478bd9Sstevel@tonic-gate 		    "type = %x\n", instance, async->async_inflow_source);
42877c478bd9Sstevel@tonic-gate 		break;
42887c478bd9Sstevel@tonic-gate 	case FLOW_START:
42897c478bd9Sstevel@tonic-gate 		async->async_inflow_source &= ~type;
42907c478bd9Sstevel@tonic-gate 		if (async->async_inflow_source == 0) {
42917c478bd9Sstevel@tonic-gate 			async->async_flags = (async->async_flags &
42927c478bd9Sstevel@tonic-gate 			    ~ASYNC_SW_IN_FLOW) | ASYNC_SW_IN_NEEDED;
42937c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_SFLOW, "async%d: "
42947c478bd9Sstevel@tonic-gate 			    "input sflow start\n", instance);
42957c478bd9Sstevel@tonic-gate 		}
42967c478bd9Sstevel@tonic-gate 		break;
42977c478bd9Sstevel@tonic-gate 	default:
42987c478bd9Sstevel@tonic-gate 		break;
42997c478bd9Sstevel@tonic-gate 	}
43007c478bd9Sstevel@tonic-gate 
43017c478bd9Sstevel@tonic-gate 	if (((async->async_flags & (ASYNC_SW_IN_NEEDED | ASYNC_BREAK |
43027c478bd9Sstevel@tonic-gate 	    ASYNC_OUT_SUSPEND)) == ASYNC_SW_IN_NEEDED) &&
43034ab75253Smrj 	    (ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) & XHRE)) {
43047c478bd9Sstevel@tonic-gate 		/*
43057c478bd9Sstevel@tonic-gate 		 * If we get this far, then we know we need to send out
43067c478bd9Sstevel@tonic-gate 		 * XON or XOFF char.
43077c478bd9Sstevel@tonic-gate 		 */
43087c478bd9Sstevel@tonic-gate 		async->async_flags = (async->async_flags &
43097c478bd9Sstevel@tonic-gate 		    ~ASYNC_SW_IN_NEEDED) | ASYNC_BUSY;
43104ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
43117c478bd9Sstevel@tonic-gate 		    async->async_flags & ASYNC_SW_IN_FLOW ?
43127c478bd9Sstevel@tonic-gate 		    async->async_stopc : async->async_startc);
43137c478bd9Sstevel@tonic-gate 		rval = B_TRUE;
43147c478bd9Sstevel@tonic-gate 	}
43157c478bd9Sstevel@tonic-gate 	return (rval);
43167c478bd9Sstevel@tonic-gate }
43177c478bd9Sstevel@tonic-gate 
43187c478bd9Sstevel@tonic-gate /*
43197c478bd9Sstevel@tonic-gate  * Software output flow control
43207c478bd9Sstevel@tonic-gate  * This function can be executed sucessfully at any situation.
43217c478bd9Sstevel@tonic-gate  * It does not handle HW, and just change the SW output flow control flag.
43227c478bd9Sstevel@tonic-gate  * INPUT VALUE of onoff:
43237c478bd9Sstevel@tonic-gate  *                 FLOW_START means to clear SW output flow control flag,
43247c478bd9Sstevel@tonic-gate  *			also combine with HW output flow control status to
43257c478bd9Sstevel@tonic-gate  *			determine if we need to set ASYNC_OUT_FLW_RESUME.
43267c478bd9Sstevel@tonic-gate  *                 FLOW_STOP means to set SW output flow control flag,
43277c478bd9Sstevel@tonic-gate  *			also clear ASYNC_OUT_FLW_RESUME.
43287c478bd9Sstevel@tonic-gate  */
43297c478bd9Sstevel@tonic-gate static void
43307c478bd9Sstevel@tonic-gate async_flowcontrol_sw_output(struct asycom *asy, async_flowc_action onoff)
43317c478bd9Sstevel@tonic-gate {
43327c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
43337c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
43347c478bd9Sstevel@tonic-gate 
43357c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
43367c478bd9Sstevel@tonic-gate 
43377c478bd9Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_iflag & IXON))
43387c478bd9Sstevel@tonic-gate 		return;
43397c478bd9Sstevel@tonic-gate 
43407c478bd9Sstevel@tonic-gate 	switch (onoff) {
43417c478bd9Sstevel@tonic-gate 	case FLOW_STOP:
43427c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_SW_OUT_FLW;
43437c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
43447c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_SFLOW, "async%d: output sflow stop\n",
43457c478bd9Sstevel@tonic-gate 		    instance);
43467c478bd9Sstevel@tonic-gate 		break;
43477c478bd9Sstevel@tonic-gate 	case FLOW_START:
43487c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_SW_OUT_FLW;
43497c478bd9Sstevel@tonic-gate 		if (!(async->async_flags & ASYNC_HW_OUT_FLW))
43507c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_OUT_FLW_RESUME;
43517c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_SFLOW, "async%d: output sflow start\n",
43527c478bd9Sstevel@tonic-gate 		    instance);
43537c478bd9Sstevel@tonic-gate 		break;
43547c478bd9Sstevel@tonic-gate 	default:
43557c478bd9Sstevel@tonic-gate 		break;
43567c478bd9Sstevel@tonic-gate 	}
43577c478bd9Sstevel@tonic-gate }
43587c478bd9Sstevel@tonic-gate 
43597c478bd9Sstevel@tonic-gate /*
43607c478bd9Sstevel@tonic-gate  * Hardware input flow control
43617c478bd9Sstevel@tonic-gate  * This function can be executed sucessfully at any situation.
43627c478bd9Sstevel@tonic-gate  * It directly changes RTS depending on input parameter onoff.
43637c478bd9Sstevel@tonic-gate  * INPUT VALUE of onoff:
43647c478bd9Sstevel@tonic-gate  *       FLOW_START means to clear HW input flow control flag,
43657c478bd9Sstevel@tonic-gate  *                  and pull up RTS if it is low.
43667c478bd9Sstevel@tonic-gate  *       FLOW_STOP means to set HW input flow control flag,
43677c478bd9Sstevel@tonic-gate  *                  and low RTS if it is high.
43687c478bd9Sstevel@tonic-gate  * INPUT VALUE of type:
43697c478bd9Sstevel@tonic-gate  *		 IN_FLOW_RINGBUFF means flow control is due to RING BUFFER
43707c478bd9Sstevel@tonic-gate  *		 IN_FLOW_STREAMS means flow control is due to STREAMS
43717c478bd9Sstevel@tonic-gate  *		 IN_FLOW_USER means flow control is due to user's commands
43727c478bd9Sstevel@tonic-gate  */
43737c478bd9Sstevel@tonic-gate static void
43747c478bd9Sstevel@tonic-gate async_flowcontrol_hw_input(struct asycom *asy, async_flowc_action onoff,
43757c478bd9Sstevel@tonic-gate     int type)
43767c478bd9Sstevel@tonic-gate {
43777c478bd9Sstevel@tonic-gate 	uchar_t	mcr;
43787c478bd9Sstevel@tonic-gate 	uchar_t	flag;
43797c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
43807c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
43817c478bd9Sstevel@tonic-gate 
43827c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
43837c478bd9Sstevel@tonic-gate 
43847c478bd9Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_cflag & CRTSXOFF))
43857c478bd9Sstevel@tonic-gate 		return;
43867c478bd9Sstevel@tonic-gate 
43877c478bd9Sstevel@tonic-gate 	switch (onoff) {
43887c478bd9Sstevel@tonic-gate 	case FLOW_STOP:
43897c478bd9Sstevel@tonic-gate 		async->async_inflow_source |= type;
43907c478bd9Sstevel@tonic-gate 		if (async->async_inflow_source & (IN_FLOW_RINGBUFF |
43917c478bd9Sstevel@tonic-gate 		    IN_FLOW_STREAMS | IN_FLOW_USER))
43927c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_HW_IN_FLOW;
43937c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_HFLOW, "async%d: input hflow stop, "
43947c478bd9Sstevel@tonic-gate 		    "type = %x\n", instance, async->async_inflow_source);
43957c478bd9Sstevel@tonic-gate 		break;
43967c478bd9Sstevel@tonic-gate 	case FLOW_START:
43977c478bd9Sstevel@tonic-gate 		async->async_inflow_source &= ~type;
43987c478bd9Sstevel@tonic-gate 		if (async->async_inflow_source == 0) {
43997c478bd9Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_HW_IN_FLOW;
44007c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_HFLOW, "async%d: "
44017c478bd9Sstevel@tonic-gate 			    "input hflow start\n", instance);
44027c478bd9Sstevel@tonic-gate 		}
44037c478bd9Sstevel@tonic-gate 		break;
44047c478bd9Sstevel@tonic-gate 	default:
44057c478bd9Sstevel@tonic-gate 		break;
44067c478bd9Sstevel@tonic-gate 	}
44074ab75253Smrj 	mcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
44087c478bd9Sstevel@tonic-gate 	flag = (async->async_flags & ASYNC_HW_IN_FLOW) ? 0 : RTS;
44097c478bd9Sstevel@tonic-gate 
44107c478bd9Sstevel@tonic-gate 	if (((mcr ^ flag) & RTS) != 0) {
44114ab75253Smrj 		ddi_put8(asy->asy_iohandle,
44127c478bd9Sstevel@tonic-gate 		    asy->asy_ioaddr + MCR, (mcr ^ RTS));
44137c478bd9Sstevel@tonic-gate 	}
44147c478bd9Sstevel@tonic-gate }
44157c478bd9Sstevel@tonic-gate 
44167c478bd9Sstevel@tonic-gate /*
44177c478bd9Sstevel@tonic-gate  * Hardware output flow control
44187c478bd9Sstevel@tonic-gate  * This function can execute HW output flow control sucessfully
44197c478bd9Sstevel@tonic-gate  * at any situation.
44207c478bd9Sstevel@tonic-gate  * It doesn't really change RTS, and just change
44217c478bd9Sstevel@tonic-gate  * HW output flow control flag depending on CTS status.
44227c478bd9Sstevel@tonic-gate  * INPUT VALUE of onoff:
44237c478bd9Sstevel@tonic-gate  *                FLOW_START means to clear HW output flow control flag.
44247c478bd9Sstevel@tonic-gate  *			also combine with SW output flow control status to
44257c478bd9Sstevel@tonic-gate  *			determine if we need to set ASYNC_OUT_FLW_RESUME.
44267c478bd9Sstevel@tonic-gate  *                FLOW_STOP means to set HW output flow control flag.
44277c478bd9Sstevel@tonic-gate  *			also clear ASYNC_OUT_FLW_RESUME.
44287c478bd9Sstevel@tonic-gate  */
44297c478bd9Sstevel@tonic-gate static void
44307c478bd9Sstevel@tonic-gate async_flowcontrol_hw_output(struct asycom *asy, async_flowc_action onoff)
44317c478bd9Sstevel@tonic-gate {
44327c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
44337c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
44347c478bd9Sstevel@tonic-gate 
44357c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
44367c478bd9Sstevel@tonic-gate 
44377c478bd9Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_cflag & CRTSCTS))
44387c478bd9Sstevel@tonic-gate 		return;
44397c478bd9Sstevel@tonic-gate 
44407c478bd9Sstevel@tonic-gate 	switch (onoff) {
44417c478bd9Sstevel@tonic-gate 	case FLOW_STOP:
44427c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_HW_OUT_FLW;
44437c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
44447c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_HFLOW, "async%d: output hflow stop\n",
44457c478bd9Sstevel@tonic-gate 		    instance);
44467c478bd9Sstevel@tonic-gate 		break;
44477c478bd9Sstevel@tonic-gate 	case FLOW_START:
44487c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_HW_OUT_FLW;
44497c478bd9Sstevel@tonic-gate 		if (!(async->async_flags & ASYNC_SW_OUT_FLW))
44507c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_OUT_FLW_RESUME;
44517c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_HFLOW, "async%d: output hflow start\n",
44527c478bd9Sstevel@tonic-gate 		    instance);
44537c478bd9Sstevel@tonic-gate 		break;
44547c478bd9Sstevel@tonic-gate 	default:
44557c478bd9Sstevel@tonic-gate 		break;
44567c478bd9Sstevel@tonic-gate 	}
44577c478bd9Sstevel@tonic-gate }
4458