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