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