xref: /titanic_41/usr/src/uts/sun4/io/su_driver.c (revision 3db86aab554edbb4244c8d1a1c90f152eee768af)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
23 /*	  All Rights Reserved					*/
24 
25 /*
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  *	Serial I/O driver for 82510/8250/16450/16550AF chips.
34  *	Modified as sparc keyboard/mouse driver.
35  */
36 #define	SU_REGISTER_FILE_NO 0
37 #define	SU_REGOFFSET 0
38 #define	SU_REGISTER_LEN 8
39 
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/signal.h>
43 #include <sys/stream.h>
44 #include <sys/termio.h>
45 #include <sys/errno.h>
46 #include <sys/file.h>
47 #include <sys/cmn_err.h>
48 #include <sys/stropts.h>
49 #include <sys/strsubr.h>
50 #include <sys/strsun.h>
51 #include <sys/strtty.h>
52 #include <sys/debug.h>
53 #include <sys/kbio.h>
54 #include <sys/cred.h>
55 #include <sys/modctl.h>
56 #include <sys/stat.h>
57 #include <sys/consdev.h>
58 #include <sys/mkdev.h>
59 #include <sys/kmem.h>
60 #include <sys/cred.h>
61 #ifdef DEBUG
62 #include <sys/promif.h>
63 #endif
64 #include <sys/ddi.h>
65 #include <sys/sunddi.h>
66 #include <sys/sudev.h>
67 #include <sys/note.h>
68 #include <sys/timex.h>
69 #include <sys/policy.h>
70 
71 #define	async_stopc	async_ttycommon.t_stopc
72 #define	async_startc	async_ttycommon.t_startc
73 
74 #define	ASY_INIT	1
75 #define	ASY_NOINIT	0
76 
77 #ifdef DEBUG
78 #define	ASY_DEBUG_INIT	0x001
79 #define	ASY_DEBUG_INPUT	0x002
80 #define	ASY_DEBUG_EOT	0x004
81 #define	ASY_DEBUG_CLOSE	0x008
82 #define	ASY_DEBUG_HFLOW	0x010
83 #define	ASY_DEBUG_PROCS	0x020
84 #define	ASY_DEBUG_STATE	0x040
85 #define	ASY_DEBUG_INTR	0x080
86 static	int asydebug = 0;
87 #endif
88 static	int su_log = 0;
89 
90 int su_drain_check = 15000000;		/* tunable: exit drain check time */
91 
92 static	struct ppsclockev asy_ppsev;
93 
94 static	int max_asy_instance = -1;
95 static	void	*su_asycom;	/* soft state asycom pointer */
96 static	void	*su_asyncline;	/* soft state asyncline pointer */
97 static	boolean_t abort_charseq_recognize(uchar_t ch);
98 
99 static	uint_t	asysoftintr(caddr_t intarg);
100 static	uint_t	asyintr(caddr_t argasy);
101 
102 /* The async interrupt entry points */
103 static void	async_txint(struct asycom *asy, uchar_t lsr);
104 static void	async_rxint(struct asycom *asy, uchar_t lsr);
105 static void	async_msint(struct asycom *asy);
106 static int	async_softint(struct asycom *asy);
107 
108 static void	async_ioctl(struct asyncline *async, queue_t *q, mblk_t *mp,
109     boolean_t iswput);
110 static void	async_reioctl(void *);
111 static void	async_iocdata(queue_t *q, mblk_t *mp);
112 static void	async_restart(void *);
113 static void	async_start(struct asyncline *async);
114 static void	async_nstart(struct asyncline *async, int mode);
115 static void	async_resume(struct asyncline *async);
116 static int	asy_program(struct asycom *asy, int mode);
117 
118 static int	asymctl(struct asycom *, int, int);
119 static int	asytodm(int, int);
120 static int	dmtoasy(int);
121 static void	asycheckflowcontrol_hw(struct asycom *asy);
122 static boolean_t asycheckflowcontrol_sw(struct asycom *asy);
123 static void	asy_ppsevent(struct asycom *asy, int msr);
124 
125 extern kcondvar_t lbolt_cv;
126 extern int ddi_create_internal_pathname(dev_info_t *dip, char *name,
127 		int spec_type, minor_t minor_num);
128 
129 
130 /*
131  * Baud rate table. Indexed by #defines found in sys/termios.h
132  */
133 ushort_t asyspdtab[] = {
134 	0,	/* 0 baud rate */
135 	0x900,	/* 50 baud rate */
136 	0x600,	/* 75 baud rate */
137 	0x417,	/* 110 baud rate (%0.026) */
138 	0x359,	/* 134 baud rate (%0.058) */
139 	0x300,	/* 150 baud rate */
140 	0x240,	/* 200 baud rate */
141 	0x180,	/* 300 baud rate */
142 	0x0c0,	/* 600 baud rate */
143 	0x060,	/* 1200 baud rate */
144 	0x040,	/* 1800 baud rate */
145 	0x030,	/* 2400 baud rate */
146 	0x018,	/* 4800 baud rate */
147 	0x00c,	/* 9600 baud rate */
148 	0x006,	/* 19200 baud rate */
149 	0x003,	/* 38400 baud rate */
150 	0x002,	/* 57600 baud rate */
151 	0,	/* 76800 baud rate - not supported */
152 	0x001,	/* 115200 baud rate */
153 	0,	/* 153600 baud rate - not supported */
154 	0x8002,	/* 230400 baud rate - supported on specific platforms */
155 	0,	/* 307200 baud rate - not supported */
156 	0x8001	/* 460800 baud rate - supported on specific platforms */
157 };
158 
159 /*
160  * Number of speeds supported is the number of entries in
161  * the above table.
162  */
163 #define	N_SU_SPEEDS	(sizeof (asyspdtab)/sizeof (ushort_t))
164 
165 /*
166  * Human-readable baud rate table.
167  * Indexed by #defines found in sys/termios.h
168  */
169 int baudtable[] = {
170 	0,	/* 0 baud rate */
171 	50,	/* 50 baud rate */
172 	75,	/* 75 baud rate */
173 	110,	/* 110 baud rate */
174 	134,	/* 134 baud rate */
175 	150,	/* 150 baud rate */
176 	200,	/* 200 baud rate */
177 	300,	/* 300 baud rate */
178 	600,	/* 600 baud rate */
179 	1200,	/* 1200 baud rate */
180 	1800,	/* 1800 baud rate */
181 	2400,	/* 2400 baud rate */
182 	4800,	/* 4800 baud rate */
183 	9600,	/* 9600 baud rate */
184 	19200,	/* 19200 baud rate */
185 	38400,	/* 38400 baud rate */
186 	57600,	/* 57600 baud rate */
187 	76800,	/* 76800 baud rate */
188 	115200,	/* 115200 baud rate */
189 	153600,	/* 153600 baud rate */
190 	230400,	/* 230400 baud rate */
191 	307200,	/* 307200 baud rate */
192 	460800	/* 460800 baud rate */
193 };
194 
195 static int asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr);
196 static int asyclose(queue_t *q, int flag);
197 static void asywput(queue_t *q, mblk_t *mp);
198 static void asyrsrv(queue_t *q);
199 
200 struct module_info asy_info = {
201 	0,
202 	"su",
203 	0,
204 	INFPSZ,
205 	32*4096,
206 	4096
207 };
208 
209 static struct qinit asy_rint = {
210 	putq,
211 	(int (*)())asyrsrv,
212 	asyopen,
213 	asyclose,
214 	NULL,
215 	&asy_info,
216 	NULL
217 };
218 
219 static struct qinit asy_wint = {
220 	(int (*)())asywput,
221 	NULL,
222 	NULL,
223 	NULL,
224 	NULL,
225 	&asy_info,
226 	NULL
227 };
228 
229 struct streamtab asy_str_info = {
230 	&asy_rint,
231 	&asy_wint,
232 	NULL,
233 	NULL
234 };
235 
236 static int asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
237 		void **result);
238 static int asyprobe(dev_info_t *);
239 static int asyattach(dev_info_t *, ddi_attach_cmd_t);
240 static int asydetach(dev_info_t *, ddi_detach_cmd_t);
241 
242 static 	struct cb_ops cb_asy_ops = {
243 	nodev,			/* cb_open */
244 	nodev,			/* cb_close */
245 	nodev,			/* cb_strategy */
246 	nodev,			/* cb_print */
247 	nodev,			/* cb_dump */
248 	nodev,			/* cb_read */
249 	nodev,			/* cb_write */
250 	nodev,			/* cb_ioctl */
251 	nodev,			/* cb_devmap */
252 	nodev,			/* cb_mmap */
253 	nodev,			/* cb_segmap */
254 	nochpoll,		/* cb_chpoll */
255 	ddi_prop_op,		/* cb_prop_op */
256 	&asy_str_info,		/* cb_stream */
257 	D_MP			/* cb_flag */
258 };
259 
260 struct dev_ops asy_ops = {
261 	DEVO_REV,		/* devo_rev */
262 	0,			/* devo_refcnt */
263 	asyinfo,		/* devo_getinfo */
264 	nulldev,		/* devo_identify */
265 	asyprobe,		/* devo_probe */
266 	asyattach,		/* devo_attach */
267 	asydetach,		/* devo_detach */
268 	nodev,			/* devo_reset */
269 	&cb_asy_ops,		/* devo_cb_ops */
270 };
271 
272 /*
273  * Module linkage information for the kernel.
274  */
275 
276 static struct modldrv modldrv = {
277 	&mod_driverops, /* Type of module.  This one is a driver */
278 	"su driver %I%",
279 	&asy_ops,	/* driver ops */
280 };
281 
282 static struct modlinkage modlinkage = {
283 	MODREV_1,
284 	&modldrv,
285 	NULL
286 };
287 
288 int
289 _init(void)
290 {
291 	int status;
292 
293 	status = ddi_soft_state_init(&su_asycom, sizeof (struct asycom),
294 	    SU_INITIAL_SOFT_ITEMS);
295 	if (status != 0)
296 	    return (status);
297 	status = ddi_soft_state_init(&su_asyncline, sizeof (struct asyncline),
298 	    SU_INITIAL_SOFT_ITEMS);
299 	if (status != 0) {
300 		ddi_soft_state_fini(&su_asycom);
301 		return (status);
302 	}
303 
304 	if ((status = mod_install(&modlinkage)) != 0) {
305 		ddi_soft_state_fini(&su_asycom);
306 		ddi_soft_state_fini(&su_asyncline);
307 	}
308 
309 	return (status);
310 }
311 
312 int
313 _fini(void)
314 {
315 	int i;
316 
317 	i = mod_remove(&modlinkage);
318 	if (i == 0) {
319 		ddi_soft_state_fini(&su_asycom);
320 		ddi_soft_state_fini(&su_asyncline);
321 	}
322 
323 	return (i);
324 }
325 
326 int
327 _info(struct modinfo *modinfop)
328 {
329 	return (mod_info(&modlinkage, modinfop));
330 }
331 
332 static int
333 asyprobe(dev_info_t *devi)
334 {
335 	int		instance;
336 	ddi_acc_handle_t handle;
337 	uchar_t *addr;
338 	ddi_device_acc_attr_t attr;
339 
340 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
341 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
342 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
343 
344 	if (ddi_regs_map_setup(devi, SU_REGISTER_FILE_NO, (caddr_t *)&addr,
345 	    SU_REGOFFSET, SU_REGISTER_LEN, &attr, &handle) != DDI_SUCCESS) {
346 		cmn_err(CE_WARN, "asyprobe regs map setup failed");
347 		return (DDI_PROBE_FAILURE);
348 	}
349 #ifdef DEBUG
350 	if (asydebug)
351 	    printf("Probe address mapped %p\n", (void *)addr);
352 #endif
353 
354 	/*
355 	 * Probe for the device:
356 	 * 	Ser. int. uses bits 0,1,2; FIFO uses 3,6,7; 4,5 wired low.
357 	 * 	If bit 4 or 5 appears on inb() ISR, board is not there.
358 	 */
359 	if (ddi_get8(handle, addr+ISR) & 0x30)
360 	    return (DDI_PROBE_FAILURE);
361 	instance = ddi_get_instance(devi);
362 	if (max_asy_instance < instance)
363 	    max_asy_instance = instance;
364 	ddi_regs_map_free(&handle);
365 
366 	return (DDI_PROBE_SUCCESS); /* hw is present */
367 }
368 
369 static int
370 asydetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
371 {
372 	register int	instance;
373 	struct asycom	*asy;
374 	struct asyncline *async;
375 	char		name[16];
376 
377 	instance = ddi_get_instance(devi);	/* find out which unit */
378 
379 	asy = (struct asycom *)ddi_get_soft_state(su_asycom, instance);
380 	async = (struct asyncline *)ddi_get_soft_state(su_asyncline, instance);
381 
382 	switch (cmd) {
383 	    case DDI_DETACH:
384 		break;
385 	    case DDI_SUSPEND:
386 		/* grab both mutex locks */
387 		mutex_enter(asy->asy_excl);
388 		mutex_enter(asy->asy_excl_hi);
389 		if (asy->suspended) {
390 			mutex_exit(asy->asy_excl_hi);
391 			mutex_exit(asy->asy_excl);
392 			return (DDI_SUCCESS);
393 		}
394 		asy->suspended = B_TRUE;
395 
396 		/*
397 		 * The quad UART ST16C554D, version D2 (made by EXAR) has an
398 		 * anomaly of generating spurious interrups when the ICR is
399 		 * loaded with zero. The workaround would be to read any of
400 		 * status registers/SPR/ICR before such write. This anomaly
401 		 * will be fixed in future versions of the chip.
402 		 */
403 		(void) INB(ICR);
404 
405 		/* Disable further interrupts */
406 		OUTB(ICR, 0);
407 		mutex_exit(asy->asy_excl_hi);
408 		mutex_exit(asy->asy_excl);
409 		return (DDI_SUCCESS);
410 
411 	    default:
412 		return (DDI_FAILURE);
413 	}
414 
415 #ifdef DEBUG
416 	if (asydebug & ASY_DEBUG_INIT)
417 	    cmn_err(CE_NOTE, "su%d: ASY%s shutdown.", instance,
418 		asy->asy_hwtype == ASY82510 ? "82510" :
419 		asy->asy_hwtype == ASY16550AF ? "16550AF" :
420 		"8250");
421 #endif
422 	/*
423 	 * Before removing interrupts it is always better to disable
424 	 * interrupts if the chip gives a provision to disable the
425 	 * serial port interrupts.
426 	 */
427 	mutex_enter(asy->asy_excl);
428 	mutex_enter(asy->asy_excl_hi);
429 	/* disable interrupts, see EXAR bug */
430 	(void) INB(ICR);
431 	OUTB(ICR, 0);
432 	mutex_exit(asy->asy_excl_hi);
433 	mutex_exit(asy->asy_excl);
434 
435 	/* remove minor device node(s) for this device */
436 	(void) sprintf(name, "%c", (instance+'a'));	/* serial-port */
437 	ddi_remove_minor_node(devi, name);
438 	(void) sprintf(name, "%c,cu", (instance+'a')); /* serial-port:dailout */
439 	ddi_remove_minor_node(devi, name);
440 
441 	mutex_destroy(asy->asy_excl);
442 	mutex_destroy(asy->asy_excl_hi);
443 	kmem_free(asy->asy_excl, sizeof (kmutex_t));
444 	kmem_free(asy->asy_excl_hi, sizeof (kmutex_t));
445 	cv_destroy(&async->async_flags_cv);
446 	kstat_delete(asy->sukstat);
447 	ddi_remove_intr(devi, 0, asy->asy_iblock);
448 	ddi_regs_map_free(&asy->asy_handle);
449 	ddi_remove_softintr(asy->asy_softintr_id);
450 	mutex_destroy(asy->asy_soft_lock);
451 	kmem_free(asy->asy_soft_lock, sizeof (kmutex_t));
452 	ddi_soft_state_free(su_asycom, instance);
453 	ddi_soft_state_free(su_asyncline, instance);
454 	return (DDI_SUCCESS);
455 }
456 
457 static int
458 asyattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
459 {
460 	register int	instance;
461 	struct asycom	*asy;
462 	struct asyncline *async;
463 	char		name[40];
464 	ddi_device_acc_attr_t attr;
465 	enum states { EMPTY, SOFTSTATE, REGSMAP, MUTEXES, ADDINTR,
466 	    SOFTINTR, ASYINIT, KSTAT, MINORNODE };
467 	enum states state = EMPTY;
468 
469 	instance = ddi_get_instance(devi);	/* find out which unit */
470 
471 	/* cannot attach a device that has not been probed first */
472 	if (instance > max_asy_instance)
473 	    return (DDI_FAILURE);
474 
475 	if (cmd != DDI_RESUME) {
476 		/* Allocate soft state space */
477 		if (ddi_soft_state_zalloc(su_asycom, instance) != DDI_SUCCESS) {
478 			cmn_err(CE_WARN, "su%d: cannot allocate soft state",
479 			    instance);
480 			goto error;
481 		}
482 	}
483 	state = SOFTSTATE;
484 
485 	asy = (struct asycom *)ddi_get_soft_state(su_asycom, instance);
486 
487 	if (asy == NULL) {
488 		cmn_err(CE_WARN, "su%d: cannot get soft state", instance);
489 		goto error;
490 	}
491 
492 	switch (cmd) {
493 	    case DDI_ATTACH:
494 		break;
495 	    case DDI_RESUME: {
496 		struct asyncline *async;
497 
498 		/* grab both mutex locks */
499 		mutex_enter(asy->asy_excl);
500 		mutex_enter(asy->asy_excl_hi);
501 		if (!asy->suspended) {
502 			mutex_exit(asy->asy_excl_hi);
503 			mutex_exit(asy->asy_excl);
504 			return (DDI_SUCCESS);
505 		}
506 		/* re-setup all the registers and enable interrupts if needed */
507 		async = (struct asyncline *)asy->asy_priv;
508 		if ((async) && (async->async_flags & ASYNC_ISOPEN))
509 		    (void) asy_program(asy, ASY_INIT);
510 		asy->suspended = B_FALSE;
511 		mutex_exit(asy->asy_excl_hi);
512 		mutex_exit(asy->asy_excl);
513 		return (DDI_SUCCESS);
514 	    }
515 	    default:
516 		goto error;
517 	}
518 
519 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
520 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
521 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
522 
523 	if (ddi_regs_map_setup(devi, SU_REGISTER_FILE_NO,
524 	    (caddr_t *)&asy->asy_ioaddr, SU_REGOFFSET, SU_REGISTER_LEN,
525 	    &attr, &asy->asy_handle) != DDI_SUCCESS) {
526 		cmn_err(CE_WARN, "asyprobe regs map setup failed");
527 		goto error;
528 	}
529 	state = REGSMAP;
530 
531 #ifdef DEBUG
532 	if (asydebug)
533 	    printf("su attach mapped %p\n", (void *)asy->asy_ioaddr);
534 #endif
535 
536 	/*
537 	 * Initialize the port with default settings.
538 	 */
539 	asy->asy_fifo_buf = 1;
540 	asy->asy_use_fifo = FIFO_OFF;
541 
542 	/*
543 	 * Check for baudrate generator's "baud-divisor-factor" property setup
544 	 * by OBP, since different UART chips might have different baudrate
545 	 * generator divisor. e.g., in case of NSPG's Sputnik platform, the
546 	 * baud-divisor-factor is 13, it uses dedicated 16552 "DUART" chip
547 	 * instead of SuperIO. Since the baud-divisor-factor must be a positive
548 	 * integer, the divisors will always be at least as large as the values
549 	 * in asyspdtab[].  Make the default factor 1.
550 	 */
551 	asy->asy_baud_divisor_factor = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
552 	    DDI_PROP_DONTPASS, "baud-divisor-factor", 1);
553 
554 	/* set speed cap */
555 	asy->asy_speed_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
556 	    DDI_PROP_DONTPASS, "serial-speed-cap", 115200);
557 
558 	/* check for ASY82510 chip */
559 	OUTB(ISR, 0x20);
560 	if (INB(ISR) & 0x20) { /* 82510 chip is present */
561 		/*
562 		 * Since most of the general operation of the 82510 chip
563 		 * can be done from BANK 0 (8250A/16450 compatable mode)
564 		 * we will default to BANK 0.
565 		 */
566 		asy->asy_hwtype = ASY82510;
567 		OUTB(DAT+7, 0x04); /* clear status */
568 		OUTB(ISR, 0x40); /* set to bank 2 */
569 		OUTB(MCR, 0x08); /* IMD */
570 		OUTB(DAT, 0x21); /* FMD */
571 		OUTB(ISR, 0x00); /* set to bank 0 */
572 		asy->asy_trig_level = 0;
573 	} else { /* Set the UART in FIFO mode if it has FIFO buffers */
574 		asy->asy_hwtype = ASY16550AF;
575 		OUTB(FIFOR, 0x00); /* clear fifo register */
576 		asy->asy_trig_level = 0x00; /* sets the fifo Threshold to 1 */
577 
578 		/* set/Enable FIFO */
579 		OUTB(FIFOR, FIFO_ON | FIFODMA | FIFOTXFLSH | FIFORXFLSH |
580 		    (asy->asy_trig_level & 0xff));
581 
582 		if ((INB(ISR) & 0xc0) == 0xc0)
583 		    asy->asy_use_fifo = FIFO_ON;
584 		else {
585 			asy->asy_hwtype = ASY8250;
586 			OUTB(FIFOR, 0x00); /* NO FIFOs */
587 			asy->asy_trig_level = 0;
588 		}
589 	}
590 
591 	/* disable interrupts, see EXAR bug */
592 	(void) INB(ICR);
593 	OUTB(ICR, 0);
594 	OUTB(LCR, DLAB); /* select baud rate generator */
595 	/* Set the baud rate to 9600 */
596 	OUTB(DAT+DLL, (ASY9600*asy->asy_baud_divisor_factor) & 0xff);
597 	OUTB(DAT+DLH, ((ASY9600*asy->asy_baud_divisor_factor) >> 8) & 0xff);
598 	OUTB(LCR, STOP1|BITS8);
599 	OUTB(MCR, (DTR | RTS| OUT2));
600 
601 	/*
602 	 * Set up the other components of the asycom structure for this port.
603 	 */
604 	asy->asy_excl = (kmutex_t *)
605 	    kmem_zalloc(sizeof (kmutex_t), KM_SLEEP);
606 	asy->asy_excl_hi = (kmutex_t *)
607 	    kmem_zalloc(sizeof (kmutex_t), KM_SLEEP);
608 	asy->asy_soft_lock = (kmutex_t *)
609 	    kmem_zalloc(sizeof (kmutex_t), KM_SLEEP);
610 	asy->asy_unit = instance;
611 	asy->asy_dip = devi;
612 
613 	if (ddi_get_iblock_cookie(devi, 0, &asy->asy_iblock) != DDI_SUCCESS) {
614 		cmn_err(CE_NOTE,
615 		    "Get iblock_cookie failed-Device interrupt%x\n", instance);
616 		goto error;
617 	}
618 
619 	if (ddi_get_soft_iblock_cookie(devi, DDI_SOFTINT_HIGH,
620 	    &asy->asy_soft_iblock) != DDI_SUCCESS) {
621 		cmn_err(CE_NOTE, "Get iblock_cookie failed -soft interrupt%x\n",
622 		    instance);
623 		goto error;
624 	}
625 
626 	mutex_init(asy->asy_soft_lock, NULL, MUTEX_DRIVER,
627 	    (void *)asy->asy_soft_iblock);
628 	mutex_init(asy->asy_excl, NULL, MUTEX_DRIVER, NULL);
629 	mutex_init(asy->asy_excl_hi, NULL, MUTEX_DRIVER,
630 	    (void *)asy->asy_iblock);
631 	state = MUTEXES;
632 
633 	/*
634 	 * Install interrupt handlers for this device.
635 	 */
636 	if (ddi_add_intr(devi, 0, &(asy->asy_iblock), 0, asyintr,
637 	    (caddr_t)asy) != DDI_SUCCESS) {
638 		cmn_err(CE_CONT,
639 		    "Cannot set device interrupt for su driver\n");
640 		goto error;
641 	}
642 	state = ADDINTR;
643 
644 	if (ddi_add_softintr(devi, DDI_SOFTINT_HIGH, &(asy->asy_softintr_id),
645 	    &asy->asy_soft_iblock, 0, asysoftintr, (caddr_t)asy)
646 	    != DDI_SUCCESS) {
647 		cmn_err(CE_CONT, "Cannot set soft interrupt for su driver\n");
648 		goto error;
649 	}
650 	state = SOFTINTR;
651 
652 	/* initialize the asyncline structure */
653 	if (ddi_soft_state_zalloc(su_asyncline, instance) != DDI_SUCCESS) {
654 		cmn_err(CE_CONT, "su%d: cannot allocate soft state", instance);
655 		goto error;
656 	}
657 	state = ASYINIT;
658 
659 	async = (struct asyncline *)ddi_get_soft_state(su_asyncline, instance);
660 
661 	mutex_enter(asy->asy_excl);
662 	async->async_common = asy;
663 	cv_init(&async->async_flags_cv, NULL, CV_DEFAULT, NULL);
664 	mutex_exit(asy->asy_excl);
665 
666 	if ((asy->sukstat = kstat_create("su", instance, "serialstat",
667 	    "misc", KSTAT_TYPE_NAMED, 2, KSTAT_FLAG_VIRTUAL)) != NULL) {
668 		asy->sukstat->ks_data = &asy->kstats;
669 		kstat_named_init(&asy->kstats.ringover, "ring buffer overflow",
670 		    KSTAT_DATA_UINT64);
671 		kstat_named_init(&asy->kstats.siloover, "silo overflow",
672 		    KSTAT_DATA_UINT64);
673 		kstat_install(asy->sukstat);
674 	}
675 	state = KSTAT;
676 
677 	if (strcmp(ddi_node_name(devi), "rsc-console") == 0) {
678 		/*
679 		 * If the device is configured as the 'rsc-console'
680 		 * create the minor device for this node.
681 		 */
682 		if (ddi_create_minor_node(devi, "ssp", S_IFCHR,
683 		    asy->asy_unit | RSC_DEVICE, DDI_PSEUDO, NULL)
684 		    == DDI_FAILURE) {
685 			cmn_err(CE_WARN,
686 			    "%s%d: Failed to create node rsc-console",
687 			    ddi_get_name(devi), ddi_get_instance(devi));
688 			goto error;
689 		}
690 
691 		asy->asy_lom_console = 0;
692 		asy->asy_rsc_console = 1;
693 		asy->asy_rsc_control = 0;
694 		asy->asy_device_type = ASY_SERIAL;
695 		asy->asy_flags |= ASY_IGNORE_CD;
696 
697 	} else if (strcmp(ddi_node_name(devi), "lom-console") == 0) {
698 		/*
699 		 * If the device is configured as the 'lom-console'
700 		 * create the minor device for this node.
701 		 * Do not create a dialout device.
702 		 * Use the same minor numbers as would be used for standard
703 		 * serial instances.
704 		 */
705 		if (ddi_create_minor_node(devi, "lom-console", S_IFCHR,
706 		    instance, DDI_NT_SERIAL_LOMCON, NULL) == DDI_FAILURE) {
707 			cmn_err(CE_WARN,
708 			    "%s%d: Failed to create node lom-console",
709 			    ddi_get_name(devi), ddi_get_instance(devi));
710 			goto error;
711 		}
712 		asy->asy_lom_console = 1;
713 		asy->asy_rsc_console = 0;
714 		asy->asy_rsc_control = 0;
715 		asy->asy_device_type = ASY_SERIAL;
716 		asy->asy_flags |= ASY_IGNORE_CD;
717 
718 	} else if (strcmp(ddi_node_name(devi), "rsc-control") == 0) {
719 		/*
720 		 * If the device is configured as the 'rsc-control'
721 		 * create the minor device for this node.
722 		 */
723 		if (ddi_create_minor_node(devi, "sspctl", S_IFCHR,
724 		    asy->asy_unit | RSC_DEVICE, DDI_PSEUDO, NULL)
725 		    == DDI_FAILURE) {
726 			cmn_err(CE_WARN, "%s%d: Failed to create rsc-control",
727 			    ddi_get_name(devi), ddi_get_instance(devi));
728 			goto error;
729 		}
730 
731 		asy->asy_lom_console = 0;
732 		asy->asy_rsc_console = 0;
733 		asy->asy_rsc_control = 1;
734 		asy->asy_device_type = ASY_SERIAL;
735 		asy->asy_flags |= ASY_IGNORE_CD;
736 
737 	} else if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
738 		"keyboard", 0)) {
739 		/*
740 		 * If the device is a keyboard, then create an internal
741 		 * pathname so that the dacf code will link the node into
742 		 * the keyboard console stream.  See dacf.conf.
743 		 */
744 		if (ddi_create_internal_pathname(devi, "keyboard",
745 		    S_IFCHR, instance) == DDI_FAILURE) {
746 			goto error;
747 		}
748 		asy->asy_flags |= ASY_IGNORE_CD;	/* ignore cd */
749 		asy->asy_device_type = ASY_KEYBOARD; 	/* Device type */
750 	} else if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
751 		"mouse", 0)) {
752 		/*
753 		 * If the device is a mouse, then create an internal
754 		 * pathname so that the dacf code will link the node into
755 		 * the mouse stream.  See dacf.conf.
756 		 */
757 		if (ddi_create_internal_pathname(devi, "mouse", S_IFCHR,
758 		    instance) == DDI_FAILURE) {
759 			goto error;
760 		}
761 		asy->asy_flags |= ASY_IGNORE_CD;	/* ignore cd */
762 		asy->asy_device_type = ASY_MOUSE;
763 	} else {
764 		/*
765 		 * If not used for keyboard/mouse, create minor devices nodes
766 		 * for this device
767 		 */
768 		/* serial-port */
769 		(void) sprintf(name, "%c", (instance+'a'));
770 		if (ddi_create_minor_node(devi, name, S_IFCHR, instance,
771 		    DDI_NT_SERIAL_MB, NULL) == DDI_FAILURE) {
772 			goto error;
773 		}
774 		state = MINORNODE;
775 		/* serial-port:dailout */
776 		(void) sprintf(name, "%c,cu", (instance+'a'));
777 		if (ddi_create_minor_node(devi, name, S_IFCHR, instance|OUTLINE,
778 		    DDI_NT_SERIAL_MB_DO, NULL) == DDI_FAILURE) {
779 			goto error;
780 		}
781 		/* Property for ignoring DCD */
782 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
783 		    "ignore-cd", 0)) {
784 			asy->asy_flags |= ASY_IGNORE_CD;  /* ignore cd */
785 		} else {
786 			asy->asy_flags &= ~ASY_IGNORE_CD;
787 			/*
788 			 * if ignore-cd is not available it could be
789 			 * some old legacy platform, try to see
790 			 * whether the old legacy property exists
791 			 */
792 			(void) sprintf(name,
793 			    "port-%c-ignore-cd", (instance+ 'a'));
794 			if (ddi_getprop(DDI_DEV_T_ANY, devi,
795 			    DDI_PROP_DONTPASS, name, 0))
796 				asy->asy_flags |= ASY_IGNORE_CD;
797 		}
798 		asy->asy_device_type = ASY_SERIAL;
799 	}
800 	ddi_report_dev(devi);
801 	return (DDI_SUCCESS);
802 
803 error:
804 	if (state == MINORNODE) {
805 		(void) sprintf(name, "%c", (instance+'a'));
806 		ddi_remove_minor_node(devi, name);
807 	}
808 	if (state >= KSTAT)
809 		kstat_delete(asy->sukstat);
810 	if (state >= ASYINIT) {
811 		cv_destroy(&async->async_flags_cv);
812 		ddi_soft_state_free(su_asyncline, instance);
813 	}
814 	if (state >= SOFTINTR)
815 		ddi_remove_softintr(asy->asy_softintr_id);
816 	if (state >= ADDINTR)
817 		ddi_remove_intr(devi, 0, asy->asy_iblock);
818 	if (state >= MUTEXES) {
819 		mutex_destroy(asy->asy_excl_hi);
820 		mutex_destroy(asy->asy_excl);
821 		mutex_destroy(asy->asy_soft_lock);
822 		kmem_free(asy->asy_excl_hi, sizeof (kmutex_t));
823 		kmem_free(asy->asy_excl, sizeof (kmutex_t));
824 		kmem_free(asy->asy_soft_lock, sizeof (kmutex_t));
825 	}
826 	if (state >= REGSMAP)
827 		ddi_regs_map_free(&asy->asy_handle);
828 	if (state >= SOFTSTATE)
829 		ddi_soft_state_free(su_asycom, instance);
830 	/* no action for EMPTY state */
831 	return (DDI_FAILURE);
832 }
833 
834 static int
835 asyinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
836 	void **result)
837 {
838 	_NOTE(ARGUNUSED(dip))
839 	register dev_t dev = (dev_t)arg;
840 	register int instance, error;
841 	struct asycom *asy;
842 
843 	if ((instance = UNIT(dev)) > max_asy_instance)
844 		return (DDI_FAILURE);
845 
846 	switch (infocmd) {
847 	    case DDI_INFO_DEVT2DEVINFO:
848 		asy = (struct asycom *)ddi_get_soft_state(su_asycom, instance);
849 		if (asy->asy_dip == NULL)
850 		    error = DDI_FAILURE;
851 		else {
852 			*result = (void *) asy->asy_dip;
853 			error = DDI_SUCCESS;
854 		}
855 		break;
856 	    case DDI_INFO_DEVT2INSTANCE:
857 		*result = (void *)(uintptr_t)instance;
858 		error = DDI_SUCCESS;
859 		break;
860 	    default:
861 		error = DDI_FAILURE;
862 	}
863 	return (error);
864 }
865 
866 static int
867 asyopen(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr)
868 {
869 	_NOTE(ARGUNUSED(sflag))
870 	struct asycom	*asy;
871 	struct asyncline *async;
872 	int		mcr;
873 	int		unit;
874 	int 		len;
875 	struct termios 	*termiosp;
876 
877 #ifdef DEBUG
878 	if (asydebug & ASY_DEBUG_CLOSE)
879 		printf("open\n");
880 #endif
881 	unit = UNIT(*dev);
882 	if (unit > max_asy_instance)
883 		return (ENXIO);		/* unit not configured */
884 
885 	async = (struct asyncline *)ddi_get_soft_state(su_asyncline, unit);
886 	if (async == NULL)
887 		return (ENXIO);
888 
889 	asy = async->async_common;
890 	if (asy == NULL)
891 		return (ENXIO);		/* device not found by autoconfig */
892 
893 	mutex_enter(asy->asy_excl);
894 	asy->asy_priv = (caddr_t)async;
895 
896 again:
897 	mutex_enter(asy->asy_excl_hi);
898 	/*
899 	 * Block waiting for carrier to come up, unless this is a no-delay open.
900 	 */
901 	if (!(async->async_flags & ASYNC_ISOPEN)) {
902 		/*
903 		 * If this port is for a RSC console or control
904 		 * use the following termio info
905 		 */
906 		if (asy->asy_rsc_console || asy->asy_rsc_control) {
907 			async->async_ttycommon.t_cflag = CIBAUDEXT | CBAUDEXT |
908 			    (B115200 & CBAUD);
909 			async->async_ttycommon.t_cflag |= ((B115200 << IBSHIFT)
910 			    & CIBAUD);
911 			async->async_ttycommon.t_cflag |= CS8 | CREAD | CLOCAL;
912 		} else if (asy->asy_lom_console) {
913 			async->async_ttycommon.t_cflag = B9600 & CBAUD;
914 			async->async_ttycommon.t_cflag |= ((B9600 << IBSHIFT)
915 			    & CIBAUD);
916 			async->async_ttycommon.t_cflag |= CS8 | CREAD | CLOCAL;
917 		} else {
918 
919 			/*
920 			 * Set the default termios settings (cflag).
921 			 * Others are set in ldterm.  Release the spin
922 			 * mutex as we can block here, reaquire before
923 			 * calling asy_program.
924 			 */
925 			mutex_exit(asy->asy_excl_hi);
926 			if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_root_node(),
927 			    0, "ttymodes", (caddr_t)&termiosp, &len)
928 			    == DDI_PROP_SUCCESS &&
929 			    len == sizeof (struct termios)) {
930 				async->async_ttycommon.t_cflag =
931 				    termiosp->c_cflag;
932 				kmem_free(termiosp, len);
933 			} else {
934 				cmn_err(CE_WARN,
935 					"su: couldn't get ttymodes property!");
936 			}
937 			mutex_enter(asy->asy_excl_hi);
938 		}
939 		async->async_ttycommon.t_iflag = 0;
940 		async->async_ttycommon.t_iocpending = NULL;
941 		async->async_ttycommon.t_size.ws_row = 0;
942 		async->async_ttycommon.t_size.ws_col = 0;
943 		async->async_ttycommon.t_size.ws_xpixel = 0;
944 		async->async_ttycommon.t_size.ws_ypixel = 0;
945 		async->async_dev = *dev;
946 		async->async_wbufcid = 0;
947 
948 		async->async_startc = CSTART;
949 		async->async_stopc = CSTOP;
950 		(void) asy_program(asy, ASY_INIT);
951 	} else if ((async->async_ttycommon.t_flags & TS_XCLUDE) &&
952 					    secpolicy_excl_open(cr) != 0) {
953 		mutex_exit(asy->asy_excl_hi);
954 		mutex_exit(asy->asy_excl);
955 		return (EBUSY);
956 	} else if ((*dev & OUTLINE) && !(async->async_flags & ASYNC_OUT)) {
957 		mutex_exit(asy->asy_excl_hi);
958 		mutex_exit(asy->asy_excl);
959 		return (EBUSY);
960 	}
961 
962 	if (*dev & OUTLINE)
963 		async->async_flags |= ASYNC_OUT;
964 
965 	/* Raise DTR on every open */
966 	mcr = INB(MCR);
967 	OUTB(MCR, mcr|DTR);
968 
969 	/*
970 	 * Check carrier.
971 	 */
972 	if (asy->asy_flags & ASY_IGNORE_CD)
973 		async->async_ttycommon.t_flags |= TS_SOFTCAR;
974 	if ((async->async_ttycommon.t_flags & TS_SOFTCAR) ||
975 					(INB(MSR) & DCD))
976 		async->async_flags |= ASYNC_CARR_ON;
977 	else
978 		async->async_flags &= ~ASYNC_CARR_ON;
979 	mutex_exit(asy->asy_excl_hi);
980 
981 	/*
982 	 * If FNDELAY and FNONBLOCK are clear, block until carrier up.
983 	 * Quit on interrupt.
984 	 */
985 	if (!(flag & (FNDELAY|FNONBLOCK)) &&
986 			!(async->async_ttycommon.t_cflag & CLOCAL)) {
987 		if (!(async->async_flags & (ASYNC_CARR_ON|ASYNC_OUT)) ||
988 				((async->async_flags & ASYNC_OUT) &&
989 				!(*dev & OUTLINE))) {
990 			async->async_flags |= ASYNC_WOPEN;
991 			if (cv_wait_sig(&async->async_flags_cv,
992 			    asy->asy_excl) == 0) {
993 				async->async_flags &= ~ASYNC_WOPEN;
994 				mutex_exit(asy->asy_excl);
995 				return (EINTR);
996 			}
997 			async->async_flags &= ~ASYNC_WOPEN;
998 			goto again;
999 		}
1000 	} else if ((async->async_flags & ASYNC_OUT) && !(*dev & OUTLINE)) {
1001 			mutex_exit(asy->asy_excl);
1002 			return (EBUSY);
1003 	}
1004 
1005 	if (asy->suspended) {
1006 		mutex_exit(asy->asy_excl);
1007 		(void) ddi_dev_is_needed(asy->asy_dip, 0, 1);
1008 		mutex_enter(asy->asy_excl);
1009 	}
1010 
1011 	async->async_ttycommon.t_readq = rq;
1012 	async->async_ttycommon.t_writeq = WR(rq);
1013 	rq->q_ptr = WR(rq)->q_ptr = (caddr_t)async;
1014 	mutex_exit(asy->asy_excl);
1015 	qprocson(rq);
1016 	async->async_flags |= ASYNC_ISOPEN;
1017 	async->async_polltid = 0;
1018 	return (0);
1019 }
1020 
1021 static void
1022 async_progress_check(void *arg)
1023 {
1024 	struct asyncline *async = arg;
1025 	struct asycom	 *asy = async->async_common;
1026 	mblk_t *bp;
1027 
1028 	/*
1029 	 * We define "progress" as either waiting on a timed break or delay, or
1030 	 * having had at least one transmitter interrupt.  If none of these are
1031 	 * true, then just terminate the output and wake up that close thread.
1032 	 */
1033 	mutex_enter(asy->asy_excl);
1034 	mutex_enter(asy->asy_excl_hi);
1035 	if (!(async->async_flags & (ASYNC_BREAK|ASYNC_DELAY|ASYNC_PROGRESS))) {
1036 		async->async_ocnt = 0;
1037 		async->async_flags &= ~ASYNC_BUSY;
1038 		async->async_timer = 0;
1039 		bp = async->async_xmitblk;
1040 		async->async_xmitblk = NULL;
1041 		mutex_exit(asy->asy_excl_hi);
1042 		if (bp != NULL)
1043 			freeb(bp);
1044 		/*
1045 		 * Since this timer is running, we know that we're in exit(2).
1046 		 * That means that the user can't possibly be waiting on any
1047 		 * valid ioctl(2) completion anymore, and we should just flush
1048 		 * everything.
1049 		 */
1050 		flushq(async->async_ttycommon.t_writeq, FLUSHALL);
1051 		cv_broadcast(&async->async_flags_cv);
1052 	} else {
1053 		async->async_flags &= ~ASYNC_PROGRESS;
1054 		async->async_timer = timeout(async_progress_check, async,
1055 		    drv_usectohz(su_drain_check));
1056 		mutex_exit(asy->asy_excl_hi);
1057 	}
1058 	mutex_exit(asy->asy_excl);
1059 }
1060 
1061 /*
1062  * Close routine.
1063  */
1064 static int
1065 asyclose(queue_t *q, int flag)
1066 {
1067 	struct asyncline *async;
1068 	struct asycom	 *asy;
1069 	int icr, lcr;
1070 	int		nohupcl;
1071 
1072 
1073 #ifdef DEBUG
1074 	if (asydebug & ASY_DEBUG_CLOSE)
1075 		printf("close\n");
1076 #endif
1077 	async = q->q_ptr;
1078 	ASSERT(async != NULL);
1079 	asy = async->async_common;
1080 
1081 	/* get the nohupcl OBP property of this device */
1082 	nohupcl = ddi_getprop(DDI_DEV_T_ANY, asy->asy_dip, DDI_PROP_DONTPASS,
1083 	    "nohupcl", 0);
1084 
1085 	mutex_enter(asy->asy_excl);
1086 	async->async_flags |= ASYNC_CLOSING;
1087 
1088 	/*
1089 	 * Turn off PPS handling early to avoid events occuring during
1090 	 * close.  Also reset the DCD edge monitoring bit.
1091 	 */
1092 	mutex_enter(asy->asy_excl_hi);
1093 	asy->asy_flags &= ~(ASY_PPS | ASY_PPS_EDGE);
1094 	mutex_exit(asy->asy_excl_hi);
1095 
1096 	/*
1097 	 * There are two flavors of break -- timed (M_BREAK or TCSBRK) and
1098 	 * untimed (TIOCSBRK).  For the timed case, these are enqueued on our
1099 	 * write queue and there's a timer running, so we don't have to worry
1100 	 * about them.  For the untimed case, though, the user obviously made a
1101 	 * mistake, because these are handled immediately.  We'll terminate the
1102 	 * break now and honor his implicit request by discarding the rest of
1103 	 * the data.
1104 	 */
1105 	if (!(async->async_flags & ASYNC_BREAK)) {
1106 		mutex_enter(asy->asy_excl_hi);
1107 		lcr = INB(LCR);
1108 		if (lcr & SETBREAK) {
1109 			OUTB(LCR, (lcr & ~SETBREAK));
1110 		}
1111 		mutex_exit(asy->asy_excl_hi);
1112 		if (lcr & SETBREAK)
1113 			goto nodrain;
1114 	}
1115 
1116 	/*
1117 	 * If the user told us not to delay the close ("non-blocking"), then
1118 	 * don't bother trying to drain.
1119 	 *
1120 	 * If the user did M_STOP (ASYNC_STOPPED), there's no hope of ever
1121 	 * getting an M_START (since these messages aren't enqueued), and the
1122 	 * only other way to clear the stop condition is by loss of DCD, which
1123 	 * would discard the queue data.  Thus, we drop the output data if
1124 	 * ASYNC_STOPPED is set.
1125 	 */
1126 	if ((flag & (FNDELAY|FNONBLOCK)) ||
1127 	    (async->async_flags & ASYNC_STOPPED)) {
1128 		goto nodrain;
1129 	}
1130 
1131 	/*
1132 	 * If there's any pending output, then we have to try to drain it.
1133 	 * There are two main cases to be handled:
1134 	 *	- called by close(2): need to drain until done or until
1135 	 *	  a signal is received.  No timeout.
1136 	 *	- called by exit(2): need to drain while making progress
1137 	 *	  or until a timeout occurs.  No signals.
1138 	 *
1139 	 * If we can't rely on receiving a signal to get us out of a hung
1140 	 * session, then we have to use a timer.  In this case, we set a timer
1141 	 * to check for progress in sending the output data -- all that we ask
1142 	 * (at each interval) is that there's been some progress made.  Since
1143 	 * the interrupt routine grabs buffers from the write queue, we can't
1144 	 * trust async_ocnt.  Instead, we use a flag.
1145 	 *
1146 	 * Note that loss of carrier will cause the output queue to be flushed,
1147 	 * and we'll wake up again and finish normally.
1148 	 */
1149 	if (!ddi_can_receive_sig() && su_drain_check != 0) {
1150 		async->async_flags &= ~ASYNC_PROGRESS;
1151 		async->async_timer = timeout(async_progress_check, async,
1152 		    drv_usectohz(su_drain_check));
1153 	}
1154 
1155 	while (async->async_ocnt > 0 ||
1156 	    async->async_ttycommon.t_writeq->q_first != NULL ||
1157 	    (async->async_flags & (ASYNC_BUSY|ASYNC_BREAK|ASYNC_DELAY))) {
1158 		if (cv_wait_sig(&async->async_flags_cv, asy->asy_excl) == 0)
1159 			break;
1160 	}
1161 	if (async->async_timer != 0) {
1162 		(void) untimeout(async->async_timer);
1163 		async->async_timer = 0;
1164 	}
1165 
1166 nodrain:
1167 	mutex_enter(asy->asy_excl_hi);
1168 
1169 	/* turn off the loopback mode */
1170 	if ((async->async_dev != rconsdev) &&
1171 	    (async->async_dev != kbddev) &&
1172 	    (async->async_dev != stdindev)) {
1173 		OUTB(MCR, INB(MCR) & ~ ASY_LOOP);
1174 	}
1175 
1176 	async->async_ocnt = 0;
1177 	if (async->async_xmitblk != NULL)
1178 		freeb(async->async_xmitblk);
1179 	async->async_xmitblk = NULL;
1180 
1181 	/*
1182 	 * If the "nohupcl" OBP property is set for this device, do
1183 	 * not turn off DTR and RTS no matter what.  Otherwise, if the
1184 	 * line has HUPCL set or is incompletely opened, turn off DTR
1185 	 * and RTS to fix the modem line.
1186 	 */
1187 	if (!nohupcl && ((async->async_ttycommon.t_cflag & HUPCL) ||
1188 	    (async->async_flags & ASYNC_WOPEN))) {
1189 		/* turn off DTR, RTS but NOT interrupt to 386 */
1190 		OUTB(MCR, OUT2);
1191 		mutex_exit(asy->asy_excl_hi);
1192 		/*
1193 		 * Don't let an interrupt in the middle of close
1194 		 * bounce us back to the top; just continue closing
1195 		 * as if nothing had happened.
1196 		 */
1197 		if (cv_wait_sig(&lbolt_cv, asy->asy_excl) == 0)
1198 			goto out;
1199 		mutex_enter(asy->asy_excl_hi);
1200 	}
1201 
1202 	/*
1203 	 * If nobody's using it now, turn off receiver interrupts.
1204 	 */
1205 	if ((async->async_flags & (ASYNC_WOPEN|ASYNC_ISOPEN)) == 0) {
1206 		icr = INB(ICR);
1207 		OUTB(ICR, (icr & ~RIEN));
1208 	}
1209 	mutex_exit(asy->asy_excl_hi);
1210 out:
1211 	/*
1212 	 * Clear out device state.
1213 	 */
1214 	async->async_flags = 0;
1215 	ttycommon_close(&async->async_ttycommon);
1216 	cv_broadcast(&async->async_flags_cv);
1217 
1218 	/*
1219 	 * Clear ASY_DOINGSOFT and ASY_NEEDSOFT in case we were in
1220 	 * async_softint or an interrupt was pending when the process
1221 	 * using the port exited.
1222 	 */
1223 	asy->asy_flags &= ~ASY_DOINGSOFT & ~ASY_NEEDSOFT;
1224 
1225 	/*
1226 	 * Cancel outstanding "bufcall" request.
1227 	 */
1228 	if (async->async_wbufcid) {
1229 		unbufcall(async->async_wbufcid);
1230 		async->async_wbufcid = 0;
1231 	}
1232 
1233 	/*
1234 	 * If inperim is true, it means the port is closing while there's
1235 	 * a pending software interrupt.  async_flags has been zeroed out,
1236 	 * so this instance of leaveq() needs to be called before we call
1237 	 * qprocsoff() to disable services on the q.  If inperim is false,
1238 	 * leaveq() has already been called or we're not in a perimeter.
1239 	 */
1240 	if (asy->inperim == B_TRUE) {
1241 		asy->inperim = B_FALSE;
1242 		mutex_exit(asy->asy_excl);
1243 		leaveq(q);
1244 	} else {
1245 		mutex_exit(asy->asy_excl);
1246 	}
1247 
1248 	/* Note that qprocsoff can't be done until after interrupts are off */
1249 	qprocsoff(q);
1250 	q->q_ptr = WR(q)->q_ptr = NULL;
1251 	async->async_ttycommon.t_readq = NULL;
1252 	async->async_ttycommon.t_writeq = NULL;
1253 
1254 	return (0);
1255 }
1256 
1257 /*
1258  * Checks to see if the serial port is still transmitting
1259  * characters.  It returns true when there are characters
1260  * queued to transmit,  when the holding register contains
1261  * a byte, or when the shifting register still contains
1262  * data to send.
1263  *
1264  */
1265 static boolean_t
1266 asy_isbusy(struct asycom *asy)
1267 {
1268 	struct asyncline *async;
1269 
1270 #ifdef DEBUG
1271 	if (asydebug & ASY_DEBUG_EOT)
1272 		printf("isbusy\n");
1273 #endif
1274 	async = (struct asyncline *)asy->asy_priv;
1275 	ASSERT(mutex_owned(asy->asy_excl));
1276 	ASSERT(mutex_owned(asy->asy_excl_hi));
1277 	return ((async->async_ocnt > 0) ||
1278 			((INB(LSR) & XSRE) == 0));
1279 }
1280 
1281 /*
1282  * Program the ASY port. Most of the async operation is based on the values
1283  * of 'c_iflag' and 'c_cflag'.
1284  */
1285 static int
1286 asy_program(struct asycom *asy, int mode)
1287 {
1288 	struct asyncline *async;
1289 	int baudrate, c_flag;
1290 	int icr, lcr;
1291 	int ocflags;
1292 	int error = 0;
1293 
1294 	ASSERT(mutex_owned(asy->asy_excl));
1295 	ASSERT(mutex_owned(asy->asy_excl_hi));
1296 
1297 #ifdef DEBUG
1298 	if (asydebug & ASY_DEBUG_PROCS)
1299 		printf("program\n");
1300 #endif
1301 	async = (struct asyncline *)asy->asy_priv;
1302 
1303 	baudrate = async->async_ttycommon.t_cflag & CBAUD;
1304 	if (async->async_ttycommon.t_cflag & CBAUDEXT)
1305 		baudrate += 16;
1306 
1307 	/* Limit baudrate so it can't index out of baudtable */
1308 	if (baudrate >= N_SU_SPEEDS) baudrate = B9600;
1309 
1310 	/*
1311 	 * If baud rate requested is greater than the speed cap
1312 	 * or is an unsupported baud rate then reset t_cflag baud
1313 	 * to the last valid baud rate.  If this is the initial
1314 	 * pass through asy_program then set it to 9600.
1315 	 */
1316 	if (((baudrate > 0) && (asyspdtab[baudrate] == 0)) ||
1317 	    (baudtable[baudrate] > asy->asy_speed_cap)) {
1318 		async->async_ttycommon.t_cflag &= ~CBAUD & ~CBAUDEXT &
1319 		    ~CIBAUD & ~CIBAUDEXT;
1320 		if (mode == ASY_INIT) {
1321 			async->async_ttycommon.t_cflag |= B9600;
1322 			baudrate = B9600;
1323 		} else {
1324 			async->async_ttycommon.t_cflag |=
1325 			    (asy->asy_ocflags & (CBAUD | CBAUDEXT |
1326 			    CIBAUD | CIBAUDEXT));
1327 		}
1328 		error = EINVAL;
1329 		goto end;
1330 	}
1331 
1332 	/* set the baud rate */
1333 	if (async->async_ttycommon.t_cflag & (CIBAUD|CIBAUDEXT)) {
1334 		async->async_ttycommon.t_cflag &= ~(CIBAUD);
1335 		if (baudrate > CBAUD) {
1336 			async->async_ttycommon.t_cflag |= CIBAUDEXT;
1337 			async->async_ttycommon.t_cflag |=
1338 				(((baudrate - CBAUD -1)<< IBSHIFT) & CIBAUD);
1339 		} else {
1340 			async->async_ttycommon.t_cflag &= ~CIBAUDEXT;
1341 			async->async_ttycommon.t_cflag |=
1342 				((baudrate << IBSHIFT) & CIBAUD);
1343 		}
1344 	}
1345 
1346 	c_flag = async->async_ttycommon.t_cflag &
1347 	    (CLOCAL | CREAD | CSTOPB | CSIZE | PARENB | PARODD | CBAUD |
1348 	    CBAUDEXT | CIBAUD | CIBAUDEXT);
1349 
1350 	/* disable interrupts, see EXAR bug */
1351 	(void) INB(ICR);
1352 	OUTB(ICR, 0);
1353 
1354 	ocflags = asy->asy_ocflags;
1355 
1356 	/* flush/reset the status registers */
1357 	if (mode == ASY_INIT) {
1358 		(void) INB(DAT);
1359 		(void) INB(ISR);
1360 		(void) INB(LSR);
1361 		(void) INB(MSR);
1362 	}
1363 
1364 	if (ocflags != (c_flag & ~CLOCAL) || mode == ASY_INIT) {
1365 		/* Set line control */
1366 		lcr = INB(LCR);
1367 		lcr &= ~(WLS0|WLS1|STB|PEN|EPS);
1368 
1369 		if (c_flag & CSTOPB)
1370 			lcr |= STB;	/* 2 stop bits */
1371 
1372 		if (c_flag & PARENB)
1373 			lcr |= PEN;
1374 
1375 		if ((c_flag & PARODD) == 0)
1376 			lcr |= EPS;
1377 
1378 		switch (c_flag & CSIZE) {
1379 		case CS5:
1380 			lcr |= BITS5;
1381 			break;
1382 		case CS6:
1383 			lcr |= BITS6;
1384 			break;
1385 		case CS7:
1386 			lcr |= BITS7;
1387 			break;
1388 		case CS8:
1389 			lcr |= BITS8;
1390 			break;
1391 		}
1392 
1393 		/* set the baud rate when the rate is NOT B0 */
1394 		if (baudrate != 0) {
1395 			OUTB(LCR, DLAB);
1396 			OUTB(DAT, (asyspdtab[baudrate] *
1397 				asy->asy_baud_divisor_factor) & 0xff);
1398 			OUTB(ICR, ((asyspdtab[baudrate] *
1399 				asy->asy_baud_divisor_factor) >> 8) & 0xff);
1400 		}
1401 		/* set the line control modes */
1402 		OUTB(LCR, lcr);
1403 
1404 		/*
1405 		 * if transitioning from CREAD off to CREAD on,
1406 		 * flush the FIFO buffer if we have one.
1407 		 */
1408 		if ((ocflags & CREAD) == 0 && (c_flag & CREAD)) {
1409 			if (asy->asy_use_fifo == FIFO_ON) {
1410 				OUTB(FIFOR, FIFO_ON | FIFODMA | FIFORXFLSH |
1411 				    (asy->asy_trig_level & 0xff));
1412 			}
1413 		}
1414 
1415 		/* remember the new cflags */
1416 		asy->asy_ocflags = c_flag & ~CLOCAL;
1417 	}
1418 
1419 	/* whether or not CLOCAL is set, modify the modem control lines */
1420 	if (baudrate == 0)
1421 		/* B0 has been issued, lower DTR */
1422 		OUTB(MCR, RTS|OUT2);
1423 	else
1424 		/* raise DTR */
1425 		OUTB(MCR, DTR|RTS|OUT2);
1426 
1427 	/*
1428 	 * Call the modem status interrupt handler to check for the carrier
1429 	 * in case CLOCAL was turned off after the carrier came on.
1430 	 * (Note: Modem status interrupt is not enabled if CLOCAL is ON.)
1431 	 */
1432 	async_msint(asy);
1433 
1434 	/* Set interrupt control */
1435 	if ((c_flag & CLOCAL) && !(async->async_ttycommon.t_cflag & CRTSCTS))
1436 		/*
1437 		 * direct-wired line ignores DCD, so we don't enable modem
1438 		 * status interrupts.
1439 		 */
1440 		icr = (TIEN | SIEN);
1441 	else
1442 		icr = (TIEN | SIEN | MIEN);
1443 
1444 	if (c_flag & CREAD)
1445 		icr |= RIEN;
1446 
1447 	OUTB(ICR, icr);
1448 end:
1449 	return (error);
1450 }
1451 
1452 /*
1453  * asyintr() is the High Level Interrupt Handler.
1454  *
1455  * There are four different interrupt types indexed by ISR register values:
1456  *		0: modem
1457  *		1: Tx holding register is empty, ready for next char
1458  *		2: Rx register now holds a char to be picked up
1459  *		3: error or break on line
1460  * This routine checks the Bit 0 (interrupt-not-pending) to determine if
1461  * the interrupt is from this port.
1462  */
1463 uint_t
1464 asyintr(caddr_t argasy)
1465 {
1466 	struct asycom		*asy = (struct asycom *)argasy;
1467 	struct asyncline	*async;
1468 	int			ret_status = DDI_INTR_UNCLAIMED;
1469 	uchar_t			interrupt_id, lsr;
1470 
1471 	interrupt_id = INB(ISR) & 0x0F;
1472 	async = (struct asyncline *)asy->asy_priv;
1473 	if ((async == NULL) ||
1474 		!(async->async_flags & (ASYNC_ISOPEN|ASYNC_WOPEN))) {
1475 		if (interrupt_id & NOINTERRUPT)  {
1476 			return (DDI_INTR_UNCLAIMED);
1477 		} else {
1478 			lsr = INB(LSR);
1479 			if ((lsr & BRKDET) &&
1480 			    ((abort_enable == KIOCABORTENABLE) &&
1481 			    (async->async_dev == rconsdev)))
1482 				abort_sequence_enter((char *)NULL);
1483 			else {
1484 				/* reset line status */
1485 				(void) INB(LSR);
1486 				/* discard any data */
1487 				(void) INB(DAT);
1488 				/* reset modem status */
1489 				(void) INB(MSR);
1490 				return (DDI_INTR_CLAIMED);
1491 			}
1492 		}
1493 	}
1494 	/*
1495 	 * Spurious interrupts happen in this driver
1496 	 * because of the transmission on serial port not handled
1497 	 * properly.
1498 	 *
1499 	 * The reasons for Spurious interrupts are:
1500 	 *    1. There is a path in async_nstart which transmits
1501 	 *	 characters without going through interrupt services routine
1502 	 *	 which causes spurious interrupts to happen.
1503 	 *    2. In the async_txint more than one character is sent
1504 	 *	 in one interrupt service.
1505 	 *    3. In async_rxint more than one characters are received in
1506 	 *	 in one interrupt service.
1507 	 *
1508 	 * Hence we have flags to indicate that such scenerio has happened.
1509 	 * and claim only such interrupts and others we donot claim it
1510 	 * as it could be a indicator of some hardware problem.
1511 	 *
1512 	 */
1513 	if (interrupt_id & NOINTERRUPT) {
1514 		mutex_enter(asy->asy_excl_hi);
1515 		if ((asy->asy_xmit_count > 1) ||
1516 			    (asy->asy_out_of_band_xmit > 0) ||
1517 				(asy->asy_rx_count > 1)) {
1518 			asy->asy_xmit_count = 0;
1519 			asy->asy_out_of_band_xmit = 0;
1520 			asy->asy_rx_count = 0;
1521 			mutex_exit(asy->asy_excl_hi);
1522 			return (DDI_INTR_CLAIMED);
1523 		} else {
1524 			mutex_exit(asy->asy_excl_hi);
1525 			return (DDI_INTR_UNCLAIMED);
1526 		}
1527 	}
1528 	ret_status = DDI_INTR_CLAIMED;
1529 	mutex_enter(asy->asy_excl_hi);
1530 	if (asy->asy_hwtype == ASY82510)
1531 		OUTB(ISR, 0x00); /* set bank 0 */
1532 
1533 #ifdef DEBUG
1534 	if (asydebug & ASY_DEBUG_INTR)
1535 		prom_printf("l");
1536 #endif
1537 	lsr = INB(LSR);
1538 	switch (interrupt_id) {
1539 	case RxRDY:
1540 	case RSTATUS:
1541 	case FFTMOUT:
1542 		/* receiver interrupt or receiver errors */
1543 		async_rxint(asy, lsr);
1544 		break;
1545 	case TxRDY:
1546 		/* transmit interrupt */
1547 		async_txint(asy, lsr);
1548 		break;
1549 	case MSTATUS:
1550 		/* modem status interrupt */
1551 		async_msint(asy);
1552 		break;
1553 	}
1554 	mutex_exit(asy->asy_excl_hi);
1555 	return (ret_status);
1556 }
1557 
1558 /*
1559  * Transmitter interrupt service routine.
1560  * If there is more data to transmit in the current pseudo-DMA block,
1561  * send the next character if output is not stopped or draining.
1562  * Otherwise, queue up a soft interrupt.
1563  *
1564  * XXX -  Needs review for HW FIFOs.
1565  */
1566 static void
1567 async_txint(struct asycom *asy, uchar_t lsr)
1568 {
1569 	struct asyncline *async = (struct asyncline *)asy->asy_priv;
1570 	int		fifo_len;
1571 	int		xmit_progress;
1572 
1573 	asycheckflowcontrol_hw(asy);
1574 
1575 	/*
1576 	 * If ASYNC_BREAK has been set, return to asyintr()'s context to
1577 	 * claim the interrupt without performing any action.
1578 	 */
1579 	if (async->async_flags & ASYNC_BREAK)
1580 		return;
1581 
1582 	fifo_len = asy->asy_fifo_buf; /* with FIFO buffers */
1583 
1584 	/*
1585 	 * Check for flow control and do the needed action.
1586 	 */
1587 	if (asycheckflowcontrol_sw(asy)) {
1588 		return;
1589 	}
1590 
1591 	if (async->async_ocnt > 0 &&
1592 	    !(async->async_flags & (ASYNC_HW_OUT_FLW|ASYNC_STOPPED))) {
1593 		xmit_progress = 0;
1594 		while (fifo_len > 0 && async->async_ocnt > 0) {
1595 			if (lsr & XHRE) {
1596 				OUTB(DAT, *async->async_optr++);
1597 				fifo_len--;
1598 				async->async_ocnt--;
1599 				xmit_progress++;
1600 			}
1601 			/*
1602 			 * Reading the lsr, (moved reading at the end of
1603 			 * while loop) as already we have read once at
1604 			 * the beginning of interrupt service
1605 			 */
1606 			lsr = INB(LSR);
1607 		}
1608 		asy->asy_xmit_count = xmit_progress;
1609 		if (xmit_progress > 0)
1610 			async->async_flags |= ASYNC_PROGRESS;
1611 	}
1612 
1613 	if (fifo_len == 0) {
1614 		return;
1615 	}
1616 
1617 
1618 	ASYSETSOFT(asy);
1619 }
1620 
1621 /*
1622  * Receiver interrupt: RxRDY interrupt, FIFO timeout interrupt or receive
1623  * error interrupt.
1624  * Try to put the character into the circular buffer for this line; if it
1625  * overflows, indicate a circular buffer overrun. If this port is always
1626  * to be serviced immediately, or the character is a STOP character, or
1627  * more than 15 characters have arrived, queue up a soft interrupt to
1628  * drain the circular buffer.
1629  * XXX - needs review for hw FIFOs support.
1630  */
1631 
1632 static void
1633 async_rxint(struct asycom *asy, uchar_t lsr)
1634 {
1635 	struct asyncline *async = (struct asyncline *)asy->asy_priv;
1636 	uchar_t c = 0;
1637 	uint_t s = 0, needsoft = 0;
1638 	register tty_common_t *tp;
1639 
1640 	tp = &async->async_ttycommon;
1641 	if (!(tp->t_cflag & CREAD)) {
1642 		if (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
1643 			(void) (INB(DAT) & 0xff);
1644 		}
1645 		return; /* line is not open for read? */
1646 	}
1647 	asy->asy_rx_count = 0;
1648 	while (lsr & (RCA|PARERR|FRMERR|BRKDET|OVRRUN)) {
1649 		c = 0;
1650 		s = 0;
1651 		asy->asy_rx_count++;
1652 		if (lsr & RCA) {
1653 			c = INB(DAT) & 0xff;
1654 			/*
1655 			 * Even a single character is received
1656 			 * we need Soft interrupt to pass it to
1657 			 * higher layers.
1658 			 */
1659 			needsoft = 1;
1660 		}
1661 
1662 		/* Check for character break sequence */
1663 		if ((abort_enable == KIOCABORTALTERNATE) &&
1664 		    (async->async_dev == rconsdev)) {
1665 			if (abort_charseq_recognize(c))
1666 				abort_sequence_enter((char *)NULL);
1667 			}
1668 
1669 		/* Handle framing errors */
1670 		if (lsr & (PARERR|FRMERR|BRKDET|OVRRUN)) {
1671 			if (lsr & PARERR) {
1672 				if (tp->t_iflag & INPCK) /* parity enabled */
1673 					s |= PERROR;
1674 			}
1675 			if (lsr & (FRMERR|BRKDET))
1676 				s |= FRERROR;
1677 			if (lsr & OVRRUN) {
1678 				async->async_hw_overrun = 1;
1679 				s |= OVERRUN;
1680 			}
1681 		}
1682 
1683 		if (s == 0)
1684 			if ((tp->t_iflag & PARMRK) &&
1685 					!(tp->t_iflag & (IGNPAR|ISTRIP)) &&
1686 						(c == 0377))
1687 				if (RING_POK(async, 2)) {
1688 					RING_PUT(async, 0377);
1689 					RING_PUT(async, c);
1690 				} else
1691 					async->async_sw_overrun = 1;
1692 			else
1693 				if (RING_POK(async, 1))
1694 					RING_PUT(async, c);
1695 				else
1696 					async->async_sw_overrun = 1;
1697 		else
1698 			if (s & FRERROR) { /* Handle framing errors */
1699 				if (c == 0)  {
1700 		/* Look for break on kbd, stdin, or rconsdev */
1701 					if ((async->async_dev == kbddev) ||
1702 					    ((async->async_dev == rconsdev) ||
1703 					    (async->async_dev == stdindev)) &&
1704 					    (abort_enable !=
1705 					    KIOCABORTALTERNATE))
1706 						abort_sequence_enter((char *)0);
1707 					else
1708 						async->async_break++;
1709 				} else {
1710 					if (RING_POK(async, 1))
1711 						RING_MARK(async, c, s);
1712 					else
1713 						async->async_sw_overrun = 1;
1714 				}
1715 			} else  { /* Parity errors  handled by ldterm */
1716 				if (RING_POK(async, 1))
1717 					RING_MARK(async, c, s);
1718 				else
1719 					async->async_sw_overrun = 1;
1720 			}
1721 		lsr = INB(LSR);
1722 		if (asy->asy_rx_count > 16) break;
1723 	}
1724 	/* Check whether there is a request for hw/sw inbound/input flow ctrl */
1725 	if ((async->async_ttycommon.t_cflag & CRTSXOFF) ||
1726 		(async->async_ttycommon.t_iflag & IXOFF))
1727 		if ((int)(RING_CNT(async)) > (RINGSIZE * 3)/4) {
1728 #ifdef DEBUG
1729 			if (asydebug & ASY_DEBUG_HFLOW)
1730 				printf("asy%d: hardware flow stop input.\n",
1731 				UNIT(async->async_dev));
1732 #endif
1733 			async->async_flags |= ASYNC_HW_IN_FLOW;
1734 			async->async_flowc = async->async_stopc;
1735 			async->async_ringbuf_overflow = 1;
1736 		}
1737 
1738 	if ((async->async_flags & ASYNC_SERVICEIMM) || needsoft ||
1739 			(RING_FRAC(async)) || (async->async_polltid == 0))
1740 		ASYSETSOFT(asy);	/* need a soft interrupt */
1741 }
1742 
1743 /*
1744  * Interrupt on port: handle PPS event.  This function is only called
1745  * for a port on which PPS event handling has been enabled.
1746  */
1747 static void
1748 asy_ppsevent(struct asycom *asy, int msr)
1749 {
1750 	if (asy->asy_flags & ASY_PPS_EDGE) {
1751 		/* Have seen leading edge, now look for and record drop */
1752 		if ((msr & DCD) == 0)
1753 			asy->asy_flags &= ~ASY_PPS_EDGE;
1754 		/*
1755 		 * Waiting for leading edge, look for rise; stamp event and
1756 		 * calibrate kernel clock.
1757 		 */
1758 	} else if (msr & DCD) {
1759 		/*
1760 		 * This code captures a timestamp at the designated
1761 		 * transition of the PPS signal (DCD asserted).  The
1762 		 * code provides a pointer to the timestamp, as well
1763 		 * as the hardware counter value at the capture.
1764 		 *
1765 		 * Note: the kernel has nano based time values while
1766 		 * NTP requires micro based, an in-line fast algorithm
1767 		 * to convert nsec to usec is used here -- see hrt2ts()
1768 		 * in common/os/timers.c for a full description.
1769 		 */
1770 		struct timeval *tvp = &asy_ppsev.tv;
1771 		timestruc_t ts;
1772 		long nsec, usec;
1773 
1774 		asy->asy_flags |= ASY_PPS_EDGE;
1775 		gethrestime(&ts);
1776 		nsec = ts.tv_nsec;
1777 		usec = nsec + (nsec >> 2);
1778 		usec = nsec + (usec >> 1);
1779 		usec = nsec + (usec >> 2);
1780 		usec = nsec + (usec >> 4);
1781 		usec = nsec - (usec >> 3);
1782 		usec = nsec + (usec >> 2);
1783 		usec = nsec + (usec >> 3);
1784 		usec = nsec + (usec >> 4);
1785 		usec = nsec + (usec >> 1);
1786 		usec = nsec + (usec >> 6);
1787 		tvp->tv_usec = usec >> 10;
1788 		tvp->tv_sec = ts.tv_sec;
1789 
1790 		++asy_ppsev.serial;
1791 
1792 		/*
1793 		 * Because the kernel keeps a high-resolution time,
1794 		 * pass the current highres timestamp in tvp and zero
1795 		 * in usec.
1796 		 */
1797 		ddi_hardpps(tvp, 0);
1798 	}
1799 }
1800 
1801 /*
1802  * Modem status interrupt.
1803  *
1804  * (Note: It is assumed that the MSR hasn't been read by asyintr().)
1805  */
1806 
1807 static void
1808 async_msint(struct asycom *asy)
1809 {
1810 	struct asyncline *async = (struct asyncline *)asy->asy_priv;
1811 	int msr;
1812 
1813 	msr = INB(MSR);	/* this resets the interrupt */
1814 	asy->asy_cached_msr = msr;
1815 #ifdef DEBUG
1816 	if (asydebug & ASY_DEBUG_STATE) {
1817 		printf("   transition: %3s %3s %3s %3s\n"
1818 			"current state: %3s %3s %3s %3s\n",
1819 			(msr & DCTS) ? "CTS" : "   ",
1820 			(msr & DDSR) ? "DSR" : "   ",
1821 			(msr & DRI) ?  "RI " : "   ",
1822 			(msr & DDCD) ? "DCD" : "   ",
1823 			(msr & CTS) ?  "CTS" : "   ",
1824 			(msr & DSR) ?  "DSR" : "   ",
1825 			(msr & RI) ?   "RI " : "   ",
1826 			(msr & DCD) ?  "DCD" : "   ");
1827 	}
1828 #endif
1829 	if (async->async_ttycommon.t_cflag & CRTSCTS && !(msr & CTS)) {
1830 #ifdef DEBUG
1831 		if (asydebug & ASY_DEBUG_HFLOW)
1832 			printf("asy%d: hflow start\n",
1833 				UNIT(async->async_dev));
1834 #endif
1835 		async->async_flags |= ASYNC_HW_OUT_FLW;
1836 	}
1837 	if (asy->asy_hwtype == ASY82510)
1838 		OUTB(MSR, (msr & 0xF0));
1839 
1840 	/* Handle PPS event */
1841 	if (asy->asy_flags & ASY_PPS)
1842 		asy_ppsevent(asy, msr);
1843 
1844 	async->async_ext++;
1845 	ASYSETSOFT(asy);
1846 }
1847 
1848 /*
1849  * Handle a second-stage interrupt.
1850  */
1851 uint_t
1852 asysoftintr(caddr_t intarg)
1853 {
1854 	struct asycom *asy = (struct asycom *)intarg;
1855 	struct asyncline *async;
1856 	int rv;
1857 	int cc;
1858 	/*
1859 	 * Test and clear soft interrupt.
1860 	 */
1861 	mutex_enter(asy->asy_soft_lock);
1862 #ifdef DEBUG
1863 	if (asydebug & ASY_DEBUG_PROCS)
1864 		printf("softintr\n");
1865 #endif
1866 	rv = asy->asysoftpend;
1867 	if (rv != 0)
1868 		asy->asysoftpend = 0;
1869 	mutex_exit(asy->asy_soft_lock);
1870 
1871 	if (rv) {
1872 		if (asy->asy_priv == NULL)
1873 			return (rv);
1874 		async = (struct asyncline *)asy->asy_priv;
1875 		mutex_enter(asy->asy_excl_hi);
1876 		if (asy->asy_flags & ASY_NEEDSOFT) {
1877 			asy->asy_flags &= ~ASY_NEEDSOFT;
1878 			mutex_exit(asy->asy_excl_hi);
1879 			(void) async_softint(asy);
1880 			mutex_enter(asy->asy_excl_hi);
1881 		}
1882 		/*
1883 		 * There are some instances where the softintr is not
1884 		 * scheduled and hence not called. It so happened that makes
1885 		 * the last few characters to be stuck in ringbuffer.
1886 		 * Hence, call once again the  handler so that the last few
1887 		 * characters are cleared.
1888 		 */
1889 		cc = RING_CNT(async);
1890 		mutex_exit(asy->asy_excl_hi);
1891 		if (cc > 0) {
1892 			(void) async_softint(asy);
1893 		}
1894 	}
1895 	return (rv);
1896 }
1897 
1898 /*
1899  * Handle a software interrupt.
1900  */
1901 static int
1902 async_softint(struct asycom *asy)
1903 {
1904 	struct asyncline *async = (struct asyncline *)asy->asy_priv;
1905 	uint_t	cc;
1906 	mblk_t	*bp;
1907 	queue_t	*q;
1908 	uchar_t	val;
1909 	uchar_t	c;
1910 	tty_common_t	*tp;
1911 
1912 #ifdef DEBUG
1913 	if (asydebug & ASY_DEBUG_PROCS)
1914 		printf("process\n");
1915 #endif
1916 	mutex_enter(asy->asy_excl);
1917 	if (asy->asy_flags & ASY_DOINGSOFT) {
1918 		mutex_exit(asy->asy_excl);
1919 		return (0);
1920 	}
1921 	tp = &async->async_ttycommon;
1922 	q = tp->t_readq;
1923 	if (q != NULL) {
1924 		mutex_exit(asy->asy_excl);
1925 		enterq(q);
1926 		mutex_enter(asy->asy_excl);
1927 	}
1928 	mutex_enter(asy->asy_excl_hi);
1929 	asy->asy_flags |= ASY_DOINGSOFT;
1930 
1931 	if (INB(ICR) & MIEN)
1932 		val = asy->asy_cached_msr & 0xFF;
1933 	else
1934 		val = INB(MSR) & 0xFF;
1935 
1936 	if (async->async_ttycommon.t_cflag & CRTSCTS) {
1937 		if ((val & CTS) && (async->async_flags & ASYNC_HW_OUT_FLW)) {
1938 #ifdef DEBUG
1939 			if (asydebug & ASY_DEBUG_HFLOW)
1940 				printf("asy%d: hflow start\n",
1941 					UNIT(async->async_dev));
1942 #endif
1943 			async->async_flags &= ~ASYNC_HW_OUT_FLW;
1944 			mutex_exit(asy->asy_excl_hi);
1945 			if (async->async_ocnt > 0) {
1946 				mutex_enter(asy->asy_excl_hi);
1947 				async_resume(async);
1948 				mutex_exit(asy->asy_excl_hi);
1949 			} else {
1950 				async_start(async);
1951 			}
1952 			mutex_enter(asy->asy_excl_hi);
1953 		}
1954 	}
1955 	if (async->async_ext) {
1956 		async->async_ext = 0;
1957 		/* check for carrier up */
1958 		if ((val & DCD) || (tp->t_flags & TS_SOFTCAR)) {
1959 			/* carrier present */
1960 			if ((async->async_flags & ASYNC_CARR_ON) == 0) {
1961 				async->async_flags |= ASYNC_CARR_ON;
1962 				mutex_exit(asy->asy_excl_hi);
1963 				mutex_exit(asy->asy_excl);
1964 				if (async->async_flags & ASYNC_ISOPEN)
1965 					(void) putctl(q, M_UNHANGUP);
1966 				cv_broadcast(&async->async_flags_cv);
1967 				mutex_enter(asy->asy_excl);
1968 				mutex_enter(asy->asy_excl_hi);
1969 			}
1970 		} else {
1971 			if ((async->async_flags & ASYNC_CARR_ON) &&
1972 			    !(tp->t_cflag & CLOCAL)) {
1973 				int flushflag;
1974 
1975 				/*
1976 				 * Carrier went away.
1977 				 * Drop DTR, abort any output in
1978 				 * progress, indicate that output is
1979 				 * not stopped, and send a hangup
1980 				 * notification upstream.
1981 				 *
1982 				 * If we're in the midst of close, then flush
1983 				 * everything.  Don't leave stale ioctls lying
1984 				 * about.
1985 				 */
1986 				val = INB(MCR);
1987 				OUTB(MCR, (val & ~DTR));
1988 				flushflag = (async->async_flags &
1989 				    ASYNC_CLOSING) ? FLUSHALL : FLUSHDATA;
1990 				flushq(tp->t_writeq, flushflag);
1991 				if (async->async_xmitblk != NULL) {
1992 					freeb(async->async_xmitblk);
1993 					async->async_xmitblk = NULL;
1994 				}
1995 				if (async->async_flags & ASYNC_BUSY) {
1996 					async->async_ocnt = 0;
1997 					async->async_flags &= ~ASYNC_BUSY;
1998 				}
1999 				async->async_flags &= ~ASYNC_STOPPED;
2000 				if (async->async_flags & ASYNC_ISOPEN) {
2001 					mutex_exit(asy->asy_excl_hi);
2002 					mutex_exit(asy->asy_excl);
2003 					(void) putctl(q, M_HANGUP);
2004 					mutex_enter(asy->asy_excl);
2005 					mutex_enter(asy->asy_excl_hi);
2006 				}
2007 				async->async_flags &= ~ASYNC_CARR_ON;
2008 				mutex_exit(asy->asy_excl_hi);
2009 				cv_broadcast(&async->async_flags_cv);
2010 				mutex_enter(asy->asy_excl_hi);
2011 			}
2012 		}
2013 	}
2014 
2015 	/*
2016 	 * If data has been added to the circular buffer, remove
2017 	 * it from the buffer, and send it up the stream if there's
2018 	 * somebody listening. Try to do it 16 bytes at a time. If we
2019 	 * have more than 16 bytes to move, move 16 byte chunks and
2020 	 * leave the rest for next time around (maybe it will grow).
2021 	 */
2022 	if (!(async->async_flags & ASYNC_ISOPEN)) {
2023 		RING_INIT(async);
2024 		goto rv;
2025 	}
2026 	if ((cc = RING_CNT(async)) == 0) {
2027 		goto rv;
2028 	}
2029 	mutex_exit(asy->asy_excl_hi);
2030 
2031 	if (!canput(q)) {
2032 		if ((async->async_flags & ASYNC_HW_IN_FLOW) == 0) {
2033 #ifdef DEBUG
2034 			if (!(asydebug & ASY_DEBUG_HFLOW)) {
2035 				printf("asy%d: hflow stop input.\n",
2036 				UNIT(async->async_dev));
2037 				if (canputnext(q))
2038 					printf("asy%d: next queue is "
2039 						"ready\n",
2040 					UNIT(async->async_dev));
2041 			}
2042 #endif
2043 			mutex_enter(asy->asy_excl_hi);
2044 			async->async_flags |= ASYNC_HW_IN_FLOW;
2045 			async->async_flowc = async->async_stopc;
2046 		} else mutex_enter(asy->asy_excl_hi);
2047 		goto rv;
2048 	}
2049 
2050 	if (async->async_ringbuf_overflow) {
2051 		if ((async->async_flags & ASYNC_HW_IN_FLOW) &&
2052 				((int)(RING_CNT(async)) < (RINGSIZE/4))) {
2053 #ifdef DEBUG
2054 			if (asydebug & ASY_DEBUG_HFLOW)
2055 				printf("asy%d: hflow start input.\n",
2056 				UNIT(async->async_dev));
2057 #endif
2058 			mutex_enter(asy->asy_excl_hi);
2059 			async->async_flags &= ~ASYNC_HW_IN_FLOW;
2060 			async->async_flowc = async->async_startc;
2061 			async->async_ringbuf_overflow = 0;
2062 			goto rv;
2063 		}
2064 	}
2065 #ifdef DEBUG
2066 	if (asydebug & ASY_DEBUG_INPUT)
2067 		printf("asy%d: %d char(s) in queue.\n",
2068 			UNIT(async->async_dev), cc);
2069 #endif
2070 	/*
2071 	 * Before you pull the characters from the RING BUF
2072 	 * Check whether you can put into the queue again
2073 	 */
2074 	if ((!canputnext(q)) || (!canput(q))) {
2075 		mutex_enter(asy->asy_excl_hi);
2076 		if ((async->async_flags & ASYNC_HW_IN_FLOW) == 0) {
2077 			async->async_flags |= ASYNC_HW_IN_FLOW;
2078 			async->async_flowc = async->async_stopc;
2079 			async->async_queue_full = 1;
2080 		}
2081 		goto rv;
2082 	}
2083 	mutex_enter(asy->asy_excl_hi);
2084 	if (async->async_queue_full) {
2085 		/*
2086 		 * Last time the Stream queue didnot allow
2087 		 * now it allows so, relax, the flow control
2088 		 */
2089 		if (async->async_flags & ASYNC_HW_IN_FLOW) {
2090 			async->async_flags &= ~ASYNC_HW_IN_FLOW;
2091 			async->async_queue_full = 0;
2092 			async->async_flowc = async->async_startc;
2093 			goto rv;
2094 		} else
2095 			async->async_queue_full = 0;
2096 	}
2097 	mutex_exit(asy->asy_excl_hi);
2098 	if (!(bp = allocb(cc, BPRI_MED))) {
2099 		ttycommon_qfull(&async->async_ttycommon, q);
2100 		mutex_enter(asy->asy_excl_hi);
2101 		goto rv;
2102 	}
2103 	mutex_enter(asy->asy_excl_hi);
2104 	do {
2105 		if (RING_ERR(async, S_ERRORS)) {
2106 			RING_UNMARK(async);
2107 			c = RING_GET(async);
2108 			break;
2109 		} else {
2110 			*bp->b_wptr++ = RING_GET(async);
2111 		}
2112 	} while (--cc);
2113 
2114 	mutex_exit(asy->asy_excl_hi);
2115 	mutex_exit(asy->asy_excl);
2116 	if (bp->b_wptr > bp->b_rptr) {
2117 		if (!canputnext(q)) {
2118 			if (!canput(q)) {
2119 				/*
2120 				 * Even after taking all precautions that
2121 				 * Still we are unable to queue, then we
2122 				 * cannot do anything, just drop the block
2123 				 */
2124 				cmn_err(CE_NOTE,
2125 					"su%d: local queue full\n",
2126 					UNIT(async->async_dev));
2127 				freemsg(bp);
2128 				mutex_enter(asy->asy_excl_hi);
2129 				if ((async->async_flags &
2130 					ASYNC_HW_IN_FLOW) == 0) {
2131 					async->async_flags |=
2132 						ASYNC_HW_IN_FLOW;
2133 					async->async_flowc =
2134 						async->async_stopc;
2135 					async->async_queue_full = 1;
2136 				}
2137 				mutex_exit(asy->asy_excl_hi);
2138 			} else {
2139 				(void) putq(q, bp);
2140 			}
2141 		} else {
2142 			putnext(q, bp);
2143 		}
2144 	} else {
2145 		freemsg(bp);
2146 	}
2147 	/*
2148 	 * If we have a parity error, then send
2149 	 * up an M_BREAK with the "bad"
2150 	 * character as an argument. Let ldterm
2151 	 * figure out what to do with the error.
2152 	 */
2153 	if (cc)
2154 		(void) putctl1(q, M_BREAK, c);
2155 	mutex_enter(asy->asy_excl);
2156 	mutex_enter(asy->asy_excl_hi);
2157 rv:
2158 	/*
2159 	 * If a transmission has finished, indicate that it's finished,
2160 	 * and start that line up again.
2161 	 */
2162 	if (async->async_break) {
2163 		async->async_break = 0;
2164 		if (async->async_flags & ASYNC_ISOPEN) {
2165 			mutex_exit(asy->asy_excl_hi);
2166 			mutex_exit(asy->asy_excl);
2167 			(void) putctl(q, M_BREAK);
2168 			mutex_enter(asy->asy_excl);
2169 			mutex_enter(asy->asy_excl_hi);
2170 		}
2171 	}
2172 	if ((async->async_ocnt <= 0 && (async->async_flags & ASYNC_BUSY)) ||
2173 	    (async->async_flowc != '\0')) {
2174 		async->async_flags &= ~ASYNC_BUSY;
2175 		mutex_exit(asy->asy_excl_hi);
2176 		if (async->async_xmitblk)
2177 			freeb(async->async_xmitblk);
2178 		async->async_xmitblk = NULL;
2179 		if (async->async_flags & ASYNC_ISOPEN) {
2180 			asy->inperim = B_TRUE;
2181 			mutex_exit(asy->asy_excl);
2182 			enterq(async->async_ttycommon.t_writeq);
2183 			mutex_enter(asy->asy_excl);
2184 		}
2185 		async_start(async);
2186 		/*
2187 		 * We need to check for inperim and ISOPEN due to
2188 		 * multi-threading implications; it's possible to close the
2189 		 * port and nullify async_flags while completing the software
2190 		 * interrupt.  If the port is closed, leaveq() will have already
2191 		 * been called.  We don't want to call it twice.
2192 		 */
2193 		if ((asy->inperim) && (async->async_flags & ASYNC_ISOPEN)) {
2194 			mutex_exit(asy->asy_excl);
2195 			leaveq(async->async_ttycommon.t_writeq);
2196 			mutex_enter(asy->asy_excl);
2197 			asy->inperim = B_FALSE;
2198 		}
2199 		if (!(async->async_flags & ASYNC_BUSY))
2200 			cv_broadcast(&async->async_flags_cv);
2201 		mutex_enter(asy->asy_excl_hi);
2202 	}
2203 	/*
2204 	 * A note about these overrun bits: all they do is *tell* someone
2205 	 * about an error- They do not track multiple errors. In fact,
2206 	 * you could consider them latched register bits if you like.
2207 	 * We are only interested in printing the error message once for
2208 	 * any cluster of overrun errrors.
2209 	 */
2210 	if (async->async_hw_overrun) {
2211 		if (async->async_flags & ASYNC_ISOPEN) {
2212 			if (su_log > 0) {
2213 				mutex_exit(asy->asy_excl_hi);
2214 				mutex_exit(asy->asy_excl);
2215 				cmn_err(CE_NOTE, "su%d: silo overflow\n",
2216 				    UNIT(async->async_dev));
2217 				mutex_enter(asy->asy_excl);
2218 				mutex_enter(asy->asy_excl_hi);
2219 			}
2220 			INC64_KSTAT(asy, siloover);
2221 		}
2222 		async->async_hw_overrun = 0;
2223 	}
2224 	if (async->async_sw_overrun) {
2225 		if (async->async_flags & ASYNC_ISOPEN) {
2226 			if (su_log > 0) {
2227 				mutex_exit(asy->asy_excl_hi);
2228 				mutex_exit(asy->asy_excl);
2229 				cmn_err(CE_NOTE, "su%d: ring buffer overflow\n",
2230 				    UNIT(async->async_dev));
2231 				mutex_enter(asy->asy_excl);
2232 				mutex_enter(asy->asy_excl_hi);
2233 			}
2234 			INC64_KSTAT(asy, ringover);
2235 		}
2236 		async->async_sw_overrun = 0;
2237 	}
2238 	asy->asy_flags &= ~ASY_DOINGSOFT;
2239 	mutex_exit(asy->asy_excl_hi);
2240 	mutex_exit(asy->asy_excl);
2241 	if (q != NULL)
2242 		leaveq(q);
2243 	return (0);
2244 }
2245 
2246 /*
2247  * Restart output on a line after a delay or break timer expired.
2248  */
2249 static void
2250 async_restart(void *arg)
2251 {
2252 	struct asyncline *async = arg;
2253 	struct asycom *asy = async->async_common;
2254 	queue_t *q;
2255 	uchar_t lcr;
2256 
2257 	/*
2258 	 * If break timer expired, turn off the break bit.
2259 	 */
2260 #ifdef DEBUG
2261 	if (asydebug & ASY_DEBUG_PROCS)
2262 		printf("restart\n");
2263 #endif
2264 	mutex_enter(asy->asy_excl);
2265 	if (async->async_flags & ASYNC_BREAK) {
2266 		unsigned int rate;
2267 
2268 		mutex_enter(asy->asy_excl_hi);
2269 		lcr = INB(LCR);
2270 		OUTB(LCR, (lcr & ~SETBREAK));
2271 
2272 		/*
2273 		 * Go to sleep for the time it takes for at least one
2274 		 * stop bit to be received by the device at the other
2275 		 * end of the line as stated in the RS-232 specification.
2276 		 * The wait period is equal to:
2277 		 * 2 clock cycles * (1 MICROSEC / baud rate)
2278 		 */
2279 		rate = async->async_ttycommon.t_cflag & CBAUD;
2280 		if (async->async_ttycommon.t_cflag & CBAUDEXT)
2281 			rate += 16;
2282 		if (rate >= N_SU_SPEEDS || rate == B0) {
2283 			rate = B9600;
2284 		}
2285 
2286 		mutex_exit(asy->asy_excl_hi);
2287 		mutex_exit(asy->asy_excl);
2288 		drv_usecwait(2 * MICROSEC / baudtable[rate]);
2289 		mutex_enter(asy->asy_excl);
2290 	}
2291 	async->async_flags &= ~(ASYNC_DELAY|ASYNC_BREAK|ASYNC_DRAINING);
2292 	if ((q = async->async_ttycommon.t_writeq) != NULL) {
2293 		mutex_exit(asy->asy_excl);
2294 		enterq(q);
2295 		mutex_enter(asy->asy_excl);
2296 	}
2297 	async_start(async);
2298 	mutex_exit(asy->asy_excl);
2299 	if (q != NULL)
2300 		leaveq(q);
2301 
2302 	/* cleared break or delay flag; may have made some output progress */
2303 	cv_broadcast(&async->async_flags_cv);
2304 }
2305 
2306 static void
2307 async_start(struct asyncline *async)
2308 {
2309 	async_nstart(async, 0);
2310 }
2311 
2312 /*
2313  * Start output on a line, unless it's busy, frozen, or otherwise.
2314  */
2315 static void
2316 async_nstart(struct asyncline *async, int mode)
2317 {
2318 	register struct asycom *asy = async->async_common;
2319 	register int cc;
2320 	register queue_t *q;
2321 	mblk_t *bp, *nbp;
2322 	uchar_t *xmit_addr;
2323 	uchar_t	val;
2324 	int	fifo_len = 1;
2325 	int	xmit_progress;
2326 
2327 #ifdef DEBUG
2328 	if (asydebug & ASY_DEBUG_PROCS)
2329 		printf("start\n");
2330 #endif
2331 	if (asy->asy_use_fifo == FIFO_ON)
2332 		fifo_len = asy->asy_fifo_buf; /* with FIFO buffers */
2333 
2334 	ASSERT(mutex_owned(asy->asy_excl));
2335 	mutex_enter(asy->asy_excl_hi);
2336 	asycheckflowcontrol_hw(asy);
2337 
2338 	/*
2339 	 * If the chip is busy (i.e., we're waiting for a break timeout
2340 	 * to expire, or for the current transmission to finish, or for
2341 	 * output to finish draining from chip), don't grab anything new.
2342 	 */
2343 	if (async->async_flags & (ASYNC_BREAK|ASYNC_BUSY|ASYNC_DRAINING)) {
2344 		mutex_exit(asy->asy_excl_hi);
2345 #ifdef DEBUG
2346 		if (mode && asydebug & ASY_DEBUG_CLOSE)
2347 			printf("asy%d: start %s.\n",
2348 				UNIT(async->async_dev),
2349 				async->async_flags & ASYNC_BREAK
2350 				? "break" : "busy");
2351 #endif
2352 		return;
2353 	}
2354 
2355 	/*
2356 	 * If we have a flow-control character to transmit, do it now.
2357 	 */
2358 	if (asycheckflowcontrol_sw(asy)) {
2359 		mutex_exit(asy->asy_excl_hi);
2360 		return;
2361 	}
2362 	mutex_exit(asy->asy_excl_hi);
2363 	/*
2364 	 * If we're waiting for a delay timeout to expire, don't grab
2365 	 * anything new.
2366 	 */
2367 	if (async->async_flags & ASYNC_DELAY) {
2368 #ifdef DEBUG
2369 		if (mode && asydebug & ASY_DEBUG_CLOSE)
2370 			printf("asy%d: start ASYNC_DELAY.\n",
2371 				UNIT(async->async_dev));
2372 #endif
2373 		return;
2374 	}
2375 
2376 	if ((q = async->async_ttycommon.t_writeq) == NULL) {
2377 #ifdef DEBUG
2378 		if (mode && asydebug & ASY_DEBUG_CLOSE)
2379 			printf("asy%d: start writeq is null.\n",
2380 				UNIT(async->async_dev));
2381 #endif
2382 		return;	/* not attached to a stream */
2383 	}
2384 
2385 	for (;;) {
2386 		if ((bp = getq(q)) == NULL)
2387 			return;	/* no data to transmit */
2388 
2389 		/*
2390 		 * We have a message block to work on.
2391 		 * Check whether it's a break, a delay, or an ioctl (the latter
2392 		 * occurs if the ioctl in question was waiting for the output
2393 		 * to drain).  If it's one of those, process it immediately.
2394 		 */
2395 		switch (bp->b_datap->db_type) {
2396 
2397 		case M_BREAK:
2398 			/*
2399 			 * Set the break bit, and arrange for "async_restart"
2400 			 * to be called in 1/4 second; it will turn the
2401 			 * break bit off, and call "async_start" to grab
2402 			 * the next message.
2403 			 */
2404 			mutex_enter(asy->asy_excl_hi);
2405 			val = INB(LCR);
2406 			OUTB(LCR, (val | SETBREAK));
2407 			mutex_exit(asy->asy_excl_hi);
2408 			async->async_flags |= ASYNC_BREAK;
2409 			(void) timeout(async_restart, async, hz / 4);
2410 			freemsg(bp);
2411 			return;	/* wait for this to finish */
2412 
2413 		case M_DELAY:
2414 			/*
2415 			 * Arrange for "async_restart" to be called when the
2416 			 * delay expires; it will turn ASYNC_DELAY off,
2417 			 * and call "async_start" to grab the next message.
2418 			 */
2419 			(void) timeout(async_restart, async,
2420 				(clock_t)(*(unsigned char *)bp->b_rptr + 6));
2421 			async->async_flags |= ASYNC_DELAY;
2422 			freemsg(bp);
2423 			return;	/* wait for this to finish */
2424 
2425 		case M_IOCTL:
2426 			/*
2427 			 * This ioctl needs to wait for the output ahead of
2428 			 * it to drain.  Try to do it, and then either
2429 			 * redo the ioctl at a later time or grab the next
2430 			 * message after it.
2431 			 */
2432 
2433 			mutex_enter(asy->asy_excl_hi);
2434 			if (asy_isbusy(asy)) {
2435 				/*
2436 				 * Get the divisor by calculating the rate
2437 				 */
2438 				unsigned int rate;
2439 
2440 				mutex_exit(asy->asy_excl_hi);
2441 				rate = async->async_ttycommon.t_cflag & CBAUD;
2442 				if (async->async_ttycommon.t_cflag & CBAUDEXT)
2443 					rate += 16;
2444 				if (rate >= N_SU_SPEEDS || rate == B0) {
2445 					rate = B9600;
2446 				}
2447 
2448 				/*
2449 				 * We need to do a callback as the port will
2450 				 * be set to drain
2451 				 */
2452 				async->async_flags |= ASYNC_DRAINING;
2453 
2454 				/*
2455 				 * Put the message we just processed back onto
2456 				 * the end of the queue
2457 				 */
2458 				if (putq(q, bp) == 0)
2459 					freemsg(bp);
2460 
2461 				/*
2462 				 * We need to delay until the TSR and THR
2463 				 * have been exhausted.  We base the delay on
2464 				 * the amount of time it takes to transmit
2465 				 * 2 chars at the current baud rate in
2466 				 * microseconds.
2467 				 *
2468 				 * Therefore, the wait period is:
2469 				 *
2470 				 * (#TSR bits + #THR bits) *
2471 				 * 	1 MICROSEC / baud rate
2472 				 */
2473 				(void) timeout(async_restart, async,
2474 					drv_usectohz(16 * MICROSEC /
2475 						baudtable[rate]));
2476 				return;
2477 			}
2478 			mutex_exit(asy->asy_excl_hi);
2479 			mutex_exit(asy->asy_excl);
2480 			async_ioctl(async, q, bp, B_FALSE);
2481 			mutex_enter(asy->asy_excl);
2482 			continue;
2483 		}
2484 
2485 		while (bp != NULL && (cc = bp->b_wptr - bp->b_rptr) == 0) {
2486 			nbp = bp->b_cont;
2487 			freeb(bp);
2488 			bp = nbp;
2489 		}
2490 		if (bp != NULL)
2491 			break;
2492 	}
2493 
2494 	/*
2495 	 * We have data to transmit.  If output is stopped, put
2496 	 * it back and try again later.
2497 	 */
2498 	if (async->async_flags & (ASYNC_HW_OUT_FLW|ASYNC_STOPPED)) {
2499 #ifdef DEBUG
2500 		if (asydebug & ASY_DEBUG_HFLOW &&
2501 					async->async_flags & ASYNC_HW_OUT_FLW)
2502 			printf("asy%d: output hflow in effect.\n",
2503 				UNIT(async->async_dev));
2504 #endif
2505 		mutex_exit(asy->asy_excl);
2506 		(void) putbq(q, bp);
2507 		/*
2508 		 * We entered the routine owning the lock, we need to
2509 		 * exit the routine owning the lock.
2510 		 */
2511 		mutex_enter(asy->asy_excl);
2512 		return;
2513 	}
2514 
2515 	async->async_xmitblk = bp;
2516 	xmit_addr = bp->b_rptr;
2517 	bp = bp->b_cont;
2518 	if (bp != NULL) {
2519 		mutex_exit(asy->asy_excl);
2520 		(void) putbq(q, bp);	/* not done with this message yet */
2521 		mutex_enter(asy->asy_excl);
2522 	}
2523 
2524 	/*
2525 	 * In 5-bit mode, the high order bits are used
2526 	 * to indicate character sizes less than five,
2527 	 * so we need to explicitly mask before transmitting
2528 	 */
2529 	if ((async->async_ttycommon.t_cflag & CSIZE) == CS5) {
2530 		register unsigned char *p = xmit_addr;
2531 		register int cnt = cc;
2532 
2533 		while (cnt--)
2534 			*p++ &= (unsigned char) 0x1f;
2535 	}
2536 
2537 	/*
2538 	 * Set up this block for pseudo-DMA.
2539 	 */
2540 	mutex_enter(asy->asy_excl_hi);
2541 	async->async_optr = xmit_addr;
2542 	async->async_ocnt = cc;
2543 	/*
2544 	 * If the transmitter is ready, shove some
2545 	 * characters out.
2546 	 */
2547 	xmit_progress = 0;
2548 	while (fifo_len-- && async->async_ocnt) {
2549 		if (INB(LSR) & XHRE) {
2550 			OUTB(DAT, *async->async_optr++);
2551 			async->async_ocnt--;
2552 			xmit_progress++;
2553 		}
2554 	}
2555 	asy->asy_out_of_band_xmit = xmit_progress;
2556 	if (xmit_progress > 0)
2557 		async->async_flags |= ASYNC_PROGRESS;
2558 	async->async_flags |= ASYNC_BUSY;
2559 	mutex_exit(asy->asy_excl_hi);
2560 }
2561 
2562 /*
2563  * Resume output by poking the transmitter.
2564  */
2565 static void
2566 async_resume(struct asyncline *async)
2567 {
2568 	register struct asycom *asy = async->async_common;
2569 
2570 	ASSERT(mutex_owned(asy->asy_excl_hi));
2571 #ifdef DEBUG
2572 	if (asydebug & ASY_DEBUG_PROCS)
2573 		printf("resume\n");
2574 #endif
2575 
2576 	asycheckflowcontrol_hw(asy);
2577 
2578 	if (INB(LSR) & XHRE) {
2579 		if (asycheckflowcontrol_sw(asy)) {
2580 			return;
2581 		} else if (async->async_ocnt > 0) {
2582 			OUTB(DAT, *async->async_optr++);
2583 			async->async_ocnt--;
2584 			async->async_flags |= ASYNC_PROGRESS;
2585 		}
2586 	}
2587 }
2588 
2589 /*
2590  * Process an "ioctl" message sent down to us.
2591  * Note that we don't need to get any locks until we are ready to access
2592  * the hardware.  Nothing we access until then is going to be altered
2593  * outside of the STREAMS framework, so we should be safe.
2594  */
2595 static void
2596 async_ioctl(struct asyncline *async, queue_t *wq, mblk_t *mp, boolean_t iswput)
2597 {
2598 	register struct asycom *asy = async->async_common;
2599 	register tty_common_t  *tp = &async->async_ttycommon;
2600 	register struct iocblk *iocp;
2601 	register unsigned datasize;
2602 	size_t ioc_count;
2603 	mblk_t *datamp;
2604 	int error = 0;
2605 	uchar_t val, icr;
2606 #ifdef DEBUG
2607 	if (asydebug & ASY_DEBUG_PROCS)
2608 		printf("ioctl\n");
2609 #endif
2610 
2611 	if (tp->t_iocpending != NULL) {
2612 		/*
2613 		 * We were holding an "ioctl" response pending the
2614 		 * availability of an "mblk" to hold data to be passed up;
2615 		 * another "ioctl" came through, which means that "ioctl"
2616 		 * must have timed out or been aborted.
2617 		 */
2618 		freemsg(async->async_ttycommon.t_iocpending);
2619 		async->async_ttycommon.t_iocpending = NULL;
2620 	}
2621 
2622 	iocp = (struct iocblk *)mp->b_rptr;
2623 
2624 	/*
2625 	 * Save off the ioc count in case we need to restore it
2626 	 * because we are queuing a message block.
2627 	 */
2628 	ioc_count = iocp->ioc_count;
2629 
2630 	/*
2631 	 * For TIOCMGET, TIOCMBIC, TIOCMBIS, TIOCMSET, and PPS, do NOT call
2632 	 * ttycommon_ioctl() because this function frees up the message block
2633 	 * (mp->b_cont) that contains the address of the user variable where
2634 	 * we need to pass back the bit array.
2635 	 */
2636 	if (iocp->ioc_cmd == TIOCMGET ||
2637 		iocp->ioc_cmd == TIOCMBIC ||
2638 		iocp->ioc_cmd == TIOCMBIS ||
2639 		iocp->ioc_cmd == TIOCMSET ||
2640 		iocp->ioc_cmd == TIOCGPPS ||
2641 		iocp->ioc_cmd == TIOCSPPS ||
2642 		iocp->ioc_cmd == TIOCGPPSEV)
2643 		error = -1; /* Do Nothing */
2644 	else
2645 
2646 	/*
2647 	 * The only way in which "ttycommon_ioctl" can fail is if the "ioctl"
2648 	 * requires a response containing data to be returned to the user,
2649 	 * and no mblk could be allocated for the data.
2650 	 * No such "ioctl" alters our state.  Thus, we always go ahead and
2651 	 * do any state-changes the "ioctl" calls for.  If we couldn't allocate
2652 	 * the data, "ttycommon_ioctl" has stashed the "ioctl" away safely, so
2653 	 * we just call "bufcall" to request that we be called back when we
2654 	 * stand a better chance of allocating the data.
2655 	 */
2656 	if ((datasize = ttycommon_ioctl(tp, wq, mp, &error)) != 0) {
2657 		if (async->async_wbufcid)
2658 			unbufcall(async->async_wbufcid);
2659 		async->async_wbufcid = bufcall(datasize, BPRI_HI, async_reioctl,
2660 		    async);
2661 		return;
2662 	}
2663 
2664 	mutex_enter(asy->asy_excl);
2665 
2666 	if (error == 0) {
2667 		/*
2668 		 * "ttycommon_ioctl" did most of the work; we just use the
2669 		 * data it set up.
2670 		 */
2671 		switch (iocp->ioc_cmd) {
2672 
2673 		case TCSETS:
2674 			if (!(asy->asy_rsc_console || asy->asy_rsc_control ||
2675 			    asy->asy_lom_console)) {
2676 				mutex_enter(asy->asy_excl_hi);
2677 				error = asy_program(asy, ASY_NOINIT);
2678 				mutex_exit(asy->asy_excl_hi);
2679 			}
2680 			break;
2681 		case TCSETSF:
2682 		case TCSETSW:
2683 		case TCSETA:
2684 		case TCSETAW:
2685 		case TCSETAF:
2686 			if (!(asy->asy_rsc_console || asy->asy_rsc_control ||
2687 			    asy->asy_lom_console)) {
2688 				mutex_enter(asy->asy_excl_hi);
2689 				if (iswput && asy_isbusy(asy)) {
2690 					/*
2691 					 * ttycommon_ioctl sets the db_type to
2692 					 * M_IOCACK and ioc_count to zero
2693 					 * we need to undo this when we
2694 					 * queue a control message. This will
2695 					 * allow the control messages to be
2696 					 * processed again when the chip
2697 					 * becomes available.
2698 					 */
2699 					mp->b_datap->db_type = M_IOCTL;
2700 					iocp->ioc_count = ioc_count;
2701 
2702 					if (putq(wq, mp) == 0)
2703 						freemsg(mp);
2704 					mutex_exit(asy->asy_excl_hi);
2705 					mutex_exit(asy->asy_excl);
2706 					return;
2707 				}
2708 				error = asy_program(asy, ASY_NOINIT);
2709 				mutex_exit(asy->asy_excl_hi);
2710 			}
2711 			break;
2712 		case TIOCSSOFTCAR:
2713 			/* Set the driver state appropriately */
2714 			mutex_enter(asy->asy_excl_hi);
2715 			if (tp->t_flags & TS_SOFTCAR)
2716 				asy->asy_flags |= ASY_IGNORE_CD;
2717 			else
2718 				asy->asy_flags &= ~ASY_IGNORE_CD;
2719 			mutex_exit(asy->asy_excl_hi);
2720 			break;
2721 		}
2722 	} else if (error < 0) {
2723 		/*
2724 		 * "ttycommon_ioctl" didn't do anything; we process it here.
2725 		 */
2726 		error = 0;
2727 		switch (iocp->ioc_cmd) {
2728 
2729 		case TIOCGPPS:
2730 			/*
2731 			 * Get PPS on/off.
2732 			 */
2733 			if (mp->b_cont != NULL)
2734 				freemsg(mp->b_cont);
2735 
2736 			mp->b_cont = allocb(sizeof (int), BPRI_HI);
2737 			if (mp->b_cont == NULL) {
2738 				error = ENOMEM;
2739 				break;
2740 			}
2741 			if (asy->asy_flags & ASY_PPS)
2742 				*(int *)mp->b_cont->b_wptr = 1;
2743 			else
2744 				*(int *)mp->b_cont->b_wptr = 0;
2745 			mp->b_cont->b_wptr += sizeof (int);
2746 			mp->b_datap->db_type = M_IOCACK;
2747 			iocp->ioc_count = sizeof (int);
2748 			break;
2749 
2750 		case TIOCSPPS:
2751 			/*
2752 			 * Set PPS on/off.
2753 			 */
2754 			error = miocpullup(mp, sizeof (int));
2755 			if (error != 0)
2756 				break;
2757 
2758 			mutex_enter(asy->asy_excl_hi);
2759 			if (*(int *)mp->b_cont->b_rptr)
2760 				asy->asy_flags |= ASY_PPS;
2761 			else
2762 				asy->asy_flags &= ~ASY_PPS;
2763 			/* Reset edge sense */
2764 			asy->asy_flags &= ~ASY_PPS_EDGE;
2765 			mutex_exit(asy->asy_excl_hi);
2766 			mp->b_datap->db_type = M_IOCACK;
2767 			break;
2768 
2769 		case TIOCGPPSEV: {
2770 			/*
2771 			 * Get PPS event data.
2772 			 */
2773 			mblk_t *bp;
2774 			void *buf;
2775 #ifdef _SYSCALL32_IMPL
2776 			struct ppsclockev32 p32;
2777 #endif
2778 			struct ppsclockev ppsclockev;
2779 
2780 			if (mp->b_cont != NULL) {
2781 				freemsg(mp->b_cont);
2782 				mp->b_cont = NULL;
2783 			}
2784 
2785 			if ((asy->asy_flags & ASY_PPS) == 0) {
2786 				error = ENXIO;
2787 				break;
2788 			}
2789 
2790 			/* Protect from incomplete asy_ppsev */
2791 			mutex_enter(asy->asy_excl_hi);
2792 			ppsclockev = asy_ppsev;
2793 			mutex_exit(asy->asy_excl_hi);
2794 
2795 #ifdef _SYSCALL32_IMPL
2796 			if ((iocp->ioc_flag & IOC_MODELS) != IOC_NATIVE) {
2797 				TIMEVAL_TO_TIMEVAL32(&p32.tv, &ppsclockev.tv);
2798 				p32.serial = ppsclockev.serial;
2799 				buf = &p32;
2800 				iocp->ioc_count = sizeof (struct ppsclockev32);
2801 			} else
2802 #endif
2803 			{
2804 				buf = &ppsclockev;
2805 				iocp->ioc_count = sizeof (struct ppsclockev);
2806 			}
2807 
2808 			if ((bp = allocb(iocp->ioc_count, BPRI_HI)) == NULL) {
2809 				error = ENOMEM;
2810 				break;
2811 			}
2812 			mp->b_cont = bp;
2813 
2814 			bcopy(buf, bp->b_wptr, iocp->ioc_count);
2815 			bp->b_wptr += iocp->ioc_count;
2816 			mp->b_datap->db_type = M_IOCACK;
2817 			break;
2818 		}
2819 
2820 		case TCSBRK:
2821 			error = miocpullup(mp, sizeof (int));
2822 			if (error != 0)
2823 				break;
2824 
2825 			mutex_enter(asy->asy_excl_hi);
2826 			if (*(int *)mp->b_cont->b_rptr == 0) {
2827 				/*
2828 				 * Get the divisor by calculating the rate
2829 				 */
2830 				unsigned int rate, divisor;
2831 				rate = async->async_ttycommon.t_cflag & CBAUD;
2832 				if (async->async_ttycommon.t_cflag & CBAUDEXT)
2833 					rate += 16;
2834 				if (rate >= N_SU_SPEEDS) rate = B9600;
2835 				divisor = asyspdtab[rate] & 0xfff;
2836 
2837 				/*
2838 				 * To ensure that erroneous characters are
2839 				 * not sent out when the break is set, SB
2840 				 * recommends three steps:
2841 				 *
2842 				 * 1) pad the TSR with 0 bits
2843 				 * 2) When the TSR is full, set break
2844 				 * 3) When the TSR has been flushed, unset
2845 				 *    the break when transmission must be
2846 				 *    restored.
2847 				 *
2848 				 * We loop until the TSR is empty and then
2849 				 * set the break.  ASYNC_BREAK has been set
2850 				 * to ensure that no characters are
2851 				 * transmitted while the TSR is being
2852 				 * flushed and SOUT is being used for the
2853 				 * break signal.
2854 				 *
2855 				 * The wait period is equal to
2856 				 * clock / (baud * 16) * 16 * 2.
2857 				 */
2858 				async->async_flags |= ASYNC_BREAK;
2859 				while ((INB(LSR) & XSRE) == 0) {
2860 					mutex_exit(asy->asy_excl_hi);
2861 					mutex_exit(asy->asy_excl);
2862 					drv_usecwait(32*divisor);
2863 					mutex_enter(asy->asy_excl);
2864 					mutex_enter(asy->asy_excl_hi);
2865 				}
2866 
2867 				/*
2868 				 * Set the break bit, and arrange for
2869 				 * "async_restart" to be called in 1/4 second;
2870 				 * it will turn the break bit off, and call
2871 				 * "async_start" to grab the next message.
2872 				 */
2873 				val = INB(LCR);
2874 				OUTB(LCR, (val | SETBREAK));
2875 				mutex_exit(asy->asy_excl_hi);
2876 				(void) timeout(async_restart, async, hz / 4);
2877 			} else {
2878 #ifdef DEBUG
2879 				if (asydebug & ASY_DEBUG_CLOSE)
2880 					printf("asy%d: wait for flush.\n",
2881 					UNIT(async->async_dev));
2882 #endif
2883 				if (iswput && asy_isbusy(asy)) {
2884 					if (putq(wq, mp) == 0)
2885 						freemsg(mp);
2886 					mutex_exit(asy->asy_excl_hi);
2887 					mutex_exit(asy->asy_excl);
2888 					return;
2889 				}
2890 				mutex_exit(asy->asy_excl_hi);
2891 #ifdef DEBUG
2892 				if (asydebug & ASY_DEBUG_CLOSE)
2893 					printf("asy%d: ldterm satisfied.\n",
2894 					UNIT(async->async_dev));
2895 #endif
2896 			}
2897 			break;
2898 
2899 		case TIOCSBRK:
2900 			mutex_enter(asy->asy_excl_hi);
2901 			val = INB(LCR);
2902 			OUTB(LCR, (val | SETBREAK));
2903 			mutex_exit(asy->asy_excl_hi);
2904 			mutex_exit(asy->asy_excl);
2905 			miocack(wq, mp, 0, 0);
2906 			return;
2907 
2908 		case TIOCCBRK:
2909 			mutex_enter(asy->asy_excl_hi);
2910 			val = INB(LCR);
2911 			OUTB(LCR, (val & ~SETBREAK));
2912 			mutex_exit(asy->asy_excl_hi);
2913 			mutex_exit(asy->asy_excl);
2914 			miocack(wq, mp, 0, 0);
2915 			return;
2916 
2917 		case TIOCMSET:
2918 		case TIOCMBIS:
2919 		case TIOCMBIC:
2920 			if (iocp->ioc_count == TRANSPARENT)
2921 				mcopyin(mp, NULL, sizeof (int), NULL);
2922 			else {
2923 				error = miocpullup(mp, sizeof (int));
2924 				if (error != 0)
2925 					break;
2926 
2927 				mutex_enter(asy->asy_excl_hi);
2928 
2929 				(void) asymctl(asy,
2930 					dmtoasy(*(int *)mp->b_cont->b_rptr),
2931 					iocp->ioc_cmd);
2932 
2933 				mutex_exit(asy->asy_excl_hi);
2934 				iocp->ioc_error = 0;
2935 				mp->b_datap->db_type = M_IOCACK;
2936 			}
2937 			break;
2938 
2939 		case TIOCSILOOP:
2940 			mutex_enter(asy->asy_excl_hi);
2941 			/*
2942 			 * If somebody misues this Ioctl when used for
2943 			 * driving keyboard and mouse indicate not supported
2944 			 */
2945 			if ((asy->asy_device_type == ASY_KEYBOARD) ||
2946 				(asy->asy_device_type == ASY_MOUSE)) {
2947 				mutex_exit(asy->asy_excl_hi);
2948 				error = ENOTTY;
2949 				break;
2950 			}
2951 
2952 			/* should not use when we're the console */
2953 			if ((async->async_dev == kbddev) ||
2954 			    (async->async_dev == rconsdev) ||
2955 			    (async->async_dev == stdindev)) {
2956 				mutex_exit(asy->asy_excl_hi);
2957 				error = EINVAL;
2958 				break;
2959 			}
2960 
2961 			val = INB(MCR);
2962 			icr = INB(ICR);
2963 			/*
2964 			 * Disable the Modem Status Interrupt
2965 			 * The reason for disabling is  the status of
2966 			 * modem signal are in the higher 4 bits instead of
2967 			 * lower four bits when in loopback mode,
2968 			 * so, donot worry about Modem interrupt when
2969 			 * you are planning to set
2970 			 * this in loopback mode until it is cleared by
2971 			 * another ioctl to get out of the loopback mode
2972 			 */
2973 			OUTB(ICR, icr & ~ MIEN);
2974 			OUTB(MCR, val | ASY_LOOP);
2975 			mutex_exit(asy->asy_excl_hi);
2976 			iocp->ioc_error = 0;
2977 			mp->b_datap->db_type = M_IOCACK;
2978 			break;
2979 
2980 		case TIOCMGET:
2981 			datamp = allocb(sizeof (int), BPRI_MED);
2982 			if (datamp == NULL) {
2983 				error = EAGAIN;
2984 				break;
2985 			}
2986 
2987 			mutex_enter(asy->asy_excl_hi);
2988 			*(int *)datamp->b_rptr = asymctl(asy, 0, TIOCMGET);
2989 			mutex_exit(asy->asy_excl_hi);
2990 
2991 			if (iocp->ioc_count == TRANSPARENT) {
2992 				mcopyout(mp, NULL, sizeof (int), NULL, datamp);
2993 			} else {
2994 				if (mp->b_cont != NULL)
2995 					freemsg(mp->b_cont);
2996 				mp->b_cont = datamp;
2997 				mp->b_cont->b_wptr += sizeof (int);
2998 				mp->b_datap->db_type = M_IOCACK;
2999 				iocp->ioc_count = sizeof (int);
3000 			}
3001 			break;
3002 
3003 		default: /* unexpected ioctl type */
3004 			/*
3005 			 * If we don't understand it, it's an error.  NAK it.
3006 			 */
3007 			error = EINVAL;
3008 			break;
3009 		}
3010 	}
3011 	if (error != 0) {
3012 		iocp->ioc_error = error;
3013 		mp->b_datap->db_type = M_IOCNAK;
3014 	}
3015 	mutex_exit(asy->asy_excl);
3016 	qreply(wq, mp);
3017 }
3018 
3019 static void
3020 asyrsrv(queue_t *q)
3021 {
3022 	mblk_t *bp;
3023 	struct asyncline *async;
3024 
3025 	async = (struct asyncline *)q->q_ptr;
3026 
3027 	while (canputnext(q) && (bp = getq(q)))
3028 		putnext(q, bp);
3029 	ASYSETSOFT(async->async_common);
3030 	async->async_polltid = 0;
3031 }
3032 
3033 /*
3034  * Put procedure for write queue.
3035  * Respond to M_STOP, M_START, M_IOCTL, and M_FLUSH messages here;
3036  * set the flow control character for M_STOPI and M_STARTI messages;
3037  * queue up M_BREAK, M_DELAY, and M_DATA messages for processing
3038  * by the start routine, and then call the start routine; discard
3039  * everything else.  Note that this driver does not incorporate any
3040  * mechanism to negotiate to handle the canonicalization process.
3041  * It expects that these functions are handled in upper module(s),
3042  * as we do in ldterm.
3043  */
3044 static void
3045 asywput(queue_t *q, mblk_t *mp)
3046 {
3047 	register struct asyncline *async;
3048 	register struct asycom *asy;
3049 	int error;
3050 
3051 	async = (struct asyncline *)q->q_ptr;
3052 	asy = async->async_common;
3053 
3054 	switch (mp->b_datap->db_type) {
3055 
3056 	case M_STOP:
3057 		/*
3058 		 * Since we don't do real DMA, we can just let the
3059 		 * chip coast to a stop after applying the brakes.
3060 		 */
3061 		mutex_enter(asy->asy_excl);
3062 		async->async_flags |= ASYNC_STOPPED;
3063 		mutex_exit(asy->asy_excl);
3064 		freemsg(mp);
3065 		break;
3066 
3067 	case M_START:
3068 		mutex_enter(asy->asy_excl);
3069 		if (async->async_flags & ASYNC_STOPPED) {
3070 			async->async_flags &= ~ASYNC_STOPPED;
3071 			/*
3072 			 * If an output operation is in progress,
3073 			 * resume it.  Otherwise, prod the start
3074 			 * routine.
3075 			 */
3076 			if (async->async_ocnt > 0) {
3077 				mutex_enter(asy->asy_excl_hi);
3078 				async_resume(async);
3079 				mutex_exit(asy->asy_excl_hi);
3080 			} else {
3081 				async_start(async);
3082 			}
3083 		}
3084 		mutex_exit(asy->asy_excl);
3085 		freemsg(mp);
3086 		break;
3087 
3088 	case M_IOCTL:
3089 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
3090 
3091 		case TCSBRK:
3092 			error = miocpullup(mp, sizeof (int));
3093 			if (error != 0) {
3094 				miocnak(q, mp, 0, error);
3095 				return;
3096 			}
3097 
3098 			if (*(int *)mp->b_cont->b_rptr != 0) {
3099 #ifdef DEBUG
3100 				if (asydebug & ASY_DEBUG_CLOSE)
3101 					printf("asy%d: flush request.\n",
3102 					    UNIT(async->async_dev));
3103 #endif
3104 				(void) putq(q, mp);
3105 				mutex_enter(asy->asy_excl);
3106 				async_nstart(async, 1);
3107 				mutex_exit(asy->asy_excl);
3108 				break;
3109 			}
3110 			/*FALLTHROUGH*/
3111 		case TCSETSW:
3112 		case TCSETSF:
3113 		case TCSETAW:
3114 		case TCSETAF:
3115 			/*
3116 			 * The changes do not take effect until all
3117 			 * output queued before them is drained.
3118 			 * Put this message on the queue, so that
3119 			 * "async_start" will see it when it's done
3120 			 * with the output before it.  Poke the
3121 			 * start routine, just in case.
3122 			 */
3123 			(void) putq(q, mp);
3124 			mutex_enter(asy->asy_excl);
3125 			async_start(async);
3126 			mutex_exit(asy->asy_excl);
3127 			break;
3128 
3129 		default:
3130 			/*
3131 			 * Do it now.
3132 			 */
3133 			async_ioctl(async, q, mp, B_TRUE);
3134 			break;
3135 		}
3136 		break;
3137 
3138 	case M_FLUSH:
3139 		if (*mp->b_rptr & FLUSHW) {
3140 			mutex_enter(asy->asy_excl);
3141 
3142 			/*
3143 			 * Abort any output in progress.
3144 			 */
3145 			mutex_enter(asy->asy_excl_hi);
3146 			if (async->async_flags & ASYNC_BUSY) {
3147 				async->async_ocnt = 0;
3148 				async->async_flags &= ~ASYNC_BUSY;
3149 			}
3150 			mutex_exit(asy->asy_excl_hi);
3151 
3152 			/* Flush FIFO buffers */
3153 			if (asy->asy_use_fifo == FIFO_ON) {
3154 				OUTB(FIFOR, FIFO_ON | FIFODMA | FIFOTXFLSH |
3155 				    (asy->asy_trig_level & 0xff));
3156 			}
3157 
3158 			/*
3159 			 * Flush our write queue.
3160 			 */
3161 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
3162 			if (async->async_xmitblk != NULL) {
3163 				freeb(async->async_xmitblk);
3164 				async->async_xmitblk = NULL;
3165 			}
3166 
3167 			mutex_exit(asy->asy_excl);
3168 			*mp->b_rptr &= ~FLUSHW;	/* it has been flushed */
3169 		}
3170 		if (*mp->b_rptr & FLUSHR) {
3171 			/* Flush FIFO buffers */
3172 			if (asy->asy_use_fifo == FIFO_ON) {
3173 				OUTB(FIFOR, FIFO_ON | FIFODMA | FIFORXFLSH |
3174 				    (asy->asy_trig_level & 0xff));
3175 			}
3176 			flushq(RD(q), FLUSHDATA);
3177 			qreply(q, mp);	/* give the read queues a crack at it */
3178 		} else {
3179 			freemsg(mp);
3180 		}
3181 
3182 		/*
3183 		 * We must make sure we process messages that survive the
3184 		 * write-side flush.  Without this call, the close protocol
3185 		 * with ldterm can hang forever.  (ldterm will have sent us a
3186 		 * TCSBRK ioctl that it expects a response to.)
3187 		 */
3188 		mutex_enter(asy->asy_excl);
3189 		async_start(async);
3190 		mutex_exit(asy->asy_excl);
3191 		break;
3192 	case M_BREAK:
3193 	case M_DELAY:
3194 	case M_DATA:
3195 		/*
3196 		 * Queue the message up to be transmitted,
3197 		 * and poke the start routine.
3198 		 */
3199 		(void) putq(q, mp);
3200 		mutex_enter(asy->asy_excl);
3201 		async_start(async);
3202 		mutex_exit(asy->asy_excl);
3203 		break;
3204 
3205 	case M_STOPI:
3206 		mutex_enter(asy->asy_excl);
3207 		async->async_flowc = async->async_stopc;
3208 		async_start(async);		/* poke the start routine */
3209 		mutex_exit(asy->asy_excl);
3210 		freemsg(mp);
3211 		break;
3212 
3213 	case M_STARTI:
3214 		mutex_enter(asy->asy_excl);
3215 		async->async_flowc = async->async_startc;
3216 		async_start(async);		/* poke the start routine */
3217 		mutex_exit(asy->asy_excl);
3218 		freemsg(mp);
3219 		break;
3220 
3221 	case M_CTL:
3222 		if (MBLKL(mp) >= sizeof (struct iocblk) &&
3223 		    ((struct iocblk *)mp->b_rptr)->ioc_cmd == MC_POSIXQUERY) {
3224 			((struct iocblk *)mp->b_rptr)->ioc_cmd = MC_HAS_POSIX;
3225 			qreply(q, mp);
3226 		} else {
3227 			/*
3228 			 * These MC_SERVICE type messages are used by upper
3229 			 * modules to tell this driver to send input up
3230 			 * immediately, or that it can wait for normal
3231 			 * processing that may or may not be done.  Sun
3232 			 * requires these for the mouse module.
3233 			 * (XXX - for x86?)
3234 			 */
3235 			mutex_enter(asy->asy_excl);
3236 			switch (*mp->b_rptr) {
3237 
3238 			case MC_SERVICEIMM:
3239 				async->async_flags |= ASYNC_SERVICEIMM;
3240 				break;
3241 
3242 			case MC_SERVICEDEF:
3243 				async->async_flags &= ~ASYNC_SERVICEIMM;
3244 				break;
3245 			}
3246 			mutex_exit(asy->asy_excl);
3247 			freemsg(mp);
3248 		}
3249 		break;
3250 
3251 	case M_IOCDATA:
3252 		async_iocdata(q, mp);
3253 		break;
3254 
3255 	default:
3256 		freemsg(mp);
3257 		break;
3258 	}
3259 }
3260 
3261 /*
3262  * Retry an "ioctl", now that "bufcall" claims we may be able to allocate
3263  * the buffer we need.
3264  */
3265 static void
3266 async_reioctl(void *arg)
3267 {
3268 	struct asyncline *async = arg;
3269 	struct asycom *asy = async->async_common;
3270 	queue_t	*q;
3271 	mblk_t		*mp;
3272 
3273 	/*
3274 	 * The bufcall is no longer pending.
3275 	 */
3276 	mutex_enter(asy->asy_excl);
3277 	async->async_wbufcid = 0;
3278 	if ((q = async->async_ttycommon.t_writeq) == NULL) {
3279 		mutex_exit(asy->asy_excl);
3280 		return;
3281 	}
3282 	if ((mp = async->async_ttycommon.t_iocpending) != NULL) {
3283 		/* not pending any more */
3284 		async->async_ttycommon.t_iocpending = NULL;
3285 		mutex_exit(asy->asy_excl);
3286 		/* not in STREAMS queue; we no longer know if we're in wput */
3287 		async_ioctl(async, q, mp, B_TRUE);
3288 	} else
3289 		mutex_exit(asy->asy_excl);
3290 }
3291 
3292 static void
3293 async_iocdata(queue_t *q, mblk_t *mp)
3294 {
3295 	struct asyncline	*async = (struct asyncline *)q->q_ptr;
3296 	struct asycom		*asy;
3297 	struct copyresp *csp;
3298 
3299 	asy = async->async_common;
3300 	csp = (struct copyresp *)mp->b_rptr;
3301 
3302 	if (csp->cp_rval != 0) {
3303 		freemsg(mp);
3304 		return;
3305 	}
3306 
3307 	mutex_enter(asy->asy_excl);
3308 
3309 	switch (csp->cp_cmd) {
3310 	case TIOCMSET:
3311 	case TIOCMBIS:
3312 	case TIOCMBIC:
3313 		if (mp->b_cont == NULL) {
3314 			mutex_exit(asy->asy_excl);
3315 			miocnak(q, mp, 0, EINVAL);
3316 			break;
3317 		}
3318 
3319 		mutex_enter(asy->asy_excl_hi);
3320 		(void) asymctl(asy, dmtoasy(*(int *)mp->b_cont->b_rptr),
3321 			csp->cp_cmd);
3322 		mutex_exit(asy->asy_excl_hi);
3323 
3324 		freemsg(mp->b_cont);
3325 		mp->b_cont = NULL;
3326 		mutex_exit(asy->asy_excl);
3327 		miocack(q, mp, 0, 0);
3328 		break;
3329 
3330 	case TIOCMGET:
3331 		if (mp->b_cont != NULL) {
3332 			freemsg(mp->b_cont);
3333 			mp->b_cont = NULL;
3334 		}
3335 		mutex_exit(asy->asy_excl);
3336 		miocack(q, mp, 0, 0);
3337 		break;
3338 
3339 	default:
3340 		mutex_exit(asy->asy_excl);
3341 		miocnak(q, mp, 0, EINVAL);
3342 		break;
3343 	}
3344 }
3345 
3346 
3347 /*
3348  * Set or get the modem control status.
3349  */
3350 static int
3351 asymctl(struct asycom *asy, int bits, int how)
3352 {
3353 	register int mcr_r, msr_r;
3354 
3355 	ASSERT(mutex_owned(asy->asy_excl_hi));
3356 	ASSERT(mutex_owned(asy->asy_excl));
3357 
3358 	/* Read Modem Control Registers */
3359 	mcr_r = INB(MCR);
3360 
3361 	switch (how) {
3362 
3363 	case TIOCMSET:
3364 		mcr_r = bits;
3365 		break;
3366 
3367 	case TIOCMBIS:
3368 		mcr_r |= bits;			/* Set bits from input	*/
3369 		break;
3370 
3371 	case TIOCMBIC:
3372 		mcr_r &= ~bits;			/* Set ~bits from input	*/
3373 		break;
3374 
3375 	case TIOCMGET:
3376 		/* Read Modem Status Registers */
3377 		if (INB(ICR) & MIEN)
3378 			msr_r = asy->asy_cached_msr;
3379 		else
3380 			msr_r = INB(MSR);
3381 		return (asytodm(mcr_r, msr_r));
3382 	}
3383 
3384 	OUTB(MCR, mcr_r);
3385 
3386 	return (mcr_r);
3387 }
3388 
3389 static int
3390 asytodm(int mcr_r, int msr_r)
3391 {
3392 	register int b = 0;
3393 
3394 
3395 	/* MCR registers */
3396 	if (mcr_r & RTS)
3397 		b |= TIOCM_RTS;
3398 
3399 	if (mcr_r & DTR)
3400 		b |= TIOCM_DTR;
3401 
3402 	/* MSR registers */
3403 	if (msr_r & DCD)
3404 		b |= TIOCM_CAR;
3405 
3406 	if (msr_r & CTS)
3407 		b |= TIOCM_CTS;
3408 
3409 	if (msr_r & DSR)
3410 		b |= TIOCM_DSR;
3411 
3412 	if (msr_r & RI)
3413 		b |= TIOCM_RNG;
3414 
3415 	return (b);
3416 }
3417 
3418 static int
3419 dmtoasy(int bits)
3420 {
3421 	register int b = 0;
3422 
3423 #ifdef	CAN_NOT_SET	/* only DTR and RTS can be set */
3424 	if (bits & TIOCM_CAR)
3425 		b |= DCD;
3426 	if (bits & TIOCM_CTS)
3427 		b |= CTS;
3428 	if (bits & TIOCM_DSR)
3429 		b |= DSR;
3430 	if (bits & TIOCM_RNG)
3431 		b |= RI;
3432 #endif
3433 
3434 	if (bits & TIOCM_RTS)
3435 		b |= RTS;
3436 	if (bits & TIOCM_DTR)
3437 		b |= DTR;
3438 
3439 	return (b);
3440 }
3441 
3442 static void
3443 asycheckflowcontrol_hw(struct asycom *asy)
3444 {
3445 	struct asyncline *async;
3446 	uchar_t	mcr, flag;
3447 
3448 	ASSERT(mutex_owned(asy->asy_excl_hi));
3449 
3450 	async = (struct asyncline *)asy->asy_priv;
3451 	ASSERT(async != NULL);
3452 
3453 	if (async->async_ttycommon.t_cflag & CRTSXOFF) {
3454 		mcr = INB(MCR);
3455 		flag = (async->async_flags & ASYNC_HW_IN_FLOW) ? 0 : RTS;
3456 		if (((mcr ^ flag) & RTS) != 0) {
3457 			OUTB(MCR, (mcr ^ RTS));
3458 		}
3459 	}
3460 }
3461 
3462 static boolean_t
3463 asycheckflowcontrol_sw(struct asycom *asy)
3464 {
3465 	uchar_t		ss;
3466 	struct asyncline *async;
3467 	int rval = B_FALSE;
3468 
3469 	ASSERT(mutex_owned(asy->asy_excl_hi));
3470 
3471 	async = (struct asyncline *)asy->asy_priv;
3472 	ASSERT(async != NULL);
3473 
3474 	if ((ss = async->async_flowc) != '\0' && (INB(LSR) & XHRE)) {
3475 		/*
3476 		 * If we get this far, then we know that flowc is non-zero and
3477 		 * that there's transmit room available.  We've "handled" the
3478 		 * request now, so clear it.  If the user didn't ask for IXOFF,
3479 		 * then don't actually send anything, but wait for the next
3480 		 * opportunity.
3481 		 */
3482 		async->async_flowc = '\0';
3483 		if (async->async_ttycommon.t_iflag & IXOFF) {
3484 			async->async_flags |= ASYNC_BUSY;
3485 			OUTB(DAT, ss);
3486 			rval = B_TRUE;
3487 		}
3488 	}
3489 
3490 	return (rval);
3491 }
3492 
3493 /*
3494  * Check for abort character sequence
3495  */
3496 static boolean_t
3497 abort_charseq_recognize(uchar_t ch)
3498 {
3499 	static int state = 0;
3500 #define	CNTRL(c) ((c)&037)
3501 	static char sequence[] = { '\r', '~', CNTRL('b') };
3502 
3503 	if (ch == sequence[state]) {
3504 		if (++state >= sizeof (sequence)) {
3505 			state = 0;
3506 			return (B_TRUE);
3507 		}
3508 	} else {
3509 		state = (ch == sequence[0]) ? 1 : 0;
3510 	}
3511 	return (B_FALSE);
3512 }
3513