xref: /titanic_44/usr/src/uts/common/io/cons.c (revision ceeba6f9f0adf370c2a0f5c81a0d6ef1ba146cb4)
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 /*
23*ceeba6f9Srui zang - Sun Microsystems - Beijing China  * Copyright (c) 1982, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Indirect console driver for Sun.
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * Redirects all I/O to the device designated as the underlying "hardware"
307c478bd9Sstevel@tonic-gate  * console, as given by the value of rconsvp.  The implementation assumes that
317c478bd9Sstevel@tonic-gate  * rconsvp denotes a STREAMS device; the assumption is justified since
327c478bd9Sstevel@tonic-gate  * consoles must be capable of effecting tty semantics.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * rconsvp is set in autoconf.c:consconfig(), based on information obtained
357c478bd9Sstevel@tonic-gate  * from the EEPROM.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * XXX:	The driver still needs to be converted to use ANSI C consistently
387c478bd9Sstevel@tonic-gate  *	throughout.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/open.h>
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/systm.h>
457c478bd9Sstevel@tonic-gate #include <sys/signal.h>
467c478bd9Sstevel@tonic-gate #include <sys/cred.h>
477c478bd9Sstevel@tonic-gate #include <sys/user.h>
487c478bd9Sstevel@tonic-gate #include <sys/proc.h>
497c478bd9Sstevel@tonic-gate #include <sys/disp.h>
507c478bd9Sstevel@tonic-gate #include <sys/file.h>
517c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
527c478bd9Sstevel@tonic-gate #include <sys/log.h>
537c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
547c478bd9Sstevel@tonic-gate #include <sys/uio.h>
557c478bd9Sstevel@tonic-gate #include <sys/stat.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <sys/console.h>
587c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <sys/stream.h>
617c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
627c478bd9Sstevel@tonic-gate #include <sys/poll.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <sys/debug.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <sys/conf.h>
677c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
687c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
69*ceeba6f9Srui zang - Sun Microsystems - Beijing China #include <sys/vt.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static int cnopen(dev_t *, int, int, struct cred *);
727c478bd9Sstevel@tonic-gate static int cnclose(dev_t, int, int, struct cred *);
737c478bd9Sstevel@tonic-gate static int cnread(dev_t, struct uio *, struct cred *);
747c478bd9Sstevel@tonic-gate static int cnwrite(dev_t, struct uio *, struct cred *);
757c478bd9Sstevel@tonic-gate static int cnioctl(dev_t, int, intptr_t, int, struct cred *, int *);
767c478bd9Sstevel@tonic-gate static int cnpoll(dev_t, short, int, short *, struct pollhead **);
777c478bd9Sstevel@tonic-gate static int cn_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
787c478bd9Sstevel@tonic-gate static int cn_attach(dev_info_t *, ddi_attach_cmd_t);
797c478bd9Sstevel@tonic-gate static int cn_detach(dev_info_t *, ddi_detach_cmd_t);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static dev_info_t *cn_dip;		/* private copy of devinfo pointer */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static struct cb_ops cn_cb_ops = {
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	cnopen,			/* open */
867c478bd9Sstevel@tonic-gate 	cnclose,		/* close */
877c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
887c478bd9Sstevel@tonic-gate 	nodev,			/* print */
897c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
907c478bd9Sstevel@tonic-gate 	cnread,			/* read */
917c478bd9Sstevel@tonic-gate 	cnwrite,		/* write */
927c478bd9Sstevel@tonic-gate 	cnioctl,		/* ioctl */
937c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
947c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
957c478bd9Sstevel@tonic-gate 	nodev, 			/* segmap */
967c478bd9Sstevel@tonic-gate 	cnpoll,			/* poll */
977c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
987c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
997c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static struct dev_ops cn_ops = {
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1067c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
1077c478bd9Sstevel@tonic-gate 	cn_info,		/* info */
1087c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
1097c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
1107c478bd9Sstevel@tonic-gate 	cn_attach,		/* attach */
1117c478bd9Sstevel@tonic-gate 	cn_detach,		/* detach */
1127c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
1137c478bd9Sstevel@tonic-gate 	&cn_cb_ops,		/* driver operations */
11419397407SSherry Moore 	(struct bus_ops *)0,	/* bus operations */
11519397407SSherry Moore 	NULL,			/* power */
11619397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate };
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * Global variables associated with the console device:
1227c478bd9Sstevel@tonic-gate  *
1237c478bd9Sstevel@tonic-gate  * XXX:	There are too many of these!
124da6c28aaSamw  * moved to space.c to become resident in the kernel so that cons
1257c478bd9Sstevel@tonic-gate  * can be loadable.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate extern dev_t	rconsdev;	/* "hardware" console */
1297c478bd9Sstevel@tonic-gate extern vnode_t	*rconsvp;	/* pointer to vnode for that device */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * XXX: consulted in prsubr.c, for /proc entry point for obtaining ps info.
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate extern dev_t	uconsdev;	/* What the user thinks is the console device */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Private driver state:
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * The underlying console device potentially can be opened through (at least)
1427c478bd9Sstevel@tonic-gate  * two paths: through this driver and through the underlying device's driver.
1437c478bd9Sstevel@tonic-gate  * To ensure that reference counts are meaningful and therefore that close
1447c478bd9Sstevel@tonic-gate  * routines are called at the right time, it's important to make sure that
1457c478bd9Sstevel@tonic-gate  * rconsvp's s_count field (i.e., the count on the underlying device) never
1467c478bd9Sstevel@tonic-gate  * has a contribution of more than one through this driver, regardless of how
1477c478bd9Sstevel@tonic-gate  * many times this driver's been opened.  rconsopen keeps track of the
1487c478bd9Sstevel@tonic-gate  * necessary information to ensure this property.
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate static uint_t	rconsopen;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate #include <sys/types.h>
1547c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1557c478bd9Sstevel@tonic-gate #include <sys/param.h>
1567c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1577c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1587c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate extern int nodev(), nulldev();
1627c478bd9Sstevel@tonic-gate extern int dseekneg_flag;
1637c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1647c478bd9Sstevel@tonic-gate extern struct dev_ops cn_ops;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1717c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
17219397407SSherry Moore 	"Console redirection driver",
1737c478bd9Sstevel@tonic-gate 	&cn_ops,	/* driver ops */
1747c478bd9Sstevel@tonic-gate };
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1777c478bd9Sstevel@tonic-gate 	MODREV_1,
1787c478bd9Sstevel@tonic-gate 	&modldrv,
1797c478bd9Sstevel@tonic-gate 	NULL
1807c478bd9Sstevel@tonic-gate };
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate int
_init(void)1837c478bd9Sstevel@tonic-gate _init(void)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate int
_fini(void)1897c478bd9Sstevel@tonic-gate _fini(void)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	return (EBUSY);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1957c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate  * DDI glue routines
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate static int
cn_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2047c478bd9Sstevel@tonic-gate cn_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
2077c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "syscon", S_IFCHR,
2107c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
2117c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "systty", S_IFCHR,
2147c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
2157c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
2167c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "console", S_IFCHR,
2197c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
2207c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
2217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2227c478bd9Sstevel@tonic-gate 	}
223aecfc01dSrui zang - Sun Microsystems - Beijing China 
2247c478bd9Sstevel@tonic-gate 	cn_dip = devi;
2257c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate static int
cn_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)2297c478bd9Sstevel@tonic-gate cn_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
2327c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2337c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
2347c478bd9Sstevel@tonic-gate 	uconsdev = NODEV;
2357c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /* ARGSUSED */
2397c478bd9Sstevel@tonic-gate static int
cn_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2407c478bd9Sstevel@tonic-gate cn_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	switch (infocmd) {
2457c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2467c478bd9Sstevel@tonic-gate 		if (getminor((dev_t)arg) == 0 && cn_dip != NULL) {
2477c478bd9Sstevel@tonic-gate 			*result = (void *) cn_dip;
2487c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 		break;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2537c478bd9Sstevel@tonic-gate 		if (getminor((dev_t)arg) == 0) {
2547c478bd9Sstevel@tonic-gate 			*result = (void *)0;
2557c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 		break;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	default:
2607c478bd9Sstevel@tonic-gate 		break;
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	return (error);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * XXX	Caution: before allowing more than 256 minor devices on the
2687c478bd9Sstevel@tonic-gate  *	console, make sure you understand the 'compatibility' hack
2697c478bd9Sstevel@tonic-gate  *	in ufs_iget() that translates old dev_t's to new dev_t's.
2707c478bd9Sstevel@tonic-gate  *	See bugid 1098104 for the sordid details.
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /* ARGSUSED */
2747c478bd9Sstevel@tonic-gate static int
cnopen(dev_t * dev,int flag,int state,struct cred * cred)2757c478bd9Sstevel@tonic-gate cnopen(dev_t *dev, int flag, int state, struct cred *cred)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	int	err;
2787c478bd9Sstevel@tonic-gate 	static int	been_here;
2797c478bd9Sstevel@tonic-gate 	vnode_t	*vp = rconsvp;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	ASSERT(cred != NULL);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL)
2847c478bd9Sstevel@tonic-gate 		return (0);
2857c478bd9Sstevel@tonic-gate 
286361ed64aSJames Anderson 	/*
287361ed64aSJames Anderson 	 * Enable virtual console I/O for console logging if needed.
288361ed64aSJames Anderson 	 */
289361ed64aSJames Anderson 	if (vsconsvp != NULL && vsconsvp->v_stream == NULL) {
290361ed64aSJames Anderson 		if (VOP_OPEN(&vsconsvp, FREAD | FWRITE, cred, NULL) != 0) {
291361ed64aSJames Anderson 			cmn_err(CE_WARN, "cnopen: failed to open vsconsvp "
292361ed64aSJames Anderson 			    "for virtual console logging");
293361ed64aSJames Anderson 		}
294361ed64aSJames Anderson 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/*
2977c478bd9Sstevel@tonic-gate 	 * XXX: Clean up inactive PIDs from previous opens if any.
2987c478bd9Sstevel@tonic-gate 	 * These would have been created as a result of an I_SETSIG
2997c478bd9Sstevel@tonic-gate 	 * issued against console.  This is a workaround, and
3007c478bd9Sstevel@tonic-gate 	 * console driver must be correctly redesigned not to need
3017c478bd9Sstevel@tonic-gate 	 * this hook.
3027c478bd9Sstevel@tonic-gate 	 */
3037c478bd9Sstevel@tonic-gate 	if (vp->v_stream) {
3047c478bd9Sstevel@tonic-gate 		str_cn_clean(vp);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * XXX:	Set hook to tell /proc about underlying console.  (There's
3097c478bd9Sstevel@tonic-gate 	 *	gotta be a better way...)
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	if (state != OTYP_CHR || getminor(*dev) != 0)
3127c478bd9Sstevel@tonic-gate 		return (ENXIO);
3137c478bd9Sstevel@tonic-gate 	if (been_here == 0) {
3147c478bd9Sstevel@tonic-gate 		uconsdev = *dev;
3157c478bd9Sstevel@tonic-gate 		been_here = 1;
3167c478bd9Sstevel@tonic-gate 		if (vn_open("/dev/console", UIO_SYSSPACE, FWRITE | FNOCTTY,
3177c478bd9Sstevel@tonic-gate 		    0, &console_vnode, 0, 0) == 0)
3187c478bd9Sstevel@tonic-gate 			console_taskq = taskq_create("console_taskq",
3197c478bd9Sstevel@tonic-gate 			    1, maxclsyspri - 1, LOG_LOWAT / LOG_MSGSIZE,
3207c478bd9Sstevel@tonic-gate 			    LOG_HIWAT / LOG_MSGSIZE, TASKQ_PREPOPULATE);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
323da6c28aaSamw 	if ((err = VOP_OPEN(&vp, flag, cred, NULL)) != 0)
3247c478bd9Sstevel@tonic-gate 		return (err);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * The underlying driver is not allowed to have cloned itself
3287c478bd9Sstevel@tonic-gate 	 * for this open.
3297c478bd9Sstevel@tonic-gate 	 */
3307c478bd9Sstevel@tonic-gate 	if (vp != rconsvp) {
3317c478bd9Sstevel@tonic-gate 		/*
3327c478bd9Sstevel@tonic-gate 		 * It might happen that someone set rconsvp to NULL
3337c478bd9Sstevel@tonic-gate 		 * whilst we were in the middle of the open.
3347c478bd9Sstevel@tonic-gate 		 */
3357c478bd9Sstevel@tonic-gate 		if (rconsvp == NULL) {
336da6c28aaSamw 			(void) VOP_CLOSE(vp, flag, 1, (offset_t)0, cred, NULL);
3377c478bd9Sstevel@tonic-gate 			return (0);
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "cnopen: cloned open");
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	rconsopen++;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	return (0);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /* ARGSUSED */
3487c478bd9Sstevel@tonic-gate static int
cnclose(dev_t dev,int flag,int state,struct cred * cred)3497c478bd9Sstevel@tonic-gate cnclose(dev_t dev, int flag, int state, struct cred *cred)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	int	err = 0;
3527c478bd9Sstevel@tonic-gate 	vnode_t	*vp;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * Since this is the _last_ close, it's our last chance to close the
3567c478bd9Sstevel@tonic-gate 	 * underlying device.  (Note that if someone else has the underlying
3577c478bd9Sstevel@tonic-gate 	 * hardware console device open, we won't get here, since spec_close
3587c478bd9Sstevel@tonic-gate 	 * will see s_count > 1.)
3597c478bd9Sstevel@tonic-gate 	 */
3607c478bd9Sstevel@tonic-gate 	if (state != OTYP_CHR)
3617c478bd9Sstevel@tonic-gate 		return (ENXIO);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL)
3647c478bd9Sstevel@tonic-gate 		return (0);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	while ((rconsopen != 0) && ((vp = rconsvp) != NULL)) {
367da6c28aaSamw 		err = VOP_CLOSE(vp, flag, 1, (offset_t)0, cred, NULL);
3687c478bd9Sstevel@tonic-gate 		if (!err) {
3697c478bd9Sstevel@tonic-gate 			rconsopen--;
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	return (err);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate /* ARGSUSED */
3767c478bd9Sstevel@tonic-gate static int
cnread(dev_t dev,struct uio * uio,struct cred * cred)3777c478bd9Sstevel@tonic-gate cnread(dev_t dev, struct uio *uio, struct cred *cred)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	kcondvar_t	sleep_forever;
3807c478bd9Sstevel@tonic-gate 	kmutex_t	sleep_forever_mutex;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL) {
3837c478bd9Sstevel@tonic-gate 		/*
3847c478bd9Sstevel@tonic-gate 		 * Go to sleep forever.  This seems like the least
3857c478bd9Sstevel@tonic-gate 		 * harmful thing to do if there's no console.
3867c478bd9Sstevel@tonic-gate 		 * EOF might be better if we're ending up single-user
3877c478bd9Sstevel@tonic-gate 		 * mode.
3887c478bd9Sstevel@tonic-gate 		 */
3897c478bd9Sstevel@tonic-gate 		cv_init(&sleep_forever, NULL, CV_DRIVER, NULL);
3907c478bd9Sstevel@tonic-gate 		mutex_init(&sleep_forever_mutex, NULL, MUTEX_DRIVER, NULL);
3917c478bd9Sstevel@tonic-gate 		mutex_enter(&sleep_forever_mutex);
3927c478bd9Sstevel@tonic-gate 		(void) cv_wait_sig(&sleep_forever, &sleep_forever_mutex);
3937c478bd9Sstevel@tonic-gate 		mutex_exit(&sleep_forever_mutex);
3947c478bd9Sstevel@tonic-gate 		return (EIO);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (rconsvp->v_stream != NULL)
3987c478bd9Sstevel@tonic-gate 		return (strread(rconsvp, uio, cred));
3997c478bd9Sstevel@tonic-gate 	else
4007c478bd9Sstevel@tonic-gate 		return (cdev_read(rconsdev, uio, cred));
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /* ARGSUSED */
4047c478bd9Sstevel@tonic-gate static int
cnwrite(dev_t dev,struct uio * uio,struct cred * cred)4057c478bd9Sstevel@tonic-gate cnwrite(dev_t dev, struct uio *uio, struct cred *cred)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL) {
4087c478bd9Sstevel@tonic-gate 		uio->uio_resid = 0;
4097c478bd9Sstevel@tonic-gate 		return (0);
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
412361ed64aSJames Anderson 	/*
413361ed64aSJames Anderson 	 * Output to virtual console for logging if enabled.
414361ed64aSJames Anderson 	 */
415361ed64aSJames Anderson 	if (vsconsvp != NULL && vsconsvp->v_stream != NULL) {
416361ed64aSJames Anderson 		struiod_t uiod;
417361ed64aSJames Anderson 
418361ed64aSJames Anderson 		/*
419361ed64aSJames Anderson 		 * strwrite modifies uio so need to make copy.
420361ed64aSJames Anderson 		 */
421361ed64aSJames Anderson 		(void) uiodup(uio, &uiod.d_uio, uiod.d_iov,
422361ed64aSJames Anderson 		    sizeof (uiod.d_iov) / sizeof (*uiod.d_iov));
423361ed64aSJames Anderson 
424361ed64aSJames Anderson 		(void) strwrite(vsconsvp, &uiod.d_uio, cred);
425361ed64aSJames Anderson 	}
426361ed64aSJames Anderson 
4277c478bd9Sstevel@tonic-gate 	if (rconsvp->v_stream != NULL)
4287c478bd9Sstevel@tonic-gate 		return (strwrite(rconsvp, uio, cred));
4297c478bd9Sstevel@tonic-gate 	else
4307c478bd9Sstevel@tonic-gate 		return (cdev_write(rconsdev, uio, cred));
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /* ARGSUSED */
4347c478bd9Sstevel@tonic-gate static int
cnprivateioc(dev_t dev,int cmd,intptr_t arg,int flag,struct cred * cred,int * rvalp)435fea9cb91Slq150181 cnprivateioc(dev_t dev, int cmd, intptr_t arg, int flag, struct cred *cred,
436fea9cb91Slq150181 	int *rvalp)
437fea9cb91Slq150181 {
438fea9cb91Slq150181 
439fea9cb91Slq150181 	/* currently we only support one ioctl */
440fea9cb91Slq150181 	if (cmd != CONS_GETTERM)
441fea9cb91Slq150181 		return (EINVAL);
442fea9cb91Slq150181 
443fea9cb91Slq150181 	/* Confirm iwscn is immediate target of cn redirection */
444fea9cb91Slq150181 	if (rconsvp != wsconsvp)
445fea9cb91Slq150181 		return (ENODEV);
446fea9cb91Slq150181 
447fea9cb91Slq150181 	/*
448fea9cb91Slq150181 	 * If the redirection client is not wc, it should return
449fea9cb91Slq150181 	 * error upon receiving the CONS_GETTERM ioctl.
450fea9cb91Slq150181 	 *
451fea9cb91Slq150181 	 * if it is wc, we know that the target supports the CONS_GETTERM
452fea9cb91Slq150181 	 * ioctl, which very conviently has the exact same data
453fea9cb91Slq150181 	 * format as this ioctl...  so let's just pass it on.
454fea9cb91Slq150181 	 */
455fea9cb91Slq150181 	return (cdev_ioctl(rconsdev, CONS_GETTERM, arg, flag, cred, rvalp));
456fea9cb91Slq150181 }
457fea9cb91Slq150181 
458fea9cb91Slq150181 /* ARGSUSED */
459fea9cb91Slq150181 static int
cnioctl(dev_t dev,int cmd,intptr_t arg,int flag,struct cred * cred,int * rvalp)4607c478bd9Sstevel@tonic-gate cnioctl(dev_t dev, int cmd, intptr_t arg, int flag, struct cred *cred,
4617c478bd9Sstevel@tonic-gate 	int *rvalp)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL)
4647c478bd9Sstevel@tonic-gate 		return (0);
4657c478bd9Sstevel@tonic-gate 
466*ceeba6f9Srui zang - Sun Microsystems - Beijing China 	/*
467*ceeba6f9Srui zang - Sun Microsystems - Beijing China 	 * In wc, VT_SET_CONSUSER which comes from minor node 0
468*ceeba6f9Srui zang - Sun Microsystems - Beijing China 	 * has two sources -- either /dev/console or /dev/vt/0 .
469*ceeba6f9Srui zang - Sun Microsystems - Beijing China 	 * We need a way to differentiate them, so here we
470*ceeba6f9Srui zang - Sun Microsystems - Beijing China 	 * change VT_SET_CONSUSER to a private VT_RESET_CONSUSER
471*ceeba6f9Srui zang - Sun Microsystems - Beijing China 	 * ioctl.
472*ceeba6f9Srui zang - Sun Microsystems - Beijing China 	 */
473*ceeba6f9Srui zang - Sun Microsystems - Beijing China 	if (cmd == VT_SET_CONSUSER)
474*ceeba6f9Srui zang - Sun Microsystems - Beijing China 		cmd = VT_RESET_CONSUSER;
475*ceeba6f9Srui zang - Sun Microsystems - Beijing China 
476fea9cb91Slq150181 	if ((cmd & _CNIOC_MASK) == _CNIOC)
477fea9cb91Slq150181 		return (cnprivateioc(dev, cmd, arg, flag, cred, rvalp));
478*ceeba6f9Srui zang - Sun Microsystems - Beijing China 
479*ceeba6f9Srui zang - Sun Microsystems - Beijing China 	if (rconsvp->v_stream != NULL)
480*ceeba6f9Srui zang - Sun Microsystems - Beijing China 		return (strioctl(rconsvp, cmd, arg, flag, U_TO_K,
481*ceeba6f9Srui zang - Sun Microsystems - Beijing China 		    cred, rvalp));
482*ceeba6f9Srui zang - Sun Microsystems - Beijing China 
4837c478bd9Sstevel@tonic-gate 	return (cdev_ioctl(rconsdev, cmd, arg, flag, cred, rvalp));
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /* ARGSUSED */
4877c478bd9Sstevel@tonic-gate static int
cnpoll(dev_t dev,short events,int anyyet,short * reventsp,struct pollhead ** phpp)4887c478bd9Sstevel@tonic-gate cnpoll(dev_t dev, short events, int anyyet, short *reventsp,
4897c478bd9Sstevel@tonic-gate 	struct pollhead **phpp)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL)
4927c478bd9Sstevel@tonic-gate 		return (nochpoll(dev, events, anyyet, reventsp, phpp));
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	if (rconsvp->v_stream != NULL)
4957c478bd9Sstevel@tonic-gate 		return (strpoll(rconsvp->v_stream, events, anyyet, reventsp,
4967c478bd9Sstevel@tonic-gate 		    phpp));
4977c478bd9Sstevel@tonic-gate 	else
4987c478bd9Sstevel@tonic-gate 		return (cdev_poll(rconsdev, events, anyyet, reventsp, phpp));
4997c478bd9Sstevel@tonic-gate }
500