xref: /illumos-gate/usr/src/uts/common/io/asy.c (revision 22eb7cb54d8a6bcf6fe2674cb4b1f0cf2d85cfb6)
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*22eb7cb5Sgd78059  * Copyright 2008 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>
6356001103Smyers #include <sys/pci.h>
647c478bd9Sstevel@tonic-gate #include <sys/asy.h>
657c478bd9Sstevel@tonic-gate #include <sys/policy.h>
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * set the RX FIFO trigger_level to half the RX FIFO size for now
697c478bd9Sstevel@tonic-gate  * we may want to make this configurable later.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate static	int asy_trig_level = FIFO_TRIG_8;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate int asy_drain_check = 15000000;		/* tunable: exit drain check time */
747c478bd9Sstevel@tonic-gate int asy_min_dtr_low = 500000;		/* tunable: minimum DTR down time */
757c478bd9Sstevel@tonic-gate int asy_min_utbrk = 100000;		/* tunable: minumum untimed brk time */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate int asymaxchip = ASY16750;	/* tunable: limit chip support we look for */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * Just in case someone has a chip with broken loopback mode, we provide a
817c478bd9Sstevel@tonic-gate  * means to disable the loopback test. By default, we only loopback test
827c478bd9Sstevel@tonic-gate  * UARTs which look like they have FIFOs bigger than 16 bytes.
837c478bd9Sstevel@tonic-gate  * Set to 0 to suppress test, or to 2 to enable test on any size FIFO.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate int asy_fifo_test = 1;		/* tunable: set to 0, 1, or 2 */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Allow ability to switch off testing of the scratch register.
897c478bd9Sstevel@tonic-gate  * Some UART emulators might not have it. This will also disable the test
907c478bd9Sstevel@tonic-gate  * for Exar/Startech ST16C650, as that requires use of the SCR register.
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate int asy_scr_test = 1;		/* tunable: set to 0 to disable SCR reg test */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * As we don't yet support on-chip flow control, it's a bad idea to put a
967c478bd9Sstevel@tonic-gate  * large number of characters in the TX FIFO, since if other end tells us
977c478bd9Sstevel@tonic-gate  * to stop transmitting, we can only stop filling the TX FIFO, but it will
987c478bd9Sstevel@tonic-gate  * still carry on draining by itself, so remote end still gets what's left
997c478bd9Sstevel@tonic-gate  * in the FIFO.
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate int asy_max_tx_fifo = 16;	/* tunable: max fill of TX FIFO */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define	async_stopc	async_ttycommon.t_stopc
1047c478bd9Sstevel@tonic-gate #define	async_startc	async_ttycommon.t_startc
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #define	ASY_INIT	1
1077c478bd9Sstevel@tonic-gate #define	ASY_NOINIT	0
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /* enum value for sw and hw flow control action */
1107c478bd9Sstevel@tonic-gate typedef enum {
1117c478bd9Sstevel@tonic-gate 	FLOW_CHECK,
1127c478bd9Sstevel@tonic-gate 	FLOW_STOP,
1137c478bd9Sstevel@tonic-gate 	FLOW_START
1147c478bd9Sstevel@tonic-gate } async_flowc_action;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #ifdef DEBUG
1177c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_INIT	0x0001	/* Output msgs during driver initialization. */
1187c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_INPUT	0x0002	/* Report characters received during int. */
1197c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_EOT	0x0004	/* Output msgs when wait for xmit to finish. */
1207c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_CLOSE	0x0008	/* Output msgs when driver open/close called */
1217c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_HFLOW	0x0010	/* Output msgs when H/W flowcontrol is active */
1227c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_PROCS	0x0020	/* Output each proc name as it is entered. */
1237c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_STATE	0x0040	/* Output value of Interrupt Service Reg. */
1247c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_INTR	0x0080	/* Output value of Interrupt Service Reg. */
1257c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_OUT	0x0100	/* Output msgs about output events. */
1267c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_BUSY	0x0200	/* Output msgs when xmit is enabled/disabled */
1277c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_MODEM	0x0400	/* Output msgs about modem status & control. */
1287c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_MODM2	0x0800	/* Output msgs about modem status & control. */
1297c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_IOCTL	0x1000	/* Output msgs about ioctl messages. */
1307c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_CHIP	0x2000	/* Output msgs about chip identification. */
1317c478bd9Sstevel@tonic-gate #define	ASY_DEBUG_SFLOW	0x4000	/* Output msgs when S/W flowcontrol is active */
1327c478bd9Sstevel@tonic-gate #define	ASY_DEBUG(x) (debug & (x))
1337c478bd9Sstevel@tonic-gate static	int debug  = 0;
1347c478bd9Sstevel@tonic-gate #else
1357c478bd9Sstevel@tonic-gate #define	ASY_DEBUG(x) B_FALSE
1367c478bd9Sstevel@tonic-gate #endif
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /* pnpISA compressed device ids */
1397c478bd9Sstevel@tonic-gate #define	pnpMTS0219 0xb6930219	/* Multitech MT5634ZTX modem */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * PPS (Pulse Per Second) support.
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate void ddi_hardpps();
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * This is protected by the asy_excl_hi of the port on which PPS event
1477c478bd9Sstevel@tonic-gate  * handling is enabled.  Note that only one port should have this enabled at
1487c478bd9Sstevel@tonic-gate  * any one time.  Enabling PPS handling on multiple ports will result in
1497c478bd9Sstevel@tonic-gate  * unpredictable (but benign) results.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate static struct ppsclockev asy_ppsev;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate #ifdef PPSCLOCKLED
1547c478bd9Sstevel@tonic-gate /* XXX Use these to observe PPS latencies and jitter on a scope */
1557c478bd9Sstevel@tonic-gate #define	LED_ON
1567c478bd9Sstevel@tonic-gate #define	LED_OFF
1577c478bd9Sstevel@tonic-gate #else
1587c478bd9Sstevel@tonic-gate #define	LED_ON
1597c478bd9Sstevel@tonic-gate #define	LED_OFF
1607c478bd9Sstevel@tonic-gate #endif
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate static	int max_asy_instance = -1;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static	uint_t	asysoftintr(caddr_t intarg);
1657c478bd9Sstevel@tonic-gate static	uint_t	asyintr(caddr_t argasy);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static boolean_t abort_charseq_recognize(uchar_t ch);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /* The async interrupt entry points */
1707c478bd9Sstevel@tonic-gate static void	async_txint(struct asycom *asy);
1717c478bd9Sstevel@tonic-gate static void	async_rxint(struct asycom *asy, uchar_t lsr);
1727c478bd9Sstevel@tonic-gate static void	async_msint(struct asycom *asy);
1737c478bd9Sstevel@tonic-gate static void	async_softint(struct asycom *asy);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static void	async_ioctl(struct asyncline *async, queue_t *q, mblk_t *mp);
1767c478bd9Sstevel@tonic-gate static void	async_reioctl(void *unit);
1777c478bd9Sstevel@tonic-gate static void	async_iocdata(queue_t *q, mblk_t *mp);
1787c478bd9Sstevel@tonic-gate static void	async_restart(void *arg);
1797c478bd9Sstevel@tonic-gate static void	async_start(struct asyncline *async);
1807c478bd9Sstevel@tonic-gate static void	async_nstart(struct asyncline *async, int mode);
1817c478bd9Sstevel@tonic-gate static void	async_resume(struct asyncline *async);
1827c478bd9Sstevel@tonic-gate static void	asy_program(struct asycom *asy, int mode);
1837c478bd9Sstevel@tonic-gate static void	asyinit(struct asycom *asy);
1847c478bd9Sstevel@tonic-gate static void	asy_waiteot(struct asycom *asy);
185281f0747Slt200341 static void	asyputchar(cons_polledio_arg_t, uchar_t c);
186281f0747Slt200341 static int	asygetchar(cons_polledio_arg_t);
187281f0747Slt200341 static boolean_t	asyischar(cons_polledio_arg_t);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate static int	asymctl(struct asycom *, int, int);
1907c478bd9Sstevel@tonic-gate static int	asytodm(int, int);
1917c478bd9Sstevel@tonic-gate static int	dmtoasy(int);
1927c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
1937c478bd9Sstevel@tonic-gate static void	asyerror(int level, const char *fmt, ...) __KPRINTFLIKE(2);
1947c478bd9Sstevel@tonic-gate static void	asy_parse_mode(dev_info_t *devi, struct asycom *asy);
1957c478bd9Sstevel@tonic-gate static void	asy_soft_state_free(struct asycom *);
1967c478bd9Sstevel@tonic-gate static char	*asy_hw_name(struct asycom *asy);
1977c478bd9Sstevel@tonic-gate static void	async_hold_utbrk(void *arg);
1987c478bd9Sstevel@tonic-gate static void	async_resume_utbrk(struct asyncline *async);
1997c478bd9Sstevel@tonic-gate static void	async_dtr_free(struct asyncline *async);
2007c478bd9Sstevel@tonic-gate static int	asy_identify_chip(dev_info_t *devi, struct asycom *asy);
2017c478bd9Sstevel@tonic-gate static void	asy_reset_fifo(struct asycom *asy, uchar_t flags);
2027c478bd9Sstevel@tonic-gate static int	asy_getproperty(dev_info_t *devi, struct asycom *asy,
2037c478bd9Sstevel@tonic-gate 		    const char *property);
2047c478bd9Sstevel@tonic-gate static boolean_t	async_flowcontrol_sw_input(struct asycom *asy,
2057c478bd9Sstevel@tonic-gate 			    async_flowc_action onoff, int type);
2067c478bd9Sstevel@tonic-gate static void	async_flowcontrol_sw_output(struct asycom *asy,
2077c478bd9Sstevel@tonic-gate 		    async_flowc_action onoff);
2087c478bd9Sstevel@tonic-gate static void	async_flowcontrol_hw_input(struct asycom *asy,
2097c478bd9Sstevel@tonic-gate 		    async_flowc_action onoff, int type);
2107c478bd9Sstevel@tonic-gate static void	async_flowcontrol_hw_output(struct asycom *asy,
2117c478bd9Sstevel@tonic-gate 		    async_flowc_action onoff);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate #define	GET_PROP(devi, pname, pflag, pval, plen) \
2147c478bd9Sstevel@tonic-gate 		(ddi_prop_op(DDI_DEV_T_ANY, (devi), PROP_LEN_AND_VAL_BUF, \
2157c478bd9Sstevel@tonic-gate 		(pflag), (pname), (caddr_t)(pval), (plen)))
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static ddi_iblock_cookie_t asy_soft_iblock;
2187c478bd9Sstevel@tonic-gate ddi_softintr_t asy_softintr_id;
2197c478bd9Sstevel@tonic-gate static	int asy_addedsoft = 0;
2207c478bd9Sstevel@tonic-gate int	asysoftpend;	/* soft interrupt pending */
2217c478bd9Sstevel@tonic-gate kmutex_t asy_soft_lock;	/* lock protecting asysoftpend */
2227c478bd9Sstevel@tonic-gate kmutex_t asy_glob_lock; /* lock protecting global data manipulation */
2237c478bd9Sstevel@tonic-gate void *asy_soft_state;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /* Standard COM port I/O addresses */
2267c478bd9Sstevel@tonic-gate static const int standard_com_ports[] = {
2277c478bd9Sstevel@tonic-gate 	COM1_IOADDR, COM2_IOADDR, COM3_IOADDR, COM4_IOADDR
2287c478bd9Sstevel@tonic-gate };
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static int *com_ports;
2317c478bd9Sstevel@tonic-gate static uint_t num_com_ports;
2327c478bd9Sstevel@tonic-gate 
2332df1fe9cSrandyf #ifdef	DEBUG
2342df1fe9cSrandyf /*
2352df1fe9cSrandyf  * Set this to true to make the driver pretend to do a suspend.  Useful
2362df1fe9cSrandyf  * for debugging suspend/resume code with a serial debugger.
2372df1fe9cSrandyf  */
2382df1fe9cSrandyf boolean_t	asy_nosuspend = B_FALSE;
2392df1fe9cSrandyf #endif
2402df1fe9cSrandyf 
2412df1fe9cSrandyf 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * Baud rate table. Indexed by #defines found in sys/termios.h
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate ushort_t asyspdtab[] = {
2467c478bd9Sstevel@tonic-gate 	0,	/* 0 baud rate */
2477c478bd9Sstevel@tonic-gate 	0x900,	/* 50 baud rate */
2487c478bd9Sstevel@tonic-gate 	0x600,	/* 75 baud rate */
2497c478bd9Sstevel@tonic-gate 	0x417,	/* 110 baud rate (%0.026) */
2507c478bd9Sstevel@tonic-gate 	0x359,	/* 134 baud rate (%0.058) */
2517c478bd9Sstevel@tonic-gate 	0x300,	/* 150 baud rate */
2527c478bd9Sstevel@tonic-gate 	0x240,	/* 200 baud rate */
2537c478bd9Sstevel@tonic-gate 	0x180,	/* 300 baud rate */
2547c478bd9Sstevel@tonic-gate 	0x0c0,	/* 600 baud rate */
2557c478bd9Sstevel@tonic-gate 	0x060,	/* 1200 baud rate */
2567c478bd9Sstevel@tonic-gate 	0x040,	/* 1800 baud rate */
2577c478bd9Sstevel@tonic-gate 	0x030,	/* 2400 baud rate */
2587c478bd9Sstevel@tonic-gate 	0x018,	/* 4800 baud rate */
2597c478bd9Sstevel@tonic-gate 	0x00c,	/* 9600 baud rate */
2607c478bd9Sstevel@tonic-gate 	0x006,	/* 19200 baud rate */
2617c478bd9Sstevel@tonic-gate 	0x003,	/* 38400 baud rate */
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	0x002,	/* 57600 baud rate */
2647c478bd9Sstevel@tonic-gate 	0x0,	/* 76800 baud rate not supported */
2657c478bd9Sstevel@tonic-gate 	0x001,	/* 115200 baud rate */
2667c478bd9Sstevel@tonic-gate 	0x0,	/* 153600 baud rate not supported */
2677c478bd9Sstevel@tonic-gate 	0x0,	/* 0x8002 (SMC chip) 230400 baud rate not supported */
2687c478bd9Sstevel@tonic-gate 	0x0,	/* 307200 baud rate not supported */
2697c478bd9Sstevel@tonic-gate 	0x0,	/* 0x8001 (SMC chip) 460800 baud rate not supported */
2707c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2717c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2727c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2737c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2747c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2757c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2767c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2777c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2787c478bd9Sstevel@tonic-gate 	0x0,	/* unused */
2797c478bd9Sstevel@tonic-gate };
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate static int asyrsrv(queue_t *q);
2827c478bd9Sstevel@tonic-gate static int asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr);
2837c478bd9Sstevel@tonic-gate static int asyclose(queue_t *q, int flag, cred_t *credp);
2842df1fe9cSrandyf static int asywputdo(queue_t *q, mblk_t *mp, boolean_t);
2857c478bd9Sstevel@tonic-gate static int asywput(queue_t *q, mblk_t *mp);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate struct module_info asy_info = {
2887c478bd9Sstevel@tonic-gate 	0,
2897c478bd9Sstevel@tonic-gate 	"asy",
2907c478bd9Sstevel@tonic-gate 	0,
2917c478bd9Sstevel@tonic-gate 	INFPSZ,
2927c478bd9Sstevel@tonic-gate 	4096,
2937c478bd9Sstevel@tonic-gate 	128
2947c478bd9Sstevel@tonic-gate };
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate static struct qinit asy_rint = {
2977c478bd9Sstevel@tonic-gate 	putq,
2987c478bd9Sstevel@tonic-gate 	asyrsrv,
2997c478bd9Sstevel@tonic-gate 	asyopen,
3007c478bd9Sstevel@tonic-gate 	asyclose,
3017c478bd9Sstevel@tonic-gate 	NULL,
3027c478bd9Sstevel@tonic-gate 	&asy_info,
3037c478bd9Sstevel@tonic-gate 	NULL
3047c478bd9Sstevel@tonic-gate };
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate static struct qinit asy_wint = {
3077c478bd9Sstevel@tonic-gate 	asywput,
3087c478bd9Sstevel@tonic-gate 	NULL,
3097c478bd9Sstevel@tonic-gate 	NULL,
3107c478bd9Sstevel@tonic-gate 	NULL,
3117c478bd9Sstevel@tonic-gate 	NULL,
3127c478bd9Sstevel@tonic-gate 	&asy_info,
3137c478bd9Sstevel@tonic-gate 	NULL
3147c478bd9Sstevel@tonic-gate };
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate struct streamtab asy_str_info = {
3177c478bd9Sstevel@tonic-gate 	&asy_rint,
3187c478bd9Sstevel@tonic-gate 	&asy_wint,
3197c478bd9Sstevel@tonic-gate 	NULL,
3207c478bd9Sstevel@tonic-gate 	NULL
3217c478bd9Sstevel@tonic-gate };
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate static int asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
3247c478bd9Sstevel@tonic-gate 		void **result);
3257c478bd9Sstevel@tonic-gate static int asyprobe(dev_info_t *);
3267c478bd9Sstevel@tonic-gate static int asyattach(dev_info_t *, ddi_attach_cmd_t);
3277c478bd9Sstevel@tonic-gate static int asydetach(dev_info_t *, ddi_detach_cmd_t);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate static 	struct cb_ops cb_asy_ops = {
3307c478bd9Sstevel@tonic-gate 	nodev,			/* cb_open */
3317c478bd9Sstevel@tonic-gate 	nodev,			/* cb_close */
3327c478bd9Sstevel@tonic-gate 	nodev,			/* cb_strategy */
3337c478bd9Sstevel@tonic-gate 	nodev,			/* cb_print */
3347c478bd9Sstevel@tonic-gate 	nodev,			/* cb_dump */
3357c478bd9Sstevel@tonic-gate 	nodev,			/* cb_read */
3367c478bd9Sstevel@tonic-gate 	nodev,			/* cb_write */
3377c478bd9Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
3387c478bd9Sstevel@tonic-gate 	nodev,			/* cb_devmap */
3397c478bd9Sstevel@tonic-gate 	nodev,			/* cb_mmap */
3407c478bd9Sstevel@tonic-gate 	nodev,			/* cb_segmap */
3417c478bd9Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
3427c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
3437c478bd9Sstevel@tonic-gate 	&asy_str_info,		/* cb_stream */
3447c478bd9Sstevel@tonic-gate 	D_MP			/* cb_flag */
3457c478bd9Sstevel@tonic-gate };
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate struct dev_ops asy_ops = {
3487c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
3497c478bd9Sstevel@tonic-gate 	0,			/* devo_refcnt */
3507c478bd9Sstevel@tonic-gate 	asyinfo,		/* devo_getinfo */
3517c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_identify */
3527c478bd9Sstevel@tonic-gate 	asyprobe,		/* devo_probe */
3537c478bd9Sstevel@tonic-gate 	asyattach,		/* devo_attach */
3547c478bd9Sstevel@tonic-gate 	asydetach,		/* devo_detach */
3557c478bd9Sstevel@tonic-gate 	nodev,			/* devo_reset */
3567c478bd9Sstevel@tonic-gate 	&cb_asy_ops,		/* devo_cb_ops */
3577c478bd9Sstevel@tonic-gate };
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
3607c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a driver */
3617c478bd9Sstevel@tonic-gate 	"ASY driver %I%",
3627c478bd9Sstevel@tonic-gate 	&asy_ops,	/* driver ops */
3637c478bd9Sstevel@tonic-gate };
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
3667c478bd9Sstevel@tonic-gate 	MODREV_1,
3677c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
3687c478bd9Sstevel@tonic-gate 	NULL
3697c478bd9Sstevel@tonic-gate };
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate int
3727c478bd9Sstevel@tonic-gate _init(void)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	int i;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	i = ddi_soft_state_init(&asy_soft_state, sizeof (struct asycom), 2);
3777c478bd9Sstevel@tonic-gate 	if (i == 0) {
3787c478bd9Sstevel@tonic-gate 		mutex_init(&asy_glob_lock, NULL, MUTEX_DRIVER, NULL);
3797c478bd9Sstevel@tonic-gate 		if ((i = mod_install(&modlinkage)) != 0) {
3807c478bd9Sstevel@tonic-gate 			mutex_destroy(&asy_glob_lock);
3817c478bd9Sstevel@tonic-gate 			ddi_soft_state_fini(&asy_soft_state);
3827c478bd9Sstevel@tonic-gate 		} else {
3837c478bd9Sstevel@tonic-gate 			DEBUGCONT2(ASY_DEBUG_INIT, "%s, debug = %x\n",
3847c478bd9Sstevel@tonic-gate 			    modldrv.drv_linkinfo, debug);
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 	return (i);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate int
3917c478bd9Sstevel@tonic-gate _fini(void)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	int i;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	if ((i = mod_remove(&modlinkage)) == 0) {
3967c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_INIT, "%s unloading\n",
3977c478bd9Sstevel@tonic-gate 		    modldrv.drv_linkinfo);
3987c478bd9Sstevel@tonic-gate 		ASSERT(max_asy_instance == -1);
3997c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy_glob_lock);
4007c478bd9Sstevel@tonic-gate 		if (asy_addedsoft)
4017c478bd9Sstevel@tonic-gate 			ddi_remove_softintr(asy_softintr_id);
4027c478bd9Sstevel@tonic-gate 		asy_addedsoft = 0;
4037c478bd9Sstevel@tonic-gate 		/* free "motherboard-serial-ports" property if allocated */
4047c478bd9Sstevel@tonic-gate 		if (com_ports != NULL && com_ports != (int *)standard_com_ports)
4057c478bd9Sstevel@tonic-gate 			ddi_prop_free(com_ports);
4067c478bd9Sstevel@tonic-gate 		com_ports = NULL;
4077c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy_soft_lock);
4087c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&asy_soft_state);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	return (i);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate int
4147c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4192df1fe9cSrandyf void
4202df1fe9cSrandyf async_put_suspq(struct asycom *asy, mblk_t *mp)
4212df1fe9cSrandyf {
4222df1fe9cSrandyf 	struct asyncline *async = asy->asy_priv;
4232df1fe9cSrandyf 
4242df1fe9cSrandyf 	ASSERT(mutex_owned(&asy->asy_excl));
4252df1fe9cSrandyf 
4262df1fe9cSrandyf 	if (async->async_suspqf == NULL)
4272df1fe9cSrandyf 		async->async_suspqf = mp;
4282df1fe9cSrandyf 	else
4292df1fe9cSrandyf 		async->async_suspqb->b_next = mp;
4302df1fe9cSrandyf 
4312df1fe9cSrandyf 	async->async_suspqb = mp;
4322df1fe9cSrandyf }
4332df1fe9cSrandyf 
4342df1fe9cSrandyf static mblk_t *
4352df1fe9cSrandyf async_get_suspq(struct asycom *asy)
4362df1fe9cSrandyf {
4372df1fe9cSrandyf 	struct asyncline *async = asy->asy_priv;
4382df1fe9cSrandyf 	mblk_t *mp;
4392df1fe9cSrandyf 
4402df1fe9cSrandyf 	ASSERT(mutex_owned(&asy->asy_excl));
4412df1fe9cSrandyf 
4422df1fe9cSrandyf 	if ((mp = async->async_suspqf) != NULL) {
4432df1fe9cSrandyf 		async->async_suspqf = mp->b_next;
4442df1fe9cSrandyf 		mp->b_next = NULL;
4452df1fe9cSrandyf 	} else {
4462df1fe9cSrandyf 		async->async_suspqb = NULL;
4472df1fe9cSrandyf 	}
4482df1fe9cSrandyf 	return (mp);
4492df1fe9cSrandyf }
4502df1fe9cSrandyf 
4512df1fe9cSrandyf static void
4522df1fe9cSrandyf async_process_suspq(struct asycom *asy)
4532df1fe9cSrandyf {
4542df1fe9cSrandyf 	struct asyncline *async = asy->asy_priv;
4552df1fe9cSrandyf 	mblk_t *mp;
4562df1fe9cSrandyf 
4572df1fe9cSrandyf 	ASSERT(mutex_owned(&asy->asy_excl));
4582df1fe9cSrandyf 
4592df1fe9cSrandyf 	while ((mp = async_get_suspq(asy)) != NULL) {
4602df1fe9cSrandyf 		queue_t *q;
4612df1fe9cSrandyf 
4622df1fe9cSrandyf 		q = async->async_ttycommon.t_writeq;
4632df1fe9cSrandyf 		ASSERT(q != NULL);
4642df1fe9cSrandyf 		mutex_exit(&asy->asy_excl);
4652df1fe9cSrandyf 		(void) asywputdo(q, mp, B_FALSE);
4662df1fe9cSrandyf 		mutex_enter(&asy->asy_excl);
4672df1fe9cSrandyf 	}
4682df1fe9cSrandyf 	async->async_flags &= ~ASYNC_DDI_SUSPENDED;
4692df1fe9cSrandyf 	cv_broadcast(&async->async_flags_cv);
4702df1fe9cSrandyf }
4712df1fe9cSrandyf 
4727c478bd9Sstevel@tonic-gate static int
47356001103Smyers asy_get_bus_type(dev_info_t *devinfo)
47456001103Smyers {
47556001103Smyers 	char	parent_type[16];
47656001103Smyers 	int	parentlen;
47756001103Smyers 
47856001103Smyers 	parentlen = sizeof (parent_type);
47956001103Smyers 
48056001103Smyers 	if (ddi_prop_op(DDI_DEV_T_ANY, devinfo, PROP_LEN_AND_VAL_BUF, 0,
48156001103Smyers 	    "device_type", (caddr_t)parent_type, &parentlen)
48256001103Smyers 	    != DDI_PROP_SUCCESS && ddi_prop_op(DDI_DEV_T_ANY, devinfo,
48356001103Smyers 	    PROP_LEN_AND_VAL_BUF, 0, "bus-type", (caddr_t)parent_type,
48456001103Smyers 	    &parentlen) != DDI_PROP_SUCCESS) {
48556001103Smyers 			cmn_err(CE_WARN,
48656001103Smyers 			    "asy: can't figure out device type for"
48756001103Smyers 			    " parent \"%s\"",
48856001103Smyers 			    ddi_get_name(ddi_get_parent(devinfo)));
48956001103Smyers 			return (ASY_BUS_UNKNOWN);
49056001103Smyers 	}
49156001103Smyers 	if (strcmp(parent_type, "isa") == 0)
49256001103Smyers 		return (ASY_BUS_ISA);
49356001103Smyers 	else if (strcmp(parent_type, "pci") == 0)
49456001103Smyers 		return (ASY_BUS_PCI);
49556001103Smyers 	else
49656001103Smyers 		return (ASY_BUS_UNKNOWN);
49756001103Smyers }
49856001103Smyers 
49956001103Smyers static int
50056001103Smyers asy_get_io_regnum_pci(dev_info_t *devi, struct asycom *asy)
50156001103Smyers {
50256001103Smyers 	int reglen, nregs;
50356001103Smyers 	int regnum, i;
50456001103Smyers 	uint64_t size;
50556001103Smyers 	struct pci_phys_spec *reglist;
50656001103Smyers 
50756001103Smyers 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
50856001103Smyers 	    "reg", (caddr_t)&reglist, &reglen) != DDI_PROP_SUCCESS) {
50956001103Smyers 		cmn_err(CE_WARN, "asy_get_io_regnum_pci: reg property"
51056001103Smyers 		    " not found in devices property list");
51156001103Smyers 		return (-1);
51256001103Smyers 	}
51356001103Smyers 
51456001103Smyers 	/*
51556001103Smyers 	 * PCI devices are assumed to not have broken FIFOs;
51656001103Smyers 	 * Agere/Lucent Venus PCI modem chipsets are an example
51756001103Smyers 	 */
51856001103Smyers 	if (asy)
51956001103Smyers 		asy->asy_flags2 |= ASY2_NO_LOOPBACK;
52056001103Smyers 
52156001103Smyers 	regnum = -1;
52256001103Smyers 	nregs = reglen / sizeof (*reglist);
52356001103Smyers 	for (i = 0; i < nregs; i++) {
52456001103Smyers 		switch (reglist[i].pci_phys_hi & PCI_ADDR_MASK) {
52556001103Smyers 		case PCI_ADDR_IO:		/* I/O bus reg property */
52656001103Smyers 			if (regnum == -1) /* use only the first one */
52756001103Smyers 				regnum = i;
52856001103Smyers 			break;
52956001103Smyers 
53056001103Smyers 		default:
53156001103Smyers 			break;
53256001103Smyers 		}
53356001103Smyers 	}
53456001103Smyers 
53556001103Smyers 	/* check for valid count of registers */
53656001103Smyers 	if (regnum >= 0) {
53756001103Smyers 		size = ((uint64_t)reglist[regnum].pci_size_low) |
53856001103Smyers 		    ((uint64_t)reglist[regnum].pci_size_hi) << 32;
53956001103Smyers 		if (size < 8)
54056001103Smyers 			regnum = -1;
54156001103Smyers 	}
54256001103Smyers 	kmem_free(reglist, reglen);
54356001103Smyers 	return (regnum);
54456001103Smyers }
54556001103Smyers 
54656001103Smyers static int
54756001103Smyers asy_get_io_regnum_isa(dev_info_t *devi, struct asycom *asy)
54856001103Smyers {
54956001103Smyers 	int reglen, nregs;
55056001103Smyers 	int regnum, i;
55156001103Smyers 	struct {
55256001103Smyers 		uint_t bustype;
55356001103Smyers 		int base;
55456001103Smyers 		int size;
55556001103Smyers 	} *reglist;
55656001103Smyers 
55756001103Smyers 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
55856001103Smyers 	    "reg", (caddr_t)&reglist, &reglen) != DDI_PROP_SUCCESS) {
55956001103Smyers 		cmn_err(CE_WARN, "asy_get_io_regnum: reg property not found "
56056001103Smyers 		    "in devices property list");
56156001103Smyers 		return (-1);
56256001103Smyers 	}
56356001103Smyers 
56456001103Smyers 	regnum = -1;
56556001103Smyers 	nregs = reglen / sizeof (*reglist);
56656001103Smyers 	for (i = 0; i < nregs; i++) {
56756001103Smyers 		switch (reglist[i].bustype) {
56856001103Smyers 		case 1:			/* I/O bus reg property */
56956001103Smyers 			if (regnum == -1) /* only use the first one */
57056001103Smyers 				regnum = i;
57156001103Smyers 			break;
57256001103Smyers 
57356001103Smyers 		case pnpMTS0219:	/* Multitech MT5634ZTX modem */
57456001103Smyers 			/* Venus chipset can't do loopback test */
57556001103Smyers 			if (asy)
57656001103Smyers 				asy->asy_flags2 |= ASY2_NO_LOOPBACK;
57756001103Smyers 			break;
57856001103Smyers 
57956001103Smyers 		default:
58056001103Smyers 			break;
58156001103Smyers 		}
58256001103Smyers 	}
58356001103Smyers 
58456001103Smyers 	/* check for valid count of registers */
58556001103Smyers 	if ((regnum < 0) || (reglist[regnum].size < 8))
58656001103Smyers 		regnum = -1;
58756001103Smyers 	kmem_free(reglist, reglen);
58856001103Smyers 	return (regnum);
58956001103Smyers }
59056001103Smyers 
59156001103Smyers static int
59256001103Smyers asy_get_io_regnum(dev_info_t *devinfo, struct asycom *asy)
59356001103Smyers {
59456001103Smyers 	switch (asy_get_bus_type(devinfo)) {
59556001103Smyers 	case ASY_BUS_ISA:
59656001103Smyers 		return (asy_get_io_regnum_isa(devinfo, asy));
59756001103Smyers 	case ASY_BUS_PCI:
59856001103Smyers 		return (asy_get_io_regnum_pci(devinfo, asy));
59956001103Smyers 	default:
60056001103Smyers 		return (-1);
60156001103Smyers 	}
60256001103Smyers }
60356001103Smyers 
60456001103Smyers static int
6057c478bd9Sstevel@tonic-gate asydetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	int instance;
6087c478bd9Sstevel@tonic-gate 	struct asycom *asy;
6097c478bd9Sstevel@tonic-gate 	struct asyncline *async;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(devi);	/* find out which unit */
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, instance);
6147c478bd9Sstevel@tonic-gate 	if (asy == NULL)
6157c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6167c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
6177c478bd9Sstevel@tonic-gate 
6182df1fe9cSrandyf 	switch (cmd) {
6192df1fe9cSrandyf 	case DDI_DETACH:
6207c478bd9Sstevel@tonic-gate 		DEBUGNOTE2(ASY_DEBUG_INIT, "asy%d: %s shutdown.",
6217c478bd9Sstevel@tonic-gate 		    instance, asy_hw_name(asy));
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 		/* cancel DTR hold timeout */
6247c478bd9Sstevel@tonic-gate 		if (async->async_dtrtid != 0) {
6257c478bd9Sstevel@tonic-gate 			(void) untimeout(async->async_dtrtid);
6267c478bd9Sstevel@tonic-gate 			async->async_dtrtid = 0;
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 		/* remove all minor device node(s) for this device */
6307c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
6337c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
6347c478bd9Sstevel@tonic-gate 		cv_destroy(&async->async_flags_cv);
6357c478bd9Sstevel@tonic-gate 		ddi_remove_intr(devi, 0, asy->asy_iblock);
6367c478bd9Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
6377c478bd9Sstevel@tonic-gate 		asy_soft_state_free(asy);
6382df1fe9cSrandyf 		DEBUGNOTE1(ASY_DEBUG_INIT, "asy%d: shutdown complete",
6392df1fe9cSrandyf 		    instance);
6402df1fe9cSrandyf 		break;
6412df1fe9cSrandyf 	case DDI_SUSPEND:
6422df1fe9cSrandyf 		{
6432df1fe9cSrandyf 		unsigned i;
6442df1fe9cSrandyf 		uchar_t lsr;
6452df1fe9cSrandyf 
6462df1fe9cSrandyf #ifdef	DEBUG
6472df1fe9cSrandyf 		if (asy_nosuspend)
6482df1fe9cSrandyf 			return (DDI_SUCCESS);
6492df1fe9cSrandyf #endif
6502df1fe9cSrandyf 		mutex_enter(&asy->asy_excl);
6512df1fe9cSrandyf 
6522df1fe9cSrandyf 		ASSERT(async->async_ops >= 0);
6532df1fe9cSrandyf 		while (async->async_ops > 0)
6542df1fe9cSrandyf 			cv_wait(&async->async_ops_cv, &asy->asy_excl);
6552df1fe9cSrandyf 
6562df1fe9cSrandyf 		async->async_flags |= ASYNC_DDI_SUSPENDED;
6572df1fe9cSrandyf 
6582df1fe9cSrandyf 		/* Wait for timed break and delay to complete */
6592df1fe9cSrandyf 		while ((async->async_flags & (ASYNC_BREAK|ASYNC_DELAY))) {
6602df1fe9cSrandyf 			if (cv_wait_sig(&async->async_flags_cv, &asy->asy_excl)
6612df1fe9cSrandyf 			    == 0) {
6622df1fe9cSrandyf 				async_process_suspq(asy);
6632df1fe9cSrandyf 				mutex_exit(&asy->asy_excl);
6642df1fe9cSrandyf 				return (DDI_FAILURE);
6652df1fe9cSrandyf 			}
6662df1fe9cSrandyf 		}
6672df1fe9cSrandyf 
6682df1fe9cSrandyf 		/* Clear untimed break */
6692df1fe9cSrandyf 		if (async->async_flags & ASYNC_OUT_SUSPEND)
6702df1fe9cSrandyf 			async_resume_utbrk(async);
6712df1fe9cSrandyf 
6722df1fe9cSrandyf 		mutex_exit(&asy->asy_excl);
6732df1fe9cSrandyf 
6742df1fe9cSrandyf 		mutex_enter(&asy->asy_soft_sr);
6752df1fe9cSrandyf 		mutex_enter(&asy->asy_excl);
6762df1fe9cSrandyf 		if (async->async_wbufcid != 0) {
6772df1fe9cSrandyf 			bufcall_id_t bcid = async->async_wbufcid;
6782df1fe9cSrandyf 			async->async_wbufcid = 0;
6792df1fe9cSrandyf 			async->async_flags |= ASYNC_RESUME_BUFCALL;
6802df1fe9cSrandyf 			mutex_exit(&asy->asy_excl);
6812df1fe9cSrandyf 			unbufcall(bcid);
6822df1fe9cSrandyf 			mutex_enter(&asy->asy_excl);
6832df1fe9cSrandyf 		}
6842df1fe9cSrandyf 		mutex_enter(&asy->asy_excl_hi);
6852df1fe9cSrandyf 
6862df1fe9cSrandyf 		/* Disable interrupts from chip */
6872df1fe9cSrandyf 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0);
6882df1fe9cSrandyf 		asy->asy_flags |= ASY_DDI_SUSPENDED;
6892df1fe9cSrandyf 
6902df1fe9cSrandyf 		/* Process remaining RX characters and RX errors, if any */
6912df1fe9cSrandyf 		lsr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR);
6922df1fe9cSrandyf 		async_rxint(asy, lsr);
6932df1fe9cSrandyf 
6942df1fe9cSrandyf 		/* Wait for TX to drain */
6952df1fe9cSrandyf 		for (i = 1000; i > 0; i--) {
6962df1fe9cSrandyf 			lsr = ddi_get8(asy->asy_iohandle,
6972df1fe9cSrandyf 			    asy->asy_ioaddr + LSR);
6982df1fe9cSrandyf 			if ((lsr & (XSRE | XHRE)) == (XSRE | XHRE))
6992df1fe9cSrandyf 				break;
7002df1fe9cSrandyf 			delay(drv_usectohz(10000));
7012df1fe9cSrandyf 		}
7022df1fe9cSrandyf 		if (i == 0)
7032df1fe9cSrandyf 			cmn_err(CE_WARN,
7042df1fe9cSrandyf 			    "asy: transmitter wasn't drained before "
7052df1fe9cSrandyf 			    "driver was suspended");
7062df1fe9cSrandyf 
7072df1fe9cSrandyf 		mutex_exit(&asy->asy_excl_hi);
7082df1fe9cSrandyf 		mutex_exit(&asy->asy_excl);
7092df1fe9cSrandyf 		mutex_exit(&asy->asy_soft_sr);
7102df1fe9cSrandyf 		break;
7112df1fe9cSrandyf 	}
7122df1fe9cSrandyf 	default:
7132df1fe9cSrandyf 		return (DDI_FAILURE);
7142df1fe9cSrandyf 	}
7152df1fe9cSrandyf 
7167c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate /*
7207c478bd9Sstevel@tonic-gate  * asyprobe
7217c478bd9Sstevel@tonic-gate  * We don't bother probing for the hardware, as since Solaris 2.6, device
7227c478bd9Sstevel@tonic-gate  * nodes are only created for auto-detected hardware or nodes explicitly
7237c478bd9Sstevel@tonic-gate  * created by the user, e.g. via the DCA. However, we should check the
7247c478bd9Sstevel@tonic-gate  * device node is at least vaguely usable, i.e. we have a block of 8 i/o
7257c478bd9Sstevel@tonic-gate  * ports. This prevents attempting to attach to bogus serial ports which
7267c478bd9Sstevel@tonic-gate  * some BIOSs still partially report when they are disabled in the BIOS.
7277c478bd9Sstevel@tonic-gate  */
7287c478bd9Sstevel@tonic-gate static int
7297c478bd9Sstevel@tonic-gate asyprobe(dev_info_t *devi)
7307c478bd9Sstevel@tonic-gate {
73156001103Smyers 	return ((asy_get_io_regnum(devi, NULL) < 0) ?
73256001103Smyers 	    DDI_PROBE_FAILURE : DDI_PROBE_DONTCARE);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate static int
7367c478bd9Sstevel@tonic-gate asyattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7377c478bd9Sstevel@tonic-gate {
7387c478bd9Sstevel@tonic-gate 	int instance;
7397c478bd9Sstevel@tonic-gate 	int mcr;
7407c478bd9Sstevel@tonic-gate 	int ret;
7417c478bd9Sstevel@tonic-gate 	int regnum = 0;
7427c478bd9Sstevel@tonic-gate 	int i;
7437c478bd9Sstevel@tonic-gate 	struct asycom *asy;
74456001103Smyers 	char name[ASY_MINOR_LEN];
7457c478bd9Sstevel@tonic-gate 	int status;
7467c478bd9Sstevel@tonic-gate 	static ddi_device_acc_attr_t ioattr = {
7477c478bd9Sstevel@tonic-gate 		DDI_DEVICE_ATTR_V0,
7487c478bd9Sstevel@tonic-gate 		DDI_NEVERSWAP_ACC,
7497c478bd9Sstevel@tonic-gate 		DDI_STRICTORDER_ACC,
7507c478bd9Sstevel@tonic-gate 	};
7517c478bd9Sstevel@tonic-gate 
7522df1fe9cSrandyf 	instance = ddi_get_instance(devi);	/* find out which unit */
7532df1fe9cSrandyf 
7542df1fe9cSrandyf 	switch (cmd) {
7552df1fe9cSrandyf 	case DDI_ATTACH:
7562df1fe9cSrandyf 		break;
7572df1fe9cSrandyf 	case DDI_RESUME:
7582df1fe9cSrandyf 	{
7592df1fe9cSrandyf 		struct asyncline *async;
7602df1fe9cSrandyf 
7612df1fe9cSrandyf #ifdef	DEBUG
7622df1fe9cSrandyf 		if (asy_nosuspend)
7632df1fe9cSrandyf 			return (DDI_SUCCESS);
7642df1fe9cSrandyf #endif
7652df1fe9cSrandyf 		asy = ddi_get_soft_state(asy_soft_state, instance);
7662df1fe9cSrandyf 		if (asy == NULL)
7677c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
7687c478bd9Sstevel@tonic-gate 
7692df1fe9cSrandyf 		mutex_enter(&asy->asy_soft_sr);
7702df1fe9cSrandyf 		mutex_enter(&asy->asy_excl);
7712df1fe9cSrandyf 		mutex_enter(&asy->asy_excl_hi);
7722df1fe9cSrandyf 
7732df1fe9cSrandyf 		async = asy->asy_priv;
7742df1fe9cSrandyf 		/* Disable interrupts */
7752df1fe9cSrandyf 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0);
7762df1fe9cSrandyf 		if (asy_identify_chip(devi, asy) != DDI_SUCCESS) {
7772df1fe9cSrandyf 			mutex_exit(&asy->asy_excl_hi);
7782df1fe9cSrandyf 			mutex_exit(&asy->asy_excl);
7792df1fe9cSrandyf 			mutex_exit(&asy->asy_soft_sr);
7802df1fe9cSrandyf 			cmn_err(CE_WARN, "Cannot identify UART chip at %p\n",
7812df1fe9cSrandyf 			    (void *)asy->asy_ioaddr);
7822df1fe9cSrandyf 			return (DDI_FAILURE);
7832df1fe9cSrandyf 		}
7842df1fe9cSrandyf 		asy->asy_flags &= ~ASY_DDI_SUSPENDED;
7852df1fe9cSrandyf 		if (async->async_flags & ASYNC_ISOPEN) {
7862df1fe9cSrandyf 			asy_program(asy, ASY_INIT);
7872df1fe9cSrandyf 			/* Kick off output */
7882df1fe9cSrandyf 			if (async->async_ocnt > 0) {
7892df1fe9cSrandyf 				async_resume(async);
7902df1fe9cSrandyf 			} else {
7912df1fe9cSrandyf 				mutex_exit(&asy->asy_excl_hi);
7922df1fe9cSrandyf 				if (async->async_xmitblk)
7932df1fe9cSrandyf 					freeb(async->async_xmitblk);
7942df1fe9cSrandyf 				async->async_xmitblk = NULL;
7952df1fe9cSrandyf 				async_start(async);
7962df1fe9cSrandyf 				mutex_enter(&asy->asy_excl_hi);
7972df1fe9cSrandyf 			}
7982df1fe9cSrandyf 			ASYSETSOFT(asy);
7992df1fe9cSrandyf 		}
8002df1fe9cSrandyf 		mutex_exit(&asy->asy_excl_hi);
8012df1fe9cSrandyf 		mutex_exit(&asy->asy_excl);
8022df1fe9cSrandyf 		mutex_exit(&asy->asy_soft_sr);
8032df1fe9cSrandyf 
8042df1fe9cSrandyf 		mutex_enter(&asy->asy_excl);
8052df1fe9cSrandyf 		if (async->async_flags & ASYNC_RESUME_BUFCALL) {
8062df1fe9cSrandyf 			async->async_wbufcid = bufcall(async->async_wbufcds,
8072df1fe9cSrandyf 			    BPRI_HI, (void (*)(void *)) async_reioctl,
8082df1fe9cSrandyf 			    (void *)(intptr_t)async->async_common->asy_unit);
8092df1fe9cSrandyf 			async->async_flags &= ~ASYNC_RESUME_BUFCALL;
8102df1fe9cSrandyf 		}
8112df1fe9cSrandyf 		async_process_suspq(asy);
8122df1fe9cSrandyf 		mutex_exit(&asy->asy_excl);
8132df1fe9cSrandyf 		return (DDI_SUCCESS);
8142df1fe9cSrandyf 	}
8152df1fe9cSrandyf 	default:
8162df1fe9cSrandyf 		return (DDI_FAILURE);
8172df1fe9cSrandyf 	}
8182df1fe9cSrandyf 
8197c478bd9Sstevel@tonic-gate 	ret = ddi_soft_state_zalloc(asy_soft_state, instance);
8207c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
8217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8227c478bd9Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, instance);
8237c478bd9Sstevel@tonic-gate 	ASSERT(asy != NULL);	/* can't fail - we only just allocated it */
8247c478bd9Sstevel@tonic-gate 	asy->asy_unit = instance;
8257c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
8267c478bd9Sstevel@tonic-gate 	if (instance > max_asy_instance)
8277c478bd9Sstevel@tonic-gate 		max_asy_instance = instance;
8287c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
8297c478bd9Sstevel@tonic-gate 
83056001103Smyers 	regnum = asy_get_io_regnum(devi, asy);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if (regnum < 0 ||
8337c478bd9Sstevel@tonic-gate 	    ddi_regs_map_setup(devi, regnum, (caddr_t *)&asy->asy_ioaddr,
8347c478bd9Sstevel@tonic-gate 	    (offset_t)0, (offset_t)0, &ioattr, &asy->asy_iohandle)
8357c478bd9Sstevel@tonic-gate 	    != DDI_SUCCESS) {
8367c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "asy%d: could not map UART registers @ %p",
8377c478bd9Sstevel@tonic-gate 		    instance, (void *)asy->asy_ioaddr);
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		asy_soft_state_free(asy);
8407c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_INIT, "asy%dattach: UART @ %p\n",
8447c478bd9Sstevel@tonic-gate 	    instance, (void *)asy->asy_ioaddr);
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
8477c478bd9Sstevel@tonic-gate 	if (com_ports == NULL) {	/* need to initialize com_ports */
8487c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, devi, 0,
8497c478bd9Sstevel@tonic-gate 		    "motherboard-serial-ports", &com_ports, &num_com_ports) !=
8507c478bd9Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
8517c478bd9Sstevel@tonic-gate 			/* Use our built-in COM[1234] values */
8527c478bd9Sstevel@tonic-gate 			com_ports = (int *)standard_com_ports;
8537c478bd9Sstevel@tonic-gate 			num_com_ports = sizeof (standard_com_ports) /
8547c478bd9Sstevel@tonic-gate 			    sizeof (standard_com_ports[0]);
8557c478bd9Sstevel@tonic-gate 		}
8567c478bd9Sstevel@tonic-gate 		if (num_com_ports > 10) {
8577c478bd9Sstevel@tonic-gate 			/* We run out of single digits for device properties */
8587c478bd9Sstevel@tonic-gate 			num_com_ports = 10;
8597c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
8607c478bd9Sstevel@tonic-gate 			    "More than %d motherboard-serial-ports",
8617c478bd9Sstevel@tonic-gate 			    num_com_ports);
8627c478bd9Sstevel@tonic-gate 		}
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	/*
8677c478bd9Sstevel@tonic-gate 	 * Lookup the i/o address to see if this is a standard COM port
8687c478bd9Sstevel@tonic-gate 	 * in which case we assign it the correct tty[a-d] to match the
8697c478bd9Sstevel@tonic-gate 	 * COM port number, or some other i/o address in which case it
8707c478bd9Sstevel@tonic-gate 	 * will be assigned /dev/term/[0123...] in some rather arbitrary
8717c478bd9Sstevel@tonic-gate 	 * fashion.
8727c478bd9Sstevel@tonic-gate 	 */
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_com_ports; i++) {
8757c478bd9Sstevel@tonic-gate 		if (asy->asy_ioaddr == (uint8_t *)(uintptr_t)com_ports[i]) {
8767c478bd9Sstevel@tonic-gate 			asy->asy_com_port = i + 1;
8777c478bd9Sstevel@tonic-gate 			break;
8787c478bd9Sstevel@tonic-gate 		}
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	/*
8827c478bd9Sstevel@tonic-gate 	 * It appears that there was async hardware that on reset
8837c478bd9Sstevel@tonic-gate 	 * did not clear ICR.  Hence when we get to
8847c478bd9Sstevel@tonic-gate 	 * ddi_get_iblock_cookie below, this hardware would cause
8857c478bd9Sstevel@tonic-gate 	 * the system to hang if there was input available.
8867c478bd9Sstevel@tonic-gate 	 */
8877c478bd9Sstevel@tonic-gate 
8884ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0x00);
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/* establish default usage */
8917c478bd9Sstevel@tonic-gate 	asy->asy_mcr |= RTS|DTR;		/* do use RTS/DTR after open */
8927c478bd9Sstevel@tonic-gate 	asy->asy_lcr = STOP1|BITS8;		/* default to 1 stop 8 bits */
8937c478bd9Sstevel@tonic-gate 	asy->asy_bidx = B9600;			/* default to 9600  */
8947c478bd9Sstevel@tonic-gate #ifdef DEBUG
8957c478bd9Sstevel@tonic-gate 	asy->asy_msint_cnt = 0;			/* # of times in async_msint */
8967c478bd9Sstevel@tonic-gate #endif
8977c478bd9Sstevel@tonic-gate 	mcr = 0;				/* don't enable until open */
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	if (asy->asy_com_port != 0) {
9007c478bd9Sstevel@tonic-gate 		/*
9017c478bd9Sstevel@tonic-gate 		 * For motherboard ports, emulate tty eeprom properties.
9027c478bd9Sstevel@tonic-gate 		 * Actually, we can't tell if a port is motherboard or not,
9037c478bd9Sstevel@tonic-gate 		 * so for "motherboard ports", read standard DOS COM ports.
9047c478bd9Sstevel@tonic-gate 		 */
9057c478bd9Sstevel@tonic-gate 		switch (asy_getproperty(devi, asy, "ignore-cd")) {
9067c478bd9Sstevel@tonic-gate 		case 0:				/* *-ignore-cd=False */
9077c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
9087c478bd9Sstevel@tonic-gate 			    "asy%dattach: clear ASY_IGNORE_CD\n", instance);
9097c478bd9Sstevel@tonic-gate 			asy->asy_flags &= ~ASY_IGNORE_CD; /* wait for cd */
9107c478bd9Sstevel@tonic-gate 			break;
9117c478bd9Sstevel@tonic-gate 		case 1:				/* *-ignore-cd=True */
9127c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
9137c478bd9Sstevel@tonic-gate 		default:			/* *-ignore-cd not defined */
9147c478bd9Sstevel@tonic-gate 			/*
9157c478bd9Sstevel@tonic-gate 			 * We set rather silly defaults of soft carrier on
9167c478bd9Sstevel@tonic-gate 			 * and DTR/RTS raised here because it might be that
9177c478bd9Sstevel@tonic-gate 			 * one of the motherboard ports is the system console.
9187c478bd9Sstevel@tonic-gate 			 */
9197c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
9207c478bd9Sstevel@tonic-gate 			    "asy%dattach: set ASY_IGNORE_CD, set RTS & DTR\n",
9217c478bd9Sstevel@tonic-gate 			    instance);
9227c478bd9Sstevel@tonic-gate 			mcr = asy->asy_mcr;		/* rts/dtr on */
9237c478bd9Sstevel@tonic-gate 			asy->asy_flags |= ASY_IGNORE_CD;	/* ignore cd */
9247c478bd9Sstevel@tonic-gate 			break;
9257c478bd9Sstevel@tonic-gate 		}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		/* Property for not raising DTR/RTS */
9287c478bd9Sstevel@tonic-gate 		switch (asy_getproperty(devi, asy, "rts-dtr-off")) {
9297c478bd9Sstevel@tonic-gate 		case 0:				/* *-rts-dtr-off=False */
9307c478bd9Sstevel@tonic-gate 			asy->asy_flags |= ASY_RTS_DTR_OFF;	/* OFF */
9317c478bd9Sstevel@tonic-gate 			mcr = asy->asy_mcr;		/* rts/dtr on */
9327c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM, "asy%dattach: "
9337c478bd9Sstevel@tonic-gate 			    "ASY_RTS_DTR_OFF set and DTR & RTS set\n",
9347c478bd9Sstevel@tonic-gate 			    instance);
9357c478bd9Sstevel@tonic-gate 			break;
9367c478bd9Sstevel@tonic-gate 		case 1:				/* *-rts-dtr-off=True */
9377c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
9387c478bd9Sstevel@tonic-gate 		default:			/* *-rts-dtr-off undefined */
9397c478bd9Sstevel@tonic-gate 			break;
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		/* Parse property for tty modes */
9437c478bd9Sstevel@tonic-gate 		asy_parse_mode(devi, asy);
9447c478bd9Sstevel@tonic-gate 	} else {
9457c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_MODEM,
9467c478bd9Sstevel@tonic-gate 		    "asy%dattach: clear ASY_IGNORE_CD, clear RTS & DTR\n",
9477c478bd9Sstevel@tonic-gate 		    instance);
9487c478bd9Sstevel@tonic-gate 		asy->asy_flags &= ~ASY_IGNORE_CD;	/* wait for cd */
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	/*
9527c478bd9Sstevel@tonic-gate 	 * Initialize the port with default settings.
9537c478bd9Sstevel@tonic-gate 	 */
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	asy->asy_fifo_buf = 1;
9567c478bd9Sstevel@tonic-gate 	asy->asy_use_fifo = FIFO_OFF;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	/*
9597c478bd9Sstevel@tonic-gate 	 * Get icookie for mutexes initialization
9607c478bd9Sstevel@tonic-gate 	 */
9617c478bd9Sstevel@tonic-gate 	if ((ddi_get_iblock_cookie(devi, 0, &asy->asy_iblock) !=
9627c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS) ||
9637c478bd9Sstevel@tonic-gate 	    (ddi_get_soft_iblock_cookie(devi, DDI_SOFTINT_MED,
9647c478bd9Sstevel@tonic-gate 	    &asy_soft_iblock) != DDI_SUCCESS)) {
9657c478bd9Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
9667c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
9677c478bd9Sstevel@tonic-gate 		    "asy%d: could not hook interrupt for UART @ %p\n",
9687c478bd9Sstevel@tonic-gate 		    instance, (void *)asy->asy_ioaddr);
9697c478bd9Sstevel@tonic-gate 		asy_soft_state_free(asy);
9707c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	/*
9747c478bd9Sstevel@tonic-gate 	 * Initialize mutexes before accessing the hardware
9757c478bd9Sstevel@tonic-gate 	 */
9767c478bd9Sstevel@tonic-gate 	mutex_init(&asy->asy_excl, NULL, MUTEX_DRIVER, asy_soft_iblock);
9777c478bd9Sstevel@tonic-gate 	mutex_init(&asy->asy_excl_hi, NULL, MUTEX_DRIVER,
9787c478bd9Sstevel@tonic-gate 	    (void *)asy->asy_iblock);
9792df1fe9cSrandyf 	mutex_init(&asy->asy_soft_sr, NULL, MUTEX_DRIVER, asy_soft_iblock);
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
9827c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	if (asy_identify_chip(devi, asy) != DDI_SUCCESS) {
9857c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
9867c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
9877c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
9887c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
9892df1fe9cSrandyf 		mutex_destroy(&asy->asy_soft_sr);
9907c478bd9Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
9917c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "Cannot identify UART chip at %p\n",
9927c478bd9Sstevel@tonic-gate 		    (void *)asy->asy_ioaddr);
9937c478bd9Sstevel@tonic-gate 		asy_soft_state_free(asy);
9947c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	/* disable all interrupts */
9984ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0);
9997c478bd9Sstevel@tonic-gate 	/* select baud rate generator */
10004ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, DLAB);
10017c478bd9Sstevel@tonic-gate 	/* Set the baud rate to 9600 */
10024ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + (DAT+DLL),
10037c478bd9Sstevel@tonic-gate 	    asyspdtab[asy->asy_bidx] & 0xff);
10044ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + (DAT+DLH),
10057c478bd9Sstevel@tonic-gate 	    (asyspdtab[asy->asy_bidx] >> 8) & 0xff);
10062df1fe9cSrandyf 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, asy->asy_lcr);
10074ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR, mcr);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
10107c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	/*
10137c478bd9Sstevel@tonic-gate 	 * Set up the other components of the asycom structure for this port.
10147c478bd9Sstevel@tonic-gate 	 */
10157c478bd9Sstevel@tonic-gate 	asy->asy_dip = devi;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
10187c478bd9Sstevel@tonic-gate 	if (asy_addedsoft == 0) { /* install the soft interrupt handler */
10197c478bd9Sstevel@tonic-gate 		if (ddi_add_softintr(devi, DDI_SOFTINT_MED,
10207c478bd9Sstevel@tonic-gate 		    &asy_softintr_id, NULL, 0, asysoftintr,
10217c478bd9Sstevel@tonic-gate 		    (caddr_t)0) != DDI_SUCCESS) {
10227c478bd9Sstevel@tonic-gate 			mutex_destroy(&asy->asy_excl);
10237c478bd9Sstevel@tonic-gate 			mutex_destroy(&asy->asy_excl_hi);
10247c478bd9Sstevel@tonic-gate 			ddi_regs_map_free(&asy->asy_iohandle);
10257c478bd9Sstevel@tonic-gate 			mutex_exit(&asy_glob_lock);
10267c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
10277c478bd9Sstevel@tonic-gate 			    "Can not set soft interrupt for ASY driver\n");
10287c478bd9Sstevel@tonic-gate 			asy_soft_state_free(asy);
10297c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
10307c478bd9Sstevel@tonic-gate 		}
10317c478bd9Sstevel@tonic-gate 		mutex_init(&asy_soft_lock, NULL, MUTEX_DRIVER,
10327c478bd9Sstevel@tonic-gate 		    (void *)asy->asy_iblock);
10337c478bd9Sstevel@tonic-gate 		asy_addedsoft++;
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
10387c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	/*
10417c478bd9Sstevel@tonic-gate 	 * Install interrupt handler for this device.
10427c478bd9Sstevel@tonic-gate 	 */
10437c478bd9Sstevel@tonic-gate 	if (ddi_add_intr(devi, 0, NULL, 0, asyintr,
10447c478bd9Sstevel@tonic-gate 	    (caddr_t)asy) != DDI_SUCCESS) {
10457c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
10467c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
10477c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
10487c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
10497c478bd9Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
10507c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
10517c478bd9Sstevel@tonic-gate 		    "Can not set device interrupt for ASY driver\n");
10527c478bd9Sstevel@tonic-gate 		asy_soft_state_free(asy);
10537c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
10577c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	asyinit(asy);	/* initialize the asyncline structure */
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	/* create minor device nodes for this device */
10627c478bd9Sstevel@tonic-gate 	if (asy->asy_com_port != 0) {
10637c478bd9Sstevel@tonic-gate 		/*
10647c478bd9Sstevel@tonic-gate 		 * For DOS COM ports, add letter suffix so
10657c478bd9Sstevel@tonic-gate 		 * devfsadm can create correct link names.
10667c478bd9Sstevel@tonic-gate 		 */
10677c478bd9Sstevel@tonic-gate 		name[0] = asy->asy_com_port + 'a' - 1;
10687c478bd9Sstevel@tonic-gate 		name[1] = '\0';
10697c478bd9Sstevel@tonic-gate 	} else {
10707c478bd9Sstevel@tonic-gate 		/*
107156001103Smyers 		 * asy port which isn't a standard DOS COM
107256001103Smyers 		 * port gets a numeric name based on instance
10737c478bd9Sstevel@tonic-gate 		 */
107456001103Smyers 		(void) snprintf(name, ASY_MINOR_LEN, "%d", instance);
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 	status = ddi_create_minor_node(devi, name, S_IFCHR, instance,
10777c478bd9Sstevel@tonic-gate 	    asy->asy_com_port != 0 ? DDI_NT_SERIAL_MB : DDI_NT_SERIAL, NULL);
10787c478bd9Sstevel@tonic-gate 	if (status == DDI_SUCCESS) {
10797c478bd9Sstevel@tonic-gate 		(void) strcat(name, ",cu");
10807c478bd9Sstevel@tonic-gate 		status = ddi_create_minor_node(devi, name, S_IFCHR,
10817c478bd9Sstevel@tonic-gate 		    OUTLINE | instance,
10827c478bd9Sstevel@tonic-gate 		    asy->asy_com_port != 0 ? DDI_NT_SERIAL_MB_DO :
10837c478bd9Sstevel@tonic-gate 		    DDI_NT_SERIAL_DO, NULL);
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
10877c478bd9Sstevel@tonic-gate 		struct asyncline *async = asy->asy_priv;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
10907c478bd9Sstevel@tonic-gate 		ddi_remove_intr(devi, 0, asy->asy_iblock);
10917c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl);
10927c478bd9Sstevel@tonic-gate 		mutex_destroy(&asy->asy_excl_hi);
10937c478bd9Sstevel@tonic-gate 		cv_destroy(&async->async_flags_cv);
10947c478bd9Sstevel@tonic-gate 		ddi_regs_map_free(&asy->asy_iohandle);
10957c478bd9Sstevel@tonic-gate 		asy_soft_state_free(asy);
10967c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	/*
11007c478bd9Sstevel@tonic-gate 	 * Fill in the polled I/O structure.
11017c478bd9Sstevel@tonic-gate 	 */
11027c478bd9Sstevel@tonic-gate 	asy->polledio.cons_polledio_version = CONSPOLLEDIO_V0;
1103281f0747Slt200341 	asy->polledio.cons_polledio_argument = (cons_polledio_arg_t)asy;
11047c478bd9Sstevel@tonic-gate 	asy->polledio.cons_polledio_putchar = asyputchar;
11057c478bd9Sstevel@tonic-gate 	asy->polledio.cons_polledio_getchar = asygetchar;
11067c478bd9Sstevel@tonic-gate 	asy->polledio.cons_polledio_ischar = asyischar;
11077c478bd9Sstevel@tonic-gate 	asy->polledio.cons_polledio_enter = NULL;
11087c478bd9Sstevel@tonic-gate 	asy->polledio.cons_polledio_exit = NULL;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
11117c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_INIT, "asy%dattach: done\n", instance);
11127c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11167c478bd9Sstevel@tonic-gate static int
11177c478bd9Sstevel@tonic-gate asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
11187c478bd9Sstevel@tonic-gate 	void **result)
11197c478bd9Sstevel@tonic-gate {
11207c478bd9Sstevel@tonic-gate 	dev_t dev = (dev_t)arg;
11217c478bd9Sstevel@tonic-gate 	int instance, error;
11227c478bd9Sstevel@tonic-gate 	struct asycom *asy;
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	instance = UNIT(dev);
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	switch (infocmd) {
11277c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
112870796907Scth 		asy = ddi_get_soft_state(asy_soft_state, instance);
112970796907Scth 		if ((asy == NULL) || (asy->asy_dip == NULL))
11307c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
11317c478bd9Sstevel@tonic-gate 		else {
11327c478bd9Sstevel@tonic-gate 			*result = (void *) asy->asy_dip;
11337c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
11347c478bd9Sstevel@tonic-gate 		}
11357c478bd9Sstevel@tonic-gate 		break;
11367c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
11377c478bd9Sstevel@tonic-gate 		*result = (void *)(intptr_t)instance;
11387c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
11397c478bd9Sstevel@tonic-gate 		break;
11407c478bd9Sstevel@tonic-gate 	default:
11417c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 	return (error);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate /* asy_getproperty -- walk through all name variants until we find a match */
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate static int
11497c478bd9Sstevel@tonic-gate asy_getproperty(dev_info_t *devi, struct asycom *asy, const char *property)
11507c478bd9Sstevel@tonic-gate {
11517c478bd9Sstevel@tonic-gate 	int len;
11527c478bd9Sstevel@tonic-gate 	int ret;
11537c478bd9Sstevel@tonic-gate 	char letter = asy->asy_com_port + 'a' - 1;	/* for ttya */
11547c478bd9Sstevel@tonic-gate 	char number = asy->asy_com_port + '0';		/* for COM1 */
11557c478bd9Sstevel@tonic-gate 	char val[40];
11567c478bd9Sstevel@tonic-gate 	char name[40];
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	/* Property for ignoring DCD */
11597c478bd9Sstevel@tonic-gate 	(void) sprintf(name, "tty%c-%s", letter, property);
11607c478bd9Sstevel@tonic-gate 	len = sizeof (val);
11617c478bd9Sstevel@tonic-gate 	ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
11627c478bd9Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
11637c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "com%c-%s", number, property);
11647c478bd9Sstevel@tonic-gate 		len = sizeof (val);
11652df1fe9cSrandyf 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
11687c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "tty0%c-%s", number, property);
11697c478bd9Sstevel@tonic-gate 		len = sizeof (val);
11702df1fe9cSrandyf 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
11737c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "port-%c-%s", letter, property);
11747c478bd9Sstevel@tonic-gate 		len = sizeof (val);
11752df1fe9cSrandyf 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS)
11787c478bd9Sstevel@tonic-gate 		return (-1);		/* property non-existant */
11797c478bd9Sstevel@tonic-gate 	if (val[0] == 'f' || val[0] == 'F' || val[0] == '0')
11807c478bd9Sstevel@tonic-gate 		return (0);		/* property false/0 */
11817c478bd9Sstevel@tonic-gate 	return (1);			/* property true/!0 */
11827c478bd9Sstevel@tonic-gate }
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate /* asy_soft_state_free - local wrapper for ddi_soft_state_free(9F) */
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate static void
11877c478bd9Sstevel@tonic-gate asy_soft_state_free(struct asycom *asy)
11887c478bd9Sstevel@tonic-gate {
11897c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_glob_lock);
11907c478bd9Sstevel@tonic-gate 	/* If we were the max_asy_instance, work out new value */
11917c478bd9Sstevel@tonic-gate 	if (asy->asy_unit == max_asy_instance) {
11927c478bd9Sstevel@tonic-gate 		while (--max_asy_instance >= 0) {
11937c478bd9Sstevel@tonic-gate 			if (ddi_get_soft_state(asy_soft_state,
11947c478bd9Sstevel@tonic-gate 			    max_asy_instance) != NULL)
11957c478bd9Sstevel@tonic-gate 				break;
11967c478bd9Sstevel@tonic-gate 		}
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_glob_lock);
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	if (asy->asy_priv != NULL) {
12017c478bd9Sstevel@tonic-gate 		kmem_free(asy->asy_priv, sizeof (struct asyncline));
12027c478bd9Sstevel@tonic-gate 		asy->asy_priv = NULL;
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(asy_soft_state, asy->asy_unit);
12057c478bd9Sstevel@tonic-gate }
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate static char *
12087c478bd9Sstevel@tonic-gate asy_hw_name(struct asycom *asy)
12097c478bd9Sstevel@tonic-gate {
12107c478bd9Sstevel@tonic-gate 	switch (asy->asy_hwtype) {
12117c478bd9Sstevel@tonic-gate 	case ASY8250A:
12127c478bd9Sstevel@tonic-gate 		return ("8250A/16450");
12137c478bd9Sstevel@tonic-gate 	case ASY16550:
12147c478bd9Sstevel@tonic-gate 		return ("16550");
12157c478bd9Sstevel@tonic-gate 	case ASY16550A:
12167c478bd9Sstevel@tonic-gate 		return ("16550A");
12177c478bd9Sstevel@tonic-gate 	case ASY16650:
12187c478bd9Sstevel@tonic-gate 		return ("16650");
12197c478bd9Sstevel@tonic-gate 	case ASY16750:
12207c478bd9Sstevel@tonic-gate 		return ("16750");
12217c478bd9Sstevel@tonic-gate 	default:
12227c478bd9Sstevel@tonic-gate 		DEBUGNOTE2(ASY_DEBUG_INIT,
12237c478bd9Sstevel@tonic-gate 		    "asy%d: asy_hw_name: unknown asy_hwtype: %d",
12247c478bd9Sstevel@tonic-gate 		    asy->asy_unit, asy->asy_hwtype);
12257c478bd9Sstevel@tonic-gate 		return ("?");
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate static int
12307c478bd9Sstevel@tonic-gate asy_identify_chip(dev_info_t *devi, struct asycom *asy)
12317c478bd9Sstevel@tonic-gate {
12327c478bd9Sstevel@tonic-gate 	int ret;
12337c478bd9Sstevel@tonic-gate 	int mcr;
12347c478bd9Sstevel@tonic-gate 	dev_t dev;
12357c478bd9Sstevel@tonic-gate 	uint_t hwtype;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	if (asy_scr_test) {
12387c478bd9Sstevel@tonic-gate 		/* Check scratch register works. */
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 		/* write to scratch register */
12414ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + SCR, SCRTEST);
12427c478bd9Sstevel@tonic-gate 		/* make sure that pattern doesn't just linger on the bus */
12434ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + FIFOR, 0x00);
12447c478bd9Sstevel@tonic-gate 		/* read data back from scratch register */
12454ab75253Smrj 		ret = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + SCR);
12467c478bd9Sstevel@tonic-gate 		if (ret != SCRTEST) {
12477c478bd9Sstevel@tonic-gate 			/*
12487c478bd9Sstevel@tonic-gate 			 * Scratch register not working.
12497c478bd9Sstevel@tonic-gate 			 * Probably not an async chip.
12507c478bd9Sstevel@tonic-gate 			 * 8250 and 8250B don't have scratch registers,
12517c478bd9Sstevel@tonic-gate 			 * but only worked in ancient PC XT's anyway.
12527c478bd9Sstevel@tonic-gate 			 */
12537c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "asy%d: UART @ %p "
12547c478bd9Sstevel@tonic-gate 			    "scratch register: expected 0x5a, got 0x%02x\n",
12557c478bd9Sstevel@tonic-gate 			    asy->asy_unit, (void *)asy->asy_ioaddr, ret);
12567c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
12577c478bd9Sstevel@tonic-gate 		}
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 	/*
12607c478bd9Sstevel@tonic-gate 	 * Use 16550 fifo reset sequence specified in NS application
12617c478bd9Sstevel@tonic-gate 	 * note. Disable fifos until chip is initialized.
12627c478bd9Sstevel@tonic-gate 	 */
12634ab75253Smrj 	ddi_put8(asy->asy_iohandle,
12647c478bd9Sstevel@tonic-gate 	    asy->asy_ioaddr + FIFOR, 0x00);	/* clear */
12654ab75253Smrj 	ddi_put8(asy->asy_iohandle,
12667c478bd9Sstevel@tonic-gate 	    asy->asy_ioaddr + FIFOR, FIFO_ON);	/* enable */
12674ab75253Smrj 	ddi_put8(asy->asy_iohandle,
12687c478bd9Sstevel@tonic-gate 	    asy->asy_ioaddr + FIFOR, FIFO_ON | FIFORXFLSH);
12697c478bd9Sstevel@tonic-gate 						/* reset */
12707c478bd9Sstevel@tonic-gate 	if (asymaxchip >= ASY16650 && asy_scr_test) {
12717c478bd9Sstevel@tonic-gate 		/*
12727c478bd9Sstevel@tonic-gate 		 * Reset 16650 enhanced regs also, in case we have one of these
12737c478bd9Sstevel@tonic-gate 		 */
12744ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
12757c478bd9Sstevel@tonic-gate 		    EFRACCESS);
12764ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + EFR,
12777c478bd9Sstevel@tonic-gate 		    0);
12784ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
12797c478bd9Sstevel@tonic-gate 		    STOP1|BITS8);
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	/*
12837c478bd9Sstevel@tonic-gate 	 * See what sort of FIFO we have.
12847c478bd9Sstevel@tonic-gate 	 * Try enabling it and see what chip makes of this.
12857c478bd9Sstevel@tonic-gate 	 */
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	asy->asy_fifor = 0;
12887c478bd9Sstevel@tonic-gate 	asy->asy_hwtype = asymaxchip; /* just for asy_reset_fifo() */
12897c478bd9Sstevel@tonic-gate 	if (asymaxchip >= ASY16550A)
12907c478bd9Sstevel@tonic-gate 		asy->asy_fifor |=
12917c478bd9Sstevel@tonic-gate 		    FIFO_ON | FIFODMA | (asy_trig_level & 0xff);
12927c478bd9Sstevel@tonic-gate 	if (asymaxchip >= ASY16650)
12937c478bd9Sstevel@tonic-gate 		asy->asy_fifor |= FIFOEXTRA1 | FIFOEXTRA2;
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	asy_reset_fifo(asy, FIFOTXFLSH | FIFORXFLSH);
12967c478bd9Sstevel@tonic-gate 
12974ab75253Smrj 	mcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
12984ab75253Smrj 	ret = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + ISR);
12997c478bd9Sstevel@tonic-gate 	DEBUGCONT4(ASY_DEBUG_CHIP,
13007c478bd9Sstevel@tonic-gate 	    "asy%d: probe fifo FIFOR=0x%02x ISR=0x%02x MCR=0x%02x\n",
13017c478bd9Sstevel@tonic-gate 	    asy->asy_unit, asy->asy_fifor | FIFOTXFLSH | FIFORXFLSH,
13027c478bd9Sstevel@tonic-gate 	    ret, mcr);
13037c478bd9Sstevel@tonic-gate 	switch (ret & 0xf0) {
13047c478bd9Sstevel@tonic-gate 	case 0x40:
13057c478bd9Sstevel@tonic-gate 		hwtype = ASY16550; /* 16550 with broken FIFO */
13067c478bd9Sstevel@tonic-gate 		asy->asy_fifor = 0;
13077c478bd9Sstevel@tonic-gate 		break;
13087c478bd9Sstevel@tonic-gate 	case 0xc0:
13097c478bd9Sstevel@tonic-gate 		hwtype = ASY16550A;
13107c478bd9Sstevel@tonic-gate 		asy->asy_fifo_buf = 16;
13117c478bd9Sstevel@tonic-gate 		asy->asy_use_fifo = FIFO_ON;
13127c478bd9Sstevel@tonic-gate 		asy->asy_fifor &= ~(FIFOEXTRA1 | FIFOEXTRA2);
13137c478bd9Sstevel@tonic-gate 		break;
13147c478bd9Sstevel@tonic-gate 	case 0xe0:
13157c478bd9Sstevel@tonic-gate 		hwtype = ASY16650;
13167c478bd9Sstevel@tonic-gate 		asy->asy_fifo_buf = 32;
13177c478bd9Sstevel@tonic-gate 		asy->asy_use_fifo = FIFO_ON;
13187c478bd9Sstevel@tonic-gate 		asy->asy_fifor &= ~(FIFOEXTRA1);
13197c478bd9Sstevel@tonic-gate 		break;
13207c478bd9Sstevel@tonic-gate 	case 0xf0:
13217c478bd9Sstevel@tonic-gate 		/*
13227c478bd9Sstevel@tonic-gate 		 * Note we get 0xff if chip didn't return us anything,
13237c478bd9Sstevel@tonic-gate 		 * e.g. if there's no chip there.
13247c478bd9Sstevel@tonic-gate 		 */
13257c478bd9Sstevel@tonic-gate 		if (ret == 0xff) {
13267c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "asy%d: UART @ %p "
13277c478bd9Sstevel@tonic-gate 			    "interrupt register: got 0xff\n",
13287c478bd9Sstevel@tonic-gate 			    asy->asy_unit, (void *)asy->asy_ioaddr);
13297c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
13307c478bd9Sstevel@tonic-gate 		}
13317c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
13327c478bd9Sstevel@tonic-gate 	case 0xd0:
13337c478bd9Sstevel@tonic-gate 		hwtype = ASY16750;
13347c478bd9Sstevel@tonic-gate 		asy->asy_fifo_buf = 64;
13357c478bd9Sstevel@tonic-gate 		asy->asy_use_fifo = FIFO_ON;
13367c478bd9Sstevel@tonic-gate 		break;
13377c478bd9Sstevel@tonic-gate 	default:
13387c478bd9Sstevel@tonic-gate 		hwtype = ASY8250A; /* No FIFO */
13397c478bd9Sstevel@tonic-gate 		asy->asy_fifor = 0;
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	if (hwtype > asymaxchip) {
13437c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "asy%d: UART @ %p "
13447c478bd9Sstevel@tonic-gate 		    "unexpected probe result: "
13457c478bd9Sstevel@tonic-gate 		    "FIFOR=0x%02x ISR=0x%02x MCR=0x%02x\n",
13467c478bd9Sstevel@tonic-gate 		    asy->asy_unit, (void *)asy->asy_ioaddr,
13477c478bd9Sstevel@tonic-gate 		    asy->asy_fifor | FIFOTXFLSH | FIFORXFLSH, ret, mcr);
13487c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
13497c478bd9Sstevel@tonic-gate 	}
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	/*
13527c478bd9Sstevel@tonic-gate 	 * Now reset the FIFO operation appropriate for the chip type.
13537c478bd9Sstevel@tonic-gate 	 * Note we must call asy_reset_fifo() before any possible
13547c478bd9Sstevel@tonic-gate 	 * downgrade of the asy->asy_hwtype, or it may not disable
13557c478bd9Sstevel@tonic-gate 	 * the more advanced features we specifically want downgraded.
13567c478bd9Sstevel@tonic-gate 	 */
13577c478bd9Sstevel@tonic-gate 	asy_reset_fifo(asy, 0);
13587c478bd9Sstevel@tonic-gate 	asy->asy_hwtype = hwtype;
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	/*
13617c478bd9Sstevel@tonic-gate 	 * Check for Exar/Startech ST16C650, which will still look like a
13627c478bd9Sstevel@tonic-gate 	 * 16550A until we enable its enhanced mode.
13637c478bd9Sstevel@tonic-gate 	 */
13647c478bd9Sstevel@tonic-gate 	if (asy->asy_hwtype == ASY16550A && asymaxchip >= ASY16650 &&
13657c478bd9Sstevel@tonic-gate 	    asy_scr_test) {
13667c478bd9Sstevel@tonic-gate 		/* Enable enhanced mode register access */
13674ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
13687c478bd9Sstevel@tonic-gate 		    EFRACCESS);
13697c478bd9Sstevel@tonic-gate 		/* zero scratch register (not scratch register if enhanced) */
13704ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + SCR, 0);
13717c478bd9Sstevel@tonic-gate 		/* Disable enhanced mode register access */
13724ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
13737c478bd9Sstevel@tonic-gate 		    STOP1|BITS8);
13747c478bd9Sstevel@tonic-gate 		/* read back scratch register */
13754ab75253Smrj 		ret = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + SCR);
13767c478bd9Sstevel@tonic-gate 		if (ret == SCRTEST) {
13777c478bd9Sstevel@tonic-gate 			/* looks like we have an ST16650 -- enable it */
13784ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
13797c478bd9Sstevel@tonic-gate 			    EFRACCESS);
13804ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + EFR,
13817c478bd9Sstevel@tonic-gate 			    ENHENABLE);
13824ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
13837c478bd9Sstevel@tonic-gate 			    STOP1|BITS8);
13847c478bd9Sstevel@tonic-gate 			asy->asy_hwtype = ASY16650;
13857c478bd9Sstevel@tonic-gate 			asy->asy_fifo_buf = 32;
13867c478bd9Sstevel@tonic-gate 			asy->asy_fifor |= 0x10; /* 24 byte txfifo trigger */
13877c478bd9Sstevel@tonic-gate 			asy_reset_fifo(asy, 0);
13887c478bd9Sstevel@tonic-gate 		}
13897c478bd9Sstevel@tonic-gate 	}
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	/*
13927c478bd9Sstevel@tonic-gate 	 * If we think we might have a FIFO larger than 16 characters,
13937c478bd9Sstevel@tonic-gate 	 * measure FIFO size and check it against expected.
13947c478bd9Sstevel@tonic-gate 	 */
13957c478bd9Sstevel@tonic-gate 	if (asy_fifo_test > 0 &&
13967c478bd9Sstevel@tonic-gate 	    !(asy->asy_flags2 & ASY2_NO_LOOPBACK) &&
13977c478bd9Sstevel@tonic-gate 	    (asy->asy_fifo_buf > 16 ||
13987c478bd9Sstevel@tonic-gate 	    (asy_fifo_test > 1 && asy->asy_use_fifo == FIFO_ON) ||
13997c478bd9Sstevel@tonic-gate 	    ASY_DEBUG(ASY_DEBUG_CHIP))) {
14007c478bd9Sstevel@tonic-gate 		int i;
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 		/* Set baud rate to 57600 (fairly arbitrary choice) */
14034ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
14047c478bd9Sstevel@tonic-gate 		    DLAB);
14054ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
14067c478bd9Sstevel@tonic-gate 		    asyspdtab[B57600] & 0xff);
14074ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR,
14087c478bd9Sstevel@tonic-gate 		    (asyspdtab[B57600] >> 8) & 0xff);
14097c478bd9Sstevel@tonic-gate 		/* Set 8 bits, 1 stop bit */
14104ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
14117c478bd9Sstevel@tonic-gate 		    STOP1|BITS8);
14127c478bd9Sstevel@tonic-gate 		/* Set loopback mode */
14134ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
14147c478bd9Sstevel@tonic-gate 		    DTR | RTS | ASY_LOOP | OUT1 | OUT2);
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 		/* Overfill fifo */
14177c478bd9Sstevel@tonic-gate 		for (i = 0; i < asy->asy_fifo_buf * 2; i++) {
14184ab75253Smrj 			ddi_put8(asy->asy_iohandle,
14197c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT, i);
14207c478bd9Sstevel@tonic-gate 		}
14217c478bd9Sstevel@tonic-gate 		/*
14227c478bd9Sstevel@tonic-gate 		 * Now there's an interesting question here about which
14237c478bd9Sstevel@tonic-gate 		 * FIFO we're testing the size of, RX or TX. We just
14247c478bd9Sstevel@tonic-gate 		 * filled the TX FIFO much faster than it can empty,
14257c478bd9Sstevel@tonic-gate 		 * although it is possible one or two characters may
14267c478bd9Sstevel@tonic-gate 		 * have gone from it to the TX shift register.
14277c478bd9Sstevel@tonic-gate 		 * We wait for enough time for all the characters to
14287c478bd9Sstevel@tonic-gate 		 * move into the RX FIFO and any excess characters to
14297c478bd9Sstevel@tonic-gate 		 * have been lost, and then read all the RX FIFO. So
14307c478bd9Sstevel@tonic-gate 		 * the answer we finally get will be the size which is
14317c478bd9Sstevel@tonic-gate 		 * the MIN(RX FIFO,(TX FIFO + 1 or 2)). The critical
14327c478bd9Sstevel@tonic-gate 		 * one is actually the TX FIFO, because if we overfill
14337c478bd9Sstevel@tonic-gate 		 * it in normal operation, the excess characters are
14347c478bd9Sstevel@tonic-gate 		 * lost with no warning.
14357c478bd9Sstevel@tonic-gate 		 */
14367c478bd9Sstevel@tonic-gate 		/*
14377c478bd9Sstevel@tonic-gate 		 * Wait for characters to move into RX FIFO.
14387c478bd9Sstevel@tonic-gate 		 * In theory, 200 * asy->asy_fifo_buf * 2 should be
14397c478bd9Sstevel@tonic-gate 		 * enough. However, in practice it isn't always, so we
14407c478bd9Sstevel@tonic-gate 		 * increase to 400 so some slow 16550A's finish, and we
14417c478bd9Sstevel@tonic-gate 		 * increase to 3 so we spot more characters coming back
14427c478bd9Sstevel@tonic-gate 		 * than we sent, in case that should ever happen.
14437c478bd9Sstevel@tonic-gate 		 */
14447c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(400 * asy->asy_fifo_buf * 3));
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 		/* Now see how many characters we can read back */
14477c478bd9Sstevel@tonic-gate 		for (i = 0; i < asy->asy_fifo_buf * 3; i++) {
14484ab75253Smrj 			ret = ddi_get8(asy->asy_iohandle,
14497c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + LSR);
14507c478bd9Sstevel@tonic-gate 			if (!(ret & RCA))
14517c478bd9Sstevel@tonic-gate 				break;	/* FIFO emptied */
14524ab75253Smrj 			(void) ddi_get8(asy->asy_iohandle,
14537c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT); /* lose another */
14547c478bd9Sstevel@tonic-gate 		}
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 		DEBUGCONT3(ASY_DEBUG_CHIP,
14577c478bd9Sstevel@tonic-gate 		    "asy%d FIFO size: expected=%d, measured=%d\n",
14587c478bd9Sstevel@tonic-gate 		    asy->asy_unit, asy->asy_fifo_buf, i);
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 		hwtype = asy->asy_hwtype;
14617c478bd9Sstevel@tonic-gate 		if (i < asy->asy_fifo_buf) {
14627c478bd9Sstevel@tonic-gate 			/*
14637c478bd9Sstevel@tonic-gate 			 * FIFO is somewhat smaller than we anticipated.
14647c478bd9Sstevel@tonic-gate 			 * If we have 16 characters usable, then this
14657c478bd9Sstevel@tonic-gate 			 * UART will probably work well enough in
14667c478bd9Sstevel@tonic-gate 			 * 16550A mode. If less than 16 characters,
14677c478bd9Sstevel@tonic-gate 			 * then we'd better not use it at all.
14687c478bd9Sstevel@tonic-gate 			 * UARTs with busted FIFOs do crop up.
14697c478bd9Sstevel@tonic-gate 			 */
14707c478bd9Sstevel@tonic-gate 			if (i >= 16 && asy->asy_fifo_buf >= 16) {
14717c478bd9Sstevel@tonic-gate 				/* fall back to a 16550A */
14727c478bd9Sstevel@tonic-gate 				hwtype = ASY16550A;
14737c478bd9Sstevel@tonic-gate 				asy->asy_fifo_buf = 16;
14747c478bd9Sstevel@tonic-gate 				asy->asy_fifor &= ~(FIFOEXTRA1 | FIFOEXTRA2);
14757c478bd9Sstevel@tonic-gate 			} else {
14767c478bd9Sstevel@tonic-gate 				/* fall back to no FIFO at all */
14777c478bd9Sstevel@tonic-gate 				hwtype = ASY16550;
14787c478bd9Sstevel@tonic-gate 				asy->asy_fifo_buf = 1;
14797c478bd9Sstevel@tonic-gate 				asy->asy_use_fifo = FIFO_OFF;
14807c478bd9Sstevel@tonic-gate 				asy->asy_fifor &=
14817c478bd9Sstevel@tonic-gate 				    ~(FIFO_ON | FIFOEXTRA1 | FIFOEXTRA2);
14827c478bd9Sstevel@tonic-gate 			}
14837c478bd9Sstevel@tonic-gate 		}
14847c478bd9Sstevel@tonic-gate 		/*
14857c478bd9Sstevel@tonic-gate 		 * We will need to reprogram the FIFO if we changed
14867c478bd9Sstevel@tonic-gate 		 * our mind about how to drive it above, and in any
14877c478bd9Sstevel@tonic-gate 		 * case, it would be a good idea to flush any garbage
14887c478bd9Sstevel@tonic-gate 		 * out incase the loopback test left anything behind.
14897c478bd9Sstevel@tonic-gate 		 * Again as earlier above, we must call asy_reset_fifo()
14907c478bd9Sstevel@tonic-gate 		 * before any possible downgrade of asy->asy_hwtype.
14917c478bd9Sstevel@tonic-gate 		 */
14927c478bd9Sstevel@tonic-gate 		if (asy->asy_hwtype >= ASY16650 && hwtype < ASY16650) {
14937c478bd9Sstevel@tonic-gate 			/* Disable 16650 enhanced mode */
14944ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
14957c478bd9Sstevel@tonic-gate 			    EFRACCESS);
14964ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + EFR,
14977c478bd9Sstevel@tonic-gate 			    0);
14984ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
14997c478bd9Sstevel@tonic-gate 			    STOP1|BITS8);
15007c478bd9Sstevel@tonic-gate 		}
15017c478bd9Sstevel@tonic-gate 		asy_reset_fifo(asy, FIFOTXFLSH | FIFORXFLSH);
15027c478bd9Sstevel@tonic-gate 		asy->asy_hwtype = hwtype;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 		/* Clear loopback mode and restore DTR/RTS */
15054ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR, mcr);
15067c478bd9Sstevel@tonic-gate 	}
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	DEBUGNOTE3(ASY_DEBUG_CHIP, "asy%d %s @ %p",
15097c478bd9Sstevel@tonic-gate 	    asy->asy_unit, asy_hw_name(asy), (void *)asy->asy_ioaddr);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	/* Make UART type visible in device tree for prtconf, etc */
15127c478bd9Sstevel@tonic-gate 	dev = makedevice(DDI_MAJOR_T_UNKNOWN, asy->asy_unit);
15137c478bd9Sstevel@tonic-gate 	(void) ddi_prop_update_string(dev, devi, "uart", asy_hw_name(asy));
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	if (asy->asy_hwtype == ASY16550)	/* for broken 16550's, */
15167c478bd9Sstevel@tonic-gate 		asy->asy_hwtype = ASY8250A;	/* drive them as 8250A */
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
15197c478bd9Sstevel@tonic-gate }
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate /*
15227c478bd9Sstevel@tonic-gate  * asyinit() initializes the TTY protocol-private data for this channel
15237c478bd9Sstevel@tonic-gate  * before enabling the interrupts.
15247c478bd9Sstevel@tonic-gate  */
15257c478bd9Sstevel@tonic-gate static void
15267c478bd9Sstevel@tonic-gate asyinit(struct asycom *asy)
15277c478bd9Sstevel@tonic-gate {
15287c478bd9Sstevel@tonic-gate 	struct asyncline *async;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	asy->asy_priv = kmem_zalloc(sizeof (struct asyncline), KM_SLEEP);
15317c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
15327c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
15337c478bd9Sstevel@tonic-gate 	async->async_common = asy;
15347c478bd9Sstevel@tonic-gate 	cv_init(&async->async_flags_cv, NULL, CV_DRIVER, NULL);
15357c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
15367c478bd9Sstevel@tonic-gate }
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
15397c478bd9Sstevel@tonic-gate static int
15407c478bd9Sstevel@tonic-gate asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr)
15417c478bd9Sstevel@tonic-gate {
15427c478bd9Sstevel@tonic-gate 	struct asycom	*asy;
15437c478bd9Sstevel@tonic-gate 	struct asyncline *async;
15447c478bd9Sstevel@tonic-gate 	int		mcr;
15457c478bd9Sstevel@tonic-gate 	int		unit;
15467c478bd9Sstevel@tonic-gate 	int 		len;
15477c478bd9Sstevel@tonic-gate 	struct termios 	*termiosp;
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	unit = UNIT(*dev);
15507c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_CLOSE, "asy%dopen\n", unit);
15517c478bd9Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, unit);
15527c478bd9Sstevel@tonic-gate 	if (asy == NULL)
15537c478bd9Sstevel@tonic-gate 		return (ENXIO);		/* unit not configured */
15547c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
15557c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate again:
15587c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	/*
15617c478bd9Sstevel@tonic-gate 	 * Block waiting for carrier to come up, unless this is a no-delay open.
15627c478bd9Sstevel@tonic-gate 	 */
15637c478bd9Sstevel@tonic-gate 	if (!(async->async_flags & ASYNC_ISOPEN)) {
15647c478bd9Sstevel@tonic-gate 		/*
15657c478bd9Sstevel@tonic-gate 		 * Set the default termios settings (cflag).
15667c478bd9Sstevel@tonic-gate 		 * Others are set in ldterm.
15677c478bd9Sstevel@tonic-gate 		 */
15687c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_root_node(),
15717c478bd9Sstevel@tonic-gate 		    0, "ttymodes",
15727c478bd9Sstevel@tonic-gate 		    (caddr_t)&termiosp, &len) == DDI_PROP_SUCCESS &&
15737c478bd9Sstevel@tonic-gate 		    len == sizeof (struct termios)) {
15747c478bd9Sstevel@tonic-gate 			async->async_ttycommon.t_cflag = termiosp->c_cflag;
15757c478bd9Sstevel@tonic-gate 			kmem_free(termiosp, len);
15767c478bd9Sstevel@tonic-gate 		} else
15777c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
15787c478bd9Sstevel@tonic-gate 			    "asy: couldn't get ttymodes property!");
15797c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 		/* eeprom mode support - respect properties */
15827c478bd9Sstevel@tonic-gate 		if (asy->asy_cflag)
15837c478bd9Sstevel@tonic-gate 			async->async_ttycommon.t_cflag = asy->asy_cflag;
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_iflag = 0;
15867c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_iocpending = NULL;
15877c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_row = 0;
15887c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_col = 0;
15897c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_xpixel = 0;
15907c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_size.ws_ypixel = 0;
15917c478bd9Sstevel@tonic-gate 		async->async_dev = *dev;
15927c478bd9Sstevel@tonic-gate 		async->async_wbufcid = 0;
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 		async->async_startc = CSTART;
15957c478bd9Sstevel@tonic-gate 		async->async_stopc = CSTOP;
15967c478bd9Sstevel@tonic-gate 		asy_program(asy, ASY_INIT);
15972df1fe9cSrandyf 	} else
15982df1fe9cSrandyf 		if ((async->async_ttycommon.t_flags & TS_XCLUDE) &&
15997c478bd9Sstevel@tonic-gate 		    secpolicy_excl_open(cr) != 0) {
16007c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
16017c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
16027c478bd9Sstevel@tonic-gate 		return (EBUSY);
16037c478bd9Sstevel@tonic-gate 	} else if ((*dev & OUTLINE) && !(async->async_flags & ASYNC_OUT)) {
16047c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
16057c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
16067c478bd9Sstevel@tonic-gate 		return (EBUSY);
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	if (*dev & OUTLINE)
16107c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_OUT;
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	/* Raise DTR on every open, but delay if it was just lowered. */
16137c478bd9Sstevel@tonic-gate 	while (async->async_flags & ASYNC_DTR_DELAY) {
16147c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_MODEM,
16157c478bd9Sstevel@tonic-gate 		    "asy%dopen: waiting for the ASYNC_DTR_DELAY to be clear\n",
16167c478bd9Sstevel@tonic-gate 		    unit);
16177c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
16187c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&async->async_flags_cv,
16197c478bd9Sstevel@tonic-gate 		    &asy->asy_excl) == 0) {
16207c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
16217c478bd9Sstevel@tonic-gate 			    "asy%dopen: interrupted by signal, exiting\n",
16227c478bd9Sstevel@tonic-gate 			    unit);
16237c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
16247c478bd9Sstevel@tonic-gate 			return (EINTR);
16257c478bd9Sstevel@tonic-gate 		}
16267c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
16277c478bd9Sstevel@tonic-gate 	}
16287c478bd9Sstevel@tonic-gate 
16294ab75253Smrj 	mcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
16304ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
16317c478bd9Sstevel@tonic-gate 	    mcr|(asy->asy_mcr&DTR));
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	DEBUGCONT3(ASY_DEBUG_INIT,
16347c478bd9Sstevel@tonic-gate 	    "asy%dopen: \"Raise DTR on every open\": make mcr = %x, "
16357c478bd9Sstevel@tonic-gate 	    "make TS_SOFTCAR = %s\n",
16367c478bd9Sstevel@tonic-gate 	    unit, mcr|(asy->asy_mcr&DTR),
16377c478bd9Sstevel@tonic-gate 	    (asy->asy_flags & ASY_IGNORE_CD) ? "ON" : "OFF");
16382df1fe9cSrandyf 
16397c478bd9Sstevel@tonic-gate 	if (asy->asy_flags & ASY_IGNORE_CD) {
16407c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_MODEM,
16417c478bd9Sstevel@tonic-gate 		    "asy%dopen: ASY_IGNORE_CD set, set TS_SOFTCAR\n",
16427c478bd9Sstevel@tonic-gate 		    unit);
16437c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_flags |= TS_SOFTCAR;
16447c478bd9Sstevel@tonic-gate 	}
16457c478bd9Sstevel@tonic-gate 	else
16467c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_flags &= ~TS_SOFTCAR;
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	/*
16497c478bd9Sstevel@tonic-gate 	 * Check carrier.
16507c478bd9Sstevel@tonic-gate 	 */
16514ab75253Smrj 	asy->asy_msr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MSR);
16527c478bd9Sstevel@tonic-gate 	DEBUGCONT3(ASY_DEBUG_INIT, "asy%dopen: TS_SOFTCAR is %s, "
16537c478bd9Sstevel@tonic-gate 	    "MSR & DCD is %s\n",
16547c478bd9Sstevel@tonic-gate 	    unit,
16557c478bd9Sstevel@tonic-gate 	    (async->async_ttycommon.t_flags & TS_SOFTCAR) ? "set" : "clear",
16567c478bd9Sstevel@tonic-gate 	    (asy->asy_msr & DCD) ? "set" : "clear");
16572df1fe9cSrandyf 
16587c478bd9Sstevel@tonic-gate 	if (asy->asy_msr & DCD)
16597c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_CARR_ON;
16607c478bd9Sstevel@tonic-gate 	else
16617c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_CARR_ON;
16627c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	/*
16657c478bd9Sstevel@tonic-gate 	 * If FNDELAY and FNONBLOCK are clear, block until carrier up.
16667c478bd9Sstevel@tonic-gate 	 * Quit on interrupt.
16677c478bd9Sstevel@tonic-gate 	 */
16687c478bd9Sstevel@tonic-gate 	if (!(flag & (FNDELAY|FNONBLOCK)) &&
16697c478bd9Sstevel@tonic-gate 	    !(async->async_ttycommon.t_cflag & CLOCAL)) {
16707c478bd9Sstevel@tonic-gate 		if ((!(async->async_flags & (ASYNC_CARR_ON|ASYNC_OUT)) &&
16717c478bd9Sstevel@tonic-gate 		    !(async->async_ttycommon.t_flags & TS_SOFTCAR)) ||
16727c478bd9Sstevel@tonic-gate 		    ((async->async_flags & ASYNC_OUT) &&
16737c478bd9Sstevel@tonic-gate 		    !(*dev & OUTLINE))) {
16747c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_WOPEN;
16757c478bd9Sstevel@tonic-gate 			if (cv_wait_sig(&async->async_flags_cv,
16767c478bd9Sstevel@tonic-gate 			    &asy->asy_excl) == B_FALSE) {
16777c478bd9Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_WOPEN;
16787c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl);
16797c478bd9Sstevel@tonic-gate 				return (EINTR);
16807c478bd9Sstevel@tonic-gate 			}
16817c478bd9Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_WOPEN;
16827c478bd9Sstevel@tonic-gate 			goto again;
16837c478bd9Sstevel@tonic-gate 		}
16847c478bd9Sstevel@tonic-gate 	} else if ((async->async_flags & ASYNC_OUT) && !(*dev & OUTLINE)) {
16857c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
16867c478bd9Sstevel@tonic-gate 		return (EBUSY);
16877c478bd9Sstevel@tonic-gate 	}
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	async->async_ttycommon.t_readq = rq;
16907c478bd9Sstevel@tonic-gate 	async->async_ttycommon.t_writeq = WR(rq);
16917c478bd9Sstevel@tonic-gate 	rq->q_ptr = WR(rq)->q_ptr = (caddr_t)async;
16927c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
16937c478bd9Sstevel@tonic-gate 	/*
16947c478bd9Sstevel@tonic-gate 	 * Caution here -- qprocson sets the pointers that are used by canput
16957c478bd9Sstevel@tonic-gate 	 * called by async_softint.  ASYNC_ISOPEN must *not* be set until those
16967c478bd9Sstevel@tonic-gate 	 * pointers are valid.
16977c478bd9Sstevel@tonic-gate 	 */
16987c478bd9Sstevel@tonic-gate 	qprocson(rq);
16997c478bd9Sstevel@tonic-gate 	async->async_flags |= ASYNC_ISOPEN;
17007c478bd9Sstevel@tonic-gate 	async->async_polltid = 0;
17017c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_INIT, "asy%dopen: done\n", unit);
17027c478bd9Sstevel@tonic-gate 	return (0);
17037c478bd9Sstevel@tonic-gate }
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate static void
17067c478bd9Sstevel@tonic-gate async_progress_check(void *arg)
17077c478bd9Sstevel@tonic-gate {
17087c478bd9Sstevel@tonic-gate 	struct asyncline *async = arg;
17097c478bd9Sstevel@tonic-gate 	struct asycom	 *asy = async->async_common;
17107c478bd9Sstevel@tonic-gate 	mblk_t *bp;
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	/*
17137c478bd9Sstevel@tonic-gate 	 * We define "progress" as either waiting on a timed break or delay, or
17147c478bd9Sstevel@tonic-gate 	 * having had at least one transmitter interrupt.  If none of these are
17157c478bd9Sstevel@tonic-gate 	 * true, then just terminate the output and wake up that close thread.
17167c478bd9Sstevel@tonic-gate 	 */
17177c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
17187c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
17197c478bd9Sstevel@tonic-gate 	if (!(async->async_flags & (ASYNC_BREAK|ASYNC_DELAY|ASYNC_PROGRESS))) {
17207c478bd9Sstevel@tonic-gate 		async->async_ocnt = 0;
17217c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_BUSY;
17227c478bd9Sstevel@tonic-gate 		async->async_timer = 0;
17237c478bd9Sstevel@tonic-gate 		bp = async->async_xmitblk;
17247c478bd9Sstevel@tonic-gate 		async->async_xmitblk = NULL;
17257c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
17267c478bd9Sstevel@tonic-gate 		if (bp != NULL)
17277c478bd9Sstevel@tonic-gate 			freeb(bp);
17287c478bd9Sstevel@tonic-gate 		/*
17297c478bd9Sstevel@tonic-gate 		 * Since this timer is running, we know that we're in exit(2).
17307c478bd9Sstevel@tonic-gate 		 * That means that the user can't possibly be waiting on any
17317c478bd9Sstevel@tonic-gate 		 * valid ioctl(2) completion anymore, and we should just flush
17327c478bd9Sstevel@tonic-gate 		 * everything.
17337c478bd9Sstevel@tonic-gate 		 */
17347c478bd9Sstevel@tonic-gate 		flushq(async->async_ttycommon.t_writeq, FLUSHALL);
17357c478bd9Sstevel@tonic-gate 		cv_broadcast(&async->async_flags_cv);
17367c478bd9Sstevel@tonic-gate 	} else {
17377c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_PROGRESS;
17387c478bd9Sstevel@tonic-gate 		async->async_timer = timeout(async_progress_check, async,
17397c478bd9Sstevel@tonic-gate 		    drv_usectohz(asy_drain_check));
17407c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
17417c478bd9Sstevel@tonic-gate 	}
17427c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
17437c478bd9Sstevel@tonic-gate }
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate /*
17467c478bd9Sstevel@tonic-gate  * Release DTR so that asyopen() can raise it.
17477c478bd9Sstevel@tonic-gate  */
17487c478bd9Sstevel@tonic-gate static void
17497c478bd9Sstevel@tonic-gate async_dtr_free(struct asyncline *async)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_MODEM,
17547c478bd9Sstevel@tonic-gate 	    "async_dtr_free, clearing ASYNC_DTR_DELAY\n");
17557c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
17567c478bd9Sstevel@tonic-gate 	async->async_flags &= ~ASYNC_DTR_DELAY;
17577c478bd9Sstevel@tonic-gate 	async->async_dtrtid = 0;
17587c478bd9Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
17597c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
17607c478bd9Sstevel@tonic-gate }
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate /*
17637c478bd9Sstevel@tonic-gate  * Close routine.
17647c478bd9Sstevel@tonic-gate  */
17657c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
17667c478bd9Sstevel@tonic-gate static int
17677c478bd9Sstevel@tonic-gate asyclose(queue_t *q, int flag, cred_t *credp)
17687c478bd9Sstevel@tonic-gate {
17697c478bd9Sstevel@tonic-gate 	struct asyncline *async;
17707c478bd9Sstevel@tonic-gate 	struct asycom	 *asy;
17717c478bd9Sstevel@tonic-gate 	int icr, lcr;
17727c478bd9Sstevel@tonic-gate #ifdef DEBUG
17737c478bd9Sstevel@tonic-gate 	int instance;
17747c478bd9Sstevel@tonic-gate #endif
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	async = (struct asyncline *)q->q_ptr;
17777c478bd9Sstevel@tonic-gate 	ASSERT(async != NULL);
17787c478bd9Sstevel@tonic-gate #ifdef DEBUG
17797c478bd9Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
17807c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_CLOSE, "asy%dclose\n", instance);
17817c478bd9Sstevel@tonic-gate #endif
17827c478bd9Sstevel@tonic-gate 	asy = async->async_common;
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
17857c478bd9Sstevel@tonic-gate 	async->async_flags |= ASYNC_CLOSING;
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	/*
17887c478bd9Sstevel@tonic-gate 	 * Turn off PPS handling early to avoid events occuring during
17897c478bd9Sstevel@tonic-gate 	 * close.  Also reset the DCD edge monitoring bit.
17907c478bd9Sstevel@tonic-gate 	 */
17917c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
17927c478bd9Sstevel@tonic-gate 	asy->asy_flags &= ~(ASY_PPS | ASY_PPS_EDGE);
17937c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	/*
17967c478bd9Sstevel@tonic-gate 	 * There are two flavors of break -- timed (M_BREAK or TCSBRK) and
17977c478bd9Sstevel@tonic-gate 	 * untimed (TIOCSBRK).  For the timed case, these are enqueued on our
17987c478bd9Sstevel@tonic-gate 	 * write queue and there's a timer running, so we don't have to worry
17997c478bd9Sstevel@tonic-gate 	 * about them.  For the untimed case, though, the user obviously made a
18007c478bd9Sstevel@tonic-gate 	 * mistake, because these are handled immediately.  We'll terminate the
18017c478bd9Sstevel@tonic-gate 	 * break now and honor his implicit request by discarding the rest of
18027c478bd9Sstevel@tonic-gate 	 * the data.
18037c478bd9Sstevel@tonic-gate 	 */
18047c478bd9Sstevel@tonic-gate 	if (async->async_flags & ASYNC_OUT_SUSPEND) {
18057c478bd9Sstevel@tonic-gate 		if (async->async_utbrktid != 0) {
18067c478bd9Sstevel@tonic-gate 			(void) untimeout(async->async_utbrktid);
18077c478bd9Sstevel@tonic-gate 			async->async_utbrktid = 0;
18087c478bd9Sstevel@tonic-gate 		}
18097c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
18104ab75253Smrj 		lcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
18114ab75253Smrj 		ddi_put8(asy->asy_iohandle,
18127c478bd9Sstevel@tonic-gate 		    asy->asy_ioaddr + LCR, (lcr & ~SETBREAK));
18137c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
18147c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_SUSPEND;
18157c478bd9Sstevel@tonic-gate 		goto nodrain;
18167c478bd9Sstevel@tonic-gate 	}
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	/*
18197c478bd9Sstevel@tonic-gate 	 * If the user told us not to delay the close ("non-blocking"), then
18207c478bd9Sstevel@tonic-gate 	 * don't bother trying to drain.
18217c478bd9Sstevel@tonic-gate 	 *
18227c478bd9Sstevel@tonic-gate 	 * If the user did M_STOP (ASYNC_STOPPED), there's no hope of ever
18237c478bd9Sstevel@tonic-gate 	 * getting an M_START (since these messages aren't enqueued), and the
18247c478bd9Sstevel@tonic-gate 	 * only other way to clear the stop condition is by loss of DCD, which
18257c478bd9Sstevel@tonic-gate 	 * would discard the queue data.  Thus, we drop the output data if
18267c478bd9Sstevel@tonic-gate 	 * ASYNC_STOPPED is set.
18277c478bd9Sstevel@tonic-gate 	 */
18287c478bd9Sstevel@tonic-gate 	if ((flag & (FNDELAY|FNONBLOCK)) ||
18297c478bd9Sstevel@tonic-gate 	    (async->async_flags & ASYNC_STOPPED)) {
18307c478bd9Sstevel@tonic-gate 		goto nodrain;
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	/*
18347c478bd9Sstevel@tonic-gate 	 * If there's any pending output, then we have to try to drain it.
18357c478bd9Sstevel@tonic-gate 	 * There are two main cases to be handled:
18367c478bd9Sstevel@tonic-gate 	 *	- called by close(2): need to drain until done or until
18377c478bd9Sstevel@tonic-gate 	 *	  a signal is received.  No timeout.
18387c478bd9Sstevel@tonic-gate 	 *	- called by exit(2): need to drain while making progress
18397c478bd9Sstevel@tonic-gate 	 *	  or until a timeout occurs.  No signals.
18407c478bd9Sstevel@tonic-gate 	 *
18417c478bd9Sstevel@tonic-gate 	 * If we can't rely on receiving a signal to get us out of a hung
18427c478bd9Sstevel@tonic-gate 	 * session, then we have to use a timer.  In this case, we set a timer
18437c478bd9Sstevel@tonic-gate 	 * to check for progress in sending the output data -- all that we ask
18447c478bd9Sstevel@tonic-gate 	 * (at each interval) is that there's been some progress made.  Since
18457c478bd9Sstevel@tonic-gate 	 * the interrupt routine grabs buffers from the write queue, we can't
18467c478bd9Sstevel@tonic-gate 	 * trust changes in async_ocnt.  Instead, we use a progress flag.
18477c478bd9Sstevel@tonic-gate 	 *
18487c478bd9Sstevel@tonic-gate 	 * Note that loss of carrier will cause the output queue to be flushed,
18497c478bd9Sstevel@tonic-gate 	 * and we'll wake up again and finish normally.
18507c478bd9Sstevel@tonic-gate 	 */
18517c478bd9Sstevel@tonic-gate 	if (!ddi_can_receive_sig() && asy_drain_check != 0) {
18527c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_PROGRESS;
18537c478bd9Sstevel@tonic-gate 		async->async_timer = timeout(async_progress_check, async,
18547c478bd9Sstevel@tonic-gate 		    drv_usectohz(asy_drain_check));
18557c478bd9Sstevel@tonic-gate 	}
18567c478bd9Sstevel@tonic-gate 	while (async->async_ocnt > 0 ||
18577c478bd9Sstevel@tonic-gate 	    async->async_ttycommon.t_writeq->q_first != NULL ||
18587c478bd9Sstevel@tonic-gate 	    (async->async_flags & (ASYNC_BUSY|ASYNC_BREAK|ASYNC_DELAY))) {
18597c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&async->async_flags_cv, &asy->asy_excl) == 0)
18607c478bd9Sstevel@tonic-gate 			break;
18617c478bd9Sstevel@tonic-gate 	}
18627c478bd9Sstevel@tonic-gate 	if (async->async_timer != 0) {
18637c478bd9Sstevel@tonic-gate 		(void) untimeout(async->async_timer);
18647c478bd9Sstevel@tonic-gate 		async->async_timer = 0;
18657c478bd9Sstevel@tonic-gate 	}
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate nodrain:
18687c478bd9Sstevel@tonic-gate 	async->async_ocnt = 0;
18697c478bd9Sstevel@tonic-gate 	if (async->async_xmitblk != NULL)
18707c478bd9Sstevel@tonic-gate 		freeb(async->async_xmitblk);
18717c478bd9Sstevel@tonic-gate 	async->async_xmitblk = NULL;
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	/*
18747c478bd9Sstevel@tonic-gate 	 * If line has HUPCL set or is incompletely opened fix up the modem
18757c478bd9Sstevel@tonic-gate 	 * lines.
18767c478bd9Sstevel@tonic-gate 	 */
18772df1fe9cSrandyf 	DEBUGCONT1(ASY_DEBUG_MODEM, "asy%dclose: next check HUPCL flag\n",
18782df1fe9cSrandyf 	    instance);
18797c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
18807c478bd9Sstevel@tonic-gate 	if ((async->async_ttycommon.t_cflag & HUPCL) ||
18817c478bd9Sstevel@tonic-gate 	    (async->async_flags & ASYNC_WOPEN)) {
18827c478bd9Sstevel@tonic-gate 		DEBUGCONT3(ASY_DEBUG_MODEM,
18837c478bd9Sstevel@tonic-gate 		    "asy%dclose: HUPCL flag = %x, ASYNC_WOPEN flag = %x\n",
18847c478bd9Sstevel@tonic-gate 		    instance,
18857c478bd9Sstevel@tonic-gate 		    async->async_ttycommon.t_cflag & HUPCL,
18867c478bd9Sstevel@tonic-gate 		    async->async_ttycommon.t_cflag & ASYNC_WOPEN);
18877c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_DTR_DELAY;
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 		/* turn off DTR, RTS but NOT interrupt to 386 */
18907c478bd9Sstevel@tonic-gate 		if (asy->asy_flags & (ASY_IGNORE_CD|ASY_RTS_DTR_OFF)) {
18917c478bd9Sstevel@tonic-gate 			DEBUGCONT3(ASY_DEBUG_MODEM,
18927c478bd9Sstevel@tonic-gate 			    "asy%dclose: ASY_IGNORE_CD flag = %x, "
18937c478bd9Sstevel@tonic-gate 			    "ASY_RTS_DTR_OFF flag = %x\n",
18947c478bd9Sstevel@tonic-gate 			    instance,
18957c478bd9Sstevel@tonic-gate 			    asy->asy_flags & ASY_IGNORE_CD,
18967c478bd9Sstevel@tonic-gate 			    asy->asy_flags & ASY_RTS_DTR_OFF);
18972df1fe9cSrandyf 
18982df1fe9cSrandyf 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
18992df1fe9cSrandyf 			    asy->asy_mcr|OUT2);
19007c478bd9Sstevel@tonic-gate 		} else {
19017c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_MODEM,
19027c478bd9Sstevel@tonic-gate 			    "asy%dclose: Dropping DTR and RTS\n", instance);
19032df1fe9cSrandyf 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
19042df1fe9cSrandyf 			    OUT2);
19057c478bd9Sstevel@tonic-gate 		}
19067c478bd9Sstevel@tonic-gate 		async->async_dtrtid =
19077c478bd9Sstevel@tonic-gate 		    timeout((void (*)())async_dtr_free,
19087c478bd9Sstevel@tonic-gate 		    (caddr_t)async, drv_usectohz(asy_min_dtr_low));
19097c478bd9Sstevel@tonic-gate 	}
19107c478bd9Sstevel@tonic-gate 	/*
19117c478bd9Sstevel@tonic-gate 	 * If nobody's using it now, turn off receiver interrupts.
19127c478bd9Sstevel@tonic-gate 	 */
19137c478bd9Sstevel@tonic-gate 	if ((async->async_flags & (ASYNC_WOPEN|ASYNC_ISOPEN)) == 0) {
19142df1fe9cSrandyf 		icr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + ICR);
19154ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR,
19167c478bd9Sstevel@tonic-gate 		    (icr & ~RIEN));
19177c478bd9Sstevel@tonic-gate 	}
19187c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
19197c478bd9Sstevel@tonic-gate out:
19207c478bd9Sstevel@tonic-gate 	ttycommon_close(&async->async_ttycommon);
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	/*
19237c478bd9Sstevel@tonic-gate 	 * Cancel outstanding "bufcall" request.
19247c478bd9Sstevel@tonic-gate 	 */
19257c478bd9Sstevel@tonic-gate 	if (async->async_wbufcid != 0) {
19267c478bd9Sstevel@tonic-gate 		unbufcall(async->async_wbufcid);
19277c478bd9Sstevel@tonic-gate 		async->async_wbufcid = 0;
19287c478bd9Sstevel@tonic-gate 	}
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	/* Note that qprocsoff can't be done until after interrupts are off */
19317c478bd9Sstevel@tonic-gate 	qprocsoff(q);
19327c478bd9Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
19337c478bd9Sstevel@tonic-gate 	async->async_ttycommon.t_readq = NULL;
19347c478bd9Sstevel@tonic-gate 	async->async_ttycommon.t_writeq = NULL;
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	/*
19377c478bd9Sstevel@tonic-gate 	 * Clear out device state, except persistant device property flags.
19387c478bd9Sstevel@tonic-gate 	 */
19397c478bd9Sstevel@tonic-gate 	async->async_flags &= (ASYNC_DTR_DELAY|ASY_RTS_DTR_OFF);
19407c478bd9Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
19417c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_CLOSE, "asy%dclose: done\n", instance);
19447c478bd9Sstevel@tonic-gate 	return (0);
19457c478bd9Sstevel@tonic-gate }
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate static boolean_t
19487c478bd9Sstevel@tonic-gate asy_isbusy(struct asycom *asy)
19497c478bd9Sstevel@tonic-gate {
19507c478bd9Sstevel@tonic-gate 	struct asyncline *async;
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_EOT, "asy_isbusy\n");
19537c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
19547c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
19557c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
19562df1fe9cSrandyf /*
19572df1fe9cSrandyf  * XXXX this should be recoded
19582df1fe9cSrandyf  */
19597c478bd9Sstevel@tonic-gate 	return ((async->async_ocnt > 0) ||
19604ab75253Smrj 	    ((ddi_get8(asy->asy_iohandle,
19617c478bd9Sstevel@tonic-gate 	    asy->asy_ioaddr + LSR) & (XSRE|XHRE)) == 0));
19627c478bd9Sstevel@tonic-gate }
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate static void
19657c478bd9Sstevel@tonic-gate asy_waiteot(struct asycom *asy)
19667c478bd9Sstevel@tonic-gate {
19677c478bd9Sstevel@tonic-gate 	/*
19687c478bd9Sstevel@tonic-gate 	 * Wait for the current transmission block and the
19697c478bd9Sstevel@tonic-gate 	 * current fifo data to transmit. Once this is done
19707c478bd9Sstevel@tonic-gate 	 * we may go on.
19717c478bd9Sstevel@tonic-gate 	 */
19727c478bd9Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_EOT, "asy_waiteot\n");
19737c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
19747c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
19757c478bd9Sstevel@tonic-gate 	while (asy_isbusy(asy)) {
19767c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
19777c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
19787c478bd9Sstevel@tonic-gate 		drv_usecwait(10000);		/* wait .01 */
19797c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
19807c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
19817c478bd9Sstevel@tonic-gate 	}
19827c478bd9Sstevel@tonic-gate }
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate /* asy_reset_fifo -- flush fifos and [re]program fifo control register */
19857c478bd9Sstevel@tonic-gate static void
19867c478bd9Sstevel@tonic-gate asy_reset_fifo(struct asycom *asy, uchar_t flush)
19877c478bd9Sstevel@tonic-gate {
19887c478bd9Sstevel@tonic-gate 	uchar_t lcr;
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	/* On a 16750, we have to set DLAB in order to set FIFOEXTRA. */
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	if (asy->asy_hwtype >= ASY16750) {
19934ab75253Smrj 		lcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
19944ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
19957c478bd9Sstevel@tonic-gate 		    lcr | DLAB);
19967c478bd9Sstevel@tonic-gate 	}
19977c478bd9Sstevel@tonic-gate 
19984ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + FIFOR,
19997c478bd9Sstevel@tonic-gate 	    asy->asy_fifor | flush);
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 	/* Clear DLAB */
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	if (asy->asy_hwtype >= ASY16750) {
20044ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, lcr);
20057c478bd9Sstevel@tonic-gate 	}
20067c478bd9Sstevel@tonic-gate }
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate /*
20097c478bd9Sstevel@tonic-gate  * Program the ASY port. Most of the async operation is based on the values
20107c478bd9Sstevel@tonic-gate  * of 'c_iflag' and 'c_cflag'.
20117c478bd9Sstevel@tonic-gate  */
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate #define	BAUDINDEX(cflg)	(((cflg) & CBAUDEXT) ? \
20147c478bd9Sstevel@tonic-gate 			(((cflg) & CBAUD) + CBAUD + 1) : ((cflg) & CBAUD))
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate static void
20177c478bd9Sstevel@tonic-gate asy_program(struct asycom *asy, int mode)
20187c478bd9Sstevel@tonic-gate {
20197c478bd9Sstevel@tonic-gate 	struct asyncline *async;
20207c478bd9Sstevel@tonic-gate 	int baudrate, c_flag;
20217c478bd9Sstevel@tonic-gate 	int icr, lcr;
20227c478bd9Sstevel@tonic-gate 	int flush_reg;
20237c478bd9Sstevel@tonic-gate 	int ocflags;
20247c478bd9Sstevel@tonic-gate #ifdef DEBUG
20257c478bd9Sstevel@tonic-gate 	int instance;
20267c478bd9Sstevel@tonic-gate #endif
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
20297c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
20327c478bd9Sstevel@tonic-gate #ifdef DEBUG
20337c478bd9Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
20347c478bd9Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_PROCS,
20357c478bd9Sstevel@tonic-gate 	    "asy%d_program: mode = 0x%08X, enter\n", instance, mode);
20367c478bd9Sstevel@tonic-gate #endif
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	baudrate = BAUDINDEX(async->async_ttycommon.t_cflag);
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	async->async_ttycommon.t_cflag &= ~(CIBAUD);
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	if (baudrate > CBAUD) {
20437c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_cflag |= CIBAUDEXT;
20447c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_cflag |=
20457c478bd9Sstevel@tonic-gate 		    (((baudrate - CBAUD - 1) << IBSHIFT) & CIBAUD);
20467c478bd9Sstevel@tonic-gate 	} else {
20477c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_cflag &= ~CIBAUDEXT;
20487c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_cflag |=
20497c478bd9Sstevel@tonic-gate 		    ((baudrate << IBSHIFT) & CIBAUD);
20507c478bd9Sstevel@tonic-gate 	}
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	c_flag = async->async_ttycommon.t_cflag &
20537c478bd9Sstevel@tonic-gate 	    (CLOCAL|CREAD|CSTOPB|CSIZE|PARENB|PARODD|CBAUD|CBAUDEXT);
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	/* disable interrupts */
20564ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, 0);
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	ocflags = asy->asy_ocflag;
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	/* flush/reset the status registers */
20614ab75253Smrj 	(void) ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + ISR);
20624ab75253Smrj 	(void) ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR);
20634ab75253Smrj 	asy->asy_msr = flush_reg = ddi_get8(asy->asy_iohandle,
20647c478bd9Sstevel@tonic-gate 	    asy->asy_ioaddr + MSR);
20657c478bd9Sstevel@tonic-gate 	/*
20667c478bd9Sstevel@tonic-gate 	 * The device is programmed in the open sequence, if we
20677c478bd9Sstevel@tonic-gate 	 * have to hardware handshake, then this is a good time
20687c478bd9Sstevel@tonic-gate 	 * to check if the device can receive any data.
20697c478bd9Sstevel@tonic-gate 	 */
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	if ((CRTSCTS & async->async_ttycommon.t_cflag) && !(flush_reg & CTS)) {
20727c478bd9Sstevel@tonic-gate 		async_flowcontrol_hw_output(asy, FLOW_STOP);
20737c478bd9Sstevel@tonic-gate 	} else {
20747c478bd9Sstevel@tonic-gate 		/*
20757c478bd9Sstevel@tonic-gate 		 * We can not use async_flowcontrol_hw_output(asy, FLOW_START)
20767c478bd9Sstevel@tonic-gate 		 * here, because if CRTSCTS is clear, we need clear
20777c478bd9Sstevel@tonic-gate 		 * ASYNC_HW_OUT_FLW bit.
20787c478bd9Sstevel@tonic-gate 		 */
20797c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_HW_OUT_FLW;
20807c478bd9Sstevel@tonic-gate 	}
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	/*
20837c478bd9Sstevel@tonic-gate 	 * If IXON is not set, clear ASYNC_SW_OUT_FLW;
20847c478bd9Sstevel@tonic-gate 	 * If IXON is set, no matter what IXON flag is before this
20857c478bd9Sstevel@tonic-gate 	 * function call to asy_program,
20867c478bd9Sstevel@tonic-gate 	 * we will use the old ASYNC_SW_OUT_FLW status.
20877c478bd9Sstevel@tonic-gate 	 * Because of handling IXON in the driver, we also should re-calculate
20887c478bd9Sstevel@tonic-gate 	 * the value of ASYNC_OUT_FLW_RESUME bit, but in fact,
20897c478bd9Sstevel@tonic-gate 	 * the TCSET* commands which call asy_program
20907c478bd9Sstevel@tonic-gate 	 * are put into the write queue, so there is no output needed to
20917c478bd9Sstevel@tonic-gate 	 * be resumed at this point.
20927c478bd9Sstevel@tonic-gate 	 */
20937c478bd9Sstevel@tonic-gate 	if (!(IXON & async->async_ttycommon.t_iflag))
20947c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_SW_OUT_FLW;
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	/* manually flush receive buffer or fifo (workaround for buggy fifos) */
20977c478bd9Sstevel@tonic-gate 	if (mode == ASY_INIT)
20987c478bd9Sstevel@tonic-gate 		if (asy->asy_use_fifo == FIFO_ON) {
20997c478bd9Sstevel@tonic-gate 			for (flush_reg = asy->asy_fifo_buf; flush_reg-- > 0; ) {
21004ab75253Smrj 				(void) ddi_get8(asy->asy_iohandle,
21017c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + DAT);
21027c478bd9Sstevel@tonic-gate 			}
21037c478bd9Sstevel@tonic-gate 		} else {
21044ab75253Smrj 			flush_reg = ddi_get8(asy->asy_iohandle,
21057c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT);
21067c478bd9Sstevel@tonic-gate 		}
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	if (ocflags != (c_flag & ~CLOCAL) || mode == ASY_INIT) {
21097c478bd9Sstevel@tonic-gate 		/* Set line control */
21102df1fe9cSrandyf 		lcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
21117c478bd9Sstevel@tonic-gate 		lcr &= ~(WLS0|WLS1|STB|PEN|EPS);
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 		if (c_flag & CSTOPB)
21147c478bd9Sstevel@tonic-gate 			lcr |= STB;	/* 2 stop bits */
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 		if (c_flag & PARENB)
21177c478bd9Sstevel@tonic-gate 			lcr |= PEN;
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 		if ((c_flag & PARODD) == 0)
21207c478bd9Sstevel@tonic-gate 			lcr |= EPS;
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 		switch (c_flag & CSIZE) {
21237c478bd9Sstevel@tonic-gate 		case CS5:
21247c478bd9Sstevel@tonic-gate 			lcr |= BITS5;
21257c478bd9Sstevel@tonic-gate 			break;
21267c478bd9Sstevel@tonic-gate 		case CS6:
21277c478bd9Sstevel@tonic-gate 			lcr |= BITS6;
21287c478bd9Sstevel@tonic-gate 			break;
21297c478bd9Sstevel@tonic-gate 		case CS7:
21307c478bd9Sstevel@tonic-gate 			lcr |= BITS7;
21317c478bd9Sstevel@tonic-gate 			break;
21327c478bd9Sstevel@tonic-gate 		case CS8:
21337c478bd9Sstevel@tonic-gate 			lcr |= BITS8;
21347c478bd9Sstevel@tonic-gate 			break;
21357c478bd9Sstevel@tonic-gate 		}
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 		/* set the baud rate, unless it is "0" */
21382df1fe9cSrandyf 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, DLAB);
21392df1fe9cSrandyf 
21407c478bd9Sstevel@tonic-gate 		if (baudrate != 0) {
21414ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
21427c478bd9Sstevel@tonic-gate 			    asyspdtab[baudrate] & 0xff);
21434ab75253Smrj 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR,
21447c478bd9Sstevel@tonic-gate 			    (asyspdtab[baudrate] >> 8) & 0xff);
21457c478bd9Sstevel@tonic-gate 		}
21467c478bd9Sstevel@tonic-gate 		/* set the line control modes */
21474ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR, lcr);
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 		/*
21507c478bd9Sstevel@tonic-gate 		 * If we have a FIFO buffer, enable/flush
21517c478bd9Sstevel@tonic-gate 		 * at intialize time, flush if transitioning from
21527c478bd9Sstevel@tonic-gate 		 * CREAD off to CREAD on.
21537c478bd9Sstevel@tonic-gate 		 */
21547c478bd9Sstevel@tonic-gate 		if ((ocflags & CREAD) == 0 && (c_flag & CREAD) ||
21557c478bd9Sstevel@tonic-gate 		    mode == ASY_INIT)
21567c478bd9Sstevel@tonic-gate 			if (asy->asy_use_fifo == FIFO_ON)
21577c478bd9Sstevel@tonic-gate 				asy_reset_fifo(asy, FIFORXFLSH);
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 		/* remember the new cflags */
21607c478bd9Sstevel@tonic-gate 		asy->asy_ocflag = c_flag & ~CLOCAL;
21617c478bd9Sstevel@tonic-gate 	}
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	if (baudrate == 0)
21644ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
21657c478bd9Sstevel@tonic-gate 		    (asy->asy_mcr & RTS) | OUT2);
21667c478bd9Sstevel@tonic-gate 	else
21674ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR,
21687c478bd9Sstevel@tonic-gate 		    asy->asy_mcr | OUT2);
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	/*
21717c478bd9Sstevel@tonic-gate 	 * Call the modem status interrupt handler to check for the carrier
21727c478bd9Sstevel@tonic-gate 	 * in case CLOCAL was turned off after the carrier came on.
21737c478bd9Sstevel@tonic-gate 	 * (Note: Modem status interrupt is not enabled if CLOCAL is ON.)
21747c478bd9Sstevel@tonic-gate 	 */
21757c478bd9Sstevel@tonic-gate 	async_msint(asy);
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	/* Set interrupt control */
21787c478bd9Sstevel@tonic-gate 	DEBUGCONT3(ASY_DEBUG_MODM2,
21797c478bd9Sstevel@tonic-gate 	    "asy%d_program: c_flag & CLOCAL = %x t_cflag & CRTSCTS = %x\n",
21802df1fe9cSrandyf 	    instance, c_flag & CLOCAL,
21817c478bd9Sstevel@tonic-gate 	    async->async_ttycommon.t_cflag & CRTSCTS);
21822df1fe9cSrandyf 
21837c478bd9Sstevel@tonic-gate 	if ((c_flag & CLOCAL) && !(async->async_ttycommon.t_cflag & CRTSCTS))
21847c478bd9Sstevel@tonic-gate 		/*
21857c478bd9Sstevel@tonic-gate 		 * direct-wired line ignores DCD, so we don't enable modem
21867c478bd9Sstevel@tonic-gate 		 * status interrupts.
21877c478bd9Sstevel@tonic-gate 		 */
21887c478bd9Sstevel@tonic-gate 		icr = (TIEN | SIEN);
21897c478bd9Sstevel@tonic-gate 	else
21907c478bd9Sstevel@tonic-gate 		icr = (TIEN | SIEN | MIEN);
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	if (c_flag & CREAD)
21937c478bd9Sstevel@tonic-gate 		icr |= RIEN;
21947c478bd9Sstevel@tonic-gate 
21954ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + ICR, icr);
21967c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "asy%d_program: done\n", instance);
21977c478bd9Sstevel@tonic-gate }
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate static boolean_t
22007c478bd9Sstevel@tonic-gate asy_baudok(struct asycom *asy)
22017c478bd9Sstevel@tonic-gate {
22027c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
22037c478bd9Sstevel@tonic-gate 	int baudrate;
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	baudrate = BAUDINDEX(async->async_ttycommon.t_cflag);
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	if (baudrate >= sizeof (asyspdtab)/sizeof (*asyspdtab))
22097c478bd9Sstevel@tonic-gate 		return (0);
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 	return (baudrate == 0 || asyspdtab[baudrate]);
22127c478bd9Sstevel@tonic-gate }
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate /*
22157c478bd9Sstevel@tonic-gate  * asyintr() is the High Level Interrupt Handler.
22167c478bd9Sstevel@tonic-gate  *
22177c478bd9Sstevel@tonic-gate  * There are four different interrupt types indexed by ISR register values:
22187c478bd9Sstevel@tonic-gate  *		0: modem
22197c478bd9Sstevel@tonic-gate  *		1: Tx holding register is empty, ready for next char
22207c478bd9Sstevel@tonic-gate  *		2: Rx register now holds a char to be picked up
22217c478bd9Sstevel@tonic-gate  *		3: error or break on line
22227c478bd9Sstevel@tonic-gate  * This routine checks the Bit 0 (interrupt-not-pending) to determine if
22237c478bd9Sstevel@tonic-gate  * the interrupt is from this port.
22247c478bd9Sstevel@tonic-gate  */
22257c478bd9Sstevel@tonic-gate uint_t
22267c478bd9Sstevel@tonic-gate asyintr(caddr_t argasy)
22277c478bd9Sstevel@tonic-gate {
22287c478bd9Sstevel@tonic-gate 	struct asycom		*asy = (struct asycom *)argasy;
22297c478bd9Sstevel@tonic-gate 	struct asyncline	*async;
22307c478bd9Sstevel@tonic-gate 	int			ret_status = DDI_INTR_UNCLAIMED;
22317c478bd9Sstevel@tonic-gate 	uchar_t			interrupt_id, lsr;
22327c478bd9Sstevel@tonic-gate 
22334ab75253Smrj 	interrupt_id = ddi_get8(asy->asy_iohandle,
22347c478bd9Sstevel@tonic-gate 	    asy->asy_ioaddr + ISR) & 0x0F;
22357c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
22362df1fe9cSrandyf 
22377c478bd9Sstevel@tonic-gate 	if ((async == NULL) || asy_addedsoft == 0 ||
22387c478bd9Sstevel@tonic-gate 	    !(async->async_flags & (ASYNC_ISOPEN|ASYNC_WOPEN))) {
22397c478bd9Sstevel@tonic-gate 		if (interrupt_id & NOINTERRUPT)
22407c478bd9Sstevel@tonic-gate 			return (DDI_INTR_UNCLAIMED);
22417c478bd9Sstevel@tonic-gate 		else {
22427c478bd9Sstevel@tonic-gate 			/*
22437c478bd9Sstevel@tonic-gate 			 * reset the device by:
22447c478bd9Sstevel@tonic-gate 			 *	reading line status
22457c478bd9Sstevel@tonic-gate 			 *	reading any data from data status register
22467c478bd9Sstevel@tonic-gate 			 *	reading modem status
22477c478bd9Sstevel@tonic-gate 			 */
22484ab75253Smrj 			(void) ddi_get8(asy->asy_iohandle,
22497c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + LSR);
22504ab75253Smrj 			(void) ddi_get8(asy->asy_iohandle,
22517c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT);
22524ab75253Smrj 			asy->asy_msr = ddi_get8(asy->asy_iohandle,
22537c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + MSR);
22547c478bd9Sstevel@tonic-gate 			return (DDI_INTR_CLAIMED);
22557c478bd9Sstevel@tonic-gate 		}
22567c478bd9Sstevel@tonic-gate 	}
22572df1fe9cSrandyf 
22587c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
22597c478bd9Sstevel@tonic-gate 	/*
22607c478bd9Sstevel@tonic-gate 	 * We will loop until the interrupt line is pulled low. asy
22617c478bd9Sstevel@tonic-gate 	 * interrupt is edge triggered.
22627c478bd9Sstevel@tonic-gate 	 */
22637c478bd9Sstevel@tonic-gate 	/* CSTYLED */
22642df1fe9cSrandyf 	for (;; interrupt_id =
22652df1fe9cSrandyf 	    (ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + ISR) & 0x0F)) {
22662df1fe9cSrandyf 
22677c478bd9Sstevel@tonic-gate 		if (interrupt_id & NOINTERRUPT)
22687c478bd9Sstevel@tonic-gate 			break;
22697c478bd9Sstevel@tonic-gate 		ret_status = DDI_INTR_CLAIMED;
22707c478bd9Sstevel@tonic-gate 
22712df1fe9cSrandyf 		DEBUGCONT1(ASY_DEBUG_INTR, "asyintr: interrupt_id = 0x%d\n",
22722df1fe9cSrandyf 		    interrupt_id);
22732df1fe9cSrandyf 		lsr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR);
22747c478bd9Sstevel@tonic-gate 		switch (interrupt_id) {
22757c478bd9Sstevel@tonic-gate 		case RxRDY:
22767c478bd9Sstevel@tonic-gate 		case RSTATUS:
22777c478bd9Sstevel@tonic-gate 		case FFTMOUT:
22787c478bd9Sstevel@tonic-gate 			/* receiver interrupt or receiver errors */
22797c478bd9Sstevel@tonic-gate 			async_rxint(asy, lsr);
22807c478bd9Sstevel@tonic-gate 			break;
22817c478bd9Sstevel@tonic-gate 		case TxRDY:
22827c478bd9Sstevel@tonic-gate 			/* transmit interrupt */
22837c478bd9Sstevel@tonic-gate 			async_txint(asy);
22847c478bd9Sstevel@tonic-gate 			continue;
22857c478bd9Sstevel@tonic-gate 		case MSTATUS:
22867c478bd9Sstevel@tonic-gate 			/* modem status interrupt */
22877c478bd9Sstevel@tonic-gate 			async_msint(asy);
22887c478bd9Sstevel@tonic-gate 			break;
22897c478bd9Sstevel@tonic-gate 		}
22907c478bd9Sstevel@tonic-gate 		if ((lsr & XHRE) && (async->async_flags & ASYNC_BUSY) &&
22917c478bd9Sstevel@tonic-gate 		    (async->async_ocnt > 0))
22927c478bd9Sstevel@tonic-gate 			async_txint(asy);
22937c478bd9Sstevel@tonic-gate 	}
22947c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
22957c478bd9Sstevel@tonic-gate 	return (ret_status);
22967c478bd9Sstevel@tonic-gate }
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate /*
22997c478bd9Sstevel@tonic-gate  * Transmitter interrupt service routine.
23007c478bd9Sstevel@tonic-gate  * If there is more data to transmit in the current pseudo-DMA block,
23017c478bd9Sstevel@tonic-gate  * send the next character if output is not stopped or draining.
23027c478bd9Sstevel@tonic-gate  * Otherwise, queue up a soft interrupt.
23037c478bd9Sstevel@tonic-gate  *
23047c478bd9Sstevel@tonic-gate  * XXX -  Needs review for HW FIFOs.
23057c478bd9Sstevel@tonic-gate  */
23067c478bd9Sstevel@tonic-gate static void
23077c478bd9Sstevel@tonic-gate async_txint(struct asycom *asy)
23087c478bd9Sstevel@tonic-gate {
23097c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
23107c478bd9Sstevel@tonic-gate 	int		fifo_len;
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	/*
23137c478bd9Sstevel@tonic-gate 	 * If ASYNC_BREAK or ASYNC_OUT_SUSPEND has been set, return to
23147c478bd9Sstevel@tonic-gate 	 * asyintr()'s context to claim the interrupt without performing
23157c478bd9Sstevel@tonic-gate 	 * any action. No character will be loaded into FIFO/THR until
23167c478bd9Sstevel@tonic-gate 	 * timed or untimed break is removed
23177c478bd9Sstevel@tonic-gate 	 */
23187c478bd9Sstevel@tonic-gate 	if (async->async_flags & (ASYNC_BREAK|ASYNC_OUT_SUSPEND))
23197c478bd9Sstevel@tonic-gate 		return;
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 	fifo_len = asy->asy_fifo_buf; /* with FIFO buffers */
23227c478bd9Sstevel@tonic-gate 	if (fifo_len > asy_max_tx_fifo)
23237c478bd9Sstevel@tonic-gate 		fifo_len = asy_max_tx_fifo;
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
23267c478bd9Sstevel@tonic-gate 		fifo_len--;
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 	if (async->async_ocnt > 0 && fifo_len > 0 &&
23297c478bd9Sstevel@tonic-gate 	    !(async->async_flags &
23307c478bd9Sstevel@tonic-gate 	    (ASYNC_HW_OUT_FLW|ASYNC_SW_OUT_FLW|ASYNC_STOPPED))) {
23317c478bd9Sstevel@tonic-gate 		while (fifo_len-- > 0 && async->async_ocnt-- > 0) {
23324ab75253Smrj 			ddi_put8(asy->asy_iohandle,
23337c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT, *async->async_optr++);
23347c478bd9Sstevel@tonic-gate 		}
23357c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_PROGRESS;
23367c478bd9Sstevel@tonic-gate 	}
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 	if (fifo_len <= 0)
23397c478bd9Sstevel@tonic-gate 		return;
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 	ASYSETSOFT(asy);
23427c478bd9Sstevel@tonic-gate }
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate /*
23457c478bd9Sstevel@tonic-gate  * Interrupt on port: handle PPS event.  This function is only called
23467c478bd9Sstevel@tonic-gate  * for a port on which PPS event handling has been enabled.
23477c478bd9Sstevel@tonic-gate  */
23487c478bd9Sstevel@tonic-gate static void
23497c478bd9Sstevel@tonic-gate asy_ppsevent(struct asycom *asy, int msr)
23507c478bd9Sstevel@tonic-gate {
23517c478bd9Sstevel@tonic-gate 	if (asy->asy_flags & ASY_PPS_EDGE) {
23527c478bd9Sstevel@tonic-gate 		/* Have seen leading edge, now look for and record drop */
23537c478bd9Sstevel@tonic-gate 		if ((msr & DCD) == 0)
23547c478bd9Sstevel@tonic-gate 			asy->asy_flags &= ~ASY_PPS_EDGE;
23557c478bd9Sstevel@tonic-gate 		/*
23567c478bd9Sstevel@tonic-gate 		 * Waiting for leading edge, look for rise; stamp event and
23577c478bd9Sstevel@tonic-gate 		 * calibrate kernel clock.
23587c478bd9Sstevel@tonic-gate 		 */
23597c478bd9Sstevel@tonic-gate 	} else if (msr & DCD) {
23607c478bd9Sstevel@tonic-gate 			/*
23617c478bd9Sstevel@tonic-gate 			 * This code captures a timestamp at the designated
23627c478bd9Sstevel@tonic-gate 			 * transition of the PPS signal (DCD asserted).  The
23637c478bd9Sstevel@tonic-gate 			 * code provides a pointer to the timestamp, as well
23647c478bd9Sstevel@tonic-gate 			 * as the hardware counter value at the capture.
23657c478bd9Sstevel@tonic-gate 			 *
23667c478bd9Sstevel@tonic-gate 			 * Note: the kernel has nano based time values while
23677c478bd9Sstevel@tonic-gate 			 * NTP requires micro based, an in-line fast algorithm
23687c478bd9Sstevel@tonic-gate 			 * to convert nsec to usec is used here -- see hrt2ts()
23697c478bd9Sstevel@tonic-gate 			 * in common/os/timers.c for a full description.
23707c478bd9Sstevel@tonic-gate 			 */
23717c478bd9Sstevel@tonic-gate 			struct timeval *tvp = &asy_ppsev.tv;
23727c478bd9Sstevel@tonic-gate 			timestruc_t ts;
23737c478bd9Sstevel@tonic-gate 			long nsec, usec;
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 			asy->asy_flags |= ASY_PPS_EDGE;
23767c478bd9Sstevel@tonic-gate 			LED_OFF;
23777c478bd9Sstevel@tonic-gate 			gethrestime(&ts);
23787c478bd9Sstevel@tonic-gate 			LED_ON;
23797c478bd9Sstevel@tonic-gate 			nsec = ts.tv_nsec;
23807c478bd9Sstevel@tonic-gate 			usec = nsec + (nsec >> 2);
23817c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 1);
23827c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 2);
23837c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 4);
23847c478bd9Sstevel@tonic-gate 			usec = nsec - (usec >> 3);
23857c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 2);
23867c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 3);
23877c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 4);
23887c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 1);
23897c478bd9Sstevel@tonic-gate 			usec = nsec + (usec >> 6);
23907c478bd9Sstevel@tonic-gate 			tvp->tv_usec = usec >> 10;
23917c478bd9Sstevel@tonic-gate 			tvp->tv_sec = ts.tv_sec;
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 			++asy_ppsev.serial;
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 			/*
23967c478bd9Sstevel@tonic-gate 			 * Because the kernel keeps a high-resolution time,
23977c478bd9Sstevel@tonic-gate 			 * pass the current highres timestamp in tvp and zero
23987c478bd9Sstevel@tonic-gate 			 * in usec.
23997c478bd9Sstevel@tonic-gate 			 */
24007c478bd9Sstevel@tonic-gate 			ddi_hardpps(tvp, 0);
24017c478bd9Sstevel@tonic-gate 	}
24027c478bd9Sstevel@tonic-gate }
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate /*
24057c478bd9Sstevel@tonic-gate  * Receiver interrupt: RxRDY interrupt, FIFO timeout interrupt or receive
24067c478bd9Sstevel@tonic-gate  * error interrupt.
24077c478bd9Sstevel@tonic-gate  * Try to put the character into the circular buffer for this line; if it
24087c478bd9Sstevel@tonic-gate  * overflows, indicate a circular buffer overrun. If this port is always
24097c478bd9Sstevel@tonic-gate  * to be serviced immediately, or the character is a STOP character, or
24107c478bd9Sstevel@tonic-gate  * more than 15 characters have arrived, queue up a soft interrupt to
24117c478bd9Sstevel@tonic-gate  * drain the circular buffer.
24127c478bd9Sstevel@tonic-gate  * XXX - needs review for hw FIFOs support.
24137c478bd9Sstevel@tonic-gate  */
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate static void
24167c478bd9Sstevel@tonic-gate async_rxint(struct asycom *asy, uchar_t lsr)
24177c478bd9Sstevel@tonic-gate {
24187c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
24197c478bd9Sstevel@tonic-gate 	uchar_t c;
24207c478bd9Sstevel@tonic-gate 	uint_t s, needsoft = 0;
24217c478bd9Sstevel@tonic-gate 	tty_common_t *tp;
24227c478bd9Sstevel@tonic-gate 	int looplim = asy->asy_fifo_buf * 2;
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	tp = &async->async_ttycommon;
24257c478bd9Sstevel@tonic-gate 	if (!(tp->t_cflag & CREAD)) {
24267c478bd9Sstevel@tonic-gate 		while (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
24274ab75253Smrj 			(void) (ddi_get8(asy->asy_iohandle,
24287c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT) & 0xff);
24294ab75253Smrj 			lsr = ddi_get8(asy->asy_iohandle,
24307c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + LSR);
24317c478bd9Sstevel@tonic-gate 			if (looplim-- < 0)		/* limit loop */
24327c478bd9Sstevel@tonic-gate 				break;
24337c478bd9Sstevel@tonic-gate 		}
24347c478bd9Sstevel@tonic-gate 		return; /* line is not open for read? */
24357c478bd9Sstevel@tonic-gate 	}
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	while (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
24387c478bd9Sstevel@tonic-gate 		c = 0;
24397c478bd9Sstevel@tonic-gate 		s = 0;				/* reset error status */
24407c478bd9Sstevel@tonic-gate 		if (lsr & RCA) {
24414ab75253Smrj 			c = ddi_get8(asy->asy_iohandle,
24427c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT) & 0xff;
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 			/*
24457c478bd9Sstevel@tonic-gate 			 * We handle XON/XOFF char if IXON is set,
24467c478bd9Sstevel@tonic-gate 			 * but if received char is _POSIX_VDISABLE,
24477c478bd9Sstevel@tonic-gate 			 * we left it to the up level module.
24487c478bd9Sstevel@tonic-gate 			 */
24497c478bd9Sstevel@tonic-gate 			if (tp->t_iflag & IXON) {
24507c478bd9Sstevel@tonic-gate 				if ((c == async->async_stopc) &&
24517c478bd9Sstevel@tonic-gate 				    (c != _POSIX_VDISABLE)) {
24527c478bd9Sstevel@tonic-gate 					async_flowcontrol_sw_output(asy,
24537c478bd9Sstevel@tonic-gate 					    FLOW_STOP);
24547c478bd9Sstevel@tonic-gate 					goto check_looplim;
24557c478bd9Sstevel@tonic-gate 				} else if ((c == async->async_startc) &&
24567c478bd9Sstevel@tonic-gate 				    (c != _POSIX_VDISABLE)) {
24577c478bd9Sstevel@tonic-gate 					async_flowcontrol_sw_output(asy,
24587c478bd9Sstevel@tonic-gate 					    FLOW_START);
24597c478bd9Sstevel@tonic-gate 					needsoft = 1;
24607c478bd9Sstevel@tonic-gate 					goto check_looplim;
24617c478bd9Sstevel@tonic-gate 				}
24627c478bd9Sstevel@tonic-gate 				if ((tp->t_iflag & IXANY) &&
24637c478bd9Sstevel@tonic-gate 				    (async->async_flags & ASYNC_SW_OUT_FLW)) {
24647c478bd9Sstevel@tonic-gate 					async_flowcontrol_sw_output(asy,
24657c478bd9Sstevel@tonic-gate 					    FLOW_START);
24667c478bd9Sstevel@tonic-gate 					needsoft = 1;
24677c478bd9Sstevel@tonic-gate 				}
24687c478bd9Sstevel@tonic-gate 			}
24697c478bd9Sstevel@tonic-gate 		}
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate 		/*
24727c478bd9Sstevel@tonic-gate 		 * Check for character break sequence
24737c478bd9Sstevel@tonic-gate 		 */
24747c478bd9Sstevel@tonic-gate 		if ((abort_enable == KIOCABORTALTERNATE) &&
24757c478bd9Sstevel@tonic-gate 		    (asy->asy_flags & ASY_CONSOLE)) {
24767c478bd9Sstevel@tonic-gate 			if (abort_charseq_recognize(c))
24777c478bd9Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
24787c478bd9Sstevel@tonic-gate 		}
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 		/* Handle framing errors */
24817c478bd9Sstevel@tonic-gate 		if (lsr & (PARERR|FRMERR|BRKDET|OVRRUN)) {
24827c478bd9Sstevel@tonic-gate 			if (lsr & PARERR) {
24837c478bd9Sstevel@tonic-gate 				if (tp->t_iflag & INPCK) /* parity enabled */
24847c478bd9Sstevel@tonic-gate 					s |= PERROR;
24857c478bd9Sstevel@tonic-gate 			}
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 			if (lsr & (FRMERR|BRKDET))
24887c478bd9Sstevel@tonic-gate 				s |= FRERROR;
24897c478bd9Sstevel@tonic-gate 			if (lsr & OVRRUN) {
24907c478bd9Sstevel@tonic-gate 				async->async_hw_overrun = 1;
24917c478bd9Sstevel@tonic-gate 				s |= OVERRUN;
24927c478bd9Sstevel@tonic-gate 			}
24937c478bd9Sstevel@tonic-gate 		}
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 		if (s == 0)
24967c478bd9Sstevel@tonic-gate 			if ((tp->t_iflag & PARMRK) &&
24977c478bd9Sstevel@tonic-gate 			    !(tp->t_iflag & (IGNPAR|ISTRIP)) &&
24987c478bd9Sstevel@tonic-gate 			    (c == 0377))
24997c478bd9Sstevel@tonic-gate 				if (RING_POK(async, 2)) {
25007c478bd9Sstevel@tonic-gate 					RING_PUT(async, 0377);
25017c478bd9Sstevel@tonic-gate 					RING_PUT(async, c);
25027c478bd9Sstevel@tonic-gate 				} else
25037c478bd9Sstevel@tonic-gate 					async->async_sw_overrun = 1;
25047c478bd9Sstevel@tonic-gate 			else
25057c478bd9Sstevel@tonic-gate 				if (RING_POK(async, 1))
25067c478bd9Sstevel@tonic-gate 					RING_PUT(async, c);
25077c478bd9Sstevel@tonic-gate 				else
25087c478bd9Sstevel@tonic-gate 					async->async_sw_overrun = 1;
25097c478bd9Sstevel@tonic-gate 		else
25107c478bd9Sstevel@tonic-gate 			if (s & FRERROR) /* Handle framing errors */
25117c478bd9Sstevel@tonic-gate 				if (c == 0)
25127c478bd9Sstevel@tonic-gate 					if ((asy->asy_flags & ASY_CONSOLE) &&
25137c478bd9Sstevel@tonic-gate 					    (abort_enable !=
25147c478bd9Sstevel@tonic-gate 					    KIOCABORTALTERNATE))
25157c478bd9Sstevel@tonic-gate 						abort_sequence_enter((char *)0);
25167c478bd9Sstevel@tonic-gate 					else
25177c478bd9Sstevel@tonic-gate 						async->async_break++;
25187c478bd9Sstevel@tonic-gate 				else
25197c478bd9Sstevel@tonic-gate 					if (RING_POK(async, 1))
25207c478bd9Sstevel@tonic-gate 						RING_MARK(async, c, s);
25217c478bd9Sstevel@tonic-gate 					else
25227c478bd9Sstevel@tonic-gate 						async->async_sw_overrun = 1;
25237c478bd9Sstevel@tonic-gate 			else /* Parity errors are handled by ldterm */
25247c478bd9Sstevel@tonic-gate 				if (RING_POK(async, 1))
25257c478bd9Sstevel@tonic-gate 					RING_MARK(async, c, s);
25267c478bd9Sstevel@tonic-gate 				else
25277c478bd9Sstevel@tonic-gate 					async->async_sw_overrun = 1;
25287c478bd9Sstevel@tonic-gate check_looplim:
25292df1fe9cSrandyf 		lsr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR);
25307c478bd9Sstevel@tonic-gate 		if (looplim-- < 0)		/* limit loop */
25317c478bd9Sstevel@tonic-gate 			break;
25327c478bd9Sstevel@tonic-gate 	}
25337c478bd9Sstevel@tonic-gate 	if ((RING_CNT(async) > (RINGSIZE * 3)/4) &&
25347c478bd9Sstevel@tonic-gate 	    !(async->async_inflow_source & IN_FLOW_RINGBUFF)) {
25357c478bd9Sstevel@tonic-gate 		async_flowcontrol_hw_input(asy, FLOW_STOP, IN_FLOW_RINGBUFF);
25367c478bd9Sstevel@tonic-gate 		(void) async_flowcontrol_sw_input(asy, FLOW_STOP,
25377c478bd9Sstevel@tonic-gate 		    IN_FLOW_RINGBUFF);
25387c478bd9Sstevel@tonic-gate 	}
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate 	if ((async->async_flags & ASYNC_SERVICEIMM) || needsoft ||
25417c478bd9Sstevel@tonic-gate 	    (RING_FRAC(async)) || (async->async_polltid == 0))
25427c478bd9Sstevel@tonic-gate 		ASYSETSOFT(asy);	/* need a soft interrupt */
25437c478bd9Sstevel@tonic-gate }
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate /*
25467c478bd9Sstevel@tonic-gate  * Modem status interrupt.
25477c478bd9Sstevel@tonic-gate  *
25487c478bd9Sstevel@tonic-gate  * (Note: It is assumed that the MSR hasn't been read by asyintr().)
25497c478bd9Sstevel@tonic-gate  */
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate static void
25527c478bd9Sstevel@tonic-gate async_msint(struct asycom *asy)
25537c478bd9Sstevel@tonic-gate {
25547c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
25557c478bd9Sstevel@tonic-gate 	int msr, t_cflag = async->async_ttycommon.t_cflag;
25567c478bd9Sstevel@tonic-gate #ifdef DEBUG
25577c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
25587c478bd9Sstevel@tonic-gate #endif
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate async_msint_retry:
25617c478bd9Sstevel@tonic-gate 	/* this resets the interrupt */
25624ab75253Smrj 	msr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MSR);
25637c478bd9Sstevel@tonic-gate 	DEBUGCONT10(ASY_DEBUG_STATE,
25647c478bd9Sstevel@tonic-gate 	    "async%d_msint call #%d:\n"
25657c478bd9Sstevel@tonic-gate 	    "   transition: %3s %3s %3s %3s\n"
25667c478bd9Sstevel@tonic-gate 	    "current state: %3s %3s %3s %3s\n",
25677c478bd9Sstevel@tonic-gate 	    instance,
25687c478bd9Sstevel@tonic-gate 	    ++(asy->asy_msint_cnt),
25697c478bd9Sstevel@tonic-gate 	    (msr & DCTS) ? "DCTS" : "    ",
25707c478bd9Sstevel@tonic-gate 	    (msr & DDSR) ? "DDSR" : "    ",
25717c478bd9Sstevel@tonic-gate 	    (msr & DRI)  ? "DRI " : "    ",
25727c478bd9Sstevel@tonic-gate 	    (msr & DDCD) ? "DDCD" : "    ",
25737c478bd9Sstevel@tonic-gate 	    (msr & CTS)  ? "CTS " : "    ",
25747c478bd9Sstevel@tonic-gate 	    (msr & DSR)  ? "DSR " : "    ",
25757c478bd9Sstevel@tonic-gate 	    (msr & RI)   ? "RI  " : "    ",
25767c478bd9Sstevel@tonic-gate 	    (msr & DCD)  ? "DCD " : "    ");
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 	/* If CTS status is changed, do H/W output flow control */
25797c478bd9Sstevel@tonic-gate 	if ((t_cflag & CRTSCTS) && (((asy->asy_msr ^ msr) & CTS) != 0))
25807c478bd9Sstevel@tonic-gate 		async_flowcontrol_hw_output(asy,
25817c478bd9Sstevel@tonic-gate 		    msr & CTS ? FLOW_START : FLOW_STOP);
25827c478bd9Sstevel@tonic-gate 	/*
25837c478bd9Sstevel@tonic-gate 	 * Reading MSR resets the interrupt, we save the
25847c478bd9Sstevel@tonic-gate 	 * value of msr so that other functions could examine MSR by
25857c478bd9Sstevel@tonic-gate 	 * looking at asy_msr.
25867c478bd9Sstevel@tonic-gate 	 */
25877c478bd9Sstevel@tonic-gate 	asy->asy_msr = (uchar_t)msr;
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 	/* Handle PPS event */
25907c478bd9Sstevel@tonic-gate 	if (asy->asy_flags & ASY_PPS)
25917c478bd9Sstevel@tonic-gate 		asy_ppsevent(asy, msr);
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 	async->async_ext++;
25947c478bd9Sstevel@tonic-gate 	ASYSETSOFT(asy);
25957c478bd9Sstevel@tonic-gate 	/*
25967c478bd9Sstevel@tonic-gate 	 * We will make sure that the modem status presented to us
25977c478bd9Sstevel@tonic-gate 	 * during the previous read has not changed. If the chip samples
25987c478bd9Sstevel@tonic-gate 	 * the modem status on the falling edge of the interrupt line,
25997c478bd9Sstevel@tonic-gate 	 * and uses this state as the base for detecting change of modem
26007c478bd9Sstevel@tonic-gate 	 * status, we would miss a change of modem status event that occured
26017c478bd9Sstevel@tonic-gate 	 * after we initiated a read MSR operation.
26027c478bd9Sstevel@tonic-gate 	 */
26034ab75253Smrj 	msr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MSR);
26047c478bd9Sstevel@tonic-gate 	if (STATES(msr) != STATES(asy->asy_msr))
26057c478bd9Sstevel@tonic-gate 		goto	async_msint_retry;
26067c478bd9Sstevel@tonic-gate }
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate /*
26097c478bd9Sstevel@tonic-gate  * Handle a second-stage interrupt.
26107c478bd9Sstevel@tonic-gate  */
26117c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26127c478bd9Sstevel@tonic-gate uint_t
26137c478bd9Sstevel@tonic-gate asysoftintr(caddr_t intarg)
26147c478bd9Sstevel@tonic-gate {
26157c478bd9Sstevel@tonic-gate 	struct asycom *asy;
26167c478bd9Sstevel@tonic-gate 	int rv;
26177c478bd9Sstevel@tonic-gate 	int instance;
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	/*
26207c478bd9Sstevel@tonic-gate 	 * Test and clear soft interrupt.
26217c478bd9Sstevel@tonic-gate 	 */
26227c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_soft_lock);
26237c478bd9Sstevel@tonic-gate 	DEBUGCONT0(ASY_DEBUG_PROCS, "asysoftintr: enter\n");
26247c478bd9Sstevel@tonic-gate 	rv = asysoftpend;
26257c478bd9Sstevel@tonic-gate 	if (rv != 0)
26267c478bd9Sstevel@tonic-gate 		asysoftpend = 0;
26277c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_soft_lock);
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	if (rv) {
26307c478bd9Sstevel@tonic-gate 		/*
26317c478bd9Sstevel@tonic-gate 		 * Note - we can optimize the loop by remembering the last
26327c478bd9Sstevel@tonic-gate 		 * device that requested soft interrupt
26337c478bd9Sstevel@tonic-gate 		 */
26347c478bd9Sstevel@tonic-gate 		for (instance = 0; instance <= max_asy_instance; instance++) {
26357c478bd9Sstevel@tonic-gate 			asy = ddi_get_soft_state(asy_soft_state, instance);
26367c478bd9Sstevel@tonic-gate 			if (asy == NULL || asy->asy_priv == NULL)
26377c478bd9Sstevel@tonic-gate 				continue;
26387c478bd9Sstevel@tonic-gate 			mutex_enter(&asy_soft_lock);
26397c478bd9Sstevel@tonic-gate 			if (asy->asy_flags & ASY_NEEDSOFT) {
26407c478bd9Sstevel@tonic-gate 				asy->asy_flags &= ~ASY_NEEDSOFT;
26417c478bd9Sstevel@tonic-gate 				mutex_exit(&asy_soft_lock);
26427c478bd9Sstevel@tonic-gate 				async_softint(asy);
26437c478bd9Sstevel@tonic-gate 			} else
26447c478bd9Sstevel@tonic-gate 				mutex_exit(&asy_soft_lock);
26457c478bd9Sstevel@tonic-gate 		}
26467c478bd9Sstevel@tonic-gate 	}
26477c478bd9Sstevel@tonic-gate 	return (rv ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED);
26487c478bd9Sstevel@tonic-gate }
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate /*
26517c478bd9Sstevel@tonic-gate  * Handle a software interrupt.
26527c478bd9Sstevel@tonic-gate  */
26537c478bd9Sstevel@tonic-gate static void
26547c478bd9Sstevel@tonic-gate async_softint(struct asycom *asy)
26557c478bd9Sstevel@tonic-gate {
26567c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
26577c478bd9Sstevel@tonic-gate 	short	cc;
26587c478bd9Sstevel@tonic-gate 	mblk_t	*bp;
26597c478bd9Sstevel@tonic-gate 	queue_t	*q;
26607c478bd9Sstevel@tonic-gate 	uchar_t	val;
26617c478bd9Sstevel@tonic-gate 	uchar_t	c;
26627c478bd9Sstevel@tonic-gate 	tty_common_t	*tp;
26637c478bd9Sstevel@tonic-gate 	int nb;
26647c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_softint\n", instance);
26677c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_soft_lock);
26687c478bd9Sstevel@tonic-gate 	if (asy->asy_flags & ASY_DOINGSOFT) {
26697c478bd9Sstevel@tonic-gate 		asy->asy_flags |= ASY_DOINGSOFT_RETRY;
26707c478bd9Sstevel@tonic-gate 		mutex_exit(&asy_soft_lock);
26717c478bd9Sstevel@tonic-gate 		return;
26727c478bd9Sstevel@tonic-gate 	}
26737c478bd9Sstevel@tonic-gate 	asy->asy_flags |= ASY_DOINGSOFT;
26747c478bd9Sstevel@tonic-gate begin:
26757c478bd9Sstevel@tonic-gate 	asy->asy_flags &= ~ASY_DOINGSOFT_RETRY;
26767c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_soft_lock);
26777c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
26787c478bd9Sstevel@tonic-gate 	tp = &async->async_ttycommon;
26797c478bd9Sstevel@tonic-gate 	q = tp->t_readq;
26807c478bd9Sstevel@tonic-gate 	if (async->async_flags & ASYNC_OUT_FLW_RESUME) {
26817c478bd9Sstevel@tonic-gate 		if (async->async_ocnt > 0) {
26827c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
26837c478bd9Sstevel@tonic-gate 			async_resume(async);
26847c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
26857c478bd9Sstevel@tonic-gate 		} else {
26867c478bd9Sstevel@tonic-gate 			if (async->async_xmitblk)
26877c478bd9Sstevel@tonic-gate 				freeb(async->async_xmitblk);
26887c478bd9Sstevel@tonic-gate 			async->async_xmitblk = NULL;
26897c478bd9Sstevel@tonic-gate 			async_start(async);
26907c478bd9Sstevel@tonic-gate 		}
26917c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
26927c478bd9Sstevel@tonic-gate 	}
26937c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
26947c478bd9Sstevel@tonic-gate 	if (async->async_ext) {
26957c478bd9Sstevel@tonic-gate 		async->async_ext = 0;
26967c478bd9Sstevel@tonic-gate 		/* check for carrier up */
26977c478bd9Sstevel@tonic-gate 		DEBUGCONT3(ASY_DEBUG_MODM2,
26987c478bd9Sstevel@tonic-gate 		    "async%d_softint: asy_msr & DCD = %x, "
26997c478bd9Sstevel@tonic-gate 		    "tp->t_flags & TS_SOFTCAR = %x\n",
27002df1fe9cSrandyf 		    instance, asy->asy_msr & DCD, tp->t_flags & TS_SOFTCAR);
27012df1fe9cSrandyf 
27027c478bd9Sstevel@tonic-gate 		if (asy->asy_msr & DCD) {
27037c478bd9Sstevel@tonic-gate 			/* carrier present */
27047c478bd9Sstevel@tonic-gate 			if ((async->async_flags & ASYNC_CARR_ON) == 0) {
27057c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_MODM2,
27067c478bd9Sstevel@tonic-gate 				    "async%d_softint: set ASYNC_CARR_ON\n",
27077c478bd9Sstevel@tonic-gate 				    instance);
27087c478bd9Sstevel@tonic-gate 				async->async_flags |= ASYNC_CARR_ON;
27097c478bd9Sstevel@tonic-gate 				if (async->async_flags & ASYNC_ISOPEN) {
27107c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
27117c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
27127c478bd9Sstevel@tonic-gate 					(void) putctl(q, M_UNHANGUP);
27137c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
27147c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
27157c478bd9Sstevel@tonic-gate 				}
27167c478bd9Sstevel@tonic-gate 				cv_broadcast(&async->async_flags_cv);
27177c478bd9Sstevel@tonic-gate 			}
27187c478bd9Sstevel@tonic-gate 		} else {
27197c478bd9Sstevel@tonic-gate 			if ((async->async_flags & ASYNC_CARR_ON) &&
27207c478bd9Sstevel@tonic-gate 			    !(tp->t_cflag & CLOCAL) &&
27217c478bd9Sstevel@tonic-gate 			    !(tp->t_flags & TS_SOFTCAR)) {
27227c478bd9Sstevel@tonic-gate 				int flushflag;
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_MODEM,
27257c478bd9Sstevel@tonic-gate 				    "async%d_softint: carrier dropped, "
27267c478bd9Sstevel@tonic-gate 				    "so drop DTR\n",
27277c478bd9Sstevel@tonic-gate 				    instance);
27287c478bd9Sstevel@tonic-gate 				/*
27297c478bd9Sstevel@tonic-gate 				 * Carrier went away.
27307c478bd9Sstevel@tonic-gate 				 * Drop DTR, abort any output in
27317c478bd9Sstevel@tonic-gate 				 * progress, indicate that output is
27327c478bd9Sstevel@tonic-gate 				 * not stopped, and send a hangup
27337c478bd9Sstevel@tonic-gate 				 * notification upstream.
27347c478bd9Sstevel@tonic-gate 				 */
27354ab75253Smrj 				val = ddi_get8(asy->asy_iohandle,
27367c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + MCR);
27374ab75253Smrj 				ddi_put8(asy->asy_iohandle,
27387c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + MCR, (val & ~DTR));
27392df1fe9cSrandyf 
27407c478bd9Sstevel@tonic-gate 				if (async->async_flags & ASYNC_BUSY) {
27417c478bd9Sstevel@tonic-gate 					DEBUGCONT0(ASY_DEBUG_BUSY,
27427c478bd9Sstevel@tonic-gate 					    "async_softint: "
27437c478bd9Sstevel@tonic-gate 					    "Carrier dropped.  "
27447c478bd9Sstevel@tonic-gate 					    "Clearing async_ocnt\n");
27457c478bd9Sstevel@tonic-gate 					async->async_ocnt = 0;
27467c478bd9Sstevel@tonic-gate 				}	/* if */
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_STOPPED;
27497c478bd9Sstevel@tonic-gate 				if (async->async_flags & ASYNC_ISOPEN) {
27507c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
27517c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
27527c478bd9Sstevel@tonic-gate 					(void) putctl(q, M_HANGUP);
27537c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
27547c478bd9Sstevel@tonic-gate 					DEBUGCONT1(ASY_DEBUG_MODEM,
27557c478bd9Sstevel@tonic-gate 					    "async%d_softint: "
27567c478bd9Sstevel@tonic-gate 					    "putctl(q, M_HANGUP)\n",
27577c478bd9Sstevel@tonic-gate 					    instance);
27587c478bd9Sstevel@tonic-gate 					/*
27597c478bd9Sstevel@tonic-gate 					 * Flush FIFO buffers
27607c478bd9Sstevel@tonic-gate 					 * Any data left in there is invalid now
27617c478bd9Sstevel@tonic-gate 					 */
27627c478bd9Sstevel@tonic-gate 					if (asy->asy_use_fifo == FIFO_ON)
27637c478bd9Sstevel@tonic-gate 						asy_reset_fifo(asy, FIFOTXFLSH);
27647c478bd9Sstevel@tonic-gate 					/*
27657c478bd9Sstevel@tonic-gate 					 * Flush our write queue if we have one.
27662df1fe9cSrandyf 					 * If we're in the midst of close, then
27672df1fe9cSrandyf 					 * flush everything. Don't leave stale
27682df1fe9cSrandyf 					 * ioctls lying about.
27697c478bd9Sstevel@tonic-gate 					 */
27707c478bd9Sstevel@tonic-gate 					flushflag = (async->async_flags &
27712df1fe9cSrandyf 					    ASYNC_CLOSING) ? FLUSHALL :
27722df1fe9cSrandyf 					    FLUSHDATA;
27737c478bd9Sstevel@tonic-gate 					flushq(tp->t_writeq, flushflag);
27747c478bd9Sstevel@tonic-gate 
27752df1fe9cSrandyf 					/* active msg */
27762df1fe9cSrandyf 					bp = async->async_xmitblk;
27777c478bd9Sstevel@tonic-gate 					if (bp != NULL) {
27787c478bd9Sstevel@tonic-gate 						freeb(bp);
27797c478bd9Sstevel@tonic-gate 						async->async_xmitblk = NULL;
27807c478bd9Sstevel@tonic-gate 					}
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
27837c478bd9Sstevel@tonic-gate 					async->async_flags &= ~ASYNC_BUSY;
27847c478bd9Sstevel@tonic-gate 					/*
27857c478bd9Sstevel@tonic-gate 					 * This message warns of Carrier loss
27862df1fe9cSrandyf 					 * with data left to transmit can hang
27872df1fe9cSrandyf 					 * the system.
27887c478bd9Sstevel@tonic-gate 					 */
27897c478bd9Sstevel@tonic-gate 					DEBUGCONT0(ASY_DEBUG_MODEM,
27907c478bd9Sstevel@tonic-gate 					    "async_softint: Flushing to "
27917c478bd9Sstevel@tonic-gate 					    "prevent HUPCL hanging\n");
27927c478bd9Sstevel@tonic-gate 				}	/* if (ASYNC_ISOPEN) */
27937c478bd9Sstevel@tonic-gate 			}	/* if (ASYNC_CARR_ON && CLOCAL) */
27947c478bd9Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_CARR_ON;
27957c478bd9Sstevel@tonic-gate 			cv_broadcast(&async->async_flags_cv);
27967c478bd9Sstevel@tonic-gate 		}	/* else */
27977c478bd9Sstevel@tonic-gate 	}	/* if (async->async_ext) */
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 	/*
28027c478bd9Sstevel@tonic-gate 	 * If data has been added to the circular buffer, remove
28037c478bd9Sstevel@tonic-gate 	 * it from the buffer, and send it up the stream if there's
28047c478bd9Sstevel@tonic-gate 	 * somebody listening. Try to do it 16 bytes at a time. If we
28057c478bd9Sstevel@tonic-gate 	 * have more than 16 bytes to move, move 16 byte chunks and
28067c478bd9Sstevel@tonic-gate 	 * leave the rest for next time around (maybe it will grow).
28077c478bd9Sstevel@tonic-gate 	 */
28087c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
28097c478bd9Sstevel@tonic-gate 	if (!(async->async_flags & ASYNC_ISOPEN)) {
28107c478bd9Sstevel@tonic-gate 		RING_INIT(async);
28117c478bd9Sstevel@tonic-gate 		goto rv;
28127c478bd9Sstevel@tonic-gate 	}
28137c478bd9Sstevel@tonic-gate 	if ((cc = RING_CNT(async)) <= 0)
28147c478bd9Sstevel@tonic-gate 		goto rv;
28157c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 	if (!canput(q)) {
28187c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
28197c478bd9Sstevel@tonic-gate 		if (!(async->async_inflow_source & IN_FLOW_STREAMS)) {
28207c478bd9Sstevel@tonic-gate 			async_flowcontrol_hw_input(asy, FLOW_STOP,
28217c478bd9Sstevel@tonic-gate 			    IN_FLOW_STREAMS);
28227c478bd9Sstevel@tonic-gate 			(void) async_flowcontrol_sw_input(asy, FLOW_STOP,
28237c478bd9Sstevel@tonic-gate 			    IN_FLOW_STREAMS);
28247c478bd9Sstevel@tonic-gate 		}
28257c478bd9Sstevel@tonic-gate 		goto rv;
28267c478bd9Sstevel@tonic-gate 	}
28277c478bd9Sstevel@tonic-gate 	if (async->async_inflow_source & IN_FLOW_STREAMS) {
28287c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
28297c478bd9Sstevel@tonic-gate 		async_flowcontrol_hw_input(asy, FLOW_START,
28307c478bd9Sstevel@tonic-gate 		    IN_FLOW_STREAMS);
28317c478bd9Sstevel@tonic-gate 		(void) async_flowcontrol_sw_input(asy, FLOW_START,
28327c478bd9Sstevel@tonic-gate 		    IN_FLOW_STREAMS);
28337c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
28347c478bd9Sstevel@tonic-gate 	}
28352df1fe9cSrandyf 
28362df1fe9cSrandyf 	DEBUGCONT2(ASY_DEBUG_INPUT, "async%d_softint: %d char(s) in queue.\n",
28372df1fe9cSrandyf 	    instance, cc);
28382df1fe9cSrandyf 
28397c478bd9Sstevel@tonic-gate 	if (!(bp = allocb(cc, BPRI_MED))) {
28407c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
28417c478bd9Sstevel@tonic-gate 		ttycommon_qfull(&async->async_ttycommon, q);
28427c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
28437c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
28447c478bd9Sstevel@tonic-gate 		goto rv;
28457c478bd9Sstevel@tonic-gate 	}
28467c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
28477c478bd9Sstevel@tonic-gate 	do {
28487c478bd9Sstevel@tonic-gate 		if (RING_ERR(async, S_ERRORS)) {
28497c478bd9Sstevel@tonic-gate 			RING_UNMARK(async);
28507c478bd9Sstevel@tonic-gate 			c = RING_GET(async);
28517c478bd9Sstevel@tonic-gate 			break;
28527c478bd9Sstevel@tonic-gate 		} else
28537c478bd9Sstevel@tonic-gate 			*bp->b_wptr++ = RING_GET(async);
28547c478bd9Sstevel@tonic-gate 	} while (--cc);
28557c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
28567c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
28577c478bd9Sstevel@tonic-gate 	if (bp->b_wptr > bp->b_rptr) {
28587c478bd9Sstevel@tonic-gate 			if (!canput(q)) {
28597c478bd9Sstevel@tonic-gate 				asyerror(CE_NOTE, "asy%d: local queue full",
28607c478bd9Sstevel@tonic-gate 				    instance);
28617c478bd9Sstevel@tonic-gate 				freemsg(bp);
28627c478bd9Sstevel@tonic-gate 			} else
28637c478bd9Sstevel@tonic-gate 				(void) putq(q, bp);
28647c478bd9Sstevel@tonic-gate 	} else
28657c478bd9Sstevel@tonic-gate 		freemsg(bp);
28667c478bd9Sstevel@tonic-gate 	/*
28677c478bd9Sstevel@tonic-gate 	 * If we have a parity error, then send
28687c478bd9Sstevel@tonic-gate 	 * up an M_BREAK with the "bad"
28697c478bd9Sstevel@tonic-gate 	 * character as an argument. Let ldterm
28707c478bd9Sstevel@tonic-gate 	 * figure out what to do with the error.
28717c478bd9Sstevel@tonic-gate 	 */
28727c478bd9Sstevel@tonic-gate 	if (cc) {
28737c478bd9Sstevel@tonic-gate 		(void) putctl1(q, M_BREAK, c);
28747c478bd9Sstevel@tonic-gate 		ASYSETSOFT(async->async_common);	/* finish cc chars */
28757c478bd9Sstevel@tonic-gate 	}
28767c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
28777c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
28787c478bd9Sstevel@tonic-gate rv:
28797c478bd9Sstevel@tonic-gate 	if ((RING_CNT(async) < (RINGSIZE/4)) &&
28807c478bd9Sstevel@tonic-gate 	    (async->async_inflow_source & IN_FLOW_RINGBUFF)) {
28817c478bd9Sstevel@tonic-gate 		async_flowcontrol_hw_input(asy, FLOW_START, IN_FLOW_RINGBUFF);
28827c478bd9Sstevel@tonic-gate 		(void) async_flowcontrol_sw_input(asy, FLOW_START,
28837c478bd9Sstevel@tonic-gate 		    IN_FLOW_RINGBUFF);
28847c478bd9Sstevel@tonic-gate 	}
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate 	/*
28877c478bd9Sstevel@tonic-gate 	 * If a transmission has finished, indicate that it's finished,
28887c478bd9Sstevel@tonic-gate 	 * and start that line up again.
28897c478bd9Sstevel@tonic-gate 	 */
28907c478bd9Sstevel@tonic-gate 	if (async->async_break > 0) {
28917c478bd9Sstevel@tonic-gate 		nb = async->async_break;
28927c478bd9Sstevel@tonic-gate 		async->async_break = 0;
28937c478bd9Sstevel@tonic-gate 		if (async->async_flags & ASYNC_ISOPEN) {
28947c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
28957c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
28967c478bd9Sstevel@tonic-gate 			for (; nb > 0; nb--)
28977c478bd9Sstevel@tonic-gate 				(void) putctl(q, M_BREAK);
28987c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
28997c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
29007c478bd9Sstevel@tonic-gate 		}
29017c478bd9Sstevel@tonic-gate 	}
29027c478bd9Sstevel@tonic-gate 	if (async->async_ocnt <= 0 && (async->async_flags & ASYNC_BUSY)) {
29037c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_BUSY,
29047c478bd9Sstevel@tonic-gate 		    "async%d_softint: Clearing ASYNC_BUSY.  async_ocnt=%d\n",
29057c478bd9Sstevel@tonic-gate 		    instance,
29067c478bd9Sstevel@tonic-gate 		    async->async_ocnt);
29077c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_BUSY;
29087c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
29097c478bd9Sstevel@tonic-gate 		if (async->async_xmitblk)
29107c478bd9Sstevel@tonic-gate 			freeb(async->async_xmitblk);
29117c478bd9Sstevel@tonic-gate 		async->async_xmitblk = NULL;
29127c478bd9Sstevel@tonic-gate 		async_start(async);
29137c478bd9Sstevel@tonic-gate 		/*
29147c478bd9Sstevel@tonic-gate 		 * If the flag isn't set after doing the async_start above, we
29157c478bd9Sstevel@tonic-gate 		 * may have finished all the queued output.  Signal any thread
29167c478bd9Sstevel@tonic-gate 		 * stuck in close.
29177c478bd9Sstevel@tonic-gate 		 */
29187c478bd9Sstevel@tonic-gate 		if (!(async->async_flags & ASYNC_BUSY))
29197c478bd9Sstevel@tonic-gate 			cv_broadcast(&async->async_flags_cv);
29207c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
29217c478bd9Sstevel@tonic-gate 	}
29227c478bd9Sstevel@tonic-gate 	/*
29237c478bd9Sstevel@tonic-gate 	 * A note about these overrun bits: all they do is *tell* someone
29247c478bd9Sstevel@tonic-gate 	 * about an error- They do not track multiple errors. In fact,
29257c478bd9Sstevel@tonic-gate 	 * you could consider them latched register bits if you like.
29267c478bd9Sstevel@tonic-gate 	 * We are only interested in printing the error message once for
29277c478bd9Sstevel@tonic-gate 	 * any cluster of overrun errrors.
29287c478bd9Sstevel@tonic-gate 	 */
29297c478bd9Sstevel@tonic-gate 	if (async->async_hw_overrun) {
29307c478bd9Sstevel@tonic-gate 		if (async->async_flags & ASYNC_ISOPEN) {
29317c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
29327c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
29337c478bd9Sstevel@tonic-gate 			asyerror(CE_NOTE, "asy%d: silo overflow", instance);
29347c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
29357c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
29367c478bd9Sstevel@tonic-gate 		}
29377c478bd9Sstevel@tonic-gate 		async->async_hw_overrun = 0;
29387c478bd9Sstevel@tonic-gate 	}
29397c478bd9Sstevel@tonic-gate 	if (async->async_sw_overrun) {
29407c478bd9Sstevel@tonic-gate 		if (async->async_flags & ASYNC_ISOPEN) {
29417c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
29427c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
29437c478bd9Sstevel@tonic-gate 			asyerror(CE_NOTE, "asy%d: ring buffer overflow",
29447c478bd9Sstevel@tonic-gate 			    instance);
29457c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
29467c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
29477c478bd9Sstevel@tonic-gate 		}
29487c478bd9Sstevel@tonic-gate 		async->async_sw_overrun = 0;
29497c478bd9Sstevel@tonic-gate 	}
29507c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
29517c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
29527c478bd9Sstevel@tonic-gate 	mutex_enter(&asy_soft_lock);
29537c478bd9Sstevel@tonic-gate 	if (asy->asy_flags & ASY_DOINGSOFT_RETRY) {
29547c478bd9Sstevel@tonic-gate 		goto begin;
29557c478bd9Sstevel@tonic-gate 	}
29567c478bd9Sstevel@tonic-gate 	asy->asy_flags &= ~ASY_DOINGSOFT;
29577c478bd9Sstevel@tonic-gate 	mutex_exit(&asy_soft_lock);
29587c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_softint: done\n", instance);
29597c478bd9Sstevel@tonic-gate }
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate /*
29627c478bd9Sstevel@tonic-gate  * Restart output on a line after a delay or break timer expired.
29637c478bd9Sstevel@tonic-gate  */
29647c478bd9Sstevel@tonic-gate static void
29657c478bd9Sstevel@tonic-gate async_restart(void *arg)
29667c478bd9Sstevel@tonic-gate {
29677c478bd9Sstevel@tonic-gate 	struct asyncline *async = (struct asyncline *)arg;
29687c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
29697c478bd9Sstevel@tonic-gate 	uchar_t lcr;
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	/*
29727c478bd9Sstevel@tonic-gate 	 * If break timer expired, turn off the break bit.
29737c478bd9Sstevel@tonic-gate 	 */
29747c478bd9Sstevel@tonic-gate #ifdef DEBUG
29757c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_restart\n", instance);
29787c478bd9Sstevel@tonic-gate #endif
29797c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
29807c478bd9Sstevel@tonic-gate 	/*
29817c478bd9Sstevel@tonic-gate 	 * If ASYNC_OUT_SUSPEND is also set, we don't really
29827c478bd9Sstevel@tonic-gate 	 * clean the HW break, TIOCCBRK is responsible for this.
29837c478bd9Sstevel@tonic-gate 	 */
29847c478bd9Sstevel@tonic-gate 	if ((async->async_flags & ASYNC_BREAK) &&
29857c478bd9Sstevel@tonic-gate 	    !(async->async_flags & ASYNC_OUT_SUSPEND)) {
29867c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
29872df1fe9cSrandyf 		lcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
29884ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
29897c478bd9Sstevel@tonic-gate 		    (lcr & ~SETBREAK));
29907c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
29917c478bd9Sstevel@tonic-gate 	}
29927c478bd9Sstevel@tonic-gate 	async->async_flags &= ~(ASYNC_DELAY|ASYNC_BREAK);
29937c478bd9Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
29947c478bd9Sstevel@tonic-gate 	async_start(async);
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
29977c478bd9Sstevel@tonic-gate }
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate static void
30007c478bd9Sstevel@tonic-gate async_start(struct asyncline *async)
30017c478bd9Sstevel@tonic-gate {
30027c478bd9Sstevel@tonic-gate 	async_nstart(async, 0);
30037c478bd9Sstevel@tonic-gate }
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate /*
30067c478bd9Sstevel@tonic-gate  * Start output on a line, unless it's busy, frozen, or otherwise.
30077c478bd9Sstevel@tonic-gate  */
30087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
30097c478bd9Sstevel@tonic-gate static void
30107c478bd9Sstevel@tonic-gate async_nstart(struct asyncline *async, int mode)
30117c478bd9Sstevel@tonic-gate {
30127c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
30137c478bd9Sstevel@tonic-gate 	int cc;
30147c478bd9Sstevel@tonic-gate 	queue_t *q;
30157c478bd9Sstevel@tonic-gate 	mblk_t *bp;
30167c478bd9Sstevel@tonic-gate 	uchar_t *xmit_addr;
30177c478bd9Sstevel@tonic-gate 	uchar_t	val;
30187c478bd9Sstevel@tonic-gate 	int	fifo_len = 1;
30197c478bd9Sstevel@tonic-gate 	boolean_t didsome;
30207c478bd9Sstevel@tonic-gate 	mblk_t *nbp;
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate #ifdef DEBUG
30237c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_nstart\n", instance);
30267c478bd9Sstevel@tonic-gate #endif
30277c478bd9Sstevel@tonic-gate 	if (asy->asy_use_fifo == FIFO_ON) {
30287c478bd9Sstevel@tonic-gate 		fifo_len = asy->asy_fifo_buf; /* with FIFO buffers */
30297c478bd9Sstevel@tonic-gate 		if (fifo_len > asy_max_tx_fifo)
30307c478bd9Sstevel@tonic-gate 			fifo_len = asy_max_tx_fifo;
30317c478bd9Sstevel@tonic-gate 	}
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 	/*
30367c478bd9Sstevel@tonic-gate 	 * If the chip is busy (i.e., we're waiting for a break timeout
30377c478bd9Sstevel@tonic-gate 	 * to expire, or for the current transmission to finish, or for
30387c478bd9Sstevel@tonic-gate 	 * output to finish draining from chip), don't grab anything new.
30397c478bd9Sstevel@tonic-gate 	 */
30407c478bd9Sstevel@tonic-gate 	if (async->async_flags & (ASYNC_BREAK|ASYNC_BUSY)) {
30417c478bd9Sstevel@tonic-gate 		DEBUGCONT2((mode? ASY_DEBUG_OUT : 0),
30427c478bd9Sstevel@tonic-gate 		    "async%d_nstart: start %s.\n",
30437c478bd9Sstevel@tonic-gate 		    instance,
30447c478bd9Sstevel@tonic-gate 		    async->async_flags & ASYNC_BREAK ? "break" : "busy");
30457c478bd9Sstevel@tonic-gate 		return;
30467c478bd9Sstevel@tonic-gate 	}
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 	/*
30497c478bd9Sstevel@tonic-gate 	 * Check only pended sw input flow control.
30507c478bd9Sstevel@tonic-gate 	 */
30517c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
30527c478bd9Sstevel@tonic-gate 	if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
30537c478bd9Sstevel@tonic-gate 		fifo_len--;
30547c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 	/*
30577c478bd9Sstevel@tonic-gate 	 * If we're waiting for a delay timeout to expire, don't grab
30587c478bd9Sstevel@tonic-gate 	 * anything new.
30597c478bd9Sstevel@tonic-gate 	 */
30607c478bd9Sstevel@tonic-gate 	if (async->async_flags & ASYNC_DELAY) {
30617c478bd9Sstevel@tonic-gate 		DEBUGCONT1((mode? ASY_DEBUG_OUT : 0),
30627c478bd9Sstevel@tonic-gate 		    "async%d_nstart: start ASYNC_DELAY.\n", instance);
30637c478bd9Sstevel@tonic-gate 		return;
30647c478bd9Sstevel@tonic-gate 	}
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate 	if ((q = async->async_ttycommon.t_writeq) == NULL) {
30677c478bd9Sstevel@tonic-gate 		DEBUGCONT1((mode? ASY_DEBUG_OUT : 0),
30687c478bd9Sstevel@tonic-gate 		    "async%d_nstart: start writeq is null.\n", instance);
30697c478bd9Sstevel@tonic-gate 		return;	/* not attached to a stream */
30707c478bd9Sstevel@tonic-gate 	}
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate 	for (;;) {
30737c478bd9Sstevel@tonic-gate 		if ((bp = getq(q)) == NULL)
30747c478bd9Sstevel@tonic-gate 			return;	/* no data to transmit */
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 		/*
30777c478bd9Sstevel@tonic-gate 		 * We have a message block to work on.
30787c478bd9Sstevel@tonic-gate 		 * Check whether it's a break, a delay, or an ioctl (the latter
30797c478bd9Sstevel@tonic-gate 		 * occurs if the ioctl in question was waiting for the output
30807c478bd9Sstevel@tonic-gate 		 * to drain).  If it's one of those, process it immediately.
30817c478bd9Sstevel@tonic-gate 		 */
30827c478bd9Sstevel@tonic-gate 		switch (bp->b_datap->db_type) {
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate 		case M_BREAK:
30857c478bd9Sstevel@tonic-gate 			/*
30867c478bd9Sstevel@tonic-gate 			 * Set the break bit, and arrange for "async_restart"
30877c478bd9Sstevel@tonic-gate 			 * to be called in 1/4 second; it will turn the
30887c478bd9Sstevel@tonic-gate 			 * break bit off, and call "async_start" to grab
30897c478bd9Sstevel@tonic-gate 			 * the next message.
30907c478bd9Sstevel@tonic-gate 			 */
30917c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
30924ab75253Smrj 			val = ddi_get8(asy->asy_iohandle,
30937c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + LCR);
30942df1fe9cSrandyf 			ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
30952df1fe9cSrandyf 			    (val | SETBREAK));
30967c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
30977c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_BREAK;
30987c478bd9Sstevel@tonic-gate 			(void) timeout(async_restart, (caddr_t)async,
30997c478bd9Sstevel@tonic-gate 			    drv_usectohz(1000000)/4);
31007c478bd9Sstevel@tonic-gate 			freemsg(bp);
31017c478bd9Sstevel@tonic-gate 			return;	/* wait for this to finish */
31027c478bd9Sstevel@tonic-gate 
31037c478bd9Sstevel@tonic-gate 		case M_DELAY:
31047c478bd9Sstevel@tonic-gate 			/*
31057c478bd9Sstevel@tonic-gate 			 * Arrange for "async_restart" to be called when the
31067c478bd9Sstevel@tonic-gate 			 * delay expires; it will turn ASYNC_DELAY off,
31077c478bd9Sstevel@tonic-gate 			 * and call "async_start" to grab the next message.
31087c478bd9Sstevel@tonic-gate 			 */
31097c478bd9Sstevel@tonic-gate 			(void) timeout(async_restart, (caddr_t)async,
31107c478bd9Sstevel@tonic-gate 			    (int)(*(unsigned char *)bp->b_rptr + 6));
31117c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_DELAY;
31127c478bd9Sstevel@tonic-gate 			freemsg(bp);
31137c478bd9Sstevel@tonic-gate 			return;	/* wait for this to finish */
31147c478bd9Sstevel@tonic-gate 
31157c478bd9Sstevel@tonic-gate 		case M_IOCTL:
31167c478bd9Sstevel@tonic-gate 			/*
31177c478bd9Sstevel@tonic-gate 			 * This ioctl was waiting for the output ahead of
31187c478bd9Sstevel@tonic-gate 			 * it to drain; obviously, it has.  Do it, and
31197c478bd9Sstevel@tonic-gate 			 * then grab the next message after it.
31207c478bd9Sstevel@tonic-gate 			 */
31217c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
31227c478bd9Sstevel@tonic-gate 			async_ioctl(async, q, bp);
31237c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
31247c478bd9Sstevel@tonic-gate 			continue;
31257c478bd9Sstevel@tonic-gate 		}
31267c478bd9Sstevel@tonic-gate 
3127*22eb7cb5Sgd78059 		while (bp != NULL && ((cc = MBLKL(bp)) == 0)) {
31287c478bd9Sstevel@tonic-gate 			nbp = bp->b_cont;
31297c478bd9Sstevel@tonic-gate 			freeb(bp);
31307c478bd9Sstevel@tonic-gate 			bp = nbp;
31317c478bd9Sstevel@tonic-gate 		}
31327c478bd9Sstevel@tonic-gate 		if (bp != NULL)
31337c478bd9Sstevel@tonic-gate 			break;
31347c478bd9Sstevel@tonic-gate 	}
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate 	/*
31377c478bd9Sstevel@tonic-gate 	 * We have data to transmit.  If output is stopped, put
31387c478bd9Sstevel@tonic-gate 	 * it back and try again later.
31397c478bd9Sstevel@tonic-gate 	 */
31407c478bd9Sstevel@tonic-gate 	if (async->async_flags & (ASYNC_HW_OUT_FLW | ASYNC_SW_OUT_FLW |
31417c478bd9Sstevel@tonic-gate 	    ASYNC_STOPPED | ASYNC_OUT_SUSPEND)) {
31427c478bd9Sstevel@tonic-gate 		(void) putbq(q, bp);
31437c478bd9Sstevel@tonic-gate 		return;
31447c478bd9Sstevel@tonic-gate 	}
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate 	async->async_xmitblk = bp;
31477c478bd9Sstevel@tonic-gate 	xmit_addr = bp->b_rptr;
31487c478bd9Sstevel@tonic-gate 	bp = bp->b_cont;
31497c478bd9Sstevel@tonic-gate 	if (bp != NULL)
31507c478bd9Sstevel@tonic-gate 		(void) putbq(q, bp);	/* not done with this message yet */
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 	/*
31537c478bd9Sstevel@tonic-gate 	 * In 5-bit mode, the high order bits are used
31547c478bd9Sstevel@tonic-gate 	 * to indicate character sizes less than five,
31557c478bd9Sstevel@tonic-gate 	 * so we need to explicitly mask before transmitting
31567c478bd9Sstevel@tonic-gate 	 */
31577c478bd9Sstevel@tonic-gate 	if ((async->async_ttycommon.t_cflag & CSIZE) == CS5) {
31587c478bd9Sstevel@tonic-gate 		unsigned char *p = xmit_addr;
31597c478bd9Sstevel@tonic-gate 		int cnt = cc;
31607c478bd9Sstevel@tonic-gate 
31617c478bd9Sstevel@tonic-gate 		while (cnt--)
31627c478bd9Sstevel@tonic-gate 			*p++ &= (unsigned char) 0x1f;
31637c478bd9Sstevel@tonic-gate 	}
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 	/*
31667c478bd9Sstevel@tonic-gate 	 * Set up this block for pseudo-DMA.
31677c478bd9Sstevel@tonic-gate 	 */
31687c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
31697c478bd9Sstevel@tonic-gate 	/*
31707c478bd9Sstevel@tonic-gate 	 * If the transmitter is ready, shove the first
31717c478bd9Sstevel@tonic-gate 	 * character out.
31727c478bd9Sstevel@tonic-gate 	 */
31737c478bd9Sstevel@tonic-gate 	didsome = B_FALSE;
31747c478bd9Sstevel@tonic-gate 	while (--fifo_len >= 0 && cc > 0) {
31754ab75253Smrj 		if (!(ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) &
31767c478bd9Sstevel@tonic-gate 		    XHRE))
31777c478bd9Sstevel@tonic-gate 			break;
31784ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
31797c478bd9Sstevel@tonic-gate 		    *xmit_addr++);
31807c478bd9Sstevel@tonic-gate 		cc--;
31817c478bd9Sstevel@tonic-gate 		didsome = B_TRUE;
31827c478bd9Sstevel@tonic-gate 	}
31837c478bd9Sstevel@tonic-gate 	async->async_optr = xmit_addr;
31847c478bd9Sstevel@tonic-gate 	async->async_ocnt = cc;
31857c478bd9Sstevel@tonic-gate 	if (didsome)
31867c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_PROGRESS;
31877c478bd9Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_BUSY,
31887c478bd9Sstevel@tonic-gate 	    "async%d_nstart: Set ASYNC_BUSY.  async_ocnt=%d\n",
31892df1fe9cSrandyf 	    instance, async->async_ocnt);
31907c478bd9Sstevel@tonic-gate 	async->async_flags |= ASYNC_BUSY;
31917c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl_hi);
31927c478bd9Sstevel@tonic-gate }
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate /*
31957c478bd9Sstevel@tonic-gate  * Resume output by poking the transmitter.
31967c478bd9Sstevel@tonic-gate  */
31977c478bd9Sstevel@tonic-gate static void
31987c478bd9Sstevel@tonic-gate async_resume(struct asyncline *async)
31997c478bd9Sstevel@tonic-gate {
32007c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
32017c478bd9Sstevel@tonic-gate #ifdef DEBUG
32027c478bd9Sstevel@tonic-gate 	int instance;
32037c478bd9Sstevel@tonic-gate #endif
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
32067c478bd9Sstevel@tonic-gate #ifdef DEBUG
32077c478bd9Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
32087c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_resume\n", instance);
32097c478bd9Sstevel@tonic-gate #endif
32107c478bd9Sstevel@tonic-gate 
32114ab75253Smrj 	if (ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) & XHRE) {
32127c478bd9Sstevel@tonic-gate 		if (async_flowcontrol_sw_input(asy, FLOW_CHECK, IN_FLOW_NULL))
32137c478bd9Sstevel@tonic-gate 			return;
32147c478bd9Sstevel@tonic-gate 		if (async->async_ocnt > 0 &&
32157c478bd9Sstevel@tonic-gate 		    !(async->async_flags &
32167c478bd9Sstevel@tonic-gate 		    (ASYNC_HW_OUT_FLW|ASYNC_SW_OUT_FLW|ASYNC_OUT_SUSPEND))) {
32174ab75253Smrj 			ddi_put8(asy->asy_iohandle,
32187c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + DAT, *async->async_optr++);
32197c478bd9Sstevel@tonic-gate 			async->async_ocnt--;
32207c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_PROGRESS;
32217c478bd9Sstevel@tonic-gate 		}
32227c478bd9Sstevel@tonic-gate 	}
32237c478bd9Sstevel@tonic-gate }
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate /*
32267c478bd9Sstevel@tonic-gate  * Hold the untimed break to last the minimum time.
32277c478bd9Sstevel@tonic-gate  */
32287c478bd9Sstevel@tonic-gate static void
32297c478bd9Sstevel@tonic-gate async_hold_utbrk(void *arg)
32307c478bd9Sstevel@tonic-gate {
32317c478bd9Sstevel@tonic-gate 	struct asyncline *async = arg;
32327c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
32357c478bd9Sstevel@tonic-gate 	async->async_flags &= ~ASYNC_HOLD_UTBRK;
32367c478bd9Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
32377c478bd9Sstevel@tonic-gate 	async->async_utbrktid = 0;
32387c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
32397c478bd9Sstevel@tonic-gate }
32407c478bd9Sstevel@tonic-gate 
32417c478bd9Sstevel@tonic-gate /*
32427c478bd9Sstevel@tonic-gate  * Resume the untimed break.
32437c478bd9Sstevel@tonic-gate  */
32447c478bd9Sstevel@tonic-gate static void
32457c478bd9Sstevel@tonic-gate async_resume_utbrk(struct asyncline *async)
32467c478bd9Sstevel@tonic-gate {
32477c478bd9Sstevel@tonic-gate 	uchar_t	val;
32487c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
32497c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate 	/*
32527c478bd9Sstevel@tonic-gate 	 * Because the wait time is very short,
32537c478bd9Sstevel@tonic-gate 	 * so we use uninterruptably wait.
32547c478bd9Sstevel@tonic-gate 	 */
32557c478bd9Sstevel@tonic-gate 	while (async->async_flags & ASYNC_HOLD_UTBRK) {
32567c478bd9Sstevel@tonic-gate 		cv_wait(&async->async_flags_cv, &asy->asy_excl);
32577c478bd9Sstevel@tonic-gate 	}
32587c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl_hi);
32597c478bd9Sstevel@tonic-gate 	/*
32607c478bd9Sstevel@tonic-gate 	 * Timed break and untimed break can exist simultaneously,
32617c478bd9Sstevel@tonic-gate 	 * if ASYNC_BREAK is also set at here, we don't
32627c478bd9Sstevel@tonic-gate 	 * really clean the HW break.
32637c478bd9Sstevel@tonic-gate 	 */
32647c478bd9Sstevel@tonic-gate 	if (!(async->async_flags & ASYNC_BREAK)) {
32654ab75253Smrj 		val = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LCR);
32664ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + LCR,
32677c478bd9Sstevel@tonic-gate 		    (val & ~SETBREAK));
32687c478bd9Sstevel@tonic-gate 	}
32697c478bd9Sstevel@tonic-gate 	async->async_flags &= ~ASYNC_OUT_SUSPEND;
32707c478bd9Sstevel@tonic-gate 	cv_broadcast(&async->async_flags_cv);
32717c478bd9Sstevel@tonic-gate 	if (async->async_ocnt > 0) {
32727c478bd9Sstevel@tonic-gate 		async_resume(async);
32737c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
32747c478bd9Sstevel@tonic-gate 	} else {
32757c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_BUSY;
32767c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
32777c478bd9Sstevel@tonic-gate 		if (async->async_xmitblk != NULL) {
32787c478bd9Sstevel@tonic-gate 			freeb(async->async_xmitblk);
32797c478bd9Sstevel@tonic-gate 			async->async_xmitblk = NULL;
32807c478bd9Sstevel@tonic-gate 		}
32817c478bd9Sstevel@tonic-gate 		async_start(async);
32827c478bd9Sstevel@tonic-gate 	}
32837c478bd9Sstevel@tonic-gate }
32847c478bd9Sstevel@tonic-gate 
32857c478bd9Sstevel@tonic-gate /*
32867c478bd9Sstevel@tonic-gate  * Process an "ioctl" message sent down to us.
32877c478bd9Sstevel@tonic-gate  * Note that we don't need to get any locks until we are ready to access
32887c478bd9Sstevel@tonic-gate  * the hardware.  Nothing we access until then is going to be altered
32897c478bd9Sstevel@tonic-gate  * outside of the STREAMS framework, so we should be safe.
32907c478bd9Sstevel@tonic-gate  */
32917c478bd9Sstevel@tonic-gate int asydelay = 10000;
32927c478bd9Sstevel@tonic-gate static void
32937c478bd9Sstevel@tonic-gate async_ioctl(struct asyncline *async, queue_t *wq, mblk_t *mp)
32947c478bd9Sstevel@tonic-gate {
32957c478bd9Sstevel@tonic-gate 	struct asycom *asy = async->async_common;
32967c478bd9Sstevel@tonic-gate 	tty_common_t  *tp = &async->async_ttycommon;
32977c478bd9Sstevel@tonic-gate 	struct iocblk *iocp;
32987c478bd9Sstevel@tonic-gate 	unsigned datasize;
32997c478bd9Sstevel@tonic-gate 	int error = 0;
33007c478bd9Sstevel@tonic-gate 	uchar_t val;
33017c478bd9Sstevel@tonic-gate 	mblk_t *datamp;
33027c478bd9Sstevel@tonic-gate 	unsigned int index;
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate #ifdef DEBUG
33057c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_ioctl\n", instance);
33087c478bd9Sstevel@tonic-gate #endif
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 	if (tp->t_iocpending != NULL) {
33117c478bd9Sstevel@tonic-gate 		/*
33127c478bd9Sstevel@tonic-gate 		 * We were holding an "ioctl" response pending the
33137c478bd9Sstevel@tonic-gate 		 * availability of an "mblk" to hold data to be passed up;
33147c478bd9Sstevel@tonic-gate 		 * another "ioctl" came through, which means that "ioctl"
33157c478bd9Sstevel@tonic-gate 		 * must have timed out or been aborted.
33167c478bd9Sstevel@tonic-gate 		 */
33177c478bd9Sstevel@tonic-gate 		freemsg(async->async_ttycommon.t_iocpending);
33187c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_iocpending = NULL;
33197c478bd9Sstevel@tonic-gate 	}
33207c478bd9Sstevel@tonic-gate 
33217c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate 	/*
33247c478bd9Sstevel@tonic-gate 	 * For TIOCMGET and the PPS ioctls, do NOT call ttycommon_ioctl()
33257c478bd9Sstevel@tonic-gate 	 * because this function frees up the message block (mp->b_cont) that
33267c478bd9Sstevel@tonic-gate 	 * contains the user location where we pass back the results.
33277c478bd9Sstevel@tonic-gate 	 *
33287c478bd9Sstevel@tonic-gate 	 * Similarly, CONSOPENPOLLEDIO needs ioc_count, which ttycommon_ioctl
33297c478bd9Sstevel@tonic-gate 	 * zaps.  We know that ttycommon_ioctl doesn't know any CONS*
33307c478bd9Sstevel@tonic-gate 	 * ioctls, so keep the others safe too.
33317c478bd9Sstevel@tonic-gate 	 */
33327c478bd9Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_IOCTL, "async%d_ioctl: %s\n",
33337c478bd9Sstevel@tonic-gate 	    instance,
33347c478bd9Sstevel@tonic-gate 	    iocp->ioc_cmd == TIOCMGET ? "TIOCMGET" :
33357c478bd9Sstevel@tonic-gate 	    iocp->ioc_cmd == TIOCMSET ? "TIOCMSET" :
33367c478bd9Sstevel@tonic-gate 	    iocp->ioc_cmd == TIOCMBIS ? "TIOCMBIS" :
33377c478bd9Sstevel@tonic-gate 	    iocp->ioc_cmd == TIOCMBIC ? "TIOCMBIC" :
33387c478bd9Sstevel@tonic-gate 	    "other");
33392df1fe9cSrandyf 
33407c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
33417c478bd9Sstevel@tonic-gate 	case TIOCMGET:
33427c478bd9Sstevel@tonic-gate 	case TIOCGPPS:
33437c478bd9Sstevel@tonic-gate 	case TIOCSPPS:
33447c478bd9Sstevel@tonic-gate 	case TIOCGPPSEV:
33457c478bd9Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
33467c478bd9Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
33477c478bd9Sstevel@tonic-gate 	case CONSSETABORTENABLE:
33487c478bd9Sstevel@tonic-gate 	case CONSGETABORTENABLE:
33497c478bd9Sstevel@tonic-gate 		error = -1; /* Do Nothing */
33507c478bd9Sstevel@tonic-gate 		break;
33517c478bd9Sstevel@tonic-gate 	default:
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate 		/*
33547c478bd9Sstevel@tonic-gate 		 * The only way in which "ttycommon_ioctl" can fail is if the
33557c478bd9Sstevel@tonic-gate 		 * "ioctl" requires a response containing data to be returned
33567c478bd9Sstevel@tonic-gate 		 * to the user, and no mblk could be allocated for the data.
33577c478bd9Sstevel@tonic-gate 		 * No such "ioctl" alters our state.  Thus, we always go ahead
33587c478bd9Sstevel@tonic-gate 		 * and do any state-changes the "ioctl" calls for.  If we
33597c478bd9Sstevel@tonic-gate 		 * couldn't allocate the data, "ttycommon_ioctl" has stashed
33607c478bd9Sstevel@tonic-gate 		 * the "ioctl" away safely, so we just call "bufcall" to
33617c478bd9Sstevel@tonic-gate 		 * request that we be called back when we stand a better
33627c478bd9Sstevel@tonic-gate 		 * chance of allocating the data.
33637c478bd9Sstevel@tonic-gate 		 */
33647c478bd9Sstevel@tonic-gate 		if ((datasize = ttycommon_ioctl(tp, wq, mp, &error)) != 0) {
33657c478bd9Sstevel@tonic-gate 			if (async->async_wbufcid)
33667c478bd9Sstevel@tonic-gate 				unbufcall(async->async_wbufcid);
33677c478bd9Sstevel@tonic-gate 			async->async_wbufcid = bufcall(datasize, BPRI_HI,
33687c478bd9Sstevel@tonic-gate 			    (void (*)(void *)) async_reioctl,
33697c478bd9Sstevel@tonic-gate 			    (void *)(intptr_t)async->async_common->asy_unit);
33707c478bd9Sstevel@tonic-gate 			return;
33717c478bd9Sstevel@tonic-gate 		}
33727c478bd9Sstevel@tonic-gate 	}
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
33757c478bd9Sstevel@tonic-gate 
33767c478bd9Sstevel@tonic-gate 	if (error == 0) {
33777c478bd9Sstevel@tonic-gate 		/*
33787c478bd9Sstevel@tonic-gate 		 * "ttycommon_ioctl" did most of the work; we just use the
33797c478bd9Sstevel@tonic-gate 		 * data it set up.
33807c478bd9Sstevel@tonic-gate 		 */
33817c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
33827c478bd9Sstevel@tonic-gate 
33837c478bd9Sstevel@tonic-gate 		case TCSETS:
33847c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
33857c478bd9Sstevel@tonic-gate 			if (asy_baudok(asy))
33867c478bd9Sstevel@tonic-gate 				asy_program(asy, ASY_NOINIT);
33877c478bd9Sstevel@tonic-gate 			else
33887c478bd9Sstevel@tonic-gate 				error = EINVAL;
33897c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
33907c478bd9Sstevel@tonic-gate 			break;
33917c478bd9Sstevel@tonic-gate 		case TCSETSF:
33927c478bd9Sstevel@tonic-gate 		case TCSETSW:
33937c478bd9Sstevel@tonic-gate 		case TCSETA:
33947c478bd9Sstevel@tonic-gate 		case TCSETAW:
33957c478bd9Sstevel@tonic-gate 		case TCSETAF:
33967c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
33977c478bd9Sstevel@tonic-gate 			if (!asy_baudok(asy))
33987c478bd9Sstevel@tonic-gate 				error = EINVAL;
33997c478bd9Sstevel@tonic-gate 			else {
34007c478bd9Sstevel@tonic-gate 				if (asy_isbusy(asy))
34017c478bd9Sstevel@tonic-gate 					asy_waiteot(asy);
34027c478bd9Sstevel@tonic-gate 				asy_program(asy, ASY_NOINIT);
34037c478bd9Sstevel@tonic-gate 			}
34047c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
34057c478bd9Sstevel@tonic-gate 			break;
34067c478bd9Sstevel@tonic-gate 		}
34077c478bd9Sstevel@tonic-gate 	} else if (error < 0) {
34087c478bd9Sstevel@tonic-gate 		/*
34097c478bd9Sstevel@tonic-gate 		 * "ttycommon_ioctl" didn't do anything; we process it here.
34107c478bd9Sstevel@tonic-gate 		 */
34117c478bd9Sstevel@tonic-gate 		error = 0;
34127c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
34137c478bd9Sstevel@tonic-gate 
34147c478bd9Sstevel@tonic-gate 		case TIOCGPPS:
34157c478bd9Sstevel@tonic-gate 			/*
34167c478bd9Sstevel@tonic-gate 			 * Get PPS on/off.
34177c478bd9Sstevel@tonic-gate 			 */
34187c478bd9Sstevel@tonic-gate 			if (mp->b_cont != NULL)
34197c478bd9Sstevel@tonic-gate 				freemsg(mp->b_cont);
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate 			mp->b_cont = allocb(sizeof (int), BPRI_HI);
34227c478bd9Sstevel@tonic-gate 			if (mp->b_cont == NULL) {
34237c478bd9Sstevel@tonic-gate 				error = ENOMEM;
34247c478bd9Sstevel@tonic-gate 				break;
34257c478bd9Sstevel@tonic-gate 			}
34267c478bd9Sstevel@tonic-gate 			if (asy->asy_flags & ASY_PPS)
34277c478bd9Sstevel@tonic-gate 				*(int *)mp->b_cont->b_wptr = 1;
34287c478bd9Sstevel@tonic-gate 			else
34297c478bd9Sstevel@tonic-gate 				*(int *)mp->b_cont->b_wptr = 0;
34307c478bd9Sstevel@tonic-gate 			mp->b_cont->b_wptr += sizeof (int);
34317c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
34327c478bd9Sstevel@tonic-gate 			iocp->ioc_count = sizeof (int);
34337c478bd9Sstevel@tonic-gate 			break;
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 		case TIOCSPPS:
34367c478bd9Sstevel@tonic-gate 			/*
34377c478bd9Sstevel@tonic-gate 			 * Set PPS on/off.
34387c478bd9Sstevel@tonic-gate 			 */
34397c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
34407c478bd9Sstevel@tonic-gate 			if (error != 0)
34417c478bd9Sstevel@tonic-gate 				break;
34427c478bd9Sstevel@tonic-gate 
34437c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
34447c478bd9Sstevel@tonic-gate 			if (*(int *)mp->b_cont->b_rptr)
34457c478bd9Sstevel@tonic-gate 				asy->asy_flags |= ASY_PPS;
34467c478bd9Sstevel@tonic-gate 			else
34477c478bd9Sstevel@tonic-gate 				asy->asy_flags &= ~ASY_PPS;
34487c478bd9Sstevel@tonic-gate 			/* Reset edge sense */
34497c478bd9Sstevel@tonic-gate 			asy->asy_flags &= ~ASY_PPS_EDGE;
34507c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
34517c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
34527c478bd9Sstevel@tonic-gate 			break;
34537c478bd9Sstevel@tonic-gate 
34547c478bd9Sstevel@tonic-gate 		case TIOCGPPSEV:
34557c478bd9Sstevel@tonic-gate 		{
34567c478bd9Sstevel@tonic-gate 			/*
34577c478bd9Sstevel@tonic-gate 			 * Get PPS event data.
34587c478bd9Sstevel@tonic-gate 			 */
34597c478bd9Sstevel@tonic-gate 			mblk_t *bp;
34607c478bd9Sstevel@tonic-gate 			void *buf;
34617c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
34627c478bd9Sstevel@tonic-gate 			struct ppsclockev32 p32;
34637c478bd9Sstevel@tonic-gate #endif
34647c478bd9Sstevel@tonic-gate 			struct ppsclockev ppsclockev;
34657c478bd9Sstevel@tonic-gate 
34667c478bd9Sstevel@tonic-gate 			if (mp->b_cont != NULL) {
34677c478bd9Sstevel@tonic-gate 				freemsg(mp->b_cont);
34687c478bd9Sstevel@tonic-gate 				mp->b_cont = NULL;
34697c478bd9Sstevel@tonic-gate 			}
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 			if ((asy->asy_flags & ASY_PPS) == 0) {
34727c478bd9Sstevel@tonic-gate 				error = ENXIO;
34737c478bd9Sstevel@tonic-gate 				break;
34747c478bd9Sstevel@tonic-gate 			}
34757c478bd9Sstevel@tonic-gate 
34767c478bd9Sstevel@tonic-gate 			/* Protect from incomplete asy_ppsev */
34777c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
34787c478bd9Sstevel@tonic-gate 			ppsclockev = asy_ppsev;
34797c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
34807c478bd9Sstevel@tonic-gate 
34817c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
34827c478bd9Sstevel@tonic-gate 			if ((iocp->ioc_flag & IOC_MODELS) != IOC_NATIVE) {
34837c478bd9Sstevel@tonic-gate 				TIMEVAL_TO_TIMEVAL32(&p32.tv, &ppsclockev.tv);
34847c478bd9Sstevel@tonic-gate 				p32.serial = ppsclockev.serial;
34857c478bd9Sstevel@tonic-gate 				buf = &p32;
34867c478bd9Sstevel@tonic-gate 				iocp->ioc_count = sizeof (struct ppsclockev32);
34877c478bd9Sstevel@tonic-gate 			} else
34887c478bd9Sstevel@tonic-gate #endif
34897c478bd9Sstevel@tonic-gate 			{
34907c478bd9Sstevel@tonic-gate 				buf = &ppsclockev;
34917c478bd9Sstevel@tonic-gate 				iocp->ioc_count = sizeof (struct ppsclockev);
34927c478bd9Sstevel@tonic-gate 			}
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate 			if ((bp = allocb(iocp->ioc_count, BPRI_HI)) == NULL) {
34957c478bd9Sstevel@tonic-gate 				error = ENOMEM;
34967c478bd9Sstevel@tonic-gate 				break;
34977c478bd9Sstevel@tonic-gate 			}
34987c478bd9Sstevel@tonic-gate 			mp->b_cont = bp;
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate 			bcopy(buf, bp->b_wptr, iocp->ioc_count);
35017c478bd9Sstevel@tonic-gate 			bp->b_wptr += iocp->ioc_count;
35027c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
35037c478bd9Sstevel@tonic-gate 			break;
35047c478bd9Sstevel@tonic-gate 		}
35057c478bd9Sstevel@tonic-gate 
35067c478bd9Sstevel@tonic-gate 		case TCSBRK:
35077c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
35087c478bd9Sstevel@tonic-gate 			if (error != 0)
35097c478bd9Sstevel@tonic-gate 				break;
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate 			if (*(int *)mp->b_cont->b_rptr == 0) {
35127c478bd9Sstevel@tonic-gate 
35137c478bd9Sstevel@tonic-gate 				/*
35147c478bd9Sstevel@tonic-gate 				 * XXX Arrangements to ensure that a break
35157c478bd9Sstevel@tonic-gate 				 * isn't in progress should be sufficient.
35167c478bd9Sstevel@tonic-gate 				 * This ugly delay() is the only thing
35177c478bd9Sstevel@tonic-gate 				 * that seems to work on the NCR Worldmark.
35187c478bd9Sstevel@tonic-gate 				 * It should be replaced. Note that an
35197c478bd9Sstevel@tonic-gate 				 * asy_waiteot() also does not work.
35207c478bd9Sstevel@tonic-gate 				 */
35217c478bd9Sstevel@tonic-gate 				if (asydelay)
35227c478bd9Sstevel@tonic-gate 					delay(drv_usectohz(asydelay));
35237c478bd9Sstevel@tonic-gate 
35247c478bd9Sstevel@tonic-gate 				while (async->async_flags & ASYNC_BREAK) {
35257c478bd9Sstevel@tonic-gate 					cv_wait(&async->async_flags_cv,
35267c478bd9Sstevel@tonic-gate 					    &asy->asy_excl);
35277c478bd9Sstevel@tonic-gate 				}
35287c478bd9Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
35297c478bd9Sstevel@tonic-gate 				/*
35307c478bd9Sstevel@tonic-gate 				 * We loop until the TSR is empty and then
35317c478bd9Sstevel@tonic-gate 				 * set the break.  ASYNC_BREAK has been set
35327c478bd9Sstevel@tonic-gate 				 * to ensure that no characters are
35337c478bd9Sstevel@tonic-gate 				 * transmitted while the TSR is being
35347c478bd9Sstevel@tonic-gate 				 * flushed and SOUT is being used for the
35357c478bd9Sstevel@tonic-gate 				 * break signal.
35367c478bd9Sstevel@tonic-gate 				 *
35377c478bd9Sstevel@tonic-gate 				 * The wait period is equal to
35387c478bd9Sstevel@tonic-gate 				 * clock / (baud * 16) * 16 * 2.
35397c478bd9Sstevel@tonic-gate 				 */
35407c478bd9Sstevel@tonic-gate 				index = BAUDINDEX(
35417c478bd9Sstevel@tonic-gate 				    async->async_ttycommon.t_cflag);
35427c478bd9Sstevel@tonic-gate 				async->async_flags |= ASYNC_BREAK;
35432df1fe9cSrandyf 
35444ab75253Smrj 				while ((ddi_get8(asy->asy_iohandle,
35457c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + LSR) & XSRE) == 0) {
35467c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
35477c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
35487c478bd9Sstevel@tonic-gate 					drv_usecwait(
35497c478bd9Sstevel@tonic-gate 					    32*asyspdtab[index] & 0xfff);
35507c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
35517c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
35527c478bd9Sstevel@tonic-gate 				}
35537c478bd9Sstevel@tonic-gate 				/*
35547c478bd9Sstevel@tonic-gate 				 * Arrange for "async_restart"
35557c478bd9Sstevel@tonic-gate 				 * to be called in 1/4 second;
35567c478bd9Sstevel@tonic-gate 				 * it will turn the break bit off, and call
35577c478bd9Sstevel@tonic-gate 				 * "async_start" to grab the next message.
35587c478bd9Sstevel@tonic-gate 				 */
35594ab75253Smrj 				val = ddi_get8(asy->asy_iohandle,
35607c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + LCR);
35614ab75253Smrj 				ddi_put8(asy->asy_iohandle,
35627c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + LCR,
35637c478bd9Sstevel@tonic-gate 				    (val | SETBREAK));
35647c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
35657c478bd9Sstevel@tonic-gate 				(void) timeout(async_restart, (caddr_t)async,
35667c478bd9Sstevel@tonic-gate 				    drv_usectohz(1000000)/4);
35677c478bd9Sstevel@tonic-gate 			} else {
35687c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_OUT,
35697c478bd9Sstevel@tonic-gate 				    "async%d_ioctl: wait for flush.\n",
35707c478bd9Sstevel@tonic-gate 				    instance);
35717c478bd9Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
35727c478bd9Sstevel@tonic-gate 				asy_waiteot(asy);
35737c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
35747c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_OUT,
35757c478bd9Sstevel@tonic-gate 				    "async%d_ioctl: ldterm satisfied.\n",
35767c478bd9Sstevel@tonic-gate 				    instance);
35777c478bd9Sstevel@tonic-gate 			}
35787c478bd9Sstevel@tonic-gate 			break;
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 		case TIOCSBRK:
35817c478bd9Sstevel@tonic-gate 			if (!(async->async_flags & ASYNC_OUT_SUSPEND)) {
35827c478bd9Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
35837c478bd9Sstevel@tonic-gate 				async->async_flags |= ASYNC_OUT_SUSPEND;
35847c478bd9Sstevel@tonic-gate 				async->async_flags |= ASYNC_HOLD_UTBRK;
35857c478bd9Sstevel@tonic-gate 				index = BAUDINDEX(
35867c478bd9Sstevel@tonic-gate 				    async->async_ttycommon.t_cflag);
35874ab75253Smrj 				while ((ddi_get8(asy->asy_iohandle,
35887c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + LSR) & XSRE) == 0) {
35897c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
35907c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl);
35917c478bd9Sstevel@tonic-gate 					drv_usecwait(
35927c478bd9Sstevel@tonic-gate 					    32*asyspdtab[index] & 0xfff);
35937c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl);
35947c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
35957c478bd9Sstevel@tonic-gate 				}
35964ab75253Smrj 				val = ddi_get8(asy->asy_iohandle,
35977c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + LCR);
35984ab75253Smrj 				ddi_put8(asy->asy_iohandle,
35997c478bd9Sstevel@tonic-gate 				    asy->asy_ioaddr + LCR, (val | SETBREAK));
36007c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
36017c478bd9Sstevel@tonic-gate 				/* wait for 100ms to hold BREAK */
36027c478bd9Sstevel@tonic-gate 				async->async_utbrktid =
36037c478bd9Sstevel@tonic-gate 				    timeout((void (*)())async_hold_utbrk,
36047c478bd9Sstevel@tonic-gate 				    (caddr_t)async,
36057c478bd9Sstevel@tonic-gate 				    drv_usectohz(asy_min_utbrk));
36067c478bd9Sstevel@tonic-gate 			}
36077c478bd9Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
36087c478bd9Sstevel@tonic-gate 			break;
36097c478bd9Sstevel@tonic-gate 
36107c478bd9Sstevel@tonic-gate 		case TIOCCBRK:
36117c478bd9Sstevel@tonic-gate 			if (async->async_flags & ASYNC_OUT_SUSPEND)
36127c478bd9Sstevel@tonic-gate 				async_resume_utbrk(async);
36137c478bd9Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
36147c478bd9Sstevel@tonic-gate 			break;
36157c478bd9Sstevel@tonic-gate 
36167c478bd9Sstevel@tonic-gate 		case TIOCMSET:
36177c478bd9Sstevel@tonic-gate 		case TIOCMBIS:
36187c478bd9Sstevel@tonic-gate 		case TIOCMBIC:
36197c478bd9Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT) {
36207c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
36217c478bd9Sstevel@tonic-gate 				    "non-transparent\n", instance);
36227c478bd9Sstevel@tonic-gate 
36237c478bd9Sstevel@tonic-gate 				error = miocpullup(mp, sizeof (int));
36247c478bd9Sstevel@tonic-gate 				if (error != 0)
36257c478bd9Sstevel@tonic-gate 					break;
36267c478bd9Sstevel@tonic-gate 
36277c478bd9Sstevel@tonic-gate 				mutex_enter(&asy->asy_excl_hi);
36287c478bd9Sstevel@tonic-gate 				(void) asymctl(asy,
36297c478bd9Sstevel@tonic-gate 				    dmtoasy(*(int *)mp->b_cont->b_rptr),
36307c478bd9Sstevel@tonic-gate 				    iocp->ioc_cmd);
36317c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl_hi);
36327c478bd9Sstevel@tonic-gate 				iocp->ioc_error = 0;
36337c478bd9Sstevel@tonic-gate 				mp->b_datap->db_type = M_IOCACK;
36347c478bd9Sstevel@tonic-gate 			} else {
36357c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
36367c478bd9Sstevel@tonic-gate 				    "transparent\n", instance);
36377c478bd9Sstevel@tonic-gate 				mcopyin(mp, NULL, sizeof (int), NULL);
36387c478bd9Sstevel@tonic-gate 			}
36397c478bd9Sstevel@tonic-gate 			break;
36407c478bd9Sstevel@tonic-gate 
36417c478bd9Sstevel@tonic-gate 		case TIOCMGET:
36427c478bd9Sstevel@tonic-gate 			datamp = allocb(sizeof (int), BPRI_MED);
36437c478bd9Sstevel@tonic-gate 			if (datamp == NULL) {
36447c478bd9Sstevel@tonic-gate 				error = EAGAIN;
36457c478bd9Sstevel@tonic-gate 				break;
36467c478bd9Sstevel@tonic-gate 			}
36477c478bd9Sstevel@tonic-gate 
36487c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
36497c478bd9Sstevel@tonic-gate 			*(int *)datamp->b_rptr = asymctl(asy, 0, TIOCMGET);
36507c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
36517c478bd9Sstevel@tonic-gate 
36527c478bd9Sstevel@tonic-gate 			if (iocp->ioc_count == TRANSPARENT) {
36537c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
36547c478bd9Sstevel@tonic-gate 				    "transparent\n", instance);
36552df1fe9cSrandyf 				mcopyout(mp, NULL, sizeof (int), NULL, datamp);
36567c478bd9Sstevel@tonic-gate 			} else {
36577c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_IOCTL, "async%d_ioctl: "
36587c478bd9Sstevel@tonic-gate 				    "non-transparent\n", instance);
36597c478bd9Sstevel@tonic-gate 				mioc2ack(mp, datamp, sizeof (int), 0);
36607c478bd9Sstevel@tonic-gate 			}
36617c478bd9Sstevel@tonic-gate 			break;
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate 		case CONSOPENPOLLEDIO:
36647c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (struct cons_polledio *));
36657c478bd9Sstevel@tonic-gate 			if (error != 0)
36667c478bd9Sstevel@tonic-gate 				break;
36677c478bd9Sstevel@tonic-gate 
36687c478bd9Sstevel@tonic-gate 			*(struct cons_polledio **)mp->b_cont->b_rptr =
36697c478bd9Sstevel@tonic-gate 			    &asy->polledio;
36707c478bd9Sstevel@tonic-gate 
36717c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
36727c478bd9Sstevel@tonic-gate 			break;
36737c478bd9Sstevel@tonic-gate 
36747c478bd9Sstevel@tonic-gate 		case CONSCLOSEPOLLEDIO:
36757c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
36767c478bd9Sstevel@tonic-gate 			iocp->ioc_error = 0;
36777c478bd9Sstevel@tonic-gate 			iocp->ioc_rval = 0;
36787c478bd9Sstevel@tonic-gate 			break;
36797c478bd9Sstevel@tonic-gate 
36807c478bd9Sstevel@tonic-gate 		case CONSSETABORTENABLE:
36817c478bd9Sstevel@tonic-gate 			error = secpolicy_console(iocp->ioc_cr);
36827c478bd9Sstevel@tonic-gate 			if (error != 0)
36837c478bd9Sstevel@tonic-gate 				break;
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT) {
36867c478bd9Sstevel@tonic-gate 				error = EINVAL;
36877c478bd9Sstevel@tonic-gate 				break;
36887c478bd9Sstevel@tonic-gate 			}
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate 			if (*(intptr_t *)mp->b_cont->b_rptr)
36917c478bd9Sstevel@tonic-gate 				asy->asy_flags |= ASY_CONSOLE;
36927c478bd9Sstevel@tonic-gate 			else
36937c478bd9Sstevel@tonic-gate 				asy->asy_flags &= ~ASY_CONSOLE;
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
36967c478bd9Sstevel@tonic-gate 			iocp->ioc_error = 0;
36977c478bd9Sstevel@tonic-gate 			iocp->ioc_rval = 0;
36987c478bd9Sstevel@tonic-gate 			break;
36997c478bd9Sstevel@tonic-gate 
37007c478bd9Sstevel@tonic-gate 		case CONSGETABORTENABLE:
37017c478bd9Sstevel@tonic-gate 			/*CONSTANTCONDITION*/
37027c478bd9Sstevel@tonic-gate 			ASSERT(sizeof (boolean_t) <= sizeof (boolean_t *));
37037c478bd9Sstevel@tonic-gate 			/*
37047c478bd9Sstevel@tonic-gate 			 * Store the return value right in the payload
37057c478bd9Sstevel@tonic-gate 			 * we were passed.  Crude.
37067c478bd9Sstevel@tonic-gate 			 */
37077c478bd9Sstevel@tonic-gate 			mcopyout(mp, NULL, sizeof (boolean_t), NULL, NULL);
37087c478bd9Sstevel@tonic-gate 			*(boolean_t *)mp->b_cont->b_rptr =
37097c478bd9Sstevel@tonic-gate 			    (asy->asy_flags & ASY_CONSOLE) != 0;
37107c478bd9Sstevel@tonic-gate 			break;
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate 		default:
37137c478bd9Sstevel@tonic-gate 			/*
37147c478bd9Sstevel@tonic-gate 			 * If we don't understand it, it's an error.  NAK it.
37157c478bd9Sstevel@tonic-gate 			 */
37167c478bd9Sstevel@tonic-gate 			error = EINVAL;
37177c478bd9Sstevel@tonic-gate 			break;
37187c478bd9Sstevel@tonic-gate 		}
37197c478bd9Sstevel@tonic-gate 	}
37207c478bd9Sstevel@tonic-gate 	if (error != 0) {
37217c478bd9Sstevel@tonic-gate 		iocp->ioc_error = error;
37227c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
37237c478bd9Sstevel@tonic-gate 	}
37247c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
37257c478bd9Sstevel@tonic-gate 	qreply(wq, mp);
37267c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_PROCS, "async%d_ioctl: done\n", instance);
37277c478bd9Sstevel@tonic-gate }
37287c478bd9Sstevel@tonic-gate 
37297c478bd9Sstevel@tonic-gate static int
37307c478bd9Sstevel@tonic-gate asyrsrv(queue_t *q)
37317c478bd9Sstevel@tonic-gate {
37327c478bd9Sstevel@tonic-gate 	mblk_t *bp;
37337c478bd9Sstevel@tonic-gate 	struct asyncline *async;
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate 	async = (struct asyncline *)q->q_ptr;
37367c478bd9Sstevel@tonic-gate 
37377c478bd9Sstevel@tonic-gate 	while (canputnext(q) && (bp = getq(q)))
37387c478bd9Sstevel@tonic-gate 		putnext(q, bp);
37397c478bd9Sstevel@tonic-gate 	ASYSETSOFT(async->async_common);
37407c478bd9Sstevel@tonic-gate 	async->async_polltid = 0;
37417c478bd9Sstevel@tonic-gate 	return (0);
37427c478bd9Sstevel@tonic-gate }
37437c478bd9Sstevel@tonic-gate 
37447c478bd9Sstevel@tonic-gate /*
37452df1fe9cSrandyf  * The ASYWPUTDO_NOT_SUSP macro indicates to asywputdo() whether it should
37462df1fe9cSrandyf  * handle messages as though the driver is operating normally or is
37472df1fe9cSrandyf  * suspended.  In the suspended case, some or all of the processing may have
37482df1fe9cSrandyf  * to be delayed until the driver is resumed.
37492df1fe9cSrandyf  */
37502df1fe9cSrandyf #define	ASYWPUTDO_NOT_SUSP(async, wput) \
37512df1fe9cSrandyf 	!((wput) && ((async)->async_flags & ASYNC_DDI_SUSPENDED))
37522df1fe9cSrandyf 
37532df1fe9cSrandyf /*
37542df1fe9cSrandyf  * Processing for write queue put procedure.
37557c478bd9Sstevel@tonic-gate  * Respond to M_STOP, M_START, M_IOCTL, and M_FLUSH messages here;
37567c478bd9Sstevel@tonic-gate  * set the flow control character for M_STOPI and M_STARTI messages;
37577c478bd9Sstevel@tonic-gate  * queue up M_BREAK, M_DELAY, and M_DATA messages for processing
37587c478bd9Sstevel@tonic-gate  * by the start routine, and then call the start routine; discard
37597c478bd9Sstevel@tonic-gate  * everything else.  Note that this driver does not incorporate any
37607c478bd9Sstevel@tonic-gate  * mechanism to negotiate to handle the canonicalization process.
37617c478bd9Sstevel@tonic-gate  * It expects that these functions are handled in upper module(s),
37627c478bd9Sstevel@tonic-gate  * as we do in ldterm.
37637c478bd9Sstevel@tonic-gate  */
37647c478bd9Sstevel@tonic-gate static int
37652df1fe9cSrandyf asywputdo(queue_t *q, mblk_t *mp, boolean_t wput)
37667c478bd9Sstevel@tonic-gate {
37677c478bd9Sstevel@tonic-gate 	struct asyncline *async;
37687c478bd9Sstevel@tonic-gate 	struct asycom *asy;
37697c478bd9Sstevel@tonic-gate #ifdef DEBUG
37707c478bd9Sstevel@tonic-gate 	int instance;
37717c478bd9Sstevel@tonic-gate #endif
37727c478bd9Sstevel@tonic-gate 	int error;
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 	async = (struct asyncline *)q->q_ptr;
37752df1fe9cSrandyf 
37767c478bd9Sstevel@tonic-gate #ifdef DEBUG
37777c478bd9Sstevel@tonic-gate 	instance = UNIT(async->async_dev);
37787c478bd9Sstevel@tonic-gate #endif
37797c478bd9Sstevel@tonic-gate 	asy = async->async_common;
37807c478bd9Sstevel@tonic-gate 
37817c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
37827c478bd9Sstevel@tonic-gate 
37837c478bd9Sstevel@tonic-gate 	case M_STOP:
37847c478bd9Sstevel@tonic-gate 		/*
37857c478bd9Sstevel@tonic-gate 		 * Since we don't do real DMA, we can just let the
37867c478bd9Sstevel@tonic-gate 		 * chip coast to a stop after applying the brakes.
37877c478bd9Sstevel@tonic-gate 		 */
37887c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
37897c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_STOPPED;
37907c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
37917c478bd9Sstevel@tonic-gate 		freemsg(mp);
37927c478bd9Sstevel@tonic-gate 		break;
37937c478bd9Sstevel@tonic-gate 
37947c478bd9Sstevel@tonic-gate 	case M_START:
37957c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
37967c478bd9Sstevel@tonic-gate 		if (async->async_flags & ASYNC_STOPPED) {
37977c478bd9Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_STOPPED;
37982df1fe9cSrandyf 			if (ASYWPUTDO_NOT_SUSP(async, wput)) {
37997c478bd9Sstevel@tonic-gate 				/*
38007c478bd9Sstevel@tonic-gate 				 * If an output operation is in progress,
38017c478bd9Sstevel@tonic-gate 				 * resume it.  Otherwise, prod the start
38027c478bd9Sstevel@tonic-gate 				 * routine.
38037c478bd9Sstevel@tonic-gate 				 */
38047c478bd9Sstevel@tonic-gate 				if (async->async_ocnt > 0) {
38057c478bd9Sstevel@tonic-gate 					mutex_enter(&asy->asy_excl_hi);
38067c478bd9Sstevel@tonic-gate 					async_resume(async);
38077c478bd9Sstevel@tonic-gate 					mutex_exit(&asy->asy_excl_hi);
38087c478bd9Sstevel@tonic-gate 				} else {
38097c478bd9Sstevel@tonic-gate 					async_start(async);
38107c478bd9Sstevel@tonic-gate 				}
38117c478bd9Sstevel@tonic-gate 			}
38122df1fe9cSrandyf 		}
38137c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
38147c478bd9Sstevel@tonic-gate 		freemsg(mp);
38157c478bd9Sstevel@tonic-gate 		break;
38167c478bd9Sstevel@tonic-gate 
38177c478bd9Sstevel@tonic-gate 	case M_IOCTL:
38187c478bd9Sstevel@tonic-gate 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
38197c478bd9Sstevel@tonic-gate 
38207c478bd9Sstevel@tonic-gate 		case TCSBRK:
38217c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
38227c478bd9Sstevel@tonic-gate 			if (error != 0) {
38237c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, error);
38247c478bd9Sstevel@tonic-gate 				return (0);
38257c478bd9Sstevel@tonic-gate 			}
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate 			if (*(int *)mp->b_cont->b_rptr != 0) {
38287c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_OUT,
38297c478bd9Sstevel@tonic-gate 				    "async%d_ioctl: flush request.\n",
38307c478bd9Sstevel@tonic-gate 				    instance);
38317c478bd9Sstevel@tonic-gate 				(void) putq(q, mp);
38327c478bd9Sstevel@tonic-gate 
38332df1fe9cSrandyf 				mutex_enter(&asy->asy_excl);
38342df1fe9cSrandyf 				if (ASYWPUTDO_NOT_SUSP(async, wput)) {
38357c478bd9Sstevel@tonic-gate 					/*
38367c478bd9Sstevel@tonic-gate 					 * If an TIOCSBRK is in progress,
38377c478bd9Sstevel@tonic-gate 					 * clean it as TIOCCBRK does,
38387c478bd9Sstevel@tonic-gate 					 * then kick off output.
38397c478bd9Sstevel@tonic-gate 					 * If TIOCSBRK is not in progress,
38407c478bd9Sstevel@tonic-gate 					 * just kick off output.
38417c478bd9Sstevel@tonic-gate 					 */
38427c478bd9Sstevel@tonic-gate 					async_resume_utbrk(async);
38432df1fe9cSrandyf 				}
38447c478bd9Sstevel@tonic-gate 				mutex_exit(&asy->asy_excl);
38457c478bd9Sstevel@tonic-gate 				break;
38467c478bd9Sstevel@tonic-gate 			}
38477c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
38487c478bd9Sstevel@tonic-gate 		case TCSETSW:
38497c478bd9Sstevel@tonic-gate 		case TCSETSF:
38507c478bd9Sstevel@tonic-gate 		case TCSETAW:
38517c478bd9Sstevel@tonic-gate 		case TCSETAF:
38527c478bd9Sstevel@tonic-gate 			/*
38537c478bd9Sstevel@tonic-gate 			 * The changes do not take effect until all
38547c478bd9Sstevel@tonic-gate 			 * output queued before them is drained.
38557c478bd9Sstevel@tonic-gate 			 * Put this message on the queue, so that
38567c478bd9Sstevel@tonic-gate 			 * "async_start" will see it when it's done
38577c478bd9Sstevel@tonic-gate 			 * with the output before it.  Poke the
38587c478bd9Sstevel@tonic-gate 			 * start routine, just in case.
38597c478bd9Sstevel@tonic-gate 			 */
38607c478bd9Sstevel@tonic-gate 			(void) putq(q, mp);
38617c478bd9Sstevel@tonic-gate 
38622df1fe9cSrandyf 			mutex_enter(&asy->asy_excl);
38632df1fe9cSrandyf 			if (ASYWPUTDO_NOT_SUSP(async, wput)) {
38647c478bd9Sstevel@tonic-gate 				/*
38657c478bd9Sstevel@tonic-gate 				 * If an TIOCSBRK is in progress,
38667c478bd9Sstevel@tonic-gate 				 * clean it as TIOCCBRK does.
38677c478bd9Sstevel@tonic-gate 				 * then kick off output.
38687c478bd9Sstevel@tonic-gate 				 * If TIOCSBRK is not in progress,
38697c478bd9Sstevel@tonic-gate 				 * just kick off output.
38707c478bd9Sstevel@tonic-gate 				 */
38717c478bd9Sstevel@tonic-gate 				async_resume_utbrk(async);
38722df1fe9cSrandyf 			}
38737c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
38747c478bd9Sstevel@tonic-gate 			break;
38757c478bd9Sstevel@tonic-gate 
38767c478bd9Sstevel@tonic-gate 		default:
38777c478bd9Sstevel@tonic-gate 			/*
38787c478bd9Sstevel@tonic-gate 			 * Do it now.
38797c478bd9Sstevel@tonic-gate 			 */
38802df1fe9cSrandyf 			mutex_enter(&asy->asy_excl);
38812df1fe9cSrandyf 			if (ASYWPUTDO_NOT_SUSP(async, wput)) {
38822df1fe9cSrandyf 				mutex_exit(&asy->asy_excl);
38837c478bd9Sstevel@tonic-gate 				async_ioctl(async, q, mp);
38847c478bd9Sstevel@tonic-gate 				break;
38857c478bd9Sstevel@tonic-gate 			}
38862df1fe9cSrandyf 			async_put_suspq(asy, mp);
38872df1fe9cSrandyf 			mutex_exit(&asy->asy_excl);
38882df1fe9cSrandyf 			break;
38892df1fe9cSrandyf 		}
38907c478bd9Sstevel@tonic-gate 		break;
38917c478bd9Sstevel@tonic-gate 
38927c478bd9Sstevel@tonic-gate 	case M_FLUSH:
38937c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
38947c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
38957c478bd9Sstevel@tonic-gate 
38967c478bd9Sstevel@tonic-gate 			/*
38977c478bd9Sstevel@tonic-gate 			 * Abort any output in progress.
38987c478bd9Sstevel@tonic-gate 			 */
38997c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
39007c478bd9Sstevel@tonic-gate 			if (async->async_flags & ASYNC_BUSY) {
39017c478bd9Sstevel@tonic-gate 				DEBUGCONT1(ASY_DEBUG_BUSY, "asy%dwput: "
39027c478bd9Sstevel@tonic-gate 				    "Clearing async_ocnt, "
39037c478bd9Sstevel@tonic-gate 				    "leaving ASYNC_BUSY set\n",
39047c478bd9Sstevel@tonic-gate 				    instance);
39057c478bd9Sstevel@tonic-gate 				async->async_ocnt = 0;
39067c478bd9Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_BUSY;
39077c478bd9Sstevel@tonic-gate 			} /* if */
39082df1fe9cSrandyf 
39092df1fe9cSrandyf 			if (ASYWPUTDO_NOT_SUSP(async, wput)) {
39102df1fe9cSrandyf 				/* Flush FIFO buffers */
39112df1fe9cSrandyf 				if (asy->asy_use_fifo == FIFO_ON) {
39122df1fe9cSrandyf 					asy_reset_fifo(asy, FIFOTXFLSH);
39132df1fe9cSrandyf 				}
39142df1fe9cSrandyf 			}
39157c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
39167c478bd9Sstevel@tonic-gate 
39177c478bd9Sstevel@tonic-gate 			/* Flush FIFO buffers */
39187c478bd9Sstevel@tonic-gate 			if (asy->asy_use_fifo == FIFO_ON) {
39197c478bd9Sstevel@tonic-gate 				asy_reset_fifo(asy, FIFOTXFLSH);
39207c478bd9Sstevel@tonic-gate 			}
39217c478bd9Sstevel@tonic-gate 
39227c478bd9Sstevel@tonic-gate 			/*
39237c478bd9Sstevel@tonic-gate 			 * Flush our write queue.
39247c478bd9Sstevel@tonic-gate 			 */
39257c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
39267c478bd9Sstevel@tonic-gate 			if (async->async_xmitblk != NULL) {
39277c478bd9Sstevel@tonic-gate 				freeb(async->async_xmitblk);
39287c478bd9Sstevel@tonic-gate 				async->async_xmitblk = NULL;
39297c478bd9Sstevel@tonic-gate 			}
39307c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
39317c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;	/* it has been flushed */
39327c478bd9Sstevel@tonic-gate 		}
39337c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
39342df1fe9cSrandyf 			if (ASYWPUTDO_NOT_SUSP(async, wput)) {
39357c478bd9Sstevel@tonic-gate 				/* Flush FIFO buffers */
39367c478bd9Sstevel@tonic-gate 				if (asy->asy_use_fifo == FIFO_ON) {
39377c478bd9Sstevel@tonic-gate 					asy_reset_fifo(asy, FIFORXFLSH);
39387c478bd9Sstevel@tonic-gate 				}
39392df1fe9cSrandyf 			}
39407c478bd9Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
39417c478bd9Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
39427c478bd9Sstevel@tonic-gate 		} else {
39437c478bd9Sstevel@tonic-gate 			freemsg(mp);
39447c478bd9Sstevel@tonic-gate 		}
39457c478bd9Sstevel@tonic-gate 
39467c478bd9Sstevel@tonic-gate 		/*
39477c478bd9Sstevel@tonic-gate 		 * We must make sure we process messages that survive the
39487c478bd9Sstevel@tonic-gate 		 * write-side flush.
39497c478bd9Sstevel@tonic-gate 		 */
39502df1fe9cSrandyf 		if (ASYWPUTDO_NOT_SUSP(async, wput)) {
39517c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
39527c478bd9Sstevel@tonic-gate 			async_start(async);
39537c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
39542df1fe9cSrandyf 		}
39557c478bd9Sstevel@tonic-gate 		break;
39567c478bd9Sstevel@tonic-gate 
39577c478bd9Sstevel@tonic-gate 	case M_BREAK:
39587c478bd9Sstevel@tonic-gate 	case M_DELAY:
39597c478bd9Sstevel@tonic-gate 	case M_DATA:
39607c478bd9Sstevel@tonic-gate 		/*
39617c478bd9Sstevel@tonic-gate 		 * Queue the message up to be transmitted,
39627c478bd9Sstevel@tonic-gate 		 * and poke the start routine.
39637c478bd9Sstevel@tonic-gate 		 */
39647c478bd9Sstevel@tonic-gate 		(void) putq(q, mp);
39652df1fe9cSrandyf 		if (ASYWPUTDO_NOT_SUSP(async, wput)) {
39667c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
39677c478bd9Sstevel@tonic-gate 			async_start(async);
39687c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
39692df1fe9cSrandyf 		}
39707c478bd9Sstevel@tonic-gate 		break;
39717c478bd9Sstevel@tonic-gate 
39727c478bd9Sstevel@tonic-gate 	case M_STOPI:
39737c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
39742df1fe9cSrandyf 		if (ASYWPUTDO_NOT_SUSP(async, wput)) {
39757c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
39767c478bd9Sstevel@tonic-gate 			if (!(async->async_inflow_source & IN_FLOW_USER)) {
39777c478bd9Sstevel@tonic-gate 				async_flowcontrol_hw_input(asy, FLOW_STOP,
39787c478bd9Sstevel@tonic-gate 				    IN_FLOW_USER);
39792df1fe9cSrandyf 				(void) async_flowcontrol_sw_input(asy,
39802df1fe9cSrandyf 				    FLOW_STOP, IN_FLOW_USER);
39817c478bd9Sstevel@tonic-gate 			}
39827c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
39837c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
39847c478bd9Sstevel@tonic-gate 			freemsg(mp);
39857c478bd9Sstevel@tonic-gate 			break;
39862df1fe9cSrandyf 		}
39872df1fe9cSrandyf 		async_put_suspq(asy, mp);
39882df1fe9cSrandyf 		mutex_exit(&asy->asy_excl);
39892df1fe9cSrandyf 		break;
39907c478bd9Sstevel@tonic-gate 
39917c478bd9Sstevel@tonic-gate 	case M_STARTI:
39927c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl);
39932df1fe9cSrandyf 		if (ASYWPUTDO_NOT_SUSP(async, wput)) {
39947c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl_hi);
39957c478bd9Sstevel@tonic-gate 			if (async->async_inflow_source & IN_FLOW_USER) {
39967c478bd9Sstevel@tonic-gate 				async_flowcontrol_hw_input(asy, FLOW_START,
39977c478bd9Sstevel@tonic-gate 				    IN_FLOW_USER);
39982df1fe9cSrandyf 				(void) async_flowcontrol_sw_input(asy,
39992df1fe9cSrandyf 				    FLOW_START, IN_FLOW_USER);
40007c478bd9Sstevel@tonic-gate 			}
40017c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl_hi);
40027c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
40037c478bd9Sstevel@tonic-gate 			freemsg(mp);
40047c478bd9Sstevel@tonic-gate 			break;
40052df1fe9cSrandyf 		}
40062df1fe9cSrandyf 		async_put_suspq(asy, mp);
40072df1fe9cSrandyf 		mutex_exit(&asy->asy_excl);
40082df1fe9cSrandyf 		break;
40097c478bd9Sstevel@tonic-gate 
40107c478bd9Sstevel@tonic-gate 	case M_CTL:
40117c478bd9Sstevel@tonic-gate 		if (MBLKL(mp) >= sizeof (struct iocblk) &&
40127c478bd9Sstevel@tonic-gate 		    ((struct iocblk *)mp->b_rptr)->ioc_cmd == MC_POSIXQUERY) {
40132df1fe9cSrandyf 			mutex_enter(&asy->asy_excl);
40142df1fe9cSrandyf 			if (ASYWPUTDO_NOT_SUSP(async, wput)) {
40152df1fe9cSrandyf 				((struct iocblk *)mp->b_rptr)->ioc_cmd =
40162df1fe9cSrandyf 				    MC_HAS_POSIX;
40172df1fe9cSrandyf 				mutex_exit(&asy->asy_excl);
40187c478bd9Sstevel@tonic-gate 				qreply(q, mp);
40192df1fe9cSrandyf 				break;
40202df1fe9cSrandyf 			} else {
40212df1fe9cSrandyf 				async_put_suspq(asy, mp);
40222df1fe9cSrandyf 			}
40237c478bd9Sstevel@tonic-gate 		} else {
40247c478bd9Sstevel@tonic-gate 			/*
40257c478bd9Sstevel@tonic-gate 			 * These MC_SERVICE type messages are used by upper
40267c478bd9Sstevel@tonic-gate 			 * modules to tell this driver to send input up
40277c478bd9Sstevel@tonic-gate 			 * immediately, or that it can wait for normal
40287c478bd9Sstevel@tonic-gate 			 * processing that may or may not be done.  Sun
40297c478bd9Sstevel@tonic-gate 			 * requires these for the mouse module.
40307c478bd9Sstevel@tonic-gate 			 * (XXX - for x86?)
40317c478bd9Sstevel@tonic-gate 			 */
40327c478bd9Sstevel@tonic-gate 			mutex_enter(&asy->asy_excl);
40337c478bd9Sstevel@tonic-gate 			switch (*mp->b_rptr) {
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate 			case MC_SERVICEIMM:
40367c478bd9Sstevel@tonic-gate 				async->async_flags |= ASYNC_SERVICEIMM;
40377c478bd9Sstevel@tonic-gate 				break;
40387c478bd9Sstevel@tonic-gate 
40397c478bd9Sstevel@tonic-gate 			case MC_SERVICEDEF:
40407c478bd9Sstevel@tonic-gate 				async->async_flags &= ~ASYNC_SERVICEIMM;
40417c478bd9Sstevel@tonic-gate 				break;
40427c478bd9Sstevel@tonic-gate 			}
40437c478bd9Sstevel@tonic-gate 			mutex_exit(&asy->asy_excl);
40447c478bd9Sstevel@tonic-gate 			freemsg(mp);
40457c478bd9Sstevel@tonic-gate 		}
40467c478bd9Sstevel@tonic-gate 		break;
40477c478bd9Sstevel@tonic-gate 
40487c478bd9Sstevel@tonic-gate 	case M_IOCDATA:
40492df1fe9cSrandyf 		mutex_enter(&asy->asy_excl);
40502df1fe9cSrandyf 		if (ASYWPUTDO_NOT_SUSP(async, wput)) {
40512df1fe9cSrandyf 			mutex_exit(&asy->asy_excl);
40527c478bd9Sstevel@tonic-gate 			async_iocdata(q, mp);
40537c478bd9Sstevel@tonic-gate 			break;
40542df1fe9cSrandyf 		}
40552df1fe9cSrandyf 		async_put_suspq(asy, mp);
40562df1fe9cSrandyf 		mutex_exit(&asy->asy_excl);
40572df1fe9cSrandyf 		break;
40587c478bd9Sstevel@tonic-gate 
40597c478bd9Sstevel@tonic-gate 	default:
40607c478bd9Sstevel@tonic-gate 		freemsg(mp);
40617c478bd9Sstevel@tonic-gate 		break;
40627c478bd9Sstevel@tonic-gate 	}
40637c478bd9Sstevel@tonic-gate 	return (0);
40647c478bd9Sstevel@tonic-gate }
40657c478bd9Sstevel@tonic-gate 
40662df1fe9cSrandyf static int
40672df1fe9cSrandyf asywput(queue_t *q, mblk_t *mp)
40682df1fe9cSrandyf {
40692df1fe9cSrandyf 	return (asywputdo(q, mp, B_TRUE));
40702df1fe9cSrandyf }
40712df1fe9cSrandyf 
40727c478bd9Sstevel@tonic-gate /*
40737c478bd9Sstevel@tonic-gate  * Retry an "ioctl", now that "bufcall" claims we may be able to allocate
40747c478bd9Sstevel@tonic-gate  * the buffer we need.
40757c478bd9Sstevel@tonic-gate  */
40767c478bd9Sstevel@tonic-gate static void
40777c478bd9Sstevel@tonic-gate async_reioctl(void *unit)
40787c478bd9Sstevel@tonic-gate {
40797c478bd9Sstevel@tonic-gate 	int instance = (uintptr_t)unit;
40807c478bd9Sstevel@tonic-gate 	struct asyncline *async;
40817c478bd9Sstevel@tonic-gate 	struct asycom *asy;
40827c478bd9Sstevel@tonic-gate 	queue_t	*q;
40837c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
40847c478bd9Sstevel@tonic-gate 
40857c478bd9Sstevel@tonic-gate 	asy = ddi_get_soft_state(asy_soft_state, instance);
40867c478bd9Sstevel@tonic-gate 	ASSERT(asy != NULL);
40877c478bd9Sstevel@tonic-gate 	async = asy->asy_priv;
40887c478bd9Sstevel@tonic-gate 
40897c478bd9Sstevel@tonic-gate 	/*
40907c478bd9Sstevel@tonic-gate 	 * The bufcall is no longer pending.
40917c478bd9Sstevel@tonic-gate 	 */
40927c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
40937c478bd9Sstevel@tonic-gate 	async->async_wbufcid = 0;
40947c478bd9Sstevel@tonic-gate 	if ((q = async->async_ttycommon.t_writeq) == NULL) {
40957c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
40967c478bd9Sstevel@tonic-gate 		return;
40977c478bd9Sstevel@tonic-gate 	}
40987c478bd9Sstevel@tonic-gate 	if ((mp = async->async_ttycommon.t_iocpending) != NULL) {
40997c478bd9Sstevel@tonic-gate 		/* not pending any more */
41007c478bd9Sstevel@tonic-gate 		async->async_ttycommon.t_iocpending = NULL;
41017c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
41027c478bd9Sstevel@tonic-gate 		async_ioctl(async, q, mp);
41037c478bd9Sstevel@tonic-gate 	} else
41047c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl);
41057c478bd9Sstevel@tonic-gate }
41067c478bd9Sstevel@tonic-gate 
41077c478bd9Sstevel@tonic-gate static void
41087c478bd9Sstevel@tonic-gate async_iocdata(queue_t *q, mblk_t *mp)
41097c478bd9Sstevel@tonic-gate {
41107c478bd9Sstevel@tonic-gate 	struct asyncline	*async = (struct asyncline *)q->q_ptr;
41117c478bd9Sstevel@tonic-gate 	struct asycom		*asy;
41127c478bd9Sstevel@tonic-gate 	struct iocblk *ip;
41137c478bd9Sstevel@tonic-gate 	struct copyresp *csp;
41147c478bd9Sstevel@tonic-gate #ifdef DEBUG
41157c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
41167c478bd9Sstevel@tonic-gate #endif
41177c478bd9Sstevel@tonic-gate 
41187c478bd9Sstevel@tonic-gate 	asy = async->async_common;
41197c478bd9Sstevel@tonic-gate 	ip = (struct iocblk *)mp->b_rptr;
41207c478bd9Sstevel@tonic-gate 	csp = (struct copyresp *)mp->b_rptr;
41217c478bd9Sstevel@tonic-gate 
41227c478bd9Sstevel@tonic-gate 	if (csp->cp_rval != 0) {
41237c478bd9Sstevel@tonic-gate 		if (csp->cp_private)
41247c478bd9Sstevel@tonic-gate 			freemsg(csp->cp_private);
41257c478bd9Sstevel@tonic-gate 		freemsg(mp);
41267c478bd9Sstevel@tonic-gate 		return;
41277c478bd9Sstevel@tonic-gate 	}
41287c478bd9Sstevel@tonic-gate 
41297c478bd9Sstevel@tonic-gate 	mutex_enter(&asy->asy_excl);
41307c478bd9Sstevel@tonic-gate 	DEBUGCONT2(ASY_DEBUG_MODEM, "async%d_iocdata: case %s\n",
41317c478bd9Sstevel@tonic-gate 	    instance,
41327c478bd9Sstevel@tonic-gate 	    csp->cp_cmd == TIOCMGET ? "TIOCMGET" :
41337c478bd9Sstevel@tonic-gate 	    csp->cp_cmd == TIOCMSET ? "TIOCMSET" :
41347c478bd9Sstevel@tonic-gate 	    csp->cp_cmd == TIOCMBIS ? "TIOCMBIS" :
41357c478bd9Sstevel@tonic-gate 	    "TIOCMBIC");
41367c478bd9Sstevel@tonic-gate 	switch (csp->cp_cmd) {
41377c478bd9Sstevel@tonic-gate 
41387c478bd9Sstevel@tonic-gate 	case TIOCMGET:
41397c478bd9Sstevel@tonic-gate 		if (mp->b_cont) {
41407c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
41417c478bd9Sstevel@tonic-gate 			mp->b_cont = NULL;
41427c478bd9Sstevel@tonic-gate 		}
41437c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
41447c478bd9Sstevel@tonic-gate 		ip->ioc_error = 0;
41457c478bd9Sstevel@tonic-gate 		ip->ioc_count = 0;
41467c478bd9Sstevel@tonic-gate 		ip->ioc_rval = 0;
41477c478bd9Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct iocblk);
41487c478bd9Sstevel@tonic-gate 		break;
41497c478bd9Sstevel@tonic-gate 
41507c478bd9Sstevel@tonic-gate 	case TIOCMSET:
41517c478bd9Sstevel@tonic-gate 	case TIOCMBIS:
41527c478bd9Sstevel@tonic-gate 	case TIOCMBIC:
41537c478bd9Sstevel@tonic-gate 		mutex_enter(&asy->asy_excl_hi);
41542df1fe9cSrandyf 		(void) asymctl(asy, dmtoasy(*(int *)mp->b_cont->b_rptr),
41557c478bd9Sstevel@tonic-gate 		    csp->cp_cmd);
41567c478bd9Sstevel@tonic-gate 		mutex_exit(&asy->asy_excl_hi);
41577c478bd9Sstevel@tonic-gate 		mioc2ack(mp, NULL, 0, 0);
41587c478bd9Sstevel@tonic-gate 		break;
41597c478bd9Sstevel@tonic-gate 
41607c478bd9Sstevel@tonic-gate 	default:
41617c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
41627c478bd9Sstevel@tonic-gate 		ip->ioc_error = EINVAL;
41637c478bd9Sstevel@tonic-gate 		break;
41647c478bd9Sstevel@tonic-gate 	}
41657c478bd9Sstevel@tonic-gate 	qreply(q, mp);
41667c478bd9Sstevel@tonic-gate 	mutex_exit(&asy->asy_excl);
41677c478bd9Sstevel@tonic-gate }
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate /*
41707c478bd9Sstevel@tonic-gate  * debugger/console support routines.
41717c478bd9Sstevel@tonic-gate  */
41727c478bd9Sstevel@tonic-gate 
41737c478bd9Sstevel@tonic-gate /*
41747c478bd9Sstevel@tonic-gate  * put a character out
41757c478bd9Sstevel@tonic-gate  * Do not use interrupts.  If char is LF, put out CR, LF.
41767c478bd9Sstevel@tonic-gate  */
41777c478bd9Sstevel@tonic-gate static void
4178281f0747Slt200341 asyputchar(cons_polledio_arg_t arg, uchar_t c)
41797c478bd9Sstevel@tonic-gate {
41807c478bd9Sstevel@tonic-gate 	struct asycom *asy = (struct asycom *)arg;
41817c478bd9Sstevel@tonic-gate 
41827c478bd9Sstevel@tonic-gate 	if (c == '\n')
41837c478bd9Sstevel@tonic-gate 		asyputchar(arg, '\r');
41847c478bd9Sstevel@tonic-gate 
41854ab75253Smrj 	while ((ddi_get8(asy->asy_iohandle,
41867c478bd9Sstevel@tonic-gate 	    asy->asy_ioaddr + LSR) & XHRE) == 0) {
41877c478bd9Sstevel@tonic-gate 		/* wait for xmit to finish */
41887c478bd9Sstevel@tonic-gate 		drv_usecwait(10);
41897c478bd9Sstevel@tonic-gate 	}
41907c478bd9Sstevel@tonic-gate 
41917c478bd9Sstevel@tonic-gate 	/* put the character out */
41924ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT, c);
41937c478bd9Sstevel@tonic-gate }
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate /*
41967c478bd9Sstevel@tonic-gate  * See if there's a character available. If no character is
41977c478bd9Sstevel@tonic-gate  * available, return 0. Run in polled mode, no interrupts.
41987c478bd9Sstevel@tonic-gate  */
41997c478bd9Sstevel@tonic-gate static boolean_t
4200281f0747Slt200341 asyischar(cons_polledio_arg_t arg)
42017c478bd9Sstevel@tonic-gate {
42027c478bd9Sstevel@tonic-gate 	struct asycom *asy = (struct asycom *)arg;
42037c478bd9Sstevel@tonic-gate 
42042df1fe9cSrandyf 	return ((ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) & RCA)
42052df1fe9cSrandyf 	    != 0);
42067c478bd9Sstevel@tonic-gate }
42077c478bd9Sstevel@tonic-gate 
42087c478bd9Sstevel@tonic-gate /*
42097c478bd9Sstevel@tonic-gate  * Get a character. Run in polled mode, no interrupts.
42107c478bd9Sstevel@tonic-gate  */
42117c478bd9Sstevel@tonic-gate static int
4212281f0747Slt200341 asygetchar(cons_polledio_arg_t arg)
42137c478bd9Sstevel@tonic-gate {
42147c478bd9Sstevel@tonic-gate 	struct asycom *asy = (struct asycom *)arg;
42157c478bd9Sstevel@tonic-gate 
42167c478bd9Sstevel@tonic-gate 	while (!asyischar(arg))
42177c478bd9Sstevel@tonic-gate 		drv_usecwait(10);
42182df1fe9cSrandyf 	return (ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + DAT));
42197c478bd9Sstevel@tonic-gate }
42207c478bd9Sstevel@tonic-gate 
42217c478bd9Sstevel@tonic-gate /*
42227c478bd9Sstevel@tonic-gate  * Set or get the modem control status.
42237c478bd9Sstevel@tonic-gate  */
42247c478bd9Sstevel@tonic-gate static int
42257c478bd9Sstevel@tonic-gate asymctl(struct asycom *asy, int bits, int how)
42267c478bd9Sstevel@tonic-gate {
42277c478bd9Sstevel@tonic-gate 	int mcr_r, msr_r;
42287c478bd9Sstevel@tonic-gate 	int instance = asy->asy_unit;
42297c478bd9Sstevel@tonic-gate 
42307c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
42317c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl));
42327c478bd9Sstevel@tonic-gate 
42337c478bd9Sstevel@tonic-gate 	/* Read Modem Control Registers */
42344ab75253Smrj 	mcr_r = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
42357c478bd9Sstevel@tonic-gate 
42367c478bd9Sstevel@tonic-gate 	switch (how) {
42377c478bd9Sstevel@tonic-gate 
42387c478bd9Sstevel@tonic-gate 	case TIOCMSET:
42397c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM,
42407c478bd9Sstevel@tonic-gate 		    "asy%dmctl: TIOCMSET, bits = %x\n", instance, bits);
42417c478bd9Sstevel@tonic-gate 		mcr_r = bits;		/* Set bits	*/
42427c478bd9Sstevel@tonic-gate 		break;
42437c478bd9Sstevel@tonic-gate 
42447c478bd9Sstevel@tonic-gate 	case TIOCMBIS:
42457c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM, "asy%dmctl: TIOCMBIS, bits = %x\n",
42467c478bd9Sstevel@tonic-gate 		    instance, bits);
42477c478bd9Sstevel@tonic-gate 		mcr_r |= bits;		/* Mask in bits	*/
42487c478bd9Sstevel@tonic-gate 		break;
42497c478bd9Sstevel@tonic-gate 
42507c478bd9Sstevel@tonic-gate 	case TIOCMBIC:
42517c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM, "asy%dmctl: TIOCMBIC, bits = %x\n",
42527c478bd9Sstevel@tonic-gate 		    instance, bits);
42537c478bd9Sstevel@tonic-gate 		mcr_r &= ~bits;		/* Mask out bits */
42547c478bd9Sstevel@tonic-gate 		break;
42557c478bd9Sstevel@tonic-gate 
42567c478bd9Sstevel@tonic-gate 	case TIOCMGET:
42577c478bd9Sstevel@tonic-gate 		/* Read Modem Status Registers */
42587c478bd9Sstevel@tonic-gate 		/*
42597c478bd9Sstevel@tonic-gate 		 * If modem interrupts are enabled, we return the
42607c478bd9Sstevel@tonic-gate 		 * saved value of msr. We read MSR only in async_msint()
42617c478bd9Sstevel@tonic-gate 		 */
42624ab75253Smrj 		if (ddi_get8(asy->asy_iohandle,
42637c478bd9Sstevel@tonic-gate 		    asy->asy_ioaddr + ICR) & MIEN) {
42647c478bd9Sstevel@tonic-gate 			msr_r = asy->asy_msr;
42657c478bd9Sstevel@tonic-gate 			DEBUGCONT2(ASY_DEBUG_MODEM,
42667c478bd9Sstevel@tonic-gate 			    "asy%dmctl: TIOCMGET, read msr_r = %x\n",
42677c478bd9Sstevel@tonic-gate 			    instance, msr_r);
42687c478bd9Sstevel@tonic-gate 		} else {
42694ab75253Smrj 			msr_r = ddi_get8(asy->asy_iohandle,
42707c478bd9Sstevel@tonic-gate 			    asy->asy_ioaddr + MSR);
42717c478bd9Sstevel@tonic-gate 			DEBUGCONT2(ASY_DEBUG_MODEM,
42727c478bd9Sstevel@tonic-gate 			    "asy%dmctl: TIOCMGET, read MSR = %x\n",
42737c478bd9Sstevel@tonic-gate 			    instance, msr_r);
42747c478bd9Sstevel@tonic-gate 		}
42757c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_MODEM, "asy%dtodm: modem_lines = %x\n",
42767c478bd9Sstevel@tonic-gate 		    instance, asytodm(mcr_r, msr_r));
42777c478bd9Sstevel@tonic-gate 		return (asytodm(mcr_r, msr_r));
42787c478bd9Sstevel@tonic-gate 	}
42797c478bd9Sstevel@tonic-gate 
42804ab75253Smrj 	ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + MCR, mcr_r);
42817c478bd9Sstevel@tonic-gate 
42827c478bd9Sstevel@tonic-gate 	return (mcr_r);
42837c478bd9Sstevel@tonic-gate }
42847c478bd9Sstevel@tonic-gate 
42857c478bd9Sstevel@tonic-gate static int
42867c478bd9Sstevel@tonic-gate asytodm(int mcr_r, int msr_r)
42877c478bd9Sstevel@tonic-gate {
42887c478bd9Sstevel@tonic-gate 	int b = 0;
42897c478bd9Sstevel@tonic-gate 
42907c478bd9Sstevel@tonic-gate 	/* MCR registers */
42917c478bd9Sstevel@tonic-gate 	if (mcr_r & RTS)
42927c478bd9Sstevel@tonic-gate 		b |= TIOCM_RTS;
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate 	if (mcr_r & DTR)
42957c478bd9Sstevel@tonic-gate 		b |= TIOCM_DTR;
42967c478bd9Sstevel@tonic-gate 
42977c478bd9Sstevel@tonic-gate 	/* MSR registers */
42987c478bd9Sstevel@tonic-gate 	if (msr_r & DCD)
42997c478bd9Sstevel@tonic-gate 		b |= TIOCM_CAR;
43007c478bd9Sstevel@tonic-gate 
43017c478bd9Sstevel@tonic-gate 	if (msr_r & CTS)
43027c478bd9Sstevel@tonic-gate 		b |= TIOCM_CTS;
43037c478bd9Sstevel@tonic-gate 
43047c478bd9Sstevel@tonic-gate 	if (msr_r & DSR)
43057c478bd9Sstevel@tonic-gate 		b |= TIOCM_DSR;
43067c478bd9Sstevel@tonic-gate 
43077c478bd9Sstevel@tonic-gate 	if (msr_r & RI)
43087c478bd9Sstevel@tonic-gate 		b |= TIOCM_RNG;
43097c478bd9Sstevel@tonic-gate 	return (b);
43107c478bd9Sstevel@tonic-gate }
43117c478bd9Sstevel@tonic-gate 
43127c478bd9Sstevel@tonic-gate static int
43137c478bd9Sstevel@tonic-gate dmtoasy(int bits)
43147c478bd9Sstevel@tonic-gate {
43157c478bd9Sstevel@tonic-gate 	int b = 0;
43167c478bd9Sstevel@tonic-gate 
43177c478bd9Sstevel@tonic-gate 	DEBUGCONT1(ASY_DEBUG_MODEM, "dmtoasy: bits = %x\n", bits);
43187c478bd9Sstevel@tonic-gate #ifdef	CAN_NOT_SET	/* only DTR and RTS can be set */
43197c478bd9Sstevel@tonic-gate 	if (bits & TIOCM_CAR)
43207c478bd9Sstevel@tonic-gate 		b |= DCD;
43217c478bd9Sstevel@tonic-gate 	if (bits & TIOCM_CTS)
43227c478bd9Sstevel@tonic-gate 		b |= CTS;
43237c478bd9Sstevel@tonic-gate 	if (bits & TIOCM_DSR)
43247c478bd9Sstevel@tonic-gate 		b |= DSR;
43257c478bd9Sstevel@tonic-gate 	if (bits & TIOCM_RNG)
43267c478bd9Sstevel@tonic-gate 		b |= RI;
43277c478bd9Sstevel@tonic-gate #endif
43287c478bd9Sstevel@tonic-gate 
43297c478bd9Sstevel@tonic-gate 	if (bits & TIOCM_RTS) {
43307c478bd9Sstevel@tonic-gate 		DEBUGCONT0(ASY_DEBUG_MODEM, "dmtoasy: set b & RTS\n");
43317c478bd9Sstevel@tonic-gate 		b |= RTS;
43327c478bd9Sstevel@tonic-gate 	}
43337c478bd9Sstevel@tonic-gate 	if (bits & TIOCM_DTR) {
43347c478bd9Sstevel@tonic-gate 		DEBUGCONT0(ASY_DEBUG_MODEM, "dmtoasy: set b & DTR\n");
43357c478bd9Sstevel@tonic-gate 		b |= DTR;
43367c478bd9Sstevel@tonic-gate 	}
43377c478bd9Sstevel@tonic-gate 
43387c478bd9Sstevel@tonic-gate 	return (b);
43397c478bd9Sstevel@tonic-gate }
43407c478bd9Sstevel@tonic-gate 
43417c478bd9Sstevel@tonic-gate static void
43427c478bd9Sstevel@tonic-gate asyerror(int level, const char *fmt, ...)
43437c478bd9Sstevel@tonic-gate {
43447c478bd9Sstevel@tonic-gate 	va_list adx;
43457c478bd9Sstevel@tonic-gate 	static	time_t	last;
43467c478bd9Sstevel@tonic-gate 	static	const char *lastfmt;
43477c478bd9Sstevel@tonic-gate 	time_t	now;
43487c478bd9Sstevel@tonic-gate 
43497c478bd9Sstevel@tonic-gate 	/*
43507c478bd9Sstevel@tonic-gate 	 * Don't print the same error message too often.
43517c478bd9Sstevel@tonic-gate 	 * Print the message only if we have not printed the
43527c478bd9Sstevel@tonic-gate 	 * message within the last second.
43537c478bd9Sstevel@tonic-gate 	 * Note: that fmt cannot be a pointer to a string
43547c478bd9Sstevel@tonic-gate 	 * stored on the stack. The fmt pointer
43557c478bd9Sstevel@tonic-gate 	 * must be in the data segment otherwise lastfmt would point
43567c478bd9Sstevel@tonic-gate 	 * to non-sense.
43577c478bd9Sstevel@tonic-gate 	 */
43587c478bd9Sstevel@tonic-gate 	now = gethrestime_sec();
43597c478bd9Sstevel@tonic-gate 	if (last == now && lastfmt == fmt)
43607c478bd9Sstevel@tonic-gate 		return;
43617c478bd9Sstevel@tonic-gate 
43627c478bd9Sstevel@tonic-gate 	last = now;
43637c478bd9Sstevel@tonic-gate 	lastfmt = fmt;
43647c478bd9Sstevel@tonic-gate 
43657c478bd9Sstevel@tonic-gate 	va_start(adx, fmt);
43667c478bd9Sstevel@tonic-gate 	vcmn_err(level, fmt, adx);
43677c478bd9Sstevel@tonic-gate 	va_end(adx);
43687c478bd9Sstevel@tonic-gate }
43697c478bd9Sstevel@tonic-gate 
43707c478bd9Sstevel@tonic-gate /*
43717c478bd9Sstevel@tonic-gate  * asy_parse_mode(dev_info_t *devi, struct asycom *asy)
43727c478bd9Sstevel@tonic-gate  * The value of this property is in the form of "9600,8,n,1,-"
43737c478bd9Sstevel@tonic-gate  * 1) speed: 9600, 4800, ...
43747c478bd9Sstevel@tonic-gate  * 2) data bits
43757c478bd9Sstevel@tonic-gate  * 3) parity: n(none), e(even), o(odd)
43767c478bd9Sstevel@tonic-gate  * 4) stop bits
43777c478bd9Sstevel@tonic-gate  * 5) handshake: -(none), h(hardware: rts/cts), s(software: xon/off)
43787c478bd9Sstevel@tonic-gate  *
43797c478bd9Sstevel@tonic-gate  * This parsing came from a SPARCstation eeprom.
43807c478bd9Sstevel@tonic-gate  */
43817c478bd9Sstevel@tonic-gate static void
43827c478bd9Sstevel@tonic-gate asy_parse_mode(dev_info_t *devi, struct asycom *asy)
43837c478bd9Sstevel@tonic-gate {
43847c478bd9Sstevel@tonic-gate 	char		name[40];
43857c478bd9Sstevel@tonic-gate 	char		val[40];
43867c478bd9Sstevel@tonic-gate 	int		len;
43877c478bd9Sstevel@tonic-gate 	int		ret;
43887c478bd9Sstevel@tonic-gate 	char		*p;
43897c478bd9Sstevel@tonic-gate 	char		*p1;
43907c478bd9Sstevel@tonic-gate 
43917c478bd9Sstevel@tonic-gate 	ASSERT(asy->asy_com_port != 0);
43927c478bd9Sstevel@tonic-gate 
43937c478bd9Sstevel@tonic-gate 	/*
43947c478bd9Sstevel@tonic-gate 	 * Parse the ttyx-mode property
43957c478bd9Sstevel@tonic-gate 	 */
43967c478bd9Sstevel@tonic-gate 	(void) sprintf(name, "tty%c-mode", asy->asy_com_port + 'a' - 1);
43977c478bd9Sstevel@tonic-gate 	len = sizeof (val);
43987c478bd9Sstevel@tonic-gate 	ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
43997c478bd9Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS) {
44007c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "com%c-mode", asy->asy_com_port + '0');
44017c478bd9Sstevel@tonic-gate 		len = sizeof (val);
44027c478bd9Sstevel@tonic-gate 		ret = GET_PROP(devi, name, DDI_PROP_CANSLEEP, val, &len);
44037c478bd9Sstevel@tonic-gate 	}
44047c478bd9Sstevel@tonic-gate 
44057c478bd9Sstevel@tonic-gate 	/* no property to parse */
44067c478bd9Sstevel@tonic-gate 	asy->asy_cflag = 0;
44077c478bd9Sstevel@tonic-gate 	if (ret != DDI_PROP_SUCCESS)
44087c478bd9Sstevel@tonic-gate 		return;
44097c478bd9Sstevel@tonic-gate 
44107c478bd9Sstevel@tonic-gate 	p = val;
44117c478bd9Sstevel@tonic-gate 	/* ---- baud rate ---- */
44127c478bd9Sstevel@tonic-gate 	asy->asy_cflag = CREAD|B9600;		/* initial default */
44137c478bd9Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0) {
44147c478bd9Sstevel@tonic-gate 		*p1++ = '\0';
44157c478bd9Sstevel@tonic-gate 	} else {
44167c478bd9Sstevel@tonic-gate 		asy->asy_cflag |= BITS8;	/* add default bits */
44177c478bd9Sstevel@tonic-gate 		return;
44187c478bd9Sstevel@tonic-gate 	}
44197c478bd9Sstevel@tonic-gate 
44207c478bd9Sstevel@tonic-gate 	if (strcmp(p, "110") == 0)
44217c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B110;
44227c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "150") == 0)
44237c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B150;
44247c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "300") == 0)
44257c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B300;
44267c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "600") == 0)
44277c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B600;
44287c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "1200") == 0)
44297c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B1200;
44307c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "2400") == 0)
44317c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B2400;
44327c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "4800") == 0)
44337c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B4800;
44347c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "9600") == 0)
44357c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B9600;
44367c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "19200") == 0)
44377c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B19200;
44387c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "38400") == 0)
44397c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B38400;
44407c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "57600") == 0)
44417c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B57600;
44427c478bd9Sstevel@tonic-gate 	else if (strcmp(p, "115200") == 0)
44437c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B115200;
44447c478bd9Sstevel@tonic-gate 	else
44457c478bd9Sstevel@tonic-gate 		asy->asy_bidx = B9600;
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate 	asy->asy_cflag &= ~CBAUD;
44487c478bd9Sstevel@tonic-gate 	if (asy->asy_bidx > CBAUD) {	/* > 38400 uses the CBAUDEXT bit */
44497c478bd9Sstevel@tonic-gate 		asy->asy_cflag |= CBAUDEXT;
44507c478bd9Sstevel@tonic-gate 		asy->asy_cflag |= asy->asy_bidx - CBAUD - 1;
44517c478bd9Sstevel@tonic-gate 	} else {
44527c478bd9Sstevel@tonic-gate 		asy->asy_cflag |= asy->asy_bidx;
44537c478bd9Sstevel@tonic-gate 	}
44547c478bd9Sstevel@tonic-gate 
44557c478bd9Sstevel@tonic-gate 	ASSERT(asy->asy_bidx == BAUDINDEX(asy->asy_cflag));
44567c478bd9Sstevel@tonic-gate 
44577c478bd9Sstevel@tonic-gate 	/* ---- Next item is data bits ---- */
44587c478bd9Sstevel@tonic-gate 	p = p1;
44597c478bd9Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0)  {
44607c478bd9Sstevel@tonic-gate 		*p1++ = '\0';
44617c478bd9Sstevel@tonic-gate 	} else {
44627c478bd9Sstevel@tonic-gate 		asy->asy_cflag |= BITS8;	/* add default bits */
44637c478bd9Sstevel@tonic-gate 		return;
44647c478bd9Sstevel@tonic-gate 	}
44657c478bd9Sstevel@tonic-gate 	switch (*p) {
44667c478bd9Sstevel@tonic-gate 		default:
44677c478bd9Sstevel@tonic-gate 		case '8':
44687c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= CS8;
44697c478bd9Sstevel@tonic-gate 			asy->asy_lcr = BITS8;
44707c478bd9Sstevel@tonic-gate 			break;
44717c478bd9Sstevel@tonic-gate 		case '7':
44727c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= CS7;
44737c478bd9Sstevel@tonic-gate 			asy->asy_lcr = BITS7;
44747c478bd9Sstevel@tonic-gate 			break;
44757c478bd9Sstevel@tonic-gate 		case '6':
44767c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= CS6;
44777c478bd9Sstevel@tonic-gate 			asy->asy_lcr = BITS6;
44787c478bd9Sstevel@tonic-gate 			break;
44797c478bd9Sstevel@tonic-gate 		case '5':
44807c478bd9Sstevel@tonic-gate 			/* LINTED: CS5 is currently zero (but might change) */
44817c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= CS5;
44827c478bd9Sstevel@tonic-gate 			asy->asy_lcr = BITS5;
44837c478bd9Sstevel@tonic-gate 			break;
44847c478bd9Sstevel@tonic-gate 	}
44857c478bd9Sstevel@tonic-gate 
44867c478bd9Sstevel@tonic-gate 	/* ---- Parity info ---- */
44877c478bd9Sstevel@tonic-gate 	p = p1;
44887c478bd9Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0)  {
44897c478bd9Sstevel@tonic-gate 		*p1++ = '\0';
44907c478bd9Sstevel@tonic-gate 	} else {
44917c478bd9Sstevel@tonic-gate 		return;
44927c478bd9Sstevel@tonic-gate 	}
44937c478bd9Sstevel@tonic-gate 	switch (*p)  {
44947c478bd9Sstevel@tonic-gate 		default:
44957c478bd9Sstevel@tonic-gate 		case 'n':
44967c478bd9Sstevel@tonic-gate 			break;
44977c478bd9Sstevel@tonic-gate 		case 'e':
44987c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= PARENB;
44997c478bd9Sstevel@tonic-gate 			asy->asy_lcr |= PEN; break;
45007c478bd9Sstevel@tonic-gate 		case 'o':
45017c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= PARENB|PARODD;
45027c478bd9Sstevel@tonic-gate 			asy->asy_lcr |= PEN|EPS;
45037c478bd9Sstevel@tonic-gate 			break;
45047c478bd9Sstevel@tonic-gate 	}
45057c478bd9Sstevel@tonic-gate 
45067c478bd9Sstevel@tonic-gate 	/* ---- Find stop bits ---- */
45077c478bd9Sstevel@tonic-gate 	p = p1;
45087c478bd9Sstevel@tonic-gate 	if (p && (p1 = strchr(p, ',')) != 0)  {
45097c478bd9Sstevel@tonic-gate 		*p1++ = '\0';
45107c478bd9Sstevel@tonic-gate 	} else {
45117c478bd9Sstevel@tonic-gate 		return;
45127c478bd9Sstevel@tonic-gate 	}
45137c478bd9Sstevel@tonic-gate 	if (*p == '2') {
45147c478bd9Sstevel@tonic-gate 		asy->asy_cflag |= CSTOPB;
45157c478bd9Sstevel@tonic-gate 		asy->asy_lcr |= STB;
45167c478bd9Sstevel@tonic-gate 	}
45177c478bd9Sstevel@tonic-gate 
45187c478bd9Sstevel@tonic-gate 	/* ---- handshake is next ---- */
45197c478bd9Sstevel@tonic-gate 	p = p1;
45207c478bd9Sstevel@tonic-gate 	if (p) {
45217c478bd9Sstevel@tonic-gate 		if ((p1 = strchr(p, ',')) != 0)
45227c478bd9Sstevel@tonic-gate 			*p1++ = '\0';
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate 		if (*p == 'h')
45257c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= CRTSCTS;
45267c478bd9Sstevel@tonic-gate 		else if (*p == 's')
45277c478bd9Sstevel@tonic-gate 			asy->asy_cflag |= CRTSXOFF;
45287c478bd9Sstevel@tonic-gate 	}
45297c478bd9Sstevel@tonic-gate }
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate /*
45327c478bd9Sstevel@tonic-gate  * Check for abort character sequence
45337c478bd9Sstevel@tonic-gate  */
45347c478bd9Sstevel@tonic-gate static boolean_t
45357c478bd9Sstevel@tonic-gate abort_charseq_recognize(uchar_t ch)
45367c478bd9Sstevel@tonic-gate {
45377c478bd9Sstevel@tonic-gate 	static int state = 0;
45387c478bd9Sstevel@tonic-gate #define	CNTRL(c) ((c)&037)
45397c478bd9Sstevel@tonic-gate 	static char sequence[] = { '\r', '~', CNTRL('b') };
45407c478bd9Sstevel@tonic-gate 
45417c478bd9Sstevel@tonic-gate 	if (ch == sequence[state]) {
45427c478bd9Sstevel@tonic-gate 		if (++state >= sizeof (sequence)) {
45437c478bd9Sstevel@tonic-gate 			state = 0;
45447c478bd9Sstevel@tonic-gate 			return (B_TRUE);
45457c478bd9Sstevel@tonic-gate 		}
45467c478bd9Sstevel@tonic-gate 	} else {
45477c478bd9Sstevel@tonic-gate 		state = (ch == sequence[0]) ? 1 : 0;
45487c478bd9Sstevel@tonic-gate 	}
45497c478bd9Sstevel@tonic-gate 	return (B_FALSE);
45507c478bd9Sstevel@tonic-gate }
45517c478bd9Sstevel@tonic-gate 
45527c478bd9Sstevel@tonic-gate /*
45537c478bd9Sstevel@tonic-gate  * Flow control functions
45547c478bd9Sstevel@tonic-gate  */
45557c478bd9Sstevel@tonic-gate /*
45567c478bd9Sstevel@tonic-gate  * Software input flow control
45577c478bd9Sstevel@tonic-gate  * This function can execute software input flow control sucessfully
45587c478bd9Sstevel@tonic-gate  * at most of situations except that the line is in BREAK status
45597c478bd9Sstevel@tonic-gate  * (timed and untimed break).
45607c478bd9Sstevel@tonic-gate  * INPUT VALUE of onoff:
45617c478bd9Sstevel@tonic-gate  *               FLOW_START means to send out a XON char
45627c478bd9Sstevel@tonic-gate  *                          and clear SW input flow control flag.
45637c478bd9Sstevel@tonic-gate  *               FLOW_STOP means to send out a XOFF char
45647c478bd9Sstevel@tonic-gate  *                          and set SW input flow control flag.
45657c478bd9Sstevel@tonic-gate  *               FLOW_CHECK means to check whether there is pending XON/XOFF
45667c478bd9Sstevel@tonic-gate  *                          if it is true, send it out.
45677c478bd9Sstevel@tonic-gate  * INPUT VALUE of type:
45687c478bd9Sstevel@tonic-gate  *		 IN_FLOW_RINGBUFF means flow control is due to RING BUFFER
45697c478bd9Sstevel@tonic-gate  *		 IN_FLOW_STREAMS means flow control is due to STREAMS
45707c478bd9Sstevel@tonic-gate  *		 IN_FLOW_USER means flow control is due to user's commands
45717c478bd9Sstevel@tonic-gate  * RETURN VALUE: B_FALSE means no flow control char is sent
45727c478bd9Sstevel@tonic-gate  *               B_TRUE means one flow control char is sent
45737c478bd9Sstevel@tonic-gate  */
45747c478bd9Sstevel@tonic-gate static boolean_t
45757c478bd9Sstevel@tonic-gate async_flowcontrol_sw_input(struct asycom *asy, async_flowc_action onoff,
45767c478bd9Sstevel@tonic-gate     int type)
45777c478bd9Sstevel@tonic-gate {
45787c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
45797c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
45807c478bd9Sstevel@tonic-gate 	int rval = B_FALSE;
45817c478bd9Sstevel@tonic-gate 
45827c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
45837c478bd9Sstevel@tonic-gate 
45847c478bd9Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_iflag & IXOFF))
45857c478bd9Sstevel@tonic-gate 		return (rval);
45867c478bd9Sstevel@tonic-gate 
45877c478bd9Sstevel@tonic-gate 	/*
45887c478bd9Sstevel@tonic-gate 	 * If we get this far, then we know IXOFF is set.
45897c478bd9Sstevel@tonic-gate 	 */
45907c478bd9Sstevel@tonic-gate 	switch (onoff) {
45917c478bd9Sstevel@tonic-gate 	case FLOW_STOP:
45927c478bd9Sstevel@tonic-gate 		async->async_inflow_source |= type;
45937c478bd9Sstevel@tonic-gate 
45947c478bd9Sstevel@tonic-gate 		/*
45957c478bd9Sstevel@tonic-gate 		 * We'll send an XOFF character for each of up to
45967c478bd9Sstevel@tonic-gate 		 * three different input flow control attempts to stop input.
45977c478bd9Sstevel@tonic-gate 		 * If we already send out one XOFF, but FLOW_STOP comes again,
45987c478bd9Sstevel@tonic-gate 		 * it seems that input flow control becomes more serious,
45997c478bd9Sstevel@tonic-gate 		 * then send XOFF again.
46007c478bd9Sstevel@tonic-gate 		 */
46017c478bd9Sstevel@tonic-gate 		if (async->async_inflow_source & (IN_FLOW_RINGBUFF |
46027c478bd9Sstevel@tonic-gate 		    IN_FLOW_STREAMS | IN_FLOW_USER))
46037c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_SW_IN_FLOW |
46047c478bd9Sstevel@tonic-gate 			    ASYNC_SW_IN_NEEDED;
46057c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_SFLOW, "async%d: input sflow stop, "
46067c478bd9Sstevel@tonic-gate 		    "type = %x\n", instance, async->async_inflow_source);
46077c478bd9Sstevel@tonic-gate 		break;
46087c478bd9Sstevel@tonic-gate 	case FLOW_START:
46097c478bd9Sstevel@tonic-gate 		async->async_inflow_source &= ~type;
46107c478bd9Sstevel@tonic-gate 		if (async->async_inflow_source == 0) {
46117c478bd9Sstevel@tonic-gate 			async->async_flags = (async->async_flags &
46127c478bd9Sstevel@tonic-gate 			    ~ASYNC_SW_IN_FLOW) | ASYNC_SW_IN_NEEDED;
46137c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_SFLOW, "async%d: "
46147c478bd9Sstevel@tonic-gate 			    "input sflow start\n", instance);
46157c478bd9Sstevel@tonic-gate 		}
46167c478bd9Sstevel@tonic-gate 		break;
46177c478bd9Sstevel@tonic-gate 	default:
46187c478bd9Sstevel@tonic-gate 		break;
46197c478bd9Sstevel@tonic-gate 	}
46207c478bd9Sstevel@tonic-gate 
46217c478bd9Sstevel@tonic-gate 	if (((async->async_flags & (ASYNC_SW_IN_NEEDED | ASYNC_BREAK |
46227c478bd9Sstevel@tonic-gate 	    ASYNC_OUT_SUSPEND)) == ASYNC_SW_IN_NEEDED) &&
46234ab75253Smrj 	    (ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + LSR) & XHRE)) {
46247c478bd9Sstevel@tonic-gate 		/*
46257c478bd9Sstevel@tonic-gate 		 * If we get this far, then we know we need to send out
46267c478bd9Sstevel@tonic-gate 		 * XON or XOFF char.
46277c478bd9Sstevel@tonic-gate 		 */
46287c478bd9Sstevel@tonic-gate 		async->async_flags = (async->async_flags &
46297c478bd9Sstevel@tonic-gate 		    ~ASYNC_SW_IN_NEEDED) | ASYNC_BUSY;
46304ab75253Smrj 		ddi_put8(asy->asy_iohandle, asy->asy_ioaddr + DAT,
46317c478bd9Sstevel@tonic-gate 		    async->async_flags & ASYNC_SW_IN_FLOW ?
46327c478bd9Sstevel@tonic-gate 		    async->async_stopc : async->async_startc);
46337c478bd9Sstevel@tonic-gate 		rval = B_TRUE;
46347c478bd9Sstevel@tonic-gate 	}
46357c478bd9Sstevel@tonic-gate 	return (rval);
46367c478bd9Sstevel@tonic-gate }
46377c478bd9Sstevel@tonic-gate 
46387c478bd9Sstevel@tonic-gate /*
46397c478bd9Sstevel@tonic-gate  * Software output flow control
46407c478bd9Sstevel@tonic-gate  * This function can be executed sucessfully at any situation.
46417c478bd9Sstevel@tonic-gate  * It does not handle HW, and just change the SW output flow control flag.
46427c478bd9Sstevel@tonic-gate  * INPUT VALUE of onoff:
46437c478bd9Sstevel@tonic-gate  *                 FLOW_START means to clear SW output flow control flag,
46447c478bd9Sstevel@tonic-gate  *			also combine with HW output flow control status to
46457c478bd9Sstevel@tonic-gate  *			determine if we need to set ASYNC_OUT_FLW_RESUME.
46467c478bd9Sstevel@tonic-gate  *                 FLOW_STOP means to set SW output flow control flag,
46477c478bd9Sstevel@tonic-gate  *			also clear ASYNC_OUT_FLW_RESUME.
46487c478bd9Sstevel@tonic-gate  */
46497c478bd9Sstevel@tonic-gate static void
46507c478bd9Sstevel@tonic-gate async_flowcontrol_sw_output(struct asycom *asy, async_flowc_action onoff)
46517c478bd9Sstevel@tonic-gate {
46527c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
46537c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
46547c478bd9Sstevel@tonic-gate 
46557c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
46567c478bd9Sstevel@tonic-gate 
46577c478bd9Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_iflag & IXON))
46587c478bd9Sstevel@tonic-gate 		return;
46597c478bd9Sstevel@tonic-gate 
46607c478bd9Sstevel@tonic-gate 	switch (onoff) {
46617c478bd9Sstevel@tonic-gate 	case FLOW_STOP:
46627c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_SW_OUT_FLW;
46637c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
46647c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_SFLOW, "async%d: output sflow stop\n",
46657c478bd9Sstevel@tonic-gate 		    instance);
46667c478bd9Sstevel@tonic-gate 		break;
46677c478bd9Sstevel@tonic-gate 	case FLOW_START:
46687c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_SW_OUT_FLW;
46697c478bd9Sstevel@tonic-gate 		if (!(async->async_flags & ASYNC_HW_OUT_FLW))
46707c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_OUT_FLW_RESUME;
46717c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_SFLOW, "async%d: output sflow start\n",
46727c478bd9Sstevel@tonic-gate 		    instance);
46737c478bd9Sstevel@tonic-gate 		break;
46747c478bd9Sstevel@tonic-gate 	default:
46757c478bd9Sstevel@tonic-gate 		break;
46767c478bd9Sstevel@tonic-gate 	}
46777c478bd9Sstevel@tonic-gate }
46787c478bd9Sstevel@tonic-gate 
46797c478bd9Sstevel@tonic-gate /*
46807c478bd9Sstevel@tonic-gate  * Hardware input flow control
46817c478bd9Sstevel@tonic-gate  * This function can be executed sucessfully at any situation.
46827c478bd9Sstevel@tonic-gate  * It directly changes RTS depending on input parameter onoff.
46837c478bd9Sstevel@tonic-gate  * INPUT VALUE of onoff:
46847c478bd9Sstevel@tonic-gate  *       FLOW_START means to clear HW input flow control flag,
46857c478bd9Sstevel@tonic-gate  *                  and pull up RTS if it is low.
46867c478bd9Sstevel@tonic-gate  *       FLOW_STOP means to set HW input flow control flag,
46877c478bd9Sstevel@tonic-gate  *                  and low RTS if it is high.
46887c478bd9Sstevel@tonic-gate  * INPUT VALUE of type:
46897c478bd9Sstevel@tonic-gate  *		 IN_FLOW_RINGBUFF means flow control is due to RING BUFFER
46907c478bd9Sstevel@tonic-gate  *		 IN_FLOW_STREAMS means flow control is due to STREAMS
46917c478bd9Sstevel@tonic-gate  *		 IN_FLOW_USER means flow control is due to user's commands
46927c478bd9Sstevel@tonic-gate  */
46937c478bd9Sstevel@tonic-gate static void
46947c478bd9Sstevel@tonic-gate async_flowcontrol_hw_input(struct asycom *asy, async_flowc_action onoff,
46957c478bd9Sstevel@tonic-gate     int type)
46967c478bd9Sstevel@tonic-gate {
46977c478bd9Sstevel@tonic-gate 	uchar_t	mcr;
46987c478bd9Sstevel@tonic-gate 	uchar_t	flag;
46997c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
47007c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
47017c478bd9Sstevel@tonic-gate 
47027c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
47037c478bd9Sstevel@tonic-gate 
47047c478bd9Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_cflag & CRTSXOFF))
47057c478bd9Sstevel@tonic-gate 		return;
47067c478bd9Sstevel@tonic-gate 
47077c478bd9Sstevel@tonic-gate 	switch (onoff) {
47087c478bd9Sstevel@tonic-gate 	case FLOW_STOP:
47097c478bd9Sstevel@tonic-gate 		async->async_inflow_source |= type;
47107c478bd9Sstevel@tonic-gate 		if (async->async_inflow_source & (IN_FLOW_RINGBUFF |
47117c478bd9Sstevel@tonic-gate 		    IN_FLOW_STREAMS | IN_FLOW_USER))
47127c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_HW_IN_FLOW;
47137c478bd9Sstevel@tonic-gate 		DEBUGCONT2(ASY_DEBUG_HFLOW, "async%d: input hflow stop, "
47147c478bd9Sstevel@tonic-gate 		    "type = %x\n", instance, async->async_inflow_source);
47157c478bd9Sstevel@tonic-gate 		break;
47167c478bd9Sstevel@tonic-gate 	case FLOW_START:
47177c478bd9Sstevel@tonic-gate 		async->async_inflow_source &= ~type;
47187c478bd9Sstevel@tonic-gate 		if (async->async_inflow_source == 0) {
47197c478bd9Sstevel@tonic-gate 			async->async_flags &= ~ASYNC_HW_IN_FLOW;
47207c478bd9Sstevel@tonic-gate 			DEBUGCONT1(ASY_DEBUG_HFLOW, "async%d: "
47217c478bd9Sstevel@tonic-gate 			    "input hflow start\n", instance);
47227c478bd9Sstevel@tonic-gate 		}
47237c478bd9Sstevel@tonic-gate 		break;
47247c478bd9Sstevel@tonic-gate 	default:
47257c478bd9Sstevel@tonic-gate 		break;
47267c478bd9Sstevel@tonic-gate 	}
47274ab75253Smrj 	mcr = ddi_get8(asy->asy_iohandle, asy->asy_ioaddr + MCR);
47287c478bd9Sstevel@tonic-gate 	flag = (async->async_flags & ASYNC_HW_IN_FLOW) ? 0 : RTS;
47297c478bd9Sstevel@tonic-gate 
47307c478bd9Sstevel@tonic-gate 	if (((mcr ^ flag) & RTS) != 0) {
47314ab75253Smrj 		ddi_put8(asy->asy_iohandle,
47327c478bd9Sstevel@tonic-gate 		    asy->asy_ioaddr + MCR, (mcr ^ RTS));
47337c478bd9Sstevel@tonic-gate 	}
47347c478bd9Sstevel@tonic-gate }
47357c478bd9Sstevel@tonic-gate 
47367c478bd9Sstevel@tonic-gate /*
47377c478bd9Sstevel@tonic-gate  * Hardware output flow control
47387c478bd9Sstevel@tonic-gate  * This function can execute HW output flow control sucessfully
47397c478bd9Sstevel@tonic-gate  * at any situation.
47407c478bd9Sstevel@tonic-gate  * It doesn't really change RTS, and just change
47417c478bd9Sstevel@tonic-gate  * HW output flow control flag depending on CTS status.
47427c478bd9Sstevel@tonic-gate  * INPUT VALUE of onoff:
47437c478bd9Sstevel@tonic-gate  *                FLOW_START means to clear HW output flow control flag.
47447c478bd9Sstevel@tonic-gate  *			also combine with SW output flow control status to
47457c478bd9Sstevel@tonic-gate  *			determine if we need to set ASYNC_OUT_FLW_RESUME.
47467c478bd9Sstevel@tonic-gate  *                FLOW_STOP means to set HW output flow control flag.
47477c478bd9Sstevel@tonic-gate  *			also clear ASYNC_OUT_FLW_RESUME.
47487c478bd9Sstevel@tonic-gate  */
47497c478bd9Sstevel@tonic-gate static void
47507c478bd9Sstevel@tonic-gate async_flowcontrol_hw_output(struct asycom *asy, async_flowc_action onoff)
47517c478bd9Sstevel@tonic-gate {
47527c478bd9Sstevel@tonic-gate 	struct asyncline *async = asy->asy_priv;
47537c478bd9Sstevel@tonic-gate 	int instance = UNIT(async->async_dev);
47547c478bd9Sstevel@tonic-gate 
47557c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&asy->asy_excl_hi));
47567c478bd9Sstevel@tonic-gate 
47577c478bd9Sstevel@tonic-gate 	if (!(async->async_ttycommon.t_cflag & CRTSCTS))
47587c478bd9Sstevel@tonic-gate 		return;
47597c478bd9Sstevel@tonic-gate 
47607c478bd9Sstevel@tonic-gate 	switch (onoff) {
47617c478bd9Sstevel@tonic-gate 	case FLOW_STOP:
47627c478bd9Sstevel@tonic-gate 		async->async_flags |= ASYNC_HW_OUT_FLW;
47637c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_OUT_FLW_RESUME;
47647c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_HFLOW, "async%d: output hflow stop\n",
47657c478bd9Sstevel@tonic-gate 		    instance);
47667c478bd9Sstevel@tonic-gate 		break;
47677c478bd9Sstevel@tonic-gate 	case FLOW_START:
47687c478bd9Sstevel@tonic-gate 		async->async_flags &= ~ASYNC_HW_OUT_FLW;
47697c478bd9Sstevel@tonic-gate 		if (!(async->async_flags & ASYNC_SW_OUT_FLW))
47707c478bd9Sstevel@tonic-gate 			async->async_flags |= ASYNC_OUT_FLW_RESUME;
47717c478bd9Sstevel@tonic-gate 		DEBUGCONT1(ASY_DEBUG_HFLOW, "async%d: output hflow start\n",
47727c478bd9Sstevel@tonic-gate 		    instance);
47737c478bd9Sstevel@tonic-gate 		break;
47747c478bd9Sstevel@tonic-gate 	default:
47757c478bd9Sstevel@tonic-gate 		break;
47767c478bd9Sstevel@tonic-gate 	}
47777c478bd9Sstevel@tonic-gate }
4778