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