xref: /titanic_50/usr/src/uts/sun4u/starcat/io/cvcredir.c (revision 6d24e334e75789fbe89b6a018c5023ce278e15fa)
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/sc_cvcio.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 extern struct mod_ops mod_driverops;
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 
126 int
127 _init()
128 {
129 	return (mod_install(&modlinkage));
130 }
131 
132 int
133 _fini()
134 {
135 	return (mod_remove(&modlinkage));
136 }
137 
138 int
139 _info(modinfop)
140 	struct modinfo *modinfop;
141 {
142 	return (mod_info(&modlinkage, modinfop));
143 }
144 
145 /*ARGSUSED*/
146 static int
147 cvcr_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
148 {
149 	if (cmd == DDI_RESUME) {
150 		if (cvcr_suspend) {
151 			cvcr_suspend = 0;
152 		}
153 	} else {
154 		if (ddi_create_minor_node(devi, "cvcredir", S_IFCHR,
155 		    0, NULL, NULL) == DDI_FAILURE) {
156 			ddi_remove_minor_node(devi, NULL);
157 			return (-1);
158 		}
159 		cvcr_dip = devi;
160 	}
161 	return (DDI_SUCCESS);
162 }
163 
164 static int
165 cvcr_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
166 {
167 	if (cmd == DDI_SUSPEND) {
168 		if (!cvcr_suspend) {
169 			cvcr_suspend = 1;
170 		}
171 	} else {
172 		if (cmd != DDI_DETACH) {
173 			return (DDI_FAILURE);
174 		}
175 		ddi_remove_minor_node(dip, NULL);
176 	}
177 	return (DDI_SUCCESS);
178 }
179 
180 /* ARGSUSED */
181 static int
182 cvcr_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
183 {
184 	register int error;
185 
186 	switch (infocmd) {
187 	case DDI_INFO_DEVT2DEVINFO:
188 		if (cvcr_dip == NULL) {
189 			error = DDI_FAILURE;
190 		} else {
191 			*result = (void *)cvcr_dip;
192 			error = DDI_SUCCESS;
193 		}
194 		break;
195 	case DDI_INFO_DEVT2INSTANCE:
196 		*result = (void *)0;
197 		error = DDI_SUCCESS;
198 		break;
199 	default:
200 		error = DDI_FAILURE;
201 	}
202 	return (error);
203 }
204 
205 /* ARGSUSED */
206 static int
207 cvcr_open(queue_t *q, dev_t *dev, int flag, int sflag, cred_t *cred)
208 {
209 	WR(q)->q_ptr = q->q_ptr = (char *)2;
210 	/*
211 	 * call into the cvc driver to register our queue.  cvc will use
212 	 * our queue to send console output data upstream (our stream)to
213 	 * cvcd which has us open and is reading console data.
214 	 */
215 	if (cvc_register(RD(q)) == -1) {
216 		cmn_err(CE_WARN, "cvcr_open: cvc_register failed for q = 0x%p",
217 			q);
218 	}
219 	return (0);
220 }
221 
222 /* ARGSUSED */
223 static int
224 cvcr_close(queue_t *q, int flag, cred_t *cred)
225 {
226 	/*
227 	 * call into the cvc driver to un-register our queue.  cvc will
228 	 * no longer use our queue to send console output data upstream.
229 	 */
230 	cvc_unregister(RD(q));
231 	WR(q)->q_ptr = q->q_ptr = NULL;
232 	return (0);
233 }
234 
235 static int
236 cvcr_wput(queue_t *q, mblk_t *mp)
237 {
238 	/*
239 	 * Handle network-transmitted control messages
240 	 */
241 	if (mp->b_datap->db_type == M_IOCTL) {
242 		cvcr_ioctl(q, mp);
243 		return (0);
244 	}
245 	/*
246 	 * Call into the cvc driver to put console input data on
247 	 * its upstream queue to be picked up by the console driver.
248 	 */
249 	if (!cvc_redir(mp))
250 		freemsg(mp);
251 	return (0);
252 }
253 
254 static void
255 cvcr_ioctl(queue_t *q, mblk_t *mp)
256 {
257 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
258 
259 	/*
260 	 * If this is a CVC_BREAK, drop to OBP/kmdb from here rather than
261 	 * passing the mblk on to cvc.  If it is a disconnect, pass it on to
262 	 * cvc.  Nothing else is currently supported, so NAK all others.
263 	 * Note that cvc_redir doesn't free the mblk passed in, so we can
264 	 * reuse it for ACKing.
265 	 */
266 	if (iocp->ioc_cmd == CVC_BREAK) {
267 		abort_sequence_enter((char *)NULL);
268 		miocack(q, mp, 0, 0);
269 	} else if (iocp->ioc_cmd == CVC_DISCONNECT) {
270 		(void) cvc_redir(mp);
271 		miocack(q, mp, 0, 0);
272 	} else {
273 		miocnak(q, mp, 0, EINVAL);
274 	}
275 }
276