xref: /titanic_50/usr/src/uts/sun4u/starfire/cvcredir/cvcredir.c (revision 2115f0c6268615f93b17e507aae4a01e67538165)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * MT STREAMS Virtual Console Redirection Device Driver
31  */
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/errno.h>
36 #include <sys/kmem.h>
37 #include <sys/stat.h>
38 #include <sys/stream.h>
39 #include <sys/stropts.h>
40 #include <sys/strsun.h>
41 #include <sys/debug.h>
42 #include <sys/thread.h>
43 #include <sys/conf.h>
44 #include <sys/ddi.h>
45 #include <sys/sunddi.h>
46 #include <sys/tty.h>
47 #include <sys/cvc.h>
48 #include <sys/conf.h>
49 #include <sys/modctl.h>
50 
51 
52 /*
53  * Routine to to register/unregister our queue for console output and pass
54  * redirected data to the console.  The cvc driver will do a putnext using
55  * our queue, so we will not see the redirected console data.
56  */
57 extern int	cvc_redir(mblk_t *);
58 extern int	cvc_register(queue_t *);
59 extern int	cvc_unregister(queue_t *);
60 
61 static int	cvcr_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
62 static int	cvcr_attach(dev_info_t *, ddi_attach_cmd_t);
63 static int	cvcr_detach(dev_info_t *, ddi_detach_cmd_t);
64 static int	cvcr_wput(queue_t *, mblk_t *);
65 static int	cvcr_open(queue_t *, dev_t *, int, int, cred_t *);
66 static int	cvcr_close(queue_t *, int, cred_t *);
67 static void	cvcr_ioctl(queue_t *, mblk_t *);
68 
69 static dev_info_t	*cvcr_dip;
70 static int		cvcr_suspend = 0;
71 
72 static struct module_info minfo = {
73 	1314,		/* mi_idnum Bad luck number +1  ;-) */
74 	"cvcredir",	/* mi_idname */
75 	0,		/* mi_minpsz */
76 	INFPSZ,		/* mi_maxpsz */
77 	2048,		/* mi_hiwat */
78 	2048		/* mi_lowat */
79 };
80 
81 static struct qinit	cvcr_rinit = {
82 	NULL,		/* qi_putp */
83 	NULL,		/* qi_srvp */
84 	cvcr_open,	/* qi_qopen */
85 	cvcr_close,	/* qi_qclose */
86 	NULL,		/* qi_qadmin */
87 	&minfo,		/* qi_minfo */
88 	NULL		/* qi_mstat */
89 };
90 
91 static struct qinit	cvcr_winit = {
92 	cvcr_wput,	/* qi_putp */
93 	NULL,		/* qi_srvp */
94 	cvcr_open,	/* qi_qopen */
95 	cvcr_close,	/* qi_qclose */
96 	NULL,		/* qi_qadmin */
97 	&minfo,		/* qi_minfo */
98 	NULL		/* qi_mstat */
99 };
100 
101 struct streamtab	cvcrinfo = {
102 	&cvcr_rinit,	/* st_rdinit */
103 	&cvcr_winit,	/* st_wrinit */
104 	NULL,		/* st_muxrinit */
105 	NULL		/* st_muxwrinit */
106 };
107 
108 DDI_DEFINE_STREAM_OPS(cvcrops, nulldev, nulldev, cvcr_attach,
109     cvcr_detach, nodev, cvcr_info, (D_MTPERQ | D_MP), &cvcrinfo);
110 
111 char _depends_on[] = "drv/cvc";
112 
113 static struct modldrv modldrv = {
114 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
115 	"CVC redirect driver 'cvcredir' v%I%",
116 	&cvcrops,	/* driver ops */
117 };
118 
119 static struct modlinkage modlinkage = {
120 	MODREV_1,
121 	&modldrv,
122 	NULL
123 };
124 
125 int
126 _init(void)
127 {
128 	return (mod_install(&modlinkage));
129 }
130 
131 int
132 _fini(void)
133 {
134 	return (mod_remove(&modlinkage));
135 }
136 
137 int
138 _info(struct modinfo *modinfop)
139 {
140 	return (mod_info(&modlinkage, modinfop));
141 }
142 
143 static int
144 cvcr_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
145 {
146 #ifdef lint
147 	cvcr_suspend = cvcr_suspend;
148 #endif
149 	if (cmd == DDI_RESUME) {
150 		cvcr_suspend = 0;
151 	} else {
152 		if (ddi_create_minor_node(devi, "cvcredir", S_IFCHR,
153 		    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
154 			ddi_remove_minor_node(devi, NULL);
155 			return (-1);
156 		}
157 		cvcr_dip = devi;
158 	}
159 	return (DDI_SUCCESS);
160 }
161 
162 static int
163 cvcr_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
164 {
165 	if (cmd == DDI_SUSPEND) {
166 		cvcr_suspend = 1;
167 	} else {
168 		if (cmd != DDI_DETACH) {
169 			return (DDI_FAILURE);
170 		}
171 		ddi_remove_minor_node(dip, NULL);
172 	}
173 	return (DDI_SUCCESS);
174 }
175 
176 /* ARGSUSED */
177 static int
178 cvcr_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
179 {
180 	register int error;
181 
182 	switch (infocmd) {
183 	case DDI_INFO_DEVT2DEVINFO:
184 		if (cvcr_dip == NULL) {
185 			error = DDI_FAILURE;
186 		} else {
187 			*result = (void *)cvcr_dip;
188 			error = DDI_SUCCESS;
189 		}
190 		break;
191 	case DDI_INFO_DEVT2INSTANCE:
192 		*result = (void *)0;
193 		error = DDI_SUCCESS;
194 		break;
195 	default:
196 		error = DDI_FAILURE;
197 	}
198 	return (error);
199 }
200 
201 /* ARGSUSED */
202 static int
203 cvcr_open(queue_t *q, dev_t *dev, int flag, int sflag, cred_t *cred)
204 {
205 	WR(q)->q_ptr = q->q_ptr = (char *)2;
206 	/*
207 	 * call into the cvc driver to register our queue.  cvc will use
208 	 * our queue to send console output data upstream (our stream)to
209 	 * cvcd which has us open and is reading console data.
210 	 */
211 	if (cvc_register(RD(q)) == -1) {
212 		cmn_err(CE_WARN, "cvcr_open: cvc_register failed for q = 0x%p",
213 			q);
214 	}
215 	return (0);
216 }
217 
218 /* ARGSUSED */
219 static int
220 cvcr_close(queue_t *q, int flag, cred_t *cred)
221 {
222 	/*
223 	 * call into the cvc driver to un-register our queue.  cvc will
224 	 * no longer use our queue to send console output data upstream.
225 	 */
226 	cvc_unregister(RD(q));
227 	WR(q)->q_ptr = q->q_ptr = NULL;
228 	return (0);
229 }
230 
231 static int
232 cvcr_wput(queue_t *q, mblk_t *mp)
233 {
234 	/*
235 	 * Handle BREAK key for debugger and TIOCSWINSZ.
236 	 */
237 	if (mp->b_datap->db_type == M_IOCTL) {
238 		cvcr_ioctl(q, mp);
239 		return (0);
240 	}
241 	/*
242 	 * Call into the cvc driver to put console input data on
243 	 * its upstream queue to be picked up by the console driver.
244 	 */
245 	if (cvc_redir(mp) != 0)
246 		freemsg(mp);
247 	return (0);
248 }
249 
250 static void
251 cvcr_ioctl(queue_t *q, mblk_t *mp)
252 {
253 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
254 	int		error;
255 
256 	switch (iocp->ioc_cmd) {
257 	case CVC_BREAK:
258 		abort_sequence_enter(NULL);
259 		miocack(q, mp, 0, 0);
260 		break;
261 
262 	case CVC_DISCONNECT:
263 	case TIOCSWINSZ:
264 		/*
265 		 * Generate a SIGHUP or SIGWINCH to the console.  Note in this
266 		 * case cvc_redir does not free up mp, so we can reuse it for
267 		 * the ACK/NAK.
268 		 */
269 		error = cvc_redir(mp);
270 		if (error != 0)
271 			miocnak(q, mp, 0, error);
272 		else
273 			miocack(q, mp, 0, 0);
274 		break;
275 
276 	default:
277 		miocnak(q, mp, 0, EINVAL);
278 		break;
279 	}
280 }
281