xref: /titanic_51/usr/src/uts/common/io/cons.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1987-2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Indirect console driver for Sun.
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * Redirects all I/O to the device designated as the underlying "hardware"
33*7c478bd9Sstevel@tonic-gate  * console, as given by the value of rconsvp.  The implementation assumes that
34*7c478bd9Sstevel@tonic-gate  * rconsvp denotes a STREAMS device; the assumption is justified since
35*7c478bd9Sstevel@tonic-gate  * consoles must be capable of effecting tty semantics.
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  * rconsvp is set in autoconf.c:consconfig(), based on information obtained
38*7c478bd9Sstevel@tonic-gate  * from the EEPROM.
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  * XXX:	The driver still needs to be converted to use ANSI C consistently
41*7c478bd9Sstevel@tonic-gate  *	throughout.
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/open.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/disp.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/log.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/uio.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate #include <sys/console.h>
61*7c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
64*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
65*7c478bd9Sstevel@tonic-gate #include <sys/poll.h>
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
70*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
71*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate static int cnopen(dev_t *, int, int, struct cred *);
74*7c478bd9Sstevel@tonic-gate static int cnclose(dev_t, int, int, struct cred *);
75*7c478bd9Sstevel@tonic-gate static int cnread(dev_t, struct uio *, struct cred *);
76*7c478bd9Sstevel@tonic-gate static int cnwrite(dev_t, struct uio *, struct cred *);
77*7c478bd9Sstevel@tonic-gate static int cnioctl(dev_t, int, intptr_t, int, struct cred *, int *);
78*7c478bd9Sstevel@tonic-gate static int cnpoll(dev_t, short, int, short *, struct pollhead **);
79*7c478bd9Sstevel@tonic-gate static int cn_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
80*7c478bd9Sstevel@tonic-gate static int cn_attach(dev_info_t *, ddi_attach_cmd_t);
81*7c478bd9Sstevel@tonic-gate static int cn_detach(dev_info_t *, ddi_detach_cmd_t);
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate static dev_info_t *cn_dip;		/* private copy of devinfo pointer */
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate static struct cb_ops cn_cb_ops = {
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	cnopen,			/* open */
88*7c478bd9Sstevel@tonic-gate 	cnclose,		/* close */
89*7c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
90*7c478bd9Sstevel@tonic-gate 	nodev,			/* print */
91*7c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
92*7c478bd9Sstevel@tonic-gate 	cnread,			/* read */
93*7c478bd9Sstevel@tonic-gate 	cnwrite,		/* write */
94*7c478bd9Sstevel@tonic-gate 	cnioctl,		/* ioctl */
95*7c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
96*7c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
97*7c478bd9Sstevel@tonic-gate 	nodev, 			/* segmap */
98*7c478bd9Sstevel@tonic-gate 	cnpoll,			/* poll */
99*7c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
100*7c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
101*7c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate };
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate static struct dev_ops cn_ops = {
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
108*7c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
109*7c478bd9Sstevel@tonic-gate 	cn_info,		/* info */
110*7c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
111*7c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
112*7c478bd9Sstevel@tonic-gate 	cn_attach,		/* attach */
113*7c478bd9Sstevel@tonic-gate 	cn_detach,		/* detach */
114*7c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
115*7c478bd9Sstevel@tonic-gate 	&cn_cb_ops,		/* driver operations */
116*7c478bd9Sstevel@tonic-gate 	(struct bus_ops *)0	/* bus operations */
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate };
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate /*
121*7c478bd9Sstevel@tonic-gate  * Global variables associated with the console device:
122*7c478bd9Sstevel@tonic-gate  *
123*7c478bd9Sstevel@tonic-gate  * XXX:	There are too many of these!
124*7c478bd9Sstevel@tonic-gate  * moved to space.c to becone resident in the kernel so that cons
125*7c478bd9Sstevel@tonic-gate  * can be loadable.
126*7c478bd9Sstevel@tonic-gate  */
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate extern dev_t	rconsdev;	/* "hardware" console */
129*7c478bd9Sstevel@tonic-gate extern vnode_t	*rconsvp;	/* pointer to vnode for that device */
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate /*
132*7c478bd9Sstevel@tonic-gate  * XXX: consulted in prsubr.c, for /proc entry point for obtaining ps info.
133*7c478bd9Sstevel@tonic-gate  */
134*7c478bd9Sstevel@tonic-gate extern dev_t	uconsdev;	/* What the user thinks is the console device */
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate /*
137*7c478bd9Sstevel@tonic-gate  * Private driver state:
138*7c478bd9Sstevel@tonic-gate  */
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate /*
141*7c478bd9Sstevel@tonic-gate  * The underlying console device potentially can be opened through (at least)
142*7c478bd9Sstevel@tonic-gate  * two paths: through this driver and through the underlying device's driver.
143*7c478bd9Sstevel@tonic-gate  * To ensure that reference counts are meaningful and therefore that close
144*7c478bd9Sstevel@tonic-gate  * routines are called at the right time, it's important to make sure that
145*7c478bd9Sstevel@tonic-gate  * rconsvp's s_count field (i.e., the count on the underlying device) never
146*7c478bd9Sstevel@tonic-gate  * has a contribution of more than one through this driver, regardless of how
147*7c478bd9Sstevel@tonic-gate  * many times this driver's been opened.  rconsopen keeps track of the
148*7c478bd9Sstevel@tonic-gate  * necessary information to ensure this property.
149*7c478bd9Sstevel@tonic-gate  */
150*7c478bd9Sstevel@tonic-gate static uint_t	rconsopen;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
154*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
155*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
156*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
157*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
158*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate extern int nodev(), nulldev();
162*7c478bd9Sstevel@tonic-gate extern int dseekneg_flag;
163*7c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
164*7c478bd9Sstevel@tonic-gate extern struct dev_ops cn_ops;
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate /*
167*7c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
168*7c478bd9Sstevel@tonic-gate  */
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
171*7c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
172*7c478bd9Sstevel@tonic-gate 	"Console redirection driver %I%",
173*7c478bd9Sstevel@tonic-gate 	&cn_ops,	/* driver ops */
174*7c478bd9Sstevel@tonic-gate };
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
177*7c478bd9Sstevel@tonic-gate 	MODREV_1,
178*7c478bd9Sstevel@tonic-gate 	&modldrv,
179*7c478bd9Sstevel@tonic-gate 	NULL
180*7c478bd9Sstevel@tonic-gate };
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate int
183*7c478bd9Sstevel@tonic-gate _init(void)
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate int
189*7c478bd9Sstevel@tonic-gate _fini(void)
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 	return (EBUSY);
192*7c478bd9Sstevel@tonic-gate }
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate int
195*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
196*7c478bd9Sstevel@tonic-gate {
197*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate /*
201*7c478bd9Sstevel@tonic-gate  * DDI glue routines
202*7c478bd9Sstevel@tonic-gate  */
203*7c478bd9Sstevel@tonic-gate static int
204*7c478bd9Sstevel@tonic-gate cn_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
205*7c478bd9Sstevel@tonic-gate {
206*7c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
207*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "syscon", S_IFCHR,
210*7c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
211*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
212*7c478bd9Sstevel@tonic-gate 	}
213*7c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "systty", S_IFCHR,
214*7c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
215*7c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
216*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
217*7c478bd9Sstevel@tonic-gate 	}
218*7c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "console", S_IFCHR,
219*7c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
220*7c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
221*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 	cn_dip = devi;
224*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate static int
228*7c478bd9Sstevel@tonic-gate cn_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
229*7c478bd9Sstevel@tonic-gate {
230*7c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
231*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
232*7c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
233*7c478bd9Sstevel@tonic-gate 	uconsdev = NODEV;
234*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
238*7c478bd9Sstevel@tonic-gate static int
239*7c478bd9Sstevel@tonic-gate cn_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	switch (infocmd) {
244*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
245*7c478bd9Sstevel@tonic-gate 		if (getminor((dev_t)arg) == 0 && cn_dip != NULL) {
246*7c478bd9Sstevel@tonic-gate 			*result = (void *) cn_dip;
247*7c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
248*7c478bd9Sstevel@tonic-gate 		}
249*7c478bd9Sstevel@tonic-gate 		break;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
252*7c478bd9Sstevel@tonic-gate 		if (getminor((dev_t)arg) == 0) {
253*7c478bd9Sstevel@tonic-gate 			*result = (void *)0;
254*7c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
255*7c478bd9Sstevel@tonic-gate 		}
256*7c478bd9Sstevel@tonic-gate 		break;
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	default:
259*7c478bd9Sstevel@tonic-gate 		break;
260*7c478bd9Sstevel@tonic-gate 	}
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	return (error);
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate /*
266*7c478bd9Sstevel@tonic-gate  * XXX	Caution: before allowing more than 256 minor devices on the
267*7c478bd9Sstevel@tonic-gate  *	console, make sure you understand the 'compatibility' hack
268*7c478bd9Sstevel@tonic-gate  *	in ufs_iget() that translates old dev_t's to new dev_t's.
269*7c478bd9Sstevel@tonic-gate  *	See bugid 1098104 for the sordid details.
270*7c478bd9Sstevel@tonic-gate  */
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
273*7c478bd9Sstevel@tonic-gate static int
274*7c478bd9Sstevel@tonic-gate cnopen(dev_t *dev, int flag, int state, struct cred *cred)
275*7c478bd9Sstevel@tonic-gate {
276*7c478bd9Sstevel@tonic-gate 	int	err;
277*7c478bd9Sstevel@tonic-gate 	static int	been_here;
278*7c478bd9Sstevel@tonic-gate 	vnode_t	*vp = rconsvp;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	ASSERT(cred != NULL);
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL)
283*7c478bd9Sstevel@tonic-gate 		return (0);
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 	/*
287*7c478bd9Sstevel@tonic-gate 	 * XXX: Clean up inactive PIDs from previous opens if any.
288*7c478bd9Sstevel@tonic-gate 	 * These would have been created as a result of an I_SETSIG
289*7c478bd9Sstevel@tonic-gate 	 * issued against console.  This is a workaround, and
290*7c478bd9Sstevel@tonic-gate 	 * console driver must be correctly redesigned not to need
291*7c478bd9Sstevel@tonic-gate 	 * this hook.
292*7c478bd9Sstevel@tonic-gate 	 */
293*7c478bd9Sstevel@tonic-gate 	if (vp->v_stream) {
294*7c478bd9Sstevel@tonic-gate 		str_cn_clean(vp);
295*7c478bd9Sstevel@tonic-gate 	}
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	/*
298*7c478bd9Sstevel@tonic-gate 	 * XXX:	Set hook to tell /proc about underlying console.  (There's
299*7c478bd9Sstevel@tonic-gate 	 *	gotta be a better way...)
300*7c478bd9Sstevel@tonic-gate 	 */
301*7c478bd9Sstevel@tonic-gate 	if (state != OTYP_CHR || getminor(*dev) != 0)
302*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
303*7c478bd9Sstevel@tonic-gate 	if (been_here == 0) {
304*7c478bd9Sstevel@tonic-gate 		uconsdev = *dev;
305*7c478bd9Sstevel@tonic-gate 		been_here = 1;
306*7c478bd9Sstevel@tonic-gate 		if (vn_open("/dev/console", UIO_SYSSPACE, FWRITE | FNOCTTY,
307*7c478bd9Sstevel@tonic-gate 		    0, &console_vnode, 0, 0) == 0)
308*7c478bd9Sstevel@tonic-gate 			console_taskq = taskq_create("console_taskq",
309*7c478bd9Sstevel@tonic-gate 			    1, maxclsyspri - 1, LOG_LOWAT / LOG_MSGSIZE,
310*7c478bd9Sstevel@tonic-gate 			    LOG_HIWAT / LOG_MSGSIZE, TASKQ_PREPOPULATE);
311*7c478bd9Sstevel@tonic-gate 	}
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 	if ((err = VOP_OPEN(&vp, flag, cred)) != 0)
314*7c478bd9Sstevel@tonic-gate 		return (err);
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	/*
317*7c478bd9Sstevel@tonic-gate 	 * The underlying driver is not allowed to have cloned itself
318*7c478bd9Sstevel@tonic-gate 	 * for this open.
319*7c478bd9Sstevel@tonic-gate 	 */
320*7c478bd9Sstevel@tonic-gate 	if (vp != rconsvp) {
321*7c478bd9Sstevel@tonic-gate 		/*
322*7c478bd9Sstevel@tonic-gate 		 * It might happen that someone set rconsvp to NULL
323*7c478bd9Sstevel@tonic-gate 		 * whilst we were in the middle of the open.
324*7c478bd9Sstevel@tonic-gate 		 */
325*7c478bd9Sstevel@tonic-gate 		if (rconsvp == NULL) {
326*7c478bd9Sstevel@tonic-gate 			(void) VOP_CLOSE(vp, flag, 1, (offset_t)0, cred);
327*7c478bd9Sstevel@tonic-gate 			return (0);
328*7c478bd9Sstevel@tonic-gate 		}
329*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "cnopen: cloned open");
330*7c478bd9Sstevel@tonic-gate 	}
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	rconsopen++;
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	return (0);
335*7c478bd9Sstevel@tonic-gate }
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
338*7c478bd9Sstevel@tonic-gate static int
339*7c478bd9Sstevel@tonic-gate cnclose(dev_t dev, int flag, int state, struct cred *cred)
340*7c478bd9Sstevel@tonic-gate {
341*7c478bd9Sstevel@tonic-gate 	int	err = 0;
342*7c478bd9Sstevel@tonic-gate 	vnode_t	*vp;
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	/*
345*7c478bd9Sstevel@tonic-gate 	 * Since this is the _last_ close, it's our last chance to close the
346*7c478bd9Sstevel@tonic-gate 	 * underlying device.  (Note that if someone else has the underlying
347*7c478bd9Sstevel@tonic-gate 	 * hardware console device open, we won't get here, since spec_close
348*7c478bd9Sstevel@tonic-gate 	 * will see s_count > 1.)
349*7c478bd9Sstevel@tonic-gate 	 */
350*7c478bd9Sstevel@tonic-gate 	if (state != OTYP_CHR)
351*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL)
354*7c478bd9Sstevel@tonic-gate 		return (0);
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	while ((rconsopen != 0) && ((vp = rconsvp) != NULL)) {
357*7c478bd9Sstevel@tonic-gate 		err = VOP_CLOSE(vp, flag, 1, (offset_t)0, cred);
358*7c478bd9Sstevel@tonic-gate 		if (!err) {
359*7c478bd9Sstevel@tonic-gate 			vp->v_stream = NULL;
360*7c478bd9Sstevel@tonic-gate 			rconsopen--;
361*7c478bd9Sstevel@tonic-gate 		}
362*7c478bd9Sstevel@tonic-gate 	}
363*7c478bd9Sstevel@tonic-gate 	return (err);
364*7c478bd9Sstevel@tonic-gate }
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
367*7c478bd9Sstevel@tonic-gate static int
368*7c478bd9Sstevel@tonic-gate cnread(dev_t dev, struct uio *uio, struct cred *cred)
369*7c478bd9Sstevel@tonic-gate {
370*7c478bd9Sstevel@tonic-gate 	kcondvar_t	sleep_forever;
371*7c478bd9Sstevel@tonic-gate 	kmutex_t	sleep_forever_mutex;
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL) {
374*7c478bd9Sstevel@tonic-gate 		/*
375*7c478bd9Sstevel@tonic-gate 		 * Go to sleep forever.  This seems like the least
376*7c478bd9Sstevel@tonic-gate 		 * harmful thing to do if there's no console.
377*7c478bd9Sstevel@tonic-gate 		 * EOF might be better if we're ending up single-user
378*7c478bd9Sstevel@tonic-gate 		 * mode.
379*7c478bd9Sstevel@tonic-gate 		 */
380*7c478bd9Sstevel@tonic-gate 		cv_init(&sleep_forever, NULL, CV_DRIVER, NULL);
381*7c478bd9Sstevel@tonic-gate 		mutex_init(&sleep_forever_mutex, NULL, MUTEX_DRIVER, NULL);
382*7c478bd9Sstevel@tonic-gate 		mutex_enter(&sleep_forever_mutex);
383*7c478bd9Sstevel@tonic-gate 		(void) cv_wait_sig(&sleep_forever, &sleep_forever_mutex);
384*7c478bd9Sstevel@tonic-gate 		mutex_exit(&sleep_forever_mutex);
385*7c478bd9Sstevel@tonic-gate 		return (EIO);
386*7c478bd9Sstevel@tonic-gate 	}
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	if (rconsvp->v_stream != NULL)
389*7c478bd9Sstevel@tonic-gate 		return (strread(rconsvp, uio, cred));
390*7c478bd9Sstevel@tonic-gate 	else
391*7c478bd9Sstevel@tonic-gate 		return (cdev_read(rconsdev, uio, cred));
392*7c478bd9Sstevel@tonic-gate }
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
395*7c478bd9Sstevel@tonic-gate static int
396*7c478bd9Sstevel@tonic-gate cnwrite(dev_t dev, struct uio *uio, struct cred *cred)
397*7c478bd9Sstevel@tonic-gate {
398*7c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL) {
399*7c478bd9Sstevel@tonic-gate 		uio->uio_resid = 0;
400*7c478bd9Sstevel@tonic-gate 		return (0);
401*7c478bd9Sstevel@tonic-gate 	}
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 	if (rconsvp->v_stream != NULL)
404*7c478bd9Sstevel@tonic-gate 		return (strwrite(rconsvp, uio, cred));
405*7c478bd9Sstevel@tonic-gate 	else
406*7c478bd9Sstevel@tonic-gate 		return (cdev_write(rconsdev, uio, cred));
407*7c478bd9Sstevel@tonic-gate }
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
410*7c478bd9Sstevel@tonic-gate static int
411*7c478bd9Sstevel@tonic-gate cnioctl(dev_t dev, int cmd, intptr_t arg, int flag, struct cred *cred,
412*7c478bd9Sstevel@tonic-gate 	int *rvalp)
413*7c478bd9Sstevel@tonic-gate {
414*7c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL)
415*7c478bd9Sstevel@tonic-gate 		return (0);
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	if (rconsvp->v_stream != NULL)
418*7c478bd9Sstevel@tonic-gate 		return (strioctl(rconsvp, cmd, arg, flag, U_TO_K, cred,
419*7c478bd9Sstevel@tonic-gate 		    rvalp));
420*7c478bd9Sstevel@tonic-gate 	else
421*7c478bd9Sstevel@tonic-gate 		return (cdev_ioctl(rconsdev, cmd, arg, flag, cred, rvalp));
422*7c478bd9Sstevel@tonic-gate }
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
425*7c478bd9Sstevel@tonic-gate static int
426*7c478bd9Sstevel@tonic-gate cnpoll(dev_t dev, short events, int anyyet, short *reventsp,
427*7c478bd9Sstevel@tonic-gate 	struct pollhead **phpp)
428*7c478bd9Sstevel@tonic-gate {
429*7c478bd9Sstevel@tonic-gate 	if (rconsvp == NULL)
430*7c478bd9Sstevel@tonic-gate 		return (nochpoll(dev, events, anyyet, reventsp, phpp));
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate 	if (rconsvp->v_stream != NULL)
433*7c478bd9Sstevel@tonic-gate 		return (strpoll(rconsvp->v_stream, events, anyyet, reventsp,
434*7c478bd9Sstevel@tonic-gate 		    phpp));
435*7c478bd9Sstevel@tonic-gate 	else
436*7c478bd9Sstevel@tonic-gate 		return (cdev_poll(rconsdev, events, anyyet, reventsp, phpp));
437*7c478bd9Sstevel@tonic-gate }
438