xref: /titanic_44/usr/src/uts/common/io/wscons.c (revision 281f0747d41a786d6474ec71e7dc8dad1758a176)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5fea9cb91Slq150181  * Common Development and Distribution License (the "License").
6fea9cb91Slq150181  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21fea9cb91Slq150181 
227c478bd9Sstevel@tonic-gate /*
23fea9cb91Slq150181  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * "Workstation console" multiplexor driver for Sun.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * Sends output to the primary frame buffer using the PROM monitor;
337c478bd9Sstevel@tonic-gate  * gets input from a stream linked below us that is the "keyboard
347c478bd9Sstevel@tonic-gate  * driver", below which is linked the primary keyboard.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/param.h>
397c478bd9Sstevel@tonic-gate #include <sys/signal.h>
407c478bd9Sstevel@tonic-gate #include <sys/cred.h>
417c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
427c478bd9Sstevel@tonic-gate #include <sys/termios.h>
437c478bd9Sstevel@tonic-gate #include <sys/termio.h>
447c478bd9Sstevel@tonic-gate #include <sys/ttold.h>
457c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
467c478bd9Sstevel@tonic-gate #include <sys/stream.h>
477c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
487c478bd9Sstevel@tonic-gate #include <sys/tty.h>
497c478bd9Sstevel@tonic-gate #include <sys/buf.h>
507c478bd9Sstevel@tonic-gate #include <sys/uio.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
537c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
547c478bd9Sstevel@tonic-gate #include <sys/kbio.h>
557c478bd9Sstevel@tonic-gate #include <sys/strredir.h>
567c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
577c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
587c478bd9Sstevel@tonic-gate #include <sys/conf.h>
597c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
607c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
617c478bd9Sstevel@tonic-gate #include <sys/debug.h>
627c478bd9Sstevel@tonic-gate #include <sys/console.h>
637c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
647c478bd9Sstevel@tonic-gate #include <sys/promif.h>
657c478bd9Sstevel@tonic-gate #include <sys/policy.h>
66fea9cb91Slq150181 #include <sys/tem.h>
67fea9cb91Slq150181 #include <sys/wscons.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	MINLINES	10
707c478bd9Sstevel@tonic-gate #define	MAXLINES	48
717c478bd9Sstevel@tonic-gate #define	LOSCREENLINES	34
727c478bd9Sstevel@tonic-gate #define	HISCREENLINES	48
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #define	MINCOLS		10
757c478bd9Sstevel@tonic-gate #define	MAXCOLS		120
767c478bd9Sstevel@tonic-gate #define	LOSCREENCOLS	80
777c478bd9Sstevel@tonic-gate #define	HISCREENCOLS	120
787c478bd9Sstevel@tonic-gate 
79fea9cb91Slq150181 struct wscons {
80fea9cb91Slq150181 	struct tem *wc_tem;		/* Terminal emulator state */
817c478bd9Sstevel@tonic-gate 	int	wc_flags;		/* random flags (protected by */
827c478bd9Sstevel@tonic-gate 					/* write-side exclusion lock  */
837c478bd9Sstevel@tonic-gate 	dev_t	wc_dev;			/* major/minor for this device */
847c478bd9Sstevel@tonic-gate 	tty_common_t wc_ttycommon;	/* data common to all tty drivers */
85fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE
867c478bd9Sstevel@tonic-gate 	int	wc_pendc;		/* pending output character */
877c478bd9Sstevel@tonic-gate 	int	wc_defer_output;	/* set if output device is "slow" */
88fea9cb91Slq150181 #endif /* _HAVE_TEM_FIRMWARE */
897c478bd9Sstevel@tonic-gate 	queue_t	*wc_kbdqueue;		/* "console keyboard" device queue */
907c478bd9Sstevel@tonic-gate 					/* below us */
917c478bd9Sstevel@tonic-gate 	bufcall_id_t wc_bufcallid;	/* id returned by qbufcall */
927c478bd9Sstevel@tonic-gate 	timeout_id_t wc_timeoutid;	/* id returned by qtimeout */
937c478bd9Sstevel@tonic-gate 	cons_polledio_t		wc_polledio; /* polled I/O function pointers */
947c478bd9Sstevel@tonic-gate 	cons_polledio_t		*wc_kb_polledio; /* keyboard's polledio */
957c478bd9Sstevel@tonic-gate 	unsigned int	wc_kb_getpolledio_id; /* id for kb CONSOPENPOLLEDIO */
967c478bd9Sstevel@tonic-gate 	mblk_t	*wc_pending_link;	/* I_PLINK pending for kb polledio */
977c478bd9Sstevel@tonic-gate } wscons;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #define	WCS_ISOPEN	0x00000001	/* open is complete */
1007c478bd9Sstevel@tonic-gate #define	WCS_STOPPED	0x00000002	/* output is stopped */
1017c478bd9Sstevel@tonic-gate #define	WCS_DELAY	0x00000004	/* waiting for delay to finish */
1027c478bd9Sstevel@tonic-gate #define	WCS_BUSY	0x00000008	/* waiting for transmission to finish */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static int	wcopen(queue_t *, dev_t *, int, int, cred_t *);
1057c478bd9Sstevel@tonic-gate static int	wcclose(queue_t *, int, cred_t *);
1067c478bd9Sstevel@tonic-gate static int	wcuwput(queue_t *, mblk_t *);
1077c478bd9Sstevel@tonic-gate static int	wclrput(queue_t *, mblk_t *);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static struct module_info wcm_info = {
1107c478bd9Sstevel@tonic-gate 	0,
1117c478bd9Sstevel@tonic-gate 	"wc",
1127c478bd9Sstevel@tonic-gate 	0,
1137c478bd9Sstevel@tonic-gate 	INFPSZ,
1147c478bd9Sstevel@tonic-gate 	2048,
1157c478bd9Sstevel@tonic-gate 	128
1167c478bd9Sstevel@tonic-gate };
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static struct qinit wcurinit = {
1197c478bd9Sstevel@tonic-gate 	putq,
1207c478bd9Sstevel@tonic-gate 	NULL,
1217c478bd9Sstevel@tonic-gate 	wcopen,
1227c478bd9Sstevel@tonic-gate 	wcclose,
1237c478bd9Sstevel@tonic-gate 	NULL,
1247c478bd9Sstevel@tonic-gate 	&wcm_info,
1257c478bd9Sstevel@tonic-gate 	NULL
1267c478bd9Sstevel@tonic-gate };
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static struct qinit wcuwinit = {
1297c478bd9Sstevel@tonic-gate 	wcuwput,
1307c478bd9Sstevel@tonic-gate 	NULL,
1317c478bd9Sstevel@tonic-gate 	wcopen,
1327c478bd9Sstevel@tonic-gate 	wcclose,
1337c478bd9Sstevel@tonic-gate 	NULL,
1347c478bd9Sstevel@tonic-gate 	&wcm_info,
1357c478bd9Sstevel@tonic-gate 	NULL
1367c478bd9Sstevel@tonic-gate };
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static struct qinit wclrinit = {
1397c478bd9Sstevel@tonic-gate 	wclrput,
1407c478bd9Sstevel@tonic-gate 	NULL,
1417c478bd9Sstevel@tonic-gate 	NULL,
1427c478bd9Sstevel@tonic-gate 	NULL,
1437c478bd9Sstevel@tonic-gate 	NULL,
1447c478bd9Sstevel@tonic-gate 	&wcm_info,
1457c478bd9Sstevel@tonic-gate 	NULL
1467c478bd9Sstevel@tonic-gate };
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate  * We always putnext directly to the underlying queue.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate static struct qinit wclwinit = {
1527c478bd9Sstevel@tonic-gate 	NULL,
1537c478bd9Sstevel@tonic-gate 	NULL,
1547c478bd9Sstevel@tonic-gate 	NULL,
1557c478bd9Sstevel@tonic-gate 	NULL,
1567c478bd9Sstevel@tonic-gate 	NULL,
1577c478bd9Sstevel@tonic-gate 	&wcm_info,
1587c478bd9Sstevel@tonic-gate 	NULL
1597c478bd9Sstevel@tonic-gate };
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static struct streamtab wcinfo = {
1627c478bd9Sstevel@tonic-gate 	&wcurinit,
1637c478bd9Sstevel@tonic-gate 	&wcuwinit,
1647c478bd9Sstevel@tonic-gate 	&wclrinit,
1657c478bd9Sstevel@tonic-gate 	&wclwinit,
1667c478bd9Sstevel@tonic-gate };
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static int wc_info(dev_info_t *, ddi_info_cmd_t, void *, void **result);
1697c478bd9Sstevel@tonic-gate static int wc_attach(dev_info_t *, ddi_attach_cmd_t);
1707c478bd9Sstevel@tonic-gate static dev_info_t *wc_dip;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(wc_ops, nulldev, nulldev, wc_attach, nodev, nodev,
1737c478bd9Sstevel@tonic-gate     wc_info, D_MTPERMOD | D_MP, &wcinfo);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static void	wcreioctl(void *);
1767c478bd9Sstevel@tonic-gate static void 	wcioctl(queue_t *, mblk_t *);
177fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE
1787c478bd9Sstevel@tonic-gate static void	wcopoll(void *);
1797c478bd9Sstevel@tonic-gate static void	wconsout(void *);
180fea9cb91Slq150181 #endif /* _HAVE_TEM_FIRMWARE */
1817c478bd9Sstevel@tonic-gate static void	wcrstrt(void *);
1827c478bd9Sstevel@tonic-gate static void	wcstart(void);
1837c478bd9Sstevel@tonic-gate static void	wc_open_kb_polledio(struct wscons *wc, queue_t *q, mblk_t *mp);
1847c478bd9Sstevel@tonic-gate static void	wc_close_kb_polledio(struct wscons *wc, queue_t *q, mblk_t *mp);
185*281f0747Slt200341 static void	wc_polled_putchar(cons_polledio_arg_t arg, unsigned char c);
186*281f0747Slt200341 static boolean_t wc_polled_ischar(cons_polledio_arg_t arg);
187*281f0747Slt200341 static int	wc_polled_getchar(cons_polledio_arg_t arg);
188*281f0747Slt200341 static void	wc_polled_enter(cons_polledio_arg_t arg);
189*281f0747Slt200341 static void	wc_polled_exit(cons_polledio_arg_t arg);
1907c478bd9Sstevel@tonic-gate static void	wc_get_size(struct wscons *wscons);
191fea9cb91Slq150181 static void	wc_modechg_cb(tem_modechg_cb_arg_t arg);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate #include <sys/types.h>
1947c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1957c478bd9Sstevel@tonic-gate #include <sys/param.h>
1967c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1977c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1987c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static struct dev_ops wc_ops;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * Debug printing
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate #ifndef DPRINTF
2067c478bd9Sstevel@tonic-gate #ifdef DEBUG
2077c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
2087c478bd9Sstevel@tonic-gate static void	wc_dprintf(const char *fmt, ...) __KPRINTFLIKE(1);
2097c478bd9Sstevel@tonic-gate #define	DPRINTF(l, m, args) \
2107c478bd9Sstevel@tonic-gate 	(((l) >= wc_errlevel) && ((m) & wc_errmask) ?	\
2117c478bd9Sstevel@tonic-gate 		wc_dprintf args :			\
2127c478bd9Sstevel@tonic-gate 		(void) 0)
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * Severity levels for printing
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate #define	PRINT_L0	0	/* print every message */
2187c478bd9Sstevel@tonic-gate #define	PRINT_L1	1	/* debug */
2197c478bd9Sstevel@tonic-gate #define	PRINT_L2	2	/* quiet */
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate  * Masks
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate #define	PRINT_MASK_ALL		0xFFFFFFFFU
2257c478bd9Sstevel@tonic-gate uint_t	wc_errmask = PRINT_MASK_ALL;
2267c478bd9Sstevel@tonic-gate uint_t	wc_errlevel = PRINT_L2;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate #else
2297c478bd9Sstevel@tonic-gate #define	DPRINTF(l, m, args)	/* NOTHING */
2307c478bd9Sstevel@tonic-gate #endif
2317c478bd9Sstevel@tonic-gate #endif
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
2387c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
2397c478bd9Sstevel@tonic-gate 	"Workstation multiplexer Driver 'wc' %I%",
2407c478bd9Sstevel@tonic-gate 	&wc_ops,	/* driver ops */
2417c478bd9Sstevel@tonic-gate };
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2447c478bd9Sstevel@tonic-gate 	MODREV_1,
2457c478bd9Sstevel@tonic-gate 	&modldrv,
2467c478bd9Sstevel@tonic-gate 	NULL
2477c478bd9Sstevel@tonic-gate };
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate int
2507c478bd9Sstevel@tonic-gate _init(void)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate int
2567c478bd9Sstevel@tonic-gate _fini(void)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate int
2627c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2687c478bd9Sstevel@tonic-gate static int
2697c478bd9Sstevel@tonic-gate wc_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "wscons", S_IFCHR,
2727c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
2737c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
2747c478bd9Sstevel@tonic-gate 		return (-1);
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 	wc_dip = devi;
277fea9cb91Slq150181 
278fea9cb91Slq150181 	bzero(&(wscons.wc_polledio), sizeof (wscons.wc_polledio));
279fea9cb91Slq150181 
2807c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /* ARGSUSED */
2847c478bd9Sstevel@tonic-gate static int
2857c478bd9Sstevel@tonic-gate wc_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2867c478bd9Sstevel@tonic-gate 	void **result)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	int error;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	switch (infocmd) {
2917c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2927c478bd9Sstevel@tonic-gate 		if (wc_dip == NULL) {
2937c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
2947c478bd9Sstevel@tonic-gate 		} else {
2957c478bd9Sstevel@tonic-gate 			*result = (void *) wc_dip;
2967c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 		break;
2997c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
3007c478bd9Sstevel@tonic-gate 		*result = (void *)0;
3017c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
3027c478bd9Sstevel@tonic-gate 		break;
3037c478bd9Sstevel@tonic-gate 	default:
3047c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 	return (error);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
309fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate  * Output buffer. Protected by the per-module inner perimeter.
3127c478bd9Sstevel@tonic-gate  */
3137c478bd9Sstevel@tonic-gate #define	MAXHIWAT	2000
3147c478bd9Sstevel@tonic-gate static char obuf[MAXHIWAT];
315fea9cb91Slq150181 #endif /* _HAVE_TEM_FIRMWARE */
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3187c478bd9Sstevel@tonic-gate static int
3197c478bd9Sstevel@tonic-gate wcopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *crp)
3207c478bd9Sstevel@tonic-gate {
321fea9cb91Slq150181 	static boolean_t polledio_inited = B_FALSE;
3227c478bd9Sstevel@tonic-gate 	struct termios *termiosp;
3237c478bd9Sstevel@tonic-gate 	int len;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (getminor(*devp) != 0)
3267c478bd9Sstevel@tonic-gate 		return (ENXIO);		/* sorry, only one per customer */
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (!(wscons.wc_flags & WCS_ISOPEN)) {
3297c478bd9Sstevel@tonic-gate 		mutex_init(&wscons.wc_ttycommon.t_excl, NULL, MUTEX_DEFAULT,
3307c478bd9Sstevel@tonic-gate 		    NULL);
3317c478bd9Sstevel@tonic-gate 		wscons.wc_ttycommon.t_iflag = 0;
3327c478bd9Sstevel@tonic-gate 		/*
3337c478bd9Sstevel@tonic-gate 		 * Get the default termios settings (cflag).
3347c478bd9Sstevel@tonic-gate 		 * These are stored as a property in the
3357c478bd9Sstevel@tonic-gate 		 * "options" node.
3367c478bd9Sstevel@tonic-gate 		 */
3377c478bd9Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY,
3387c478bd9Sstevel@tonic-gate 		    ddi_root_node(), 0, "ttymodes",
3397c478bd9Sstevel@tonic-gate 		    (caddr_t)&termiosp, &len) == DDI_PROP_SUCCESS &&
3407c478bd9Sstevel@tonic-gate 		    len == sizeof (struct termios)) {
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 			wscons.wc_ttycommon.t_cflag = termiosp->c_cflag;
3437c478bd9Sstevel@tonic-gate 			kmem_free(termiosp, len);
3447c478bd9Sstevel@tonic-gate 		} else {
3457c478bd9Sstevel@tonic-gate 			/*
3467c478bd9Sstevel@tonic-gate 			 * Gack!  Whine about it.
3477c478bd9Sstevel@tonic-gate 			 */
3487c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
3497c478bd9Sstevel@tonic-gate 			    "wc: Couldn't get ttymodes property!\n");
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 		wscons.wc_ttycommon.t_iocpending = NULL;
3527c478bd9Sstevel@tonic-gate 		wscons.wc_flags = WCS_ISOPEN;
3537c478bd9Sstevel@tonic-gate 
354fea9cb91Slq150181 		wscons.wc_dev = *devp;
3557c478bd9Sstevel@tonic-gate 		wc_get_size(&wscons);
3567c478bd9Sstevel@tonic-gate 
357fea9cb91Slq150181 		if (!polledio_inited) {
358fea9cb91Slq150181 			polledio_inited = B_TRUE;
359fea9cb91Slq150181 
360fea9cb91Slq150181 			/*
361fea9cb91Slq150181 			 * Initialize the parts of the polled I/O struct that
362fea9cb91Slq150181 			 * are common to both input and output modes, but which
363fea9cb91Slq150181 			 * don't flag to the upper layers, which if any of the
364fea9cb91Slq150181 			 * two modes are available.  We don't know at this point
365fea9cb91Slq150181 			 * if system is configured CONS_KFB, but we will when
366fea9cb91Slq150181 			 * consconfig_dacf asks us with CONSOPENPOLLED I/O.
367fea9cb91Slq150181 			 */
368fea9cb91Slq150181 			wscons.wc_polledio.cons_polledio_version =
369fea9cb91Slq150181 				CONSPOLLEDIO_V0;
3707c478bd9Sstevel@tonic-gate 			wscons.wc_polledio.cons_polledio_argument =
371*281f0747Slt200341 				(cons_polledio_arg_t)&wscons;
372fea9cb91Slq150181 			wscons.wc_polledio.cons_polledio_enter =
373fea9cb91Slq150181 				wc_polled_enter;
374fea9cb91Slq150181 			wscons.wc_polledio.cons_polledio_exit =
375fea9cb91Slq150181 				wc_polled_exit;
3767c478bd9Sstevel@tonic-gate 
377fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE
3787c478bd9Sstevel@tonic-gate 			/*
3797c478bd9Sstevel@tonic-gate 			 * If we're talking directly to a framebuffer, we assume
380fea9cb91Slq150181 			 * that it's a "slow" device, so that rendering should
381fea9cb91Slq150181 			 * be deferred to a timeout or softcall so that we write
3827c478bd9Sstevel@tonic-gate 			 * a bunch of characters at once.
3837c478bd9Sstevel@tonic-gate 			 */
3847c478bd9Sstevel@tonic-gate 			wscons.wc_defer_output = prom_stdout_is_framebuffer();
385fea9cb91Slq150181 #endif /* _HAVE_TEM_FIRMWARE */
386fea9cb91Slq150181 		}
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if (wscons.wc_ttycommon.t_flags & TS_XCLUDE) {
3907c478bd9Sstevel@tonic-gate 		if (secpolicy_excl_open(crp) != 0) {
3917c478bd9Sstevel@tonic-gate 			return (EBUSY);
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 	wscons.wc_ttycommon.t_readq = q;
3957c478bd9Sstevel@tonic-gate 	wscons.wc_ttycommon.t_writeq = WR(q);
3967c478bd9Sstevel@tonic-gate 	qprocson(q);
3977c478bd9Sstevel@tonic-gate 	return (0);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4017c478bd9Sstevel@tonic-gate static int
4027c478bd9Sstevel@tonic-gate wcclose(queue_t *q, int flag, cred_t *crp)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	qprocsoff(q);
4057c478bd9Sstevel@tonic-gate 	if (wscons.wc_bufcallid != 0) {
4067c478bd9Sstevel@tonic-gate 		qunbufcall(q, wscons.wc_bufcallid);
4077c478bd9Sstevel@tonic-gate 		wscons.wc_bufcallid = 0;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 	if (wscons.wc_timeoutid != 0) {
4107c478bd9Sstevel@tonic-gate 		(void) quntimeout(q, wscons.wc_timeoutid);
4117c478bd9Sstevel@tonic-gate 		wscons.wc_timeoutid = 0;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 	ttycommon_close(&wscons.wc_ttycommon);
4147c478bd9Sstevel@tonic-gate 	wscons.wc_flags = 0;
4157c478bd9Sstevel@tonic-gate 	return (0);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate  * Put procedure for upper write queue.
4207c478bd9Sstevel@tonic-gate  * Respond to M_STOP, M_START, M_IOCTL, and M_FLUSH messages here;
4217c478bd9Sstevel@tonic-gate  * queue up M_BREAK, M_DELAY, and M_DATA messages for processing by
4227c478bd9Sstevel@tonic-gate  * the start routine, and then call the start routine; discard
4237c478bd9Sstevel@tonic-gate  * everything else.
4247c478bd9Sstevel@tonic-gate  */
4257c478bd9Sstevel@tonic-gate static int
4267c478bd9Sstevel@tonic-gate wcuwput(queue_t *q, mblk_t *mp)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	case M_STOP:
4317c478bd9Sstevel@tonic-gate 		wscons.wc_flags |= WCS_STOPPED;
4327c478bd9Sstevel@tonic-gate 		freemsg(mp);
4337c478bd9Sstevel@tonic-gate 		break;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	case M_START:
4367c478bd9Sstevel@tonic-gate 		wscons.wc_flags &= ~WCS_STOPPED;
4377c478bd9Sstevel@tonic-gate 		wcstart();
4387c478bd9Sstevel@tonic-gate 		freemsg(mp);
4397c478bd9Sstevel@tonic-gate 		break;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	case M_IOCTL: {
4427c478bd9Sstevel@tonic-gate 		struct iocblk *iocp;
4437c478bd9Sstevel@tonic-gate 		struct linkblk *linkp;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
4467c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 		case I_LINK:	/* stupid, but permitted */
4497c478bd9Sstevel@tonic-gate 		case I_PLINK:
4507c478bd9Sstevel@tonic-gate 			if (wscons.wc_kbdqueue != NULL) {
4517c478bd9Sstevel@tonic-gate 				/* somebody already linked */
4527c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, EINVAL);
4537c478bd9Sstevel@tonic-gate 				return (0);
4547c478bd9Sstevel@tonic-gate 			}
4557c478bd9Sstevel@tonic-gate 			linkp = (struct linkblk *)mp->b_cont->b_rptr;
4567c478bd9Sstevel@tonic-gate 			wscons.wc_kbdqueue = WR(linkp->l_qbot);
4577c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
4587c478bd9Sstevel@tonic-gate 			iocp->ioc_count = 0;
4597c478bd9Sstevel@tonic-gate 			wc_open_kb_polledio(&wscons, q, mp);
4607c478bd9Sstevel@tonic-gate 			break;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		case I_UNLINK:	/* stupid, but permitted */
4637c478bd9Sstevel@tonic-gate 		case I_PUNLINK:
4647c478bd9Sstevel@tonic-gate 			linkp = (struct linkblk *)mp->b_cont->b_rptr;
4657c478bd9Sstevel@tonic-gate 			if (wscons.wc_kbdqueue != WR(linkp->l_qbot)) {
4667c478bd9Sstevel@tonic-gate 				/* not us */
4677c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, EINVAL);
4687c478bd9Sstevel@tonic-gate 				return (0);
4697c478bd9Sstevel@tonic-gate 			}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
4727c478bd9Sstevel@tonic-gate 			iocp->ioc_count = 0;
4737c478bd9Sstevel@tonic-gate 			wc_close_kb_polledio(&wscons, q, mp);
4747c478bd9Sstevel@tonic-gate 			break;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 		case TCSETSW:
4777c478bd9Sstevel@tonic-gate 		case TCSETSF:
4787c478bd9Sstevel@tonic-gate 		case TCSETAW:
4797c478bd9Sstevel@tonic-gate 		case TCSETAF:
4807c478bd9Sstevel@tonic-gate 		case TCSBRK:
4817c478bd9Sstevel@tonic-gate 			/*
4827c478bd9Sstevel@tonic-gate 			 * The changes do not take effect until all
4837c478bd9Sstevel@tonic-gate 			 * output queued before them is drained.
4847c478bd9Sstevel@tonic-gate 			 * Put this message on the queue, so that
4857c478bd9Sstevel@tonic-gate 			 * "wcstart" will see it when it's done
4867c478bd9Sstevel@tonic-gate 			 * with the output before it.  Poke the
4877c478bd9Sstevel@tonic-gate 			 * start routine, just in case.
4887c478bd9Sstevel@tonic-gate 			 */
4897c478bd9Sstevel@tonic-gate 			(void) putq(q, mp);
4907c478bd9Sstevel@tonic-gate 			wcstart();
4917c478bd9Sstevel@tonic-gate 			break;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		case CONSSETABORTENABLE:
4947c478bd9Sstevel@tonic-gate 		case CONSGETABORTENABLE:
4957c478bd9Sstevel@tonic-gate 		case KIOCSDIRECT:
4967c478bd9Sstevel@tonic-gate 			if (wscons.wc_kbdqueue != NULL) {
4977c478bd9Sstevel@tonic-gate 				(void) putnext(wscons.wc_kbdqueue, mp);
4987c478bd9Sstevel@tonic-gate 				break;
4997c478bd9Sstevel@tonic-gate 			}
5007c478bd9Sstevel@tonic-gate 			/* fall through */
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		default:
5037c478bd9Sstevel@tonic-gate 			/*
5047c478bd9Sstevel@tonic-gate 			 * Do it now.
5057c478bd9Sstevel@tonic-gate 			 */
5067c478bd9Sstevel@tonic-gate 			wcioctl(q, mp);
5077c478bd9Sstevel@tonic-gate 			break;
5087c478bd9Sstevel@tonic-gate 		}
5097c478bd9Sstevel@tonic-gate 		break;
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	case M_FLUSH:
5137c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
5147c478bd9Sstevel@tonic-gate 			/*
5157c478bd9Sstevel@tonic-gate 			 * Flush our write queue.
5167c478bd9Sstevel@tonic-gate 			 */
5177c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
5187c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;	/* it has been flushed */
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
5217c478bd9Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
5227c478bd9Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
5237c478bd9Sstevel@tonic-gate 		} else
5247c478bd9Sstevel@tonic-gate 			freemsg(mp);
5257c478bd9Sstevel@tonic-gate 		break;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	case M_BREAK:
5287c478bd9Sstevel@tonic-gate 		/*
5297c478bd9Sstevel@tonic-gate 		 * Ignore these, as they make no sense.
5307c478bd9Sstevel@tonic-gate 		 */
5317c478bd9Sstevel@tonic-gate 		freemsg(mp);
5327c478bd9Sstevel@tonic-gate 		break;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	case M_DELAY:
5357c478bd9Sstevel@tonic-gate 	case M_DATA:
5367c478bd9Sstevel@tonic-gate 		/*
5377c478bd9Sstevel@tonic-gate 		 * Queue the message up to be transmitted,
5387c478bd9Sstevel@tonic-gate 		 * and poke the start routine.
5397c478bd9Sstevel@tonic-gate 		 */
5407c478bd9Sstevel@tonic-gate 		(void) putq(q, mp);
5417c478bd9Sstevel@tonic-gate 		wcstart();
5427c478bd9Sstevel@tonic-gate 		break;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	default:
5457c478bd9Sstevel@tonic-gate 		/*
5467c478bd9Sstevel@tonic-gate 		 * "No, I don't want a subscription to Chain Store Age,
5477c478bd9Sstevel@tonic-gate 		 * thank you anyway."
5487c478bd9Sstevel@tonic-gate 		 */
5497c478bd9Sstevel@tonic-gate 		freemsg(mp);
5507c478bd9Sstevel@tonic-gate 		break;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	return (0);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  * Retry an "ioctl", now that "qbufcall" claims we may be able to allocate
5587c478bd9Sstevel@tonic-gate  * the buffer we need.
5597c478bd9Sstevel@tonic-gate  */
5607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5617c478bd9Sstevel@tonic-gate static void
5627c478bd9Sstevel@tonic-gate wcreioctl(void *arg)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate 	queue_t *q;
5657c478bd9Sstevel@tonic-gate 	mblk_t *mp;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	wscons.wc_bufcallid = 0;
5687c478bd9Sstevel@tonic-gate 	q = wscons.wc_ttycommon.t_writeq;
5697c478bd9Sstevel@tonic-gate 	if ((mp = wscons.wc_ttycommon.t_iocpending) != NULL) {
5707c478bd9Sstevel@tonic-gate 		/* not pending any more */
5717c478bd9Sstevel@tonic-gate 		wscons.wc_ttycommon.t_iocpending = NULL;
5727c478bd9Sstevel@tonic-gate 		wcioctl(q, mp);
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
576fea9cb91Slq150181 static int
577fea9cb91Slq150181 wc_getterm(mblk_t *mp)
578fea9cb91Slq150181 {
579fea9cb91Slq150181 	char *term;
580fea9cb91Slq150181 	intptr_t arg;
581fea9cb91Slq150181 	int flag = ((struct iocblk *)mp->b_rptr)->ioc_flag;
582fea9cb91Slq150181 
583fea9cb91Slq150181 	STRUCT_DECL(cons_getterm, wcterm);
584fea9cb91Slq150181 	STRUCT_INIT(wcterm, flag);
585fea9cb91Slq150181 
586fea9cb91Slq150181 	arg = *((intptr_t *)mp->b_cont->b_rptr);
587fea9cb91Slq150181 
588fea9cb91Slq150181 	if (ddi_copyin((void *)arg, STRUCT_BUF(wcterm),
589fea9cb91Slq150181 	    STRUCT_SIZE(wcterm), flag) != 0) {
590fea9cb91Slq150181 		return (EFAULT);
591fea9cb91Slq150181 	}
592fea9cb91Slq150181 
593fea9cb91Slq150181 	if (consmode == CONS_FW) {
594fea9cb91Slq150181 		/* PROM terminal emulator */
595fea9cb91Slq150181 		term = "sun";
596fea9cb91Slq150181 	} else {
597fea9cb91Slq150181 		/* Kernel terminal emulator */
598fea9cb91Slq150181 		ASSERT(consmode == CONS_KFB);
599fea9cb91Slq150181 		term = "sun-color";
600fea9cb91Slq150181 	}
601fea9cb91Slq150181 
602fea9cb91Slq150181 	if (STRUCT_FGET(wcterm, cn_term_len) <
603fea9cb91Slq150181 	    strlen(term) + 1) {
604fea9cb91Slq150181 		return (EOVERFLOW);
605fea9cb91Slq150181 	}
606fea9cb91Slq150181 
607fea9cb91Slq150181 	if (ddi_copyout(term,
608fea9cb91Slq150181 	    STRUCT_FGETP(wcterm, cn_term_type),
609fea9cb91Slq150181 	    strlen(term) + 1, flag) != 0) {
610fea9cb91Slq150181 		return (EFAULT);
611fea9cb91Slq150181 	}
612fea9cb91Slq150181 
613fea9cb91Slq150181 	return (0);
614fea9cb91Slq150181 }
615fea9cb91Slq150181 
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate  * Process an "ioctl" message sent down to us.
6187c478bd9Sstevel@tonic-gate  */
6197c478bd9Sstevel@tonic-gate static void
6207c478bd9Sstevel@tonic-gate wcioctl(queue_t *q, mblk_t *mp)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	struct iocblk *iocp;
6237c478bd9Sstevel@tonic-gate 	size_t datasize;
6247c478bd9Sstevel@tonic-gate 	int error;
625fea9cb91Slq150181 	long len;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
6307c478bd9Sstevel@tonic-gate 	case TIOCSWINSZ:
6317c478bd9Sstevel@tonic-gate 		/*
6327c478bd9Sstevel@tonic-gate 		 * Ignore all attempts to set the screen size; the
6337c478bd9Sstevel@tonic-gate 		 * value in the EEPROM is guaranteed (modulo PROM bugs)
6347c478bd9Sstevel@tonic-gate 		 * to be the value used by the PROM monitor code, so it
6357c478bd9Sstevel@tonic-gate 		 * is by definition correct.  Many programs (e.g.,
6367c478bd9Sstevel@tonic-gate 		 * "login" and "tset") will attempt to reset the size
6377c478bd9Sstevel@tonic-gate 		 * to (0, 0) or (34, 80), neither of which is
6387c478bd9Sstevel@tonic-gate 		 * necessarily correct.
6397c478bd9Sstevel@tonic-gate 		 * We just ACK the message, so as not to disturb
6407c478bd9Sstevel@tonic-gate 		 * programs that set the sizes.
6417c478bd9Sstevel@tonic-gate 		 */
6427c478bd9Sstevel@tonic-gate 		iocp->ioc_count = 0;	/* no data returned */
6437c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
6447c478bd9Sstevel@tonic-gate 		qreply(q, mp);
6457c478bd9Sstevel@tonic-gate 		return;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
6487c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
6497c478bd9Sstevel@tonic-gate 			("wcioctl: CONSOPENPOLLEDIO\n"));
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct cons_polledio *));
6527c478bd9Sstevel@tonic-gate 		if (error != 0) {
6537c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
6547c478bd9Sstevel@tonic-gate 			return;
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 		/*
6587c478bd9Sstevel@tonic-gate 		 * We are given an appropriate-sized data block,
6597c478bd9Sstevel@tonic-gate 		 * and return a pointer to our structure in it.
6607c478bd9Sstevel@tonic-gate 		 */
661fea9cb91Slq150181 		if (consmode == CONS_KFB)
662fea9cb91Slq150181 			wscons.wc_polledio.cons_polledio_putchar =
663fea9cb91Slq150181 			    wc_polled_putchar;
6647c478bd9Sstevel@tonic-gate 		*(struct cons_polledio **)mp->b_cont->b_rptr =
6657c478bd9Sstevel@tonic-gate 			&wscons.wc_polledio;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 		qreply(q, mp);
6707c478bd9Sstevel@tonic-gate 		break;
6717c478bd9Sstevel@tonic-gate 
672fea9cb91Slq150181 	case CONS_GETTERM:
673fea9cb91Slq150181 		if ((error = wc_getterm(mp)) != 0)
674fea9cb91Slq150181 			miocnak(q, mp, 0, error);
675fea9cb91Slq150181 		else
676fea9cb91Slq150181 			miocack(q, mp, 0, 0);
677fea9cb91Slq150181 		return;
678fea9cb91Slq150181 
6797c478bd9Sstevel@tonic-gate 	case WC_OPEN_FB:
6807c478bd9Sstevel@tonic-gate 		/*
6817c478bd9Sstevel@tonic-gate 		 * Start out pessimistic, so that we can just jump to
6827c478bd9Sstevel@tonic-gate 		 * the reply to bail out.
6837c478bd9Sstevel@tonic-gate 		 */
6847c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 		/*
6877c478bd9Sstevel@tonic-gate 		 * First test:  really, this should be done only from
6887c478bd9Sstevel@tonic-gate 		 * inside the kernel.  Unfortunately, that information
6897c478bd9Sstevel@tonic-gate 		 * doesn't seem to be available in a streams ioctl,
6907c478bd9Sstevel@tonic-gate 		 * so restrict it to root only.  (Perhaps we could check
6917c478bd9Sstevel@tonic-gate 		 * for ioc_cr == kcred.)
6927c478bd9Sstevel@tonic-gate 		 */
6937c478bd9Sstevel@tonic-gate 		if ((iocp->ioc_error = secpolicy_console(iocp->ioc_cr)) != 0)
6947c478bd9Sstevel@tonic-gate 			goto open_fail;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 		/*
6977c478bd9Sstevel@tonic-gate 		 * Some miscellaneous checks...
6987c478bd9Sstevel@tonic-gate 		 */
6997c478bd9Sstevel@tonic-gate 		iocp->ioc_error = EINVAL;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		/*
7027c478bd9Sstevel@tonic-gate 		 * If we're already open, fail.
7037c478bd9Sstevel@tonic-gate 		 */
7047c478bd9Sstevel@tonic-gate 		if (wscons.wc_tem != NULL)
7057c478bd9Sstevel@tonic-gate 			goto open_fail;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		/*
7087c478bd9Sstevel@tonic-gate 		 * If we don't have exactly one continuation block, fail.
7097c478bd9Sstevel@tonic-gate 		 */
7107c478bd9Sstevel@tonic-gate 		if (mp->b_cont == NULL ||
7117c478bd9Sstevel@tonic-gate 		    mp->b_cont->b_cont != NULL)
7127c478bd9Sstevel@tonic-gate 			goto open_fail;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		/*
7157c478bd9Sstevel@tonic-gate 		 * If there's no null terminator in the string, fail.
7167c478bd9Sstevel@tonic-gate 		 */
7177c478bd9Sstevel@tonic-gate 		len = mp->b_cont->b_wptr - mp->b_cont->b_rptr;
7187c478bd9Sstevel@tonic-gate 		if (memchr(mp->b_cont->b_rptr, 0, len) == NULL)
7197c478bd9Sstevel@tonic-gate 			goto open_fail;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		/*
7227c478bd9Sstevel@tonic-gate 		 * NOTE:  should eventually get default
7237c478bd9Sstevel@tonic-gate 		 * dimensions from a property, e.g. screen-#rows.
7247c478bd9Sstevel@tonic-gate 		 */
7257c478bd9Sstevel@tonic-gate 		iocp->ioc_error = tem_init(&wscons.wc_tem,
726fea9cb91Slq150181 			(char *)mp->b_cont->b_rptr, iocp->ioc_cr);
7277c478bd9Sstevel@tonic-gate 		/*
7287c478bd9Sstevel@tonic-gate 		 * Of course, if the terminal emulator initialization
7297c478bd9Sstevel@tonic-gate 		 * failed, fail.
7307c478bd9Sstevel@tonic-gate 		 */
7317c478bd9Sstevel@tonic-gate 		if (iocp->ioc_error != 0)
7327c478bd9Sstevel@tonic-gate 			goto open_fail;
7337c478bd9Sstevel@tonic-gate 
734fea9cb91Slq150181 		tem_register_modechg_cb(wscons.wc_tem, wc_modechg_cb,
735fea9cb91Slq150181 			(tem_modechg_cb_arg_t)&wscons);
736fea9cb91Slq150181 
7377c478bd9Sstevel@tonic-gate 		/*
7387c478bd9Sstevel@tonic-gate 		 * Refresh terminal size with info from terminal emulator.
7397c478bd9Sstevel@tonic-gate 		 */
7407c478bd9Sstevel@tonic-gate 		wc_get_size(&wscons);
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		/*
7437c478bd9Sstevel@tonic-gate 		 * ... and succeed.
7447c478bd9Sstevel@tonic-gate 		 */
7457c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate open_fail:
7487c478bd9Sstevel@tonic-gate 		qreply(q, mp);
7497c478bd9Sstevel@tonic-gate 		break;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	case WC_CLOSE_FB:
7527c478bd9Sstevel@tonic-gate 		/*
7537c478bd9Sstevel@tonic-gate 		 * There's nothing that can call this, so it's not
7547c478bd9Sstevel@tonic-gate 		 * really implemented.
7557c478bd9Sstevel@tonic-gate 		 */
7567c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
7577c478bd9Sstevel@tonic-gate 		/*
7587c478bd9Sstevel@tonic-gate 		 * However, if it were implemented, it would clearly
7597c478bd9Sstevel@tonic-gate 		 * be root-only.
7607c478bd9Sstevel@tonic-gate 		 */
7617c478bd9Sstevel@tonic-gate 		if ((iocp->ioc_error = secpolicy_console(iocp->ioc_cr)) != 0)
7627c478bd9Sstevel@tonic-gate 			goto close_fail;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		iocp->ioc_error = EINVAL;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate close_fail:
7677c478bd9Sstevel@tonic-gate 		qreply(q, mp);
7687c478bd9Sstevel@tonic-gate 		break;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	default:
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 		/*
7737c478bd9Sstevel@tonic-gate 		 * The only way in which "ttycommon_ioctl" can fail is
7747c478bd9Sstevel@tonic-gate 		 * if the "ioctl" requires a response containing data
7757c478bd9Sstevel@tonic-gate 		 * to be returned to the user, and no mblk could be
7767c478bd9Sstevel@tonic-gate 		 * allocated for the data.  No such "ioctl" alters our
7777c478bd9Sstevel@tonic-gate 		 * state.  Thus, we always go ahead and do any
7787c478bd9Sstevel@tonic-gate 		 * state-changes the "ioctl" calls for.  If we couldn't
7797c478bd9Sstevel@tonic-gate 		 * allocate the data, "ttycommon_ioctl" has stashed the
7807c478bd9Sstevel@tonic-gate 		 * "ioctl" away safely, so we just call "qbufcall" to
7817c478bd9Sstevel@tonic-gate 		 * request that we be called back when we stand a
7827c478bd9Sstevel@tonic-gate 		 * better chance of allocating the data.
7837c478bd9Sstevel@tonic-gate 		 */
7847c478bd9Sstevel@tonic-gate 		datasize = ttycommon_ioctl(&wscons.wc_ttycommon, q, mp, &error);
7857c478bd9Sstevel@tonic-gate 		if (datasize != 0) {
7867c478bd9Sstevel@tonic-gate 			if (wscons.wc_bufcallid != 0)
7877c478bd9Sstevel@tonic-gate 				qunbufcall(q, wscons.wc_bufcallid);
7887c478bd9Sstevel@tonic-gate 			wscons.wc_bufcallid = qbufcall(q, datasize, BPRI_HI,
7897c478bd9Sstevel@tonic-gate 			    wcreioctl, NULL);
7907c478bd9Sstevel@tonic-gate 			return;
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 		if (error < 0) {
7947c478bd9Sstevel@tonic-gate 			if (iocp->ioc_cmd == TCSBRK)
7957c478bd9Sstevel@tonic-gate 				error = 0;
7967c478bd9Sstevel@tonic-gate 			else
7977c478bd9Sstevel@tonic-gate 				error = EINVAL;
7987c478bd9Sstevel@tonic-gate 		}
7997c478bd9Sstevel@tonic-gate 		if (error != 0) {
8007c478bd9Sstevel@tonic-gate 			iocp->ioc_error = error;
8017c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCNAK;
8027c478bd9Sstevel@tonic-gate 		}
8037c478bd9Sstevel@tonic-gate 		qreply(q, mp);
8047c478bd9Sstevel@tonic-gate 		break;
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate /*
8097c478bd9Sstevel@tonic-gate  * This function gets the polled I/O structures from the lower
8107c478bd9Sstevel@tonic-gate  * keyboard driver.  If any initialization or resource allocation
8117c478bd9Sstevel@tonic-gate  * needs to be done by the lower driver, it will be done when
8127c478bd9Sstevel@tonic-gate  * the lower driver services this message.
8137c478bd9Sstevel@tonic-gate  */
8147c478bd9Sstevel@tonic-gate static void
8157c478bd9Sstevel@tonic-gate wc_open_kb_polledio(struct wscons *wscons, queue_t *q, mblk_t *mp)
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	mblk_t *mp2;
8187c478bd9Sstevel@tonic-gate 	struct iocblk *iocp;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_ALL,
8217c478bd9Sstevel@tonic-gate 		("wc_open_kb_polledio: sending CONSOPENPOLLEDIO\n"));
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	mp2 = mkiocb(CONSOPENPOLLEDIO);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	if (mp2 == NULL) {
8267c478bd9Sstevel@tonic-gate 		/*
8277c478bd9Sstevel@tonic-gate 		 * If we can't get an mblk, then wait for it.
8287c478bd9Sstevel@tonic-gate 		 */
8297c478bd9Sstevel@tonic-gate 		goto nomem;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	mp2->b_cont = allocb(sizeof (struct cons_polledio *), BPRI_HI);
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	if (mp2->b_cont == NULL) {
8357c478bd9Sstevel@tonic-gate 		/*
8367c478bd9Sstevel@tonic-gate 		 * If we can't get an mblk, then wait for it, and release
8377c478bd9Sstevel@tonic-gate 		 * the mblk that we have already allocated.
8387c478bd9Sstevel@tonic-gate 		 */
8397c478bd9Sstevel@tonic-gate 		freemsg(mp2);
8407c478bd9Sstevel@tonic-gate 		goto nomem;
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp2->b_rptr;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	iocp->ioc_count = sizeof (struct cons_polledio *);
8467c478bd9Sstevel@tonic-gate 	mp2->b_cont->b_wptr = mp2->b_cont->b_rptr +
8477c478bd9Sstevel@tonic-gate 		sizeof (struct cons_polledio *);
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	wscons->wc_pending_link = mp;
8507c478bd9Sstevel@tonic-gate 	wscons->wc_kb_getpolledio_id = iocp->ioc_id;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	putnext(wscons->wc_kbdqueue, mp2);
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	return;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate nomem:
8577c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
8587c478bd9Sstevel@tonic-gate 	iocp->ioc_error = ENOMEM;
8597c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_IOCNAK;
8607c478bd9Sstevel@tonic-gate 	qreply(q, mp);
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate /*
8647c478bd9Sstevel@tonic-gate  * This function releases the polled I/O structures from the lower
8657c478bd9Sstevel@tonic-gate  * keyboard driver.  If any de-initialization needs to be done, or
8667c478bd9Sstevel@tonic-gate  * any resources need to be released, it will be done when the lower
8677c478bd9Sstevel@tonic-gate  * driver services this message.
8687c478bd9Sstevel@tonic-gate  */
8697c478bd9Sstevel@tonic-gate static void
8707c478bd9Sstevel@tonic-gate wc_close_kb_polledio(struct wscons *wscons, queue_t *q, mblk_t *mp)
8717c478bd9Sstevel@tonic-gate {
8727c478bd9Sstevel@tonic-gate 	mblk_t *mp2;
8737c478bd9Sstevel@tonic-gate 	struct iocblk *iocp;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_ALL,
8767c478bd9Sstevel@tonic-gate 		("wc_close_kb_polledio: sending CONSCLOSEPOLLEDIO\n"));
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	mp2 = mkiocb(CONSCLOSEPOLLEDIO);
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	if (mp2 == NULL) {
8817c478bd9Sstevel@tonic-gate 		/*
8827c478bd9Sstevel@tonic-gate 		 * If we can't get an mblk, then wait for it.
8837c478bd9Sstevel@tonic-gate 		 */
8847c478bd9Sstevel@tonic-gate 		goto nomem;
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	mp2->b_cont = allocb(sizeof (struct cons_polledio *), BPRI_HI);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	if (mp2->b_cont == NULL) {
8907c478bd9Sstevel@tonic-gate 		/*
8917c478bd9Sstevel@tonic-gate 		 * If we can't get an mblk, then wait for it, and release
8927c478bd9Sstevel@tonic-gate 		 * the mblk that we have already allocated.
8937c478bd9Sstevel@tonic-gate 		 */
8947c478bd9Sstevel@tonic-gate 		freemsg(mp2);
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 		goto nomem;
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp2->b_rptr;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	iocp->ioc_count = 0;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	wscons->wc_pending_link = mp;
9047c478bd9Sstevel@tonic-gate 	wscons->wc_kb_getpolledio_id = iocp->ioc_id;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	putnext(wscons->wc_kbdqueue, mp2);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	return;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate nomem:
9117c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
9127c478bd9Sstevel@tonic-gate 	iocp->ioc_error = ENOMEM;
9137c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_IOCNAK;
9147c478bd9Sstevel@tonic-gate 	qreply(q, mp);
9157c478bd9Sstevel@tonic-gate }
9167c478bd9Sstevel@tonic-gate 
917fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE
9187c478bd9Sstevel@tonic-gate /* ARGSUSED */
9197c478bd9Sstevel@tonic-gate static void
9207c478bd9Sstevel@tonic-gate wcopoll(void *arg)
9217c478bd9Sstevel@tonic-gate {
9227c478bd9Sstevel@tonic-gate 	queue_t *q;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	q = wscons.wc_ttycommon.t_writeq;
9257c478bd9Sstevel@tonic-gate 	wscons.wc_timeoutid = 0;
9267c478bd9Sstevel@tonic-gate 	/* See if we can continue output */
9277c478bd9Sstevel@tonic-gate 	if ((wscons.wc_flags & WCS_BUSY) && wscons.wc_pendc != -1) {
9287c478bd9Sstevel@tonic-gate 		if (prom_mayput((char)wscons.wc_pendc) == 0) {
9297c478bd9Sstevel@tonic-gate 			wscons.wc_pendc = -1;
9307c478bd9Sstevel@tonic-gate 			wscons.wc_flags &= ~WCS_BUSY;
9317c478bd9Sstevel@tonic-gate 			if (!(wscons.wc_flags&(WCS_DELAY|WCS_STOPPED)))
9327c478bd9Sstevel@tonic-gate 				wcstart();
9337c478bd9Sstevel@tonic-gate 		} else
9347c478bd9Sstevel@tonic-gate 			wscons.wc_timeoutid = qtimeout(q, wcopoll, NULL, 1);
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate }
937fea9cb91Slq150181 #endif	/* _HAVE_TEM_FIRMWARE */
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate /*
9407c478bd9Sstevel@tonic-gate  * Restart output on the console after a timeout.
9417c478bd9Sstevel@tonic-gate  */
9427c478bd9Sstevel@tonic-gate /* ARGSUSED */
9437c478bd9Sstevel@tonic-gate static void
9447c478bd9Sstevel@tonic-gate wcrstrt(void *arg)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate 	ASSERT(wscons.wc_ttycommon.t_writeq != NULL);
9477c478bd9Sstevel@tonic-gate 	wscons.wc_flags &= ~WCS_DELAY;
9487c478bd9Sstevel@tonic-gate 	wcstart();
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate /*
9527c478bd9Sstevel@tonic-gate  * Start console output
9537c478bd9Sstevel@tonic-gate  */
9547c478bd9Sstevel@tonic-gate static void
9557c478bd9Sstevel@tonic-gate wcstart(void)
9567c478bd9Sstevel@tonic-gate {
957fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE
9587c478bd9Sstevel@tonic-gate 	int c;
9597c478bd9Sstevel@tonic-gate 	ssize_t cc;
960fea9cb91Slq150181 #endif /* _HAVE_TEM_FIRMWARE */
9617c478bd9Sstevel@tonic-gate 	queue_t *q;
9627c478bd9Sstevel@tonic-gate 	mblk_t *bp;
9637c478bd9Sstevel@tonic-gate 	mblk_t *nbp;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	/*
9667c478bd9Sstevel@tonic-gate 	 * If we're waiting for something to happen (delay timeout to
9677c478bd9Sstevel@tonic-gate 	 * expire, current transmission to finish, output to be
9687c478bd9Sstevel@tonic-gate 	 * restarted, output to finish draining), don't grab anything
9697c478bd9Sstevel@tonic-gate 	 * new.
9707c478bd9Sstevel@tonic-gate 	 */
9717c478bd9Sstevel@tonic-gate 	if (wscons.wc_flags & (WCS_DELAY|WCS_BUSY|WCS_STOPPED))
972fea9cb91Slq150181 		return;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	q = wscons.wc_ttycommon.t_writeq;
9757c478bd9Sstevel@tonic-gate 	/*
9767c478bd9Sstevel@tonic-gate 	 * assumes that we have been called by whoever holds the
9777c478bd9Sstevel@tonic-gate 	 * exclusionary lock on the write-side queue (protects
9787c478bd9Sstevel@tonic-gate 	 * wc_flags and wc_pendc).
9797c478bd9Sstevel@tonic-gate 	 */
9807c478bd9Sstevel@tonic-gate 	for (;;) {
9817c478bd9Sstevel@tonic-gate 		if ((bp = getq(q)) == NULL)
982fea9cb91Slq150181 			return;	/* nothing to transmit */
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 		/*
9857c478bd9Sstevel@tonic-gate 		 * We have a new message to work on.
9867c478bd9Sstevel@tonic-gate 		 * Check whether it's a delay or an ioctl (the latter
9877c478bd9Sstevel@tonic-gate 		 * occurs if the ioctl in question was waiting for the output
9887c478bd9Sstevel@tonic-gate 		 * to drain).  If it's one of those, process it immediately.
9897c478bd9Sstevel@tonic-gate 		 */
9907c478bd9Sstevel@tonic-gate 		switch (bp->b_datap->db_type) {
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 		case M_DELAY:
9937c478bd9Sstevel@tonic-gate 			/*
9947c478bd9Sstevel@tonic-gate 			 * Arrange for "wcrstrt" to be called when the
9957c478bd9Sstevel@tonic-gate 			 * delay expires; it will turn WCS_DELAY off,
9967c478bd9Sstevel@tonic-gate 			 * and call "wcstart" to grab the next message.
9977c478bd9Sstevel@tonic-gate 			 */
9987c478bd9Sstevel@tonic-gate 			if (wscons.wc_timeoutid != 0)
9997c478bd9Sstevel@tonic-gate 				(void) quntimeout(q, wscons.wc_timeoutid);
10007c478bd9Sstevel@tonic-gate 			wscons.wc_timeoutid = qtimeout(q, wcrstrt, NULL,
10017c478bd9Sstevel@tonic-gate 			    (clock_t)(*(unsigned char *)bp->b_rptr + 6));
10027c478bd9Sstevel@tonic-gate 			wscons.wc_flags |= WCS_DELAY;
10037c478bd9Sstevel@tonic-gate 			freemsg(bp);
1004fea9cb91Slq150181 			return;	/* wait for this to finish */
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 		case M_IOCTL:
10077c478bd9Sstevel@tonic-gate 			/*
10087c478bd9Sstevel@tonic-gate 			 * This ioctl was waiting for the output ahead of
10097c478bd9Sstevel@tonic-gate 			 * it to drain; obviously, it has.  Do it, and
10107c478bd9Sstevel@tonic-gate 			 * then grab the next message after it.
10117c478bd9Sstevel@tonic-gate 			 */
10127c478bd9Sstevel@tonic-gate 			wcioctl(q, bp);
10137c478bd9Sstevel@tonic-gate 			continue;
10147c478bd9Sstevel@tonic-gate 		}
10157c478bd9Sstevel@tonic-gate 
1016fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE
1017fea9cb91Slq150181 		if (consmode == CONS_KFB) {
1018fea9cb91Slq150181 #endif /* _HAVE_TEM_FIRMWARE */
10197c478bd9Sstevel@tonic-gate 			if (wscons.wc_tem != NULL) {
10207c478bd9Sstevel@tonic-gate 				for (nbp = bp; nbp != NULL; nbp = nbp->b_cont) {
10217c478bd9Sstevel@tonic-gate 					if (nbp->b_wptr > nbp->b_rptr) {
10227c478bd9Sstevel@tonic-gate 						(void) tem_write(wscons.wc_tem,
1023fea9cb91Slq150181 						    nbp->b_rptr,
1024fea9cb91Slq150181 						    nbp->b_wptr - nbp->b_rptr,
10257c478bd9Sstevel@tonic-gate 						    kcred);
10267c478bd9Sstevel@tonic-gate 					}
10277c478bd9Sstevel@tonic-gate 				}
10287c478bd9Sstevel@tonic-gate 				freemsg(bp);
1029fea9cb91Slq150181 			}
1030fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE
1031fea9cb91Slq150181 			continue;
1032fea9cb91Slq150181 		}
1033fea9cb91Slq150181 
1034fea9cb91Slq150181 		/* consmode = CONS_FW */
10357c478bd9Sstevel@tonic-gate 		if ((cc = bp->b_wptr - bp->b_rptr) == 0) {
10367c478bd9Sstevel@tonic-gate 			freemsg(bp);
10377c478bd9Sstevel@tonic-gate 			continue;
10387c478bd9Sstevel@tonic-gate 		}
10397c478bd9Sstevel@tonic-gate 		/*
10407c478bd9Sstevel@tonic-gate 		 * Direct output to the frame buffer if this device
10417c478bd9Sstevel@tonic-gate 		 * is not the "hardware" console.
10427c478bd9Sstevel@tonic-gate 		 */
10437c478bd9Sstevel@tonic-gate 		if (wscons.wc_defer_output) {
10447c478bd9Sstevel@tonic-gate 			/*
10457c478bd9Sstevel@tonic-gate 			 * Never do output here;
10467c478bd9Sstevel@tonic-gate 			 * it takes forever.
10477c478bd9Sstevel@tonic-gate 			 */
10487c478bd9Sstevel@tonic-gate 			wscons.wc_flags |= WCS_BUSY;
10497c478bd9Sstevel@tonic-gate 			wscons.wc_pendc = -1;
10507c478bd9Sstevel@tonic-gate 			(void) putbq(q, bp);
10517c478bd9Sstevel@tonic-gate 			if (q->q_count > 128) { /* do it soon */
10527c478bd9Sstevel@tonic-gate 				softcall(wconsout, NULL);
10537c478bd9Sstevel@tonic-gate 			} else {	/* wait a bit */
10547c478bd9Sstevel@tonic-gate 				if (wscons.wc_timeoutid != 0)
10557c478bd9Sstevel@tonic-gate 					(void) quntimeout(q,
10567c478bd9Sstevel@tonic-gate 					    wscons.wc_timeoutid);
10577c478bd9Sstevel@tonic-gate 				wscons.wc_timeoutid = qtimeout(q, wconsout,
10587c478bd9Sstevel@tonic-gate 				    NULL, hz / 30);
10597c478bd9Sstevel@tonic-gate 			}
1060fea9cb91Slq150181 			return;
10617c478bd9Sstevel@tonic-gate 		}
10627c478bd9Sstevel@tonic-gate 		for (;;) {
10637c478bd9Sstevel@tonic-gate 			c = *bp->b_rptr++;
10647c478bd9Sstevel@tonic-gate 			cc--;
10657c478bd9Sstevel@tonic-gate 			if (prom_mayput((char)c) != 0) {
10667c478bd9Sstevel@tonic-gate 				wscons.wc_flags |= WCS_BUSY;
10677c478bd9Sstevel@tonic-gate 				wscons.wc_pendc = c;
10687c478bd9Sstevel@tonic-gate 				if (wscons.wc_timeoutid != 0)
10697c478bd9Sstevel@tonic-gate 					(void) quntimeout(q,
10707c478bd9Sstevel@tonic-gate 					    wscons.wc_timeoutid);
10717c478bd9Sstevel@tonic-gate 				wscons.wc_timeoutid = qtimeout(q, wcopoll,
10727c478bd9Sstevel@tonic-gate 				    NULL, 1);
10737c478bd9Sstevel@tonic-gate 				if (bp != NULL)
10747c478bd9Sstevel@tonic-gate 					/* not done with this message yet */
10757c478bd9Sstevel@tonic-gate 					(void) putbq(q, bp);
1076fea9cb91Slq150181 				return;
10777c478bd9Sstevel@tonic-gate 			}
10787c478bd9Sstevel@tonic-gate 			while (cc <= 0) {
10797c478bd9Sstevel@tonic-gate 				nbp = bp;
10807c478bd9Sstevel@tonic-gate 				bp = bp->b_cont;
10817c478bd9Sstevel@tonic-gate 				freeb(nbp);
10827c478bd9Sstevel@tonic-gate 				if (bp == NULL)
1083fea9cb91Slq150181 					return;
10847c478bd9Sstevel@tonic-gate 				cc = bp->b_wptr - bp->b_rptr;
10857c478bd9Sstevel@tonic-gate 			}
10867c478bd9Sstevel@tonic-gate 		}
1087fea9cb91Slq150181 #endif /* _HAVE_TEM_FIRMWARE */
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate 
1091fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE
10927c478bd9Sstevel@tonic-gate /*
10937c478bd9Sstevel@tonic-gate  * Output to frame buffer console.
10947c478bd9Sstevel@tonic-gate  * It takes a long time to scroll.
10957c478bd9Sstevel@tonic-gate  */
10967c478bd9Sstevel@tonic-gate /* ARGSUSED */
10977c478bd9Sstevel@tonic-gate static void
10987c478bd9Sstevel@tonic-gate wconsout(void *dummy)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate 	uchar_t *cp;
11017c478bd9Sstevel@tonic-gate 	ssize_t cc;
11027c478bd9Sstevel@tonic-gate 	queue_t *q;
11037c478bd9Sstevel@tonic-gate 	mblk_t *bp;
11047c478bd9Sstevel@tonic-gate 	mblk_t *nbp;
11057c478bd9Sstevel@tonic-gate 	char *current_position;
11067c478bd9Sstevel@tonic-gate 	ssize_t bytes_left;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	if ((q = wscons.wc_ttycommon.t_writeq) == NULL) {
11097c478bd9Sstevel@tonic-gate 		return;	/* not attached to a stream */
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	/*
11137c478bd9Sstevel@tonic-gate 	 * Set up to copy up to MAXHIWAT bytes.
11147c478bd9Sstevel@tonic-gate 	 */
11157c478bd9Sstevel@tonic-gate 	current_position = &obuf[0];
11167c478bd9Sstevel@tonic-gate 	bytes_left = MAXHIWAT;
11177c478bd9Sstevel@tonic-gate 	while ((bp = getq(q)) != NULL) {
11187c478bd9Sstevel@tonic-gate 		if (bp->b_datap->db_type == M_IOCTL) {
11197c478bd9Sstevel@tonic-gate 			/*
11207c478bd9Sstevel@tonic-gate 			 * This ioctl was waiting for the output ahead of
11217c478bd9Sstevel@tonic-gate 			 * it to drain; obviously, it has.  Put it back
11227c478bd9Sstevel@tonic-gate 			 * so that "wcstart" can handle it, and transmit
11237c478bd9Sstevel@tonic-gate 			 * what we've got.
11247c478bd9Sstevel@tonic-gate 			 */
11257c478bd9Sstevel@tonic-gate 			(void) putbq(q, bp);
11267c478bd9Sstevel@tonic-gate 			goto transmit;
11277c478bd9Sstevel@tonic-gate 		}
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 		do {
11307c478bd9Sstevel@tonic-gate 			cp = bp->b_rptr;
11317c478bd9Sstevel@tonic-gate 			cc = bp->b_wptr - cp;
11327c478bd9Sstevel@tonic-gate 			while (cc != 0) {
11337c478bd9Sstevel@tonic-gate 				if (bytes_left == 0) {
11347c478bd9Sstevel@tonic-gate 					/*
11357c478bd9Sstevel@tonic-gate 					 * Out of buffer space; put this
11367c478bd9Sstevel@tonic-gate 					 * buffer back on the queue, and
11377c478bd9Sstevel@tonic-gate 					 * transmit what we have.
11387c478bd9Sstevel@tonic-gate 					 */
11397c478bd9Sstevel@tonic-gate 					bp->b_rptr = cp;
11407c478bd9Sstevel@tonic-gate 					(void) putbq(q, bp);
11417c478bd9Sstevel@tonic-gate 					goto transmit;
11427c478bd9Sstevel@tonic-gate 				}
11437c478bd9Sstevel@tonic-gate 				*current_position++ = *cp++;
11447c478bd9Sstevel@tonic-gate 				cc--;
11457c478bd9Sstevel@tonic-gate 				bytes_left--;
11467c478bd9Sstevel@tonic-gate 			}
11477c478bd9Sstevel@tonic-gate 			nbp = bp;
11487c478bd9Sstevel@tonic-gate 			bp = bp->b_cont;
11497c478bd9Sstevel@tonic-gate 			freeb(nbp);
11507c478bd9Sstevel@tonic-gate 		} while (bp != NULL);
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate transmit:
11547c478bd9Sstevel@tonic-gate 	if ((cc = MAXHIWAT - bytes_left) != 0)
11557c478bd9Sstevel@tonic-gate 		console_puts(obuf, cc);
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	wscons.wc_flags &= ~WCS_BUSY;
11587c478bd9Sstevel@tonic-gate 	wcstart();
11597c478bd9Sstevel@tonic-gate }
1160fea9cb91Slq150181 #endif /* _HAVE_TEM_FIRMWARE */
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate /*
11637c478bd9Sstevel@tonic-gate  * Put procedure for lower read queue.
11647c478bd9Sstevel@tonic-gate  * Pass everything up to queue above "upper half".
11657c478bd9Sstevel@tonic-gate  */
11667c478bd9Sstevel@tonic-gate static int
11677c478bd9Sstevel@tonic-gate wclrput(queue_t *q, mblk_t *mp)
11687c478bd9Sstevel@tonic-gate {
11697c478bd9Sstevel@tonic-gate 	queue_t *upq;
11707c478bd9Sstevel@tonic-gate 	struct iocblk *iocp;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_ALL,
11737c478bd9Sstevel@tonic-gate 		("wclrput: wclrput type = 0x%x\n", mp->b_datap->db_type));
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	case M_FLUSH:
11787c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHW || *mp->b_rptr == FLUSHRW) {
11797c478bd9Sstevel@tonic-gate 			/*
11807c478bd9Sstevel@tonic-gate 			 * Flush our write queue.
11817c478bd9Sstevel@tonic-gate 			 */
11827c478bd9Sstevel@tonic-gate 			/* XXX doesn't flush M_DELAY */
11837c478bd9Sstevel@tonic-gate 			flushq(WR(q), FLUSHDATA);
11847c478bd9Sstevel@tonic-gate 			*mp->b_rptr = FLUSHR;	/* it has been flushed */
11857c478bd9Sstevel@tonic-gate 		}
11867c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHR || *mp->b_rptr == FLUSHRW) {
11877c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
11887c478bd9Sstevel@tonic-gate 			*mp->b_rptr = FLUSHW;	/* it has been flushed */
11897c478bd9Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
11907c478bd9Sstevel@tonic-gate 		} else
11917c478bd9Sstevel@tonic-gate 			freemsg(mp);
11927c478bd9Sstevel@tonic-gate 		break;
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	case M_DATA:
11957c478bd9Sstevel@tonic-gate 		if ((upq = wscons.wc_ttycommon.t_readq) != NULL) {
11967c478bd9Sstevel@tonic-gate 			if (!canput(upq->q_next)) {
11977c478bd9Sstevel@tonic-gate 				ttycommon_qfull(&wscons.wc_ttycommon, upq);
11987c478bd9Sstevel@tonic-gate 				wcstart();
11997c478bd9Sstevel@tonic-gate 				freemsg(mp);
12007c478bd9Sstevel@tonic-gate 			} else
12017c478bd9Sstevel@tonic-gate 				putnext(upq, mp);
12027c478bd9Sstevel@tonic-gate 		} else
12037c478bd9Sstevel@tonic-gate 			freemsg(mp);
12047c478bd9Sstevel@tonic-gate 		break;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	case M_IOCACK:
12077c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
12087c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
12097c478bd9Sstevel@tonic-gate 		if (wscons.wc_pending_link != NULL &&
12107c478bd9Sstevel@tonic-gate 		    iocp->ioc_id == wscons.wc_kb_getpolledio_id) {
12117c478bd9Sstevel@tonic-gate 			switch (mp->b_datap->db_type) {
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 			case M_IOCACK:
12147c478bd9Sstevel@tonic-gate 				switch (iocp->ioc_cmd) {
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 				case CONSOPENPOLLEDIO:
12187c478bd9Sstevel@tonic-gate 					DPRINTF(PRINT_L1, PRINT_MASK_ALL,
12197c478bd9Sstevel@tonic-gate 						("wclrput: "
12207c478bd9Sstevel@tonic-gate 						"ACK CONSOPENPOLLEDIO\n"));
12217c478bd9Sstevel@tonic-gate 					wscons.wc_kb_polledio =
12227c478bd9Sstevel@tonic-gate 					    *(struct cons_polledio **)
12237c478bd9Sstevel@tonic-gate 					    mp->b_cont->b_rptr;
1224fea9cb91Slq150181 					wscons.wc_polledio.
1225fea9cb91Slq150181 					    cons_polledio_getchar =
1226fea9cb91Slq150181 						wc_polled_getchar;
1227fea9cb91Slq150181 					wscons.wc_polledio.
1228fea9cb91Slq150181 					    cons_polledio_ischar =
1229fea9cb91Slq150181 						wc_polled_ischar;
12307c478bd9Sstevel@tonic-gate 					break;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 				case CONSCLOSEPOLLEDIO:
12337c478bd9Sstevel@tonic-gate 					DPRINTF(PRINT_L1, PRINT_MASK_ALL,
12347c478bd9Sstevel@tonic-gate 						("wclrput: "
12357c478bd9Sstevel@tonic-gate 						"ACK CONSCLOSEPOLLEDIO\n"));
12367c478bd9Sstevel@tonic-gate 					wscons.wc_kb_polledio = NULL;
12377c478bd9Sstevel@tonic-gate 					wscons.wc_kbdqueue = NULL;
1238fea9cb91Slq150181 					wscons.wc_polledio.
1239fea9cb91Slq150181 					    cons_polledio_getchar = NULL;
1240fea9cb91Slq150181 					wscons.wc_polledio.
1241fea9cb91Slq150181 					    cons_polledio_ischar = NULL;
12427c478bd9Sstevel@tonic-gate 					break;
12437c478bd9Sstevel@tonic-gate 				default:
12447c478bd9Sstevel@tonic-gate 					DPRINTF(PRINT_L1, PRINT_MASK_ALL,
12457c478bd9Sstevel@tonic-gate 						("wclrput: "
12467c478bd9Sstevel@tonic-gate 						"ACK UNKNOWN\n"));
12477c478bd9Sstevel@tonic-gate 				}
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 				break;
12507c478bd9Sstevel@tonic-gate 			case M_IOCNAK:
12517c478bd9Sstevel@tonic-gate 				/*
12527c478bd9Sstevel@tonic-gate 				 * Keyboard may or may not support polled I/O.
12537c478bd9Sstevel@tonic-gate 				 * This ioctl may have been rejected because
12547c478bd9Sstevel@tonic-gate 				 * we only have the wc->conskbd chain built,
12557c478bd9Sstevel@tonic-gate 				 * and the keyboard driver has not been linked
12567c478bd9Sstevel@tonic-gate 				 * underneath conskbd yet.
12577c478bd9Sstevel@tonic-gate 				 */
12587c478bd9Sstevel@tonic-gate 				DPRINTF(PRINT_L1, PRINT_MASK_ALL,
12597c478bd9Sstevel@tonic-gate 					("wclrput: NAK\n"));
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 				switch (iocp->ioc_cmd) {
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 				case CONSCLOSEPOLLEDIO:
12647c478bd9Sstevel@tonic-gate 					wscons.wc_kb_polledio = NULL;
12657c478bd9Sstevel@tonic-gate 					wscons.wc_kbdqueue = NULL;
1266fea9cb91Slq150181 					wscons.wc_polledio.
1267fea9cb91Slq150181 					    cons_polledio_getchar = NULL;
1268fea9cb91Slq150181 					wscons.wc_polledio.
1269fea9cb91Slq150181 					    cons_polledio_ischar = NULL;
12707c478bd9Sstevel@tonic-gate 					break;
12717c478bd9Sstevel@tonic-gate 				}
12727c478bd9Sstevel@tonic-gate 				break;
12737c478bd9Sstevel@tonic-gate 			}
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 			/*
12767c478bd9Sstevel@tonic-gate 			 * Discard the response, replace it with the
12777c478bd9Sstevel@tonic-gate 			 * pending response to the I_PLINK, then let it
12787c478bd9Sstevel@tonic-gate 			 * flow upward.
12797c478bd9Sstevel@tonic-gate 			 */
12807c478bd9Sstevel@tonic-gate 			freemsg(mp);
12817c478bd9Sstevel@tonic-gate 			mp = wscons.wc_pending_link;
12827c478bd9Sstevel@tonic-gate 			wscons.wc_pending_link = NULL;
12837c478bd9Sstevel@tonic-gate 			wscons.wc_kb_getpolledio_id = 0;
12847c478bd9Sstevel@tonic-gate 		}
12857c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	default:	/* inc M_ERROR, M_HANGUP, M_IOCACK, M_IOCNAK, ... */
12887c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
12897c478bd9Sstevel@tonic-gate 			("wclrput: Message DISCARDED\n"));
12907c478bd9Sstevel@tonic-gate 		if ((upq = wscons.wc_ttycommon.t_readq) != NULL) {
12917c478bd9Sstevel@tonic-gate 			putnext(upq, mp);
12927c478bd9Sstevel@tonic-gate 		} else {
12937c478bd9Sstevel@tonic-gate 			freemsg(mp);
12947c478bd9Sstevel@tonic-gate 		}
12957c478bd9Sstevel@tonic-gate 		break;
12967c478bd9Sstevel@tonic-gate 	}
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	return (0);
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate /*
13027c478bd9Sstevel@tonic-gate  * Auxiliary routines, for allowing the workstation console to be redirected.
13037c478bd9Sstevel@tonic-gate  */
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate /*
13067c478bd9Sstevel@tonic-gate  * Given a minor device number for a wscons instance, return a held vnode for
13077c478bd9Sstevel@tonic-gate  * it.
13087c478bd9Sstevel@tonic-gate  *
13097c478bd9Sstevel@tonic-gate  * We currently support only one instance, for the "workstation console".
13107c478bd9Sstevel@tonic-gate  */
13117c478bd9Sstevel@tonic-gate int
13127c478bd9Sstevel@tonic-gate wcvnget(minor_t unit, vnode_t **vpp)
13137c478bd9Sstevel@tonic-gate {
13147c478bd9Sstevel@tonic-gate 	if (unit != 0 || rwsconsvp == NULL)
13157c478bd9Sstevel@tonic-gate 		return (ENXIO);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	/*
13187c478bd9Sstevel@tonic-gate 	 * rwsconsvp is already held, so we don't have to do it here.
13197c478bd9Sstevel@tonic-gate 	 */
13207c478bd9Sstevel@tonic-gate 	*vpp = rwsconsvp;
13217c478bd9Sstevel@tonic-gate 	return (0);
13227c478bd9Sstevel@tonic-gate }
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate /*
13257c478bd9Sstevel@tonic-gate  * Release the vnode that wcvnget returned.
13267c478bd9Sstevel@tonic-gate  */
13277c478bd9Sstevel@tonic-gate /* ARGSUSED */
13287c478bd9Sstevel@tonic-gate void
13297c478bd9Sstevel@tonic-gate wcvnrele(minor_t unit, vnode_t *vp)
13307c478bd9Sstevel@tonic-gate {
13317c478bd9Sstevel@tonic-gate 	/*
13327c478bd9Sstevel@tonic-gate 	 * Nothing to do, since we only support the workstation console
13337c478bd9Sstevel@tonic-gate 	 * instance that's held throughout the system's lifetime.
13347c478bd9Sstevel@tonic-gate 	 */
13357c478bd9Sstevel@tonic-gate }
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate /*
13387c478bd9Sstevel@tonic-gate  * The declaration and initialization of the wscons_srvnops has been
13397c478bd9Sstevel@tonic-gate  * moved to space.c to allow "wc" to become a loadable module.
13407c478bd9Sstevel@tonic-gate  */
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate /*
1343fea9cb91Slq150181  * These are for systems without OBP, and for devices that cannot be
1344fea9cb91Slq150181  * shared between Solaris and the OBP.
13457c478bd9Sstevel@tonic-gate  */
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate static void
1348*281f0747Slt200341 wc_polled_putchar(cons_polledio_arg_t arg, unsigned char c)
13497c478bd9Sstevel@tonic-gate {
13507c478bd9Sstevel@tonic-gate 	if (c == '\n')
1351fea9cb91Slq150181 		wc_polled_putchar(arg, '\r');
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	if (wscons.wc_tem == NULL) {
13547c478bd9Sstevel@tonic-gate 		/*
13557c478bd9Sstevel@tonic-gate 		 * We have no terminal emulator configured.  We have no
13567c478bd9Sstevel@tonic-gate 		 * recourse but to drop the output on the floor.
13577c478bd9Sstevel@tonic-gate 		 */
13587c478bd9Sstevel@tonic-gate 		return;
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate 
1361fea9cb91Slq150181 	tem_polled_write(wscons.wc_tem, &c, 1);
13627c478bd9Sstevel@tonic-gate }
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate /*
13657c478bd9Sstevel@tonic-gate  * These are for systems without OBP, and for devices that cannot be
13667c478bd9Sstevel@tonic-gate  * shared between Solaris and the OBP.
13677c478bd9Sstevel@tonic-gate  */
13687c478bd9Sstevel@tonic-gate static int
1369*281f0747Slt200341 wc_polled_getchar(cons_polledio_arg_t arg)
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate 	struct wscons *wscons = (struct wscons *)arg;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	if (wscons->wc_kb_polledio == NULL) {
13747c478bd9Sstevel@tonic-gate 		prom_printf("wscons:  getchar with no keyboard support");
13757c478bd9Sstevel@tonic-gate 		prom_printf("Halted...");
13767c478bd9Sstevel@tonic-gate 		for (;;)
13777c478bd9Sstevel@tonic-gate 			/* HANG FOREVER */;
13787c478bd9Sstevel@tonic-gate 	}
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	return (wscons->wc_kb_polledio->cons_polledio_getchar(
13817c478bd9Sstevel@tonic-gate 	    wscons->wc_kb_polledio->cons_polledio_argument));
13827c478bd9Sstevel@tonic-gate }
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate static boolean_t
1385*281f0747Slt200341 wc_polled_ischar(cons_polledio_arg_t arg)
13867c478bd9Sstevel@tonic-gate {
13877c478bd9Sstevel@tonic-gate 	struct wscons *wscons = (struct wscons *)arg;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	if (wscons->wc_kb_polledio == NULL)
13907c478bd9Sstevel@tonic-gate 		return (B_FALSE);
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	return (wscons->wc_kb_polledio->cons_polledio_ischar(
13937c478bd9Sstevel@tonic-gate 	    wscons->wc_kb_polledio->cons_polledio_argument));
13947c478bd9Sstevel@tonic-gate }
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate static void
1397*281f0747Slt200341 wc_polled_enter(cons_polledio_arg_t arg)
13987c478bd9Sstevel@tonic-gate {
13997c478bd9Sstevel@tonic-gate 	struct wscons *wscons = (struct wscons *)arg;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	if (wscons->wc_kb_polledio == NULL)
14027c478bd9Sstevel@tonic-gate 		return;
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	if (wscons->wc_kb_polledio->cons_polledio_enter != NULL) {
14057c478bd9Sstevel@tonic-gate 		wscons->wc_kb_polledio->cons_polledio_enter(
14067c478bd9Sstevel@tonic-gate 		    wscons->wc_kb_polledio->cons_polledio_argument);
14077c478bd9Sstevel@tonic-gate 	}
14087c478bd9Sstevel@tonic-gate }
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate static void
1411*281f0747Slt200341 wc_polled_exit(cons_polledio_arg_t arg)
14127c478bd9Sstevel@tonic-gate {
14137c478bd9Sstevel@tonic-gate 	struct wscons *wscons = (struct wscons *)arg;
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	if (wscons->wc_kb_polledio == NULL)
14167c478bd9Sstevel@tonic-gate 		return;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	if (wscons->wc_kb_polledio->cons_polledio_exit != NULL) {
14197c478bd9Sstevel@tonic-gate 		wscons->wc_kb_polledio->cons_polledio_exit(
14207c478bd9Sstevel@tonic-gate 		    wscons->wc_kb_polledio->cons_polledio_argument);
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate }
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate #ifdef DEBUG
14267c478bd9Sstevel@tonic-gate static void
14277c478bd9Sstevel@tonic-gate wc_dprintf(const char *fmt, ...)
14287c478bd9Sstevel@tonic-gate {
14297c478bd9Sstevel@tonic-gate 	char buf[256];
14307c478bd9Sstevel@tonic-gate 	va_list ap;
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
14337c478bd9Sstevel@tonic-gate 	(void) vsprintf(buf, fmt, ap);
14347c478bd9Sstevel@tonic-gate 	va_end(ap);
14357c478bd9Sstevel@tonic-gate 
1436fea9cb91Slq150181 	cmn_err(CE_WARN, "wc: %s", buf);
14377c478bd9Sstevel@tonic-gate }
14387c478bd9Sstevel@tonic-gate #endif
14397c478bd9Sstevel@tonic-gate 
1440fea9cb91Slq150181 static void
1441fea9cb91Slq150181 update_property(struct wscons *wscons, char *name, ushort_t value)
14427c478bd9Sstevel@tonic-gate {
1443fea9cb91Slq150181 	char data[8];
14447c478bd9Sstevel@tonic-gate 
1445fea9cb91Slq150181 	(void) snprintf(data, sizeof (data), "%u", value);
1446fea9cb91Slq150181 	(void) ddi_prop_update_string(wscons->wc_dev, wc_dip, name, data);
14477c478bd9Sstevel@tonic-gate }
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate /*
14507c478bd9Sstevel@tonic-gate  * Gets the number of text rows and columns and the
14517c478bd9Sstevel@tonic-gate  * width and height (in pixels) of the console.
14527c478bd9Sstevel@tonic-gate  */
14537c478bd9Sstevel@tonic-gate static void
14547c478bd9Sstevel@tonic-gate wc_get_size(struct wscons *wscons)
14557c478bd9Sstevel@tonic-gate {
1456fea9cb91Slq150181 	struct winsize *t = &wscons->wc_ttycommon.t_size;
1457fea9cb91Slq150181 	ushort_t r = LOSCREENLINES, c = LOSCREENCOLS, x = 0, y = 0;
14587c478bd9Sstevel@tonic-gate 
1459fea9cb91Slq150181 	if (wscons->wc_tem != NULL)
14607c478bd9Sstevel@tonic-gate 		tem_get_size(wscons->wc_tem, &r, &c, &x, &y);
1461fea9cb91Slq150181 #ifdef _HAVE_TEM_FIRMWARE
1462fea9cb91Slq150181 	else {
1463fea9cb91Slq150181 		console_get_size(&r, &c, &x, &y);
14647c478bd9Sstevel@tonic-gate 	}
1465fea9cb91Slq150181 #endif /* _HAVE_TEM_FIRMWARE */
1466fea9cb91Slq150181 
1467fea9cb91Slq150181 	update_property(wscons, "screen-#cols",  t->ws_col = c);
1468fea9cb91Slq150181 	update_property(wscons, "screen-#rows",  t->ws_row = r);
1469fea9cb91Slq150181 	update_property(wscons, "screen-width",  t->ws_xpixel = x);
1470fea9cb91Slq150181 	update_property(wscons, "screen-height", t->ws_ypixel = y);
1471fea9cb91Slq150181 }
1472fea9cb91Slq150181 
1473fea9cb91Slq150181 static void
1474fea9cb91Slq150181 wc_modechg_cb(tem_modechg_cb_arg_t arg)
1475fea9cb91Slq150181 {
1476fea9cb91Slq150181 	wc_get_size((struct wscons *)arg);
14777c478bd9Sstevel@tonic-gate }
1478