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