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