xref: /onnv-gate/usr/src/uts/common/io/ptem.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" /* from S5R4 1.13 */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * Description:
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * The PTEM streams module is used as a pseudo driver emulator.  Its purpose
37*0Sstevel@tonic-gate  * is to emulate the ioctl() functions of a terminal device driver.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <sys/types.h>
41*0Sstevel@tonic-gate #include <sys/param.h>
42*0Sstevel@tonic-gate #include <sys/stream.h>
43*0Sstevel@tonic-gate #include <sys/stropts.h>
44*0Sstevel@tonic-gate #include <sys/strsun.h>
45*0Sstevel@tonic-gate #include <sys/termio.h>
46*0Sstevel@tonic-gate #include <sys/pcb.h>
47*0Sstevel@tonic-gate #include <sys/signal.h>
48*0Sstevel@tonic-gate #include <sys/cred.h>
49*0Sstevel@tonic-gate #include <sys/strtty.h>
50*0Sstevel@tonic-gate #include <sys/errno.h>
51*0Sstevel@tonic-gate #include <sys/cmn_err.h>
52*0Sstevel@tonic-gate #include <sys/jioctl.h>
53*0Sstevel@tonic-gate #include <sys/ptem.h>
54*0Sstevel@tonic-gate #include <sys/ptms.h>
55*0Sstevel@tonic-gate #include <sys/debug.h>
56*0Sstevel@tonic-gate #include <sys/kmem.h>
57*0Sstevel@tonic-gate #include <sys/ddi.h>
58*0Sstevel@tonic-gate #include <sys/sunddi.h>
59*0Sstevel@tonic-gate #include <sys/conf.h>
60*0Sstevel@tonic-gate #include <sys/modctl.h>
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate extern struct streamtab pteminfo;
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static struct fmodsw fsw = {
65*0Sstevel@tonic-gate 	"ptem",
66*0Sstevel@tonic-gate 	&pteminfo,
67*0Sstevel@tonic-gate 	D_MTQPAIR | D_MP
68*0Sstevel@tonic-gate };
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
71*0Sstevel@tonic-gate 	&mod_strmodops, "pty hardware emulator", &fsw
72*0Sstevel@tonic-gate };
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
75*0Sstevel@tonic-gate 	MODREV_1, &modlstrmod, NULL
76*0Sstevel@tonic-gate };
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate int
_init()79*0Sstevel@tonic-gate _init()
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate int
_fini()85*0Sstevel@tonic-gate _fini()
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate int
_info(struct modinfo * modinfop)91*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate  * stream data structure definitions
98*0Sstevel@tonic-gate  */
99*0Sstevel@tonic-gate static int ptemopen(queue_t *, dev_t  *, int, int, cred_t *);
100*0Sstevel@tonic-gate static int ptemclose(queue_t *, int, cred_t *);
101*0Sstevel@tonic-gate static void ptemrput(queue_t *, mblk_t *);
102*0Sstevel@tonic-gate static void ptemwput(queue_t *, mblk_t *);
103*0Sstevel@tonic-gate static void ptemwsrv(queue_t *);
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate static struct module_info ptem_info = {
106*0Sstevel@tonic-gate 	0xabcd,
107*0Sstevel@tonic-gate 	"ptem",
108*0Sstevel@tonic-gate 	0,
109*0Sstevel@tonic-gate 	512,
110*0Sstevel@tonic-gate 	512,
111*0Sstevel@tonic-gate 	128
112*0Sstevel@tonic-gate };
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate static struct qinit ptemrinit = {
115*0Sstevel@tonic-gate 	(int (*)()) ptemrput,
116*0Sstevel@tonic-gate 	NULL,
117*0Sstevel@tonic-gate 	ptemopen,
118*0Sstevel@tonic-gate 	ptemclose,
119*0Sstevel@tonic-gate 	NULL,
120*0Sstevel@tonic-gate 	&ptem_info,
121*0Sstevel@tonic-gate 	NULL
122*0Sstevel@tonic-gate };
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate static struct qinit ptemwinit = {
125*0Sstevel@tonic-gate 	(int (*)()) ptemwput,
126*0Sstevel@tonic-gate 	(int (*)()) ptemwsrv,
127*0Sstevel@tonic-gate 	ptemopen,
128*0Sstevel@tonic-gate 	ptemclose,
129*0Sstevel@tonic-gate 	nulldev,
130*0Sstevel@tonic-gate 	&ptem_info,
131*0Sstevel@tonic-gate 	NULL
132*0Sstevel@tonic-gate };
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate struct streamtab pteminfo = {
135*0Sstevel@tonic-gate 	&ptemrinit,
136*0Sstevel@tonic-gate 	&ptemwinit,
137*0Sstevel@tonic-gate 	NULL,
138*0Sstevel@tonic-gate 	NULL
139*0Sstevel@tonic-gate };
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate static void	ptioc(queue_t *, mblk_t *, int);
142*0Sstevel@tonic-gate static int	ptemwmsg(queue_t *, mblk_t *);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate /*
145*0Sstevel@tonic-gate  * ptemopen - open routine gets called when the module gets pushed onto the
146*0Sstevel@tonic-gate  * stream.
147*0Sstevel@tonic-gate  */
148*0Sstevel@tonic-gate /* ARGSUSED */
149*0Sstevel@tonic-gate static int
ptemopen(queue_t * q,dev_t * devp,int oflag,int sflag,cred_t * credp)150*0Sstevel@tonic-gate ptemopen(
151*0Sstevel@tonic-gate 	queue_t    *q,		/* pointer to the read side queue */
152*0Sstevel@tonic-gate 	dev_t   *devp,		/* pointer to stream tail's dev */
153*0Sstevel@tonic-gate 	int	oflag,		/* the user open(2) supplied flags */
154*0Sstevel@tonic-gate 	int	sflag,		/* open state flag */
155*0Sstevel@tonic-gate 	cred_t *credp)		/* credentials */
156*0Sstevel@tonic-gate {
157*0Sstevel@tonic-gate 	struct ptem *ntp;	/* ptem entry for this PTEM module */
158*0Sstevel@tonic-gate 	mblk_t *mop;		/* an setopts mblk */
159*0Sstevel@tonic-gate 	struct stroptions *sop;
160*0Sstevel@tonic-gate 	struct termios *termiosp;
161*0Sstevel@tonic-gate 	int len;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	if (sflag != MODOPEN)
164*0Sstevel@tonic-gate 		return (EINVAL);
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if (q->q_ptr != NULL) {
167*0Sstevel@tonic-gate 		/* It's already attached. */
168*0Sstevel@tonic-gate 		return (0);
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	/*
172*0Sstevel@tonic-gate 	 * Allocate state structure.
173*0Sstevel@tonic-gate 	 */
174*0Sstevel@tonic-gate 	ntp = kmem_alloc(sizeof (*ntp), KM_SLEEP);
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	/*
177*0Sstevel@tonic-gate 	 * Allocate a message block, used to pass the zero length message for
178*0Sstevel@tonic-gate 	 * "stty 0".
179*0Sstevel@tonic-gate 	 *
180*0Sstevel@tonic-gate 	 * NOTE: it's better to find out if such a message block can be
181*0Sstevel@tonic-gate 	 *	 allocated before it's needed than to not be able to
182*0Sstevel@tonic-gate 	 *	 deliver (for possible lack of buffers) when a hang-up
183*0Sstevel@tonic-gate 	 *	 occurs.
184*0Sstevel@tonic-gate 	 */
185*0Sstevel@tonic-gate 	if ((ntp->dack_ptr = allocb(4, BPRI_MED)) == NULL) {
186*0Sstevel@tonic-gate 		kmem_free(ntp, sizeof (*ntp));
187*0Sstevel@tonic-gate 		return (EAGAIN);
188*0Sstevel@tonic-gate 	}
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	/*
191*0Sstevel@tonic-gate 	 * Initialize an M_SETOPTS message to set up hi/lo water marks on
192*0Sstevel@tonic-gate 	 * stream head read queue and add controlling tty if not set.
193*0Sstevel@tonic-gate 	 */
194*0Sstevel@tonic-gate 	mop = allocb(sizeof (struct stroptions), BPRI_MED);
195*0Sstevel@tonic-gate 	if (mop == NULL) {
196*0Sstevel@tonic-gate 		freemsg(ntp->dack_ptr);
197*0Sstevel@tonic-gate 		kmem_free(ntp, sizeof (*ntp));
198*0Sstevel@tonic-gate 		return (EAGAIN);
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
201*0Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
202*0Sstevel@tonic-gate 	sop = (struct stroptions *)mop->b_rptr;
203*0Sstevel@tonic-gate 	sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
204*0Sstevel@tonic-gate 	sop->so_hiwat = 512;
205*0Sstevel@tonic-gate 	sop->so_lowat = 256;
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	/*
208*0Sstevel@tonic-gate 	 * Cross-link.
209*0Sstevel@tonic-gate 	 */
210*0Sstevel@tonic-gate 	ntp->q_ptr = q;
211*0Sstevel@tonic-gate 	q->q_ptr = ntp;
212*0Sstevel@tonic-gate 	WR(q)->q_ptr = ntp;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	/*
215*0Sstevel@tonic-gate 	 * Get termios defaults.  These are stored as
216*0Sstevel@tonic-gate 	 * a property in the "options" node.
217*0Sstevel@tonic-gate 	 */
218*0Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_root_node(), 0, "ttymodes",
219*0Sstevel@tonic-gate 	    (caddr_t)&termiosp, &len) == DDI_PROP_SUCCESS &&
220*0Sstevel@tonic-gate 	    len == sizeof (struct termios)) {
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 		ntp->cflags = termiosp->c_cflag;
223*0Sstevel@tonic-gate 		kmem_free(termiosp, len);
224*0Sstevel@tonic-gate 	} else {
225*0Sstevel@tonic-gate 		/*
226*0Sstevel@tonic-gate 		 * Gack!  Whine about it.
227*0Sstevel@tonic-gate 		 */
228*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "ptem: Couldn't get ttymodes property!");
229*0Sstevel@tonic-gate 	}
230*0Sstevel@tonic-gate 	ntp->wsz.ws_row = 0;
231*0Sstevel@tonic-gate 	ntp->wsz.ws_col = 0;
232*0Sstevel@tonic-gate 	ntp->wsz.ws_xpixel = 0;
233*0Sstevel@tonic-gate 	ntp->wsz.ws_ypixel = 0;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	ntp->state = 0;
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	/*
238*0Sstevel@tonic-gate 	 * Commit to the open and send the M_SETOPTS off to the stream head.
239*0Sstevel@tonic-gate 	 */
240*0Sstevel@tonic-gate 	qprocson(q);
241*0Sstevel@tonic-gate 	putnext(q, mop);
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	return (0);
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate /*
248*0Sstevel@tonic-gate  * ptemclose - This routine gets called when the module gets popped off of the
249*0Sstevel@tonic-gate  * stream.
250*0Sstevel@tonic-gate  */
251*0Sstevel@tonic-gate /* ARGSUSED */
252*0Sstevel@tonic-gate static int
ptemclose(queue_t * q,int flag,cred_t * credp)253*0Sstevel@tonic-gate ptemclose(queue_t *q, int flag, cred_t *credp)
254*0Sstevel@tonic-gate {
255*0Sstevel@tonic-gate 	struct ptem *ntp;	/* ptem entry for this PTEM module */
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	qprocsoff(q);
258*0Sstevel@tonic-gate 	ntp = (struct ptem *)q->q_ptr;
259*0Sstevel@tonic-gate 	freemsg(ntp->dack_ptr);
260*0Sstevel@tonic-gate 	kmem_free(ntp, sizeof (*ntp));
261*0Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
262*0Sstevel@tonic-gate 	return (0);
263*0Sstevel@tonic-gate }
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate /*
267*0Sstevel@tonic-gate  * ptemrput - Module read queue put procedure.
268*0Sstevel@tonic-gate  *
269*0Sstevel@tonic-gate  * This is called from the module or driver downstream.
270*0Sstevel@tonic-gate  */
271*0Sstevel@tonic-gate static void
ptemrput(queue_t * q,mblk_t * mp)272*0Sstevel@tonic-gate ptemrput(queue_t *q, mblk_t *mp)
273*0Sstevel@tonic-gate {
274*0Sstevel@tonic-gate 	struct iocblk *iocp;	/* M_IOCTL data */
275*0Sstevel@tonic-gate 	struct copyresp *resp;	/* transparent ioctl response struct */
276*0Sstevel@tonic-gate 	int error;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
279*0Sstevel@tonic-gate 	case M_DELAY:
280*0Sstevel@tonic-gate 	case M_READ:
281*0Sstevel@tonic-gate 		freemsg(mp);
282*0Sstevel@tonic-gate 		break;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	case M_IOCTL:
285*0Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
288*0Sstevel@tonic-gate 		case TCSBRK:
289*0Sstevel@tonic-gate 			/*
290*0Sstevel@tonic-gate 			 * Send a break message upstream.
291*0Sstevel@tonic-gate 			 *
292*0Sstevel@tonic-gate 			 * XXX:	Shouldn't the argument come into play in
293*0Sstevel@tonic-gate 			 *	determining whether or not so send an M_BREAK?
294*0Sstevel@tonic-gate 			 *	It certainly does in the write-side direction.
295*0Sstevel@tonic-gate 			 */
296*0Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
297*0Sstevel@tonic-gate 			if (error != 0) {
298*0Sstevel@tonic-gate 				miocnak(q, mp, 0, error);
299*0Sstevel@tonic-gate 				break;
300*0Sstevel@tonic-gate 			}
301*0Sstevel@tonic-gate 			if (!(*(int *)mp->b_cont->b_rptr)) {
302*0Sstevel@tonic-gate 				if (!putnextctl(q, M_BREAK)) {
303*0Sstevel@tonic-gate 					/*
304*0Sstevel@tonic-gate 					 * Send an NAK reply back
305*0Sstevel@tonic-gate 					 */
306*0Sstevel@tonic-gate 					miocnak(q, mp, 0, EAGAIN);
307*0Sstevel@tonic-gate 					break;
308*0Sstevel@tonic-gate 				}
309*0Sstevel@tonic-gate 			}
310*0Sstevel@tonic-gate 			/*
311*0Sstevel@tonic-gate 			 * ACK it.
312*0Sstevel@tonic-gate 			 */
313*0Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
314*0Sstevel@tonic-gate 			qreply(q, mp);
315*0Sstevel@tonic-gate 			break;
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 		case JWINSIZE:
318*0Sstevel@tonic-gate 		case TIOCGWINSZ:
319*0Sstevel@tonic-gate 		case TIOCSWINSZ:
320*0Sstevel@tonic-gate 			ptioc(q, mp, RDSIDE);
321*0Sstevel@tonic-gate 			break;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 		case TIOCSIGNAL:
324*0Sstevel@tonic-gate 			/*
325*0Sstevel@tonic-gate 			 * The following subtle logic is due to the fact that
326*0Sstevel@tonic-gate 			 * `mp' may be in any one of three distinct formats:
327*0Sstevel@tonic-gate 			 *
328*0Sstevel@tonic-gate 			 *	1. A transparent M_IOCTL with an intptr_t-sized
329*0Sstevel@tonic-gate 			 *	   payload containing the signal number.
330*0Sstevel@tonic-gate 			 *
331*0Sstevel@tonic-gate 			 *	2. An I_STR M_IOCTL with an int-sized payload
332*0Sstevel@tonic-gate 			 *	   containing the signal number.
333*0Sstevel@tonic-gate 			 *
334*0Sstevel@tonic-gate 			 *	3. An M_IOCDATA with an int-sized payload
335*0Sstevel@tonic-gate 			 *	   containing the signal number.
336*0Sstevel@tonic-gate 			 */
337*0Sstevel@tonic-gate 			if (iocp->ioc_count == TRANSPARENT) {
338*0Sstevel@tonic-gate 				intptr_t sig = *(intptr_t *)mp->b_cont->b_rptr;
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 				if (sig < 1 || sig >= NSIG) {
341*0Sstevel@tonic-gate 					/*
342*0Sstevel@tonic-gate 					 * it's transparent with pointer
343*0Sstevel@tonic-gate 					 * to the arg
344*0Sstevel@tonic-gate 					 */
345*0Sstevel@tonic-gate 					mcopyin(mp, NULL, sizeof (int), NULL);
346*0Sstevel@tonic-gate 					qreply(q, mp);
347*0Sstevel@tonic-gate 					break;
348*0Sstevel@tonic-gate 				}
349*0Sstevel@tonic-gate 			}
350*0Sstevel@tonic-gate 			ptioc(q, mp, RDSIDE);
351*0Sstevel@tonic-gate 			break;
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 		case TIOCREMOTE:
354*0Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
355*0Sstevel@tonic-gate 				ptioc(q, mp, RDSIDE);
356*0Sstevel@tonic-gate 			else {
357*0Sstevel@tonic-gate 				mcopyin(mp, NULL, sizeof (int), NULL);
358*0Sstevel@tonic-gate 				qreply(q, mp);
359*0Sstevel@tonic-gate 			}
360*0Sstevel@tonic-gate 			break;
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 		default:
363*0Sstevel@tonic-gate 			putnext(q, mp);
364*0Sstevel@tonic-gate 			break;
365*0Sstevel@tonic-gate 		}
366*0Sstevel@tonic-gate 		break;
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	case M_IOCDATA:
369*0Sstevel@tonic-gate 		resp = (struct copyresp *)mp->b_rptr;
370*0Sstevel@tonic-gate 		if (resp->cp_rval) {
371*0Sstevel@tonic-gate 			/*
372*0Sstevel@tonic-gate 			 * Just free message on failure.
373*0Sstevel@tonic-gate 			 */
374*0Sstevel@tonic-gate 			freemsg(mp);
375*0Sstevel@tonic-gate 			break;
376*0Sstevel@tonic-gate 		}
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 		/*
379*0Sstevel@tonic-gate 		 * Only need to copy data for the SET case.
380*0Sstevel@tonic-gate 		 */
381*0Sstevel@tonic-gate 		switch (resp->cp_cmd) {
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 		case TIOCSWINSZ:
384*0Sstevel@tonic-gate 		case TIOCSIGNAL:
385*0Sstevel@tonic-gate 		case TIOCREMOTE:
386*0Sstevel@tonic-gate 			ptioc(q, mp, RDSIDE);
387*0Sstevel@tonic-gate 			break;
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 		case JWINSIZE:
390*0Sstevel@tonic-gate 		case TIOCGWINSZ:
391*0Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCACK;
392*0Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
393*0Sstevel@tonic-gate 			qreply(q, mp);
394*0Sstevel@tonic-gate 			break;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 		default:
397*0Sstevel@tonic-gate 			freemsg(mp);
398*0Sstevel@tonic-gate 			break;
399*0Sstevel@tonic-gate 	}
400*0Sstevel@tonic-gate 	break;
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 	case M_IOCACK:
403*0Sstevel@tonic-gate 	case M_IOCNAK:
404*0Sstevel@tonic-gate 		/*
405*0Sstevel@tonic-gate 		 * We only pass write-side ioctls through to the master that
406*0Sstevel@tonic-gate 		 * we've already ACKed or NAKed to the stream head.  Thus, we
407*0Sstevel@tonic-gate 		 * discard ones arriving from below, since they're redundant
408*0Sstevel@tonic-gate 		 * from the point of view of modules above us.
409*0Sstevel@tonic-gate 		 */
410*0Sstevel@tonic-gate 		freemsg(mp);
411*0Sstevel@tonic-gate 		break;
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	case M_HANGUP:
414*0Sstevel@tonic-gate 		/*
415*0Sstevel@tonic-gate 		 * clear blocked state.
416*0Sstevel@tonic-gate 		 */
417*0Sstevel@tonic-gate 		{
418*0Sstevel@tonic-gate 			struct ptem *ntp = (struct ptem *)q->q_ptr;
419*0Sstevel@tonic-gate 			if (ntp->state & OFLOW_CTL) {
420*0Sstevel@tonic-gate 				ntp->state &= ~OFLOW_CTL;
421*0Sstevel@tonic-gate 				qenable(WR(q));
422*0Sstevel@tonic-gate 			}
423*0Sstevel@tonic-gate 		}
424*0Sstevel@tonic-gate 	default:
425*0Sstevel@tonic-gate 		putnext(q, mp);
426*0Sstevel@tonic-gate 		break;
427*0Sstevel@tonic-gate 	}
428*0Sstevel@tonic-gate }
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate /*
432*0Sstevel@tonic-gate  * ptemwput - Module write queue put procedure.
433*0Sstevel@tonic-gate  *
434*0Sstevel@tonic-gate  * This is called from the module or stream head upstream.
435*0Sstevel@tonic-gate  *
436*0Sstevel@tonic-gate  * XXX:	This routine is quite lazy about handling allocation failures,
437*0Sstevel@tonic-gate  *	basically just giving up and reporting failure.  It really ought to
438*0Sstevel@tonic-gate  *	set up bufcalls and only fail when it's absolutely necessary.
439*0Sstevel@tonic-gate  */
440*0Sstevel@tonic-gate static void
ptemwput(queue_t * q,mblk_t * mp)441*0Sstevel@tonic-gate ptemwput(queue_t *q, mblk_t *mp)
442*0Sstevel@tonic-gate {
443*0Sstevel@tonic-gate 	struct ptem *ntp = (struct ptem *)q->q_ptr;
444*0Sstevel@tonic-gate 	struct iocblk *iocp;	/* outgoing ioctl structure */
445*0Sstevel@tonic-gate 	struct copyresp *resp;
446*0Sstevel@tonic-gate 	unsigned char type = mp->b_datap->db_type;
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	if (type >= QPCTL) {
449*0Sstevel@tonic-gate 		switch (type) {
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 		case M_IOCDATA:
452*0Sstevel@tonic-gate 			resp = (struct copyresp *)mp->b_rptr;
453*0Sstevel@tonic-gate 			if (resp->cp_rval) {
454*0Sstevel@tonic-gate 				/*
455*0Sstevel@tonic-gate 				 * Just free message on failure.
456*0Sstevel@tonic-gate 				 */
457*0Sstevel@tonic-gate 				freemsg(mp);
458*0Sstevel@tonic-gate 				break;
459*0Sstevel@tonic-gate 			}
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 			/*
462*0Sstevel@tonic-gate 			 * Only need to copy data for the SET case.
463*0Sstevel@tonic-gate 			 */
464*0Sstevel@tonic-gate 			switch (resp->cp_cmd) {
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 				case TIOCSWINSZ:
467*0Sstevel@tonic-gate 					ptioc(q, mp, WRSIDE);
468*0Sstevel@tonic-gate 					break;
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 				case JWINSIZE:
471*0Sstevel@tonic-gate 				case TIOCGWINSZ:
472*0Sstevel@tonic-gate 					mioc2ack(mp, NULL, 0, 0);
473*0Sstevel@tonic-gate 					qreply(q, mp);
474*0Sstevel@tonic-gate 					break;
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 				default:
477*0Sstevel@tonic-gate 					freemsg(mp);
478*0Sstevel@tonic-gate 			}
479*0Sstevel@tonic-gate 			break;
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 		case M_FLUSH:
482*0Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHW) {
483*0Sstevel@tonic-gate 			    if ((ntp->state & IS_PTSTTY) &&
484*0Sstevel@tonic-gate 					(*mp->b_rptr & FLUSHBAND))
485*0Sstevel@tonic-gate 				flushband(q, *(mp->b_rptr + 1), FLUSHDATA);
486*0Sstevel@tonic-gate 			    else
487*0Sstevel@tonic-gate 				flushq(q, FLUSHDATA);
488*0Sstevel@tonic-gate 			}
489*0Sstevel@tonic-gate 			putnext(q, mp);
490*0Sstevel@tonic-gate 			break;
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate 		case M_READ:
493*0Sstevel@tonic-gate 			freemsg(mp);
494*0Sstevel@tonic-gate 			break;
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 		case M_STOP:
497*0Sstevel@tonic-gate 			/*
498*0Sstevel@tonic-gate 			 * Set the output flow control state.
499*0Sstevel@tonic-gate 			 */
500*0Sstevel@tonic-gate 			ntp->state |= OFLOW_CTL;
501*0Sstevel@tonic-gate 			putnext(q, mp);
502*0Sstevel@tonic-gate 			break;
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 		case M_START:
505*0Sstevel@tonic-gate 			/*
506*0Sstevel@tonic-gate 			 * Relieve the output flow control state.
507*0Sstevel@tonic-gate 			 */
508*0Sstevel@tonic-gate 			ntp->state &= ~OFLOW_CTL;
509*0Sstevel@tonic-gate 			putnext(q, mp);
510*0Sstevel@tonic-gate 			qenable(q);
511*0Sstevel@tonic-gate 			break;
512*0Sstevel@tonic-gate 		default:
513*0Sstevel@tonic-gate 			putnext(q, mp);
514*0Sstevel@tonic-gate 			break;
515*0Sstevel@tonic-gate 		}
516*0Sstevel@tonic-gate 		return;
517*0Sstevel@tonic-gate 	}
518*0Sstevel@tonic-gate 	/*
519*0Sstevel@tonic-gate 	 * If our queue is nonempty or flow control persists
520*0Sstevel@tonic-gate 	 * downstream or module in stopped state, queue this message.
521*0Sstevel@tonic-gate 	 */
522*0Sstevel@tonic-gate 	if (q->q_first != NULL || !bcanputnext(q, mp->b_band)) {
523*0Sstevel@tonic-gate 		/*
524*0Sstevel@tonic-gate 		 * Exception: ioctls, except for those defined to
525*0Sstevel@tonic-gate 		 * take effect after output has drained, should be
526*0Sstevel@tonic-gate 		 * processed immediately.
527*0Sstevel@tonic-gate 		 */
528*0Sstevel@tonic-gate 		switch (type) {
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate 		case M_IOCTL:
531*0Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
532*0Sstevel@tonic-gate 			switch (iocp->ioc_cmd) {
533*0Sstevel@tonic-gate 			/*
534*0Sstevel@tonic-gate 			 * Queue these.
535*0Sstevel@tonic-gate 			 */
536*0Sstevel@tonic-gate 			case TCSETSW:
537*0Sstevel@tonic-gate 			case TCSETSF:
538*0Sstevel@tonic-gate 			case TCSETAW:
539*0Sstevel@tonic-gate 			case TCSETAF:
540*0Sstevel@tonic-gate 			case TCSBRK:
541*0Sstevel@tonic-gate 				break;
542*0Sstevel@tonic-gate 
543*0Sstevel@tonic-gate 			/*
544*0Sstevel@tonic-gate 			 * Handle all others immediately.
545*0Sstevel@tonic-gate 			 */
546*0Sstevel@tonic-gate 			default:
547*0Sstevel@tonic-gate 				(void) ptemwmsg(q, mp);
548*0Sstevel@tonic-gate 				return;
549*0Sstevel@tonic-gate 			}
550*0Sstevel@tonic-gate 			break;
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 		case M_DELAY: /* tty delays not supported */
553*0Sstevel@tonic-gate 			freemsg(mp);
554*0Sstevel@tonic-gate 			return;
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 		case M_DATA:
557*0Sstevel@tonic-gate 			if ((mp->b_wptr - mp->b_rptr) < 0) {
558*0Sstevel@tonic-gate 				/*
559*0Sstevel@tonic-gate 				 * Free all bad length messages.
560*0Sstevel@tonic-gate 				 */
561*0Sstevel@tonic-gate 				freemsg(mp);
562*0Sstevel@tonic-gate 				return;
563*0Sstevel@tonic-gate 			} else if ((mp->b_wptr - mp->b_rptr) == 0) {
564*0Sstevel@tonic-gate 				if (!(ntp->state & IS_PTSTTY)) {
565*0Sstevel@tonic-gate 					freemsg(mp);
566*0Sstevel@tonic-gate 					return;
567*0Sstevel@tonic-gate 				}
568*0Sstevel@tonic-gate 			}
569*0Sstevel@tonic-gate 		}
570*0Sstevel@tonic-gate 		(void) putq(q, mp);
571*0Sstevel@tonic-gate 		return;
572*0Sstevel@tonic-gate 	}
573*0Sstevel@tonic-gate 	/*
574*0Sstevel@tonic-gate 	 * fast path into ptemwmsg to dispose of mp.
575*0Sstevel@tonic-gate 	 */
576*0Sstevel@tonic-gate 	if (!ptemwmsg(q, mp))
577*0Sstevel@tonic-gate 		(void) putq(q, mp);
578*0Sstevel@tonic-gate }
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate /*
581*0Sstevel@tonic-gate  * ptem write queue service procedure.
582*0Sstevel@tonic-gate  */
583*0Sstevel@tonic-gate static void
ptemwsrv(queue_t * q)584*0Sstevel@tonic-gate ptemwsrv(queue_t *q)
585*0Sstevel@tonic-gate {
586*0Sstevel@tonic-gate 	mblk_t *mp;
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
589*0Sstevel@tonic-gate 		if (!bcanputnext(q, mp->b_band) || !ptemwmsg(q, mp)) {
590*0Sstevel@tonic-gate 			(void) putbq(q, mp);
591*0Sstevel@tonic-gate 			break;
592*0Sstevel@tonic-gate 		}
593*0Sstevel@tonic-gate 	}
594*0Sstevel@tonic-gate }
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate /*
598*0Sstevel@tonic-gate  * This routine is called from both ptemwput and ptemwsrv to do the
599*0Sstevel@tonic-gate  * actual work of dealing with mp.  ptmewput will have already
600*0Sstevel@tonic-gate  * dealt with high priority messages.
601*0Sstevel@tonic-gate  *
602*0Sstevel@tonic-gate  * Return 1 if the message was processed completely and 0 if not.
603*0Sstevel@tonic-gate  */
604*0Sstevel@tonic-gate static int
ptemwmsg(queue_t * q,mblk_t * mp)605*0Sstevel@tonic-gate ptemwmsg(queue_t *q, mblk_t *mp)
606*0Sstevel@tonic-gate {
607*0Sstevel@tonic-gate 	struct ptem *ntp = (struct ptem *)q->q_ptr;
608*0Sstevel@tonic-gate 	struct iocblk *iocp;	/* outgoing ioctl structure */
609*0Sstevel@tonic-gate 	struct termio *termiop;
610*0Sstevel@tonic-gate 	struct termios *termiosp;
611*0Sstevel@tonic-gate 	mblk_t *dack_ptr;		/* disconnect message ACK block */
612*0Sstevel@tonic-gate 	mblk_t *pckt_msgp;		/* message sent to the PCKT module */
613*0Sstevel@tonic-gate 	mblk_t *dp;			/* ioctl reply data */
614*0Sstevel@tonic-gate 	tcflag_t cflags;
615*0Sstevel@tonic-gate 	int error;
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate 	case M_IOCTL:
620*0Sstevel@tonic-gate 		/*
621*0Sstevel@tonic-gate 		 * Note:  for each "set" type operation a copy
622*0Sstevel@tonic-gate 		 * of the M_IOCTL message is made and passed
623*0Sstevel@tonic-gate 		 * downstream.  Eventually the PCKT module, if
624*0Sstevel@tonic-gate 		 * it has been pushed, should pick up this message.
625*0Sstevel@tonic-gate 		 * If the PCKT module has not been pushed the master
626*0Sstevel@tonic-gate 		 * side stream head will free it.
627*0Sstevel@tonic-gate 		 */
628*0Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
629*0Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate 		case TCSETAF:
632*0Sstevel@tonic-gate 		case TCSETSF:
633*0Sstevel@tonic-gate 			/*
634*0Sstevel@tonic-gate 			 * Flush the read queue.
635*0Sstevel@tonic-gate 			 */
636*0Sstevel@tonic-gate 			if (putnextctl1(q, M_FLUSH, FLUSHR) == 0) {
637*0Sstevel@tonic-gate 				miocnak(q, mp, 0, EAGAIN);
638*0Sstevel@tonic-gate 				break;
639*0Sstevel@tonic-gate 			}
640*0Sstevel@tonic-gate 			/* FALLTHROUGH */
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate 		case TCSETA:
643*0Sstevel@tonic-gate 		case TCSETAW:
644*0Sstevel@tonic-gate 		case TCSETS:
645*0Sstevel@tonic-gate 		case TCSETSW:
646*0Sstevel@tonic-gate 
647*0Sstevel@tonic-gate 			switch (iocp->ioc_cmd) {
648*0Sstevel@tonic-gate 			case TCSETAF:
649*0Sstevel@tonic-gate 			case TCSETA:
650*0Sstevel@tonic-gate 			case TCSETAW:
651*0Sstevel@tonic-gate 				error = miocpullup(mp, sizeof (struct termio));
652*0Sstevel@tonic-gate 				if (error != 0) {
653*0Sstevel@tonic-gate 					miocnak(q, mp, 0, error);
654*0Sstevel@tonic-gate 					goto out;
655*0Sstevel@tonic-gate 				}
656*0Sstevel@tonic-gate 				cflags = ((struct termio *)
657*0Sstevel@tonic-gate 				    mp->b_cont->b_rptr)->c_cflag;
658*0Sstevel@tonic-gate 				ntp->cflags =
659*0Sstevel@tonic-gate 				    (ntp->cflags & 0xffff0000 | cflags);
660*0Sstevel@tonic-gate 				break;
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate 			case TCSETSF:
663*0Sstevel@tonic-gate 			case TCSETS:
664*0Sstevel@tonic-gate 			case TCSETSW:
665*0Sstevel@tonic-gate 				error = miocpullup(mp, sizeof (struct termios));
666*0Sstevel@tonic-gate 				if (error != 0) {
667*0Sstevel@tonic-gate 					miocnak(q, mp, 0, error);
668*0Sstevel@tonic-gate 					goto out;
669*0Sstevel@tonic-gate 				}
670*0Sstevel@tonic-gate 				cflags = ((struct termios *)
671*0Sstevel@tonic-gate 				    mp->b_cont->b_rptr)->c_cflag;
672*0Sstevel@tonic-gate 				ntp->cflags = cflags;
673*0Sstevel@tonic-gate 				break;
674*0Sstevel@tonic-gate 			}
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate 			if ((cflags & CBAUD) == B0) {
677*0Sstevel@tonic-gate 				/*
678*0Sstevel@tonic-gate 				 * Hang-up: Send a zero length message.
679*0Sstevel@tonic-gate 				 */
680*0Sstevel@tonic-gate 				dack_ptr = ntp->dack_ptr;
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 				if (dack_ptr) {
683*0Sstevel@tonic-gate 					ntp->dack_ptr = NULL;
684*0Sstevel@tonic-gate 					/*
685*0Sstevel@tonic-gate 					 * Send a zero length message
686*0Sstevel@tonic-gate 					 * downstream.
687*0Sstevel@tonic-gate 					 */
688*0Sstevel@tonic-gate 					putnext(q, dack_ptr);
689*0Sstevel@tonic-gate 				}
690*0Sstevel@tonic-gate 			} else {
691*0Sstevel@tonic-gate 				/*
692*0Sstevel@tonic-gate 				 * Make a copy of this message and pass it on
693*0Sstevel@tonic-gate 				 * to the PCKT module.
694*0Sstevel@tonic-gate 				 */
695*0Sstevel@tonic-gate 				if ((pckt_msgp = copymsg(mp)) == NULL) {
696*0Sstevel@tonic-gate 					miocnak(q, mp, 0, EAGAIN);
697*0Sstevel@tonic-gate 					break;
698*0Sstevel@tonic-gate 				}
699*0Sstevel@tonic-gate 				putnext(q, pckt_msgp);
700*0Sstevel@tonic-gate 			}
701*0Sstevel@tonic-gate 			/*
702*0Sstevel@tonic-gate 			 * Send ACK upstream.
703*0Sstevel@tonic-gate 			 */
704*0Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
705*0Sstevel@tonic-gate 			qreply(q, mp);
706*0Sstevel@tonic-gate out:
707*0Sstevel@tonic-gate 			break;
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate 		case TCGETA:
710*0Sstevel@tonic-gate 			dp = allocb(sizeof (struct termio), BPRI_MED);
711*0Sstevel@tonic-gate 			if (dp == NULL) {
712*0Sstevel@tonic-gate 				miocnak(q, mp, 0, EAGAIN);
713*0Sstevel@tonic-gate 				break;
714*0Sstevel@tonic-gate 			}
715*0Sstevel@tonic-gate 			termiop = (struct termio *)dp->b_rptr;
716*0Sstevel@tonic-gate 			termiop->c_cflag = (ushort_t)ntp->cflags;
717*0Sstevel@tonic-gate 			mioc2ack(mp, dp, sizeof (struct termio), 0);
718*0Sstevel@tonic-gate 			qreply(q, mp);
719*0Sstevel@tonic-gate 			break;
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 		case TCGETS:
722*0Sstevel@tonic-gate 			dp = allocb(sizeof (struct termios), BPRI_MED);
723*0Sstevel@tonic-gate 			if (dp == NULL) {
724*0Sstevel@tonic-gate 				miocnak(q, mp, 0, EAGAIN);
725*0Sstevel@tonic-gate 				break;
726*0Sstevel@tonic-gate 			}
727*0Sstevel@tonic-gate 			termiosp = (struct termios *)dp->b_rptr;
728*0Sstevel@tonic-gate 			termiosp->c_cflag = ntp->cflags;
729*0Sstevel@tonic-gate 			mioc2ack(mp, dp, sizeof (struct termios), 0);
730*0Sstevel@tonic-gate 			qreply(q, mp);
731*0Sstevel@tonic-gate 			break;
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate 		case TCSBRK:
734*0Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
735*0Sstevel@tonic-gate 			if (error != 0) {
736*0Sstevel@tonic-gate 				miocnak(q, mp, 0, error);
737*0Sstevel@tonic-gate 				break;
738*0Sstevel@tonic-gate 			}
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 			/*
741*0Sstevel@tonic-gate 			 * Need a copy of this message to pass it on to
742*0Sstevel@tonic-gate 			 * the PCKT module.
743*0Sstevel@tonic-gate 			 */
744*0Sstevel@tonic-gate 			if ((pckt_msgp = copymsg(mp)) == NULL) {
745*0Sstevel@tonic-gate 				miocnak(q, mp, 0, EAGAIN);
746*0Sstevel@tonic-gate 				break;
747*0Sstevel@tonic-gate 			}
748*0Sstevel@tonic-gate 			/*
749*0Sstevel@tonic-gate 			 * Send a copy of the M_IOCTL to the PCKT module.
750*0Sstevel@tonic-gate 			 */
751*0Sstevel@tonic-gate 			putnext(q, pckt_msgp);
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 			/*
754*0Sstevel@tonic-gate 			 * TCSBRK meaningful if data part of message is 0
755*0Sstevel@tonic-gate 			 * cf. termio(7).
756*0Sstevel@tonic-gate 			 */
757*0Sstevel@tonic-gate 			if (!(*(int *)mp->b_cont->b_rptr))
758*0Sstevel@tonic-gate 				(void) putnextctl(q, M_BREAK);
759*0Sstevel@tonic-gate 			/*
760*0Sstevel@tonic-gate 			 * ACK the ioctl.
761*0Sstevel@tonic-gate 			 */
762*0Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
763*0Sstevel@tonic-gate 			qreply(q, mp);
764*0Sstevel@tonic-gate 			break;
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 		case JWINSIZE:
767*0Sstevel@tonic-gate 		case TIOCGWINSZ:
768*0Sstevel@tonic-gate 		case TIOCSWINSZ:
769*0Sstevel@tonic-gate 			ptioc(q, mp, WRSIDE);
770*0Sstevel@tonic-gate 			break;
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate 		case TIOCSTI:
773*0Sstevel@tonic-gate 			/*
774*0Sstevel@tonic-gate 			 * Simulate typing of a character at the terminal.  In
775*0Sstevel@tonic-gate 			 * all cases, we acknowledge the ioctl and pass a copy
776*0Sstevel@tonic-gate 			 * of it along for the PCKT module to encapsulate.  If
777*0Sstevel@tonic-gate 			 * not in remote mode, we also process the ioctl
778*0Sstevel@tonic-gate 			 * itself, looping the character given as its argument
779*0Sstevel@tonic-gate 			 * back around to the read side.
780*0Sstevel@tonic-gate 			 */
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate 			/*
783*0Sstevel@tonic-gate 			 * Need a copy of this message to pass on to the PCKT
784*0Sstevel@tonic-gate 			 * module.
785*0Sstevel@tonic-gate 			 */
786*0Sstevel@tonic-gate 			if ((pckt_msgp = copymsg(mp)) == NULL) {
787*0Sstevel@tonic-gate 				miocnak(q, mp, 0, EAGAIN);
788*0Sstevel@tonic-gate 				break;
789*0Sstevel@tonic-gate 			}
790*0Sstevel@tonic-gate 			if ((ntp->state & REMOTEMODE) == 0) {
791*0Sstevel@tonic-gate 				mblk_t *bp;
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 				error = miocpullup(mp, sizeof (char));
794*0Sstevel@tonic-gate 				if (error != 0) {
795*0Sstevel@tonic-gate 					freemsg(pckt_msgp);
796*0Sstevel@tonic-gate 					miocnak(q, mp, 0, error);
797*0Sstevel@tonic-gate 					break;
798*0Sstevel@tonic-gate 				}
799*0Sstevel@tonic-gate 
800*0Sstevel@tonic-gate 				/*
801*0Sstevel@tonic-gate 				 * The permission checking has already been
802*0Sstevel@tonic-gate 				 * done at the stream head, since it has to be
803*0Sstevel@tonic-gate 				 * done in the context of the process doing
804*0Sstevel@tonic-gate 				 * the call.
805*0Sstevel@tonic-gate 				 */
806*0Sstevel@tonic-gate 				if ((bp = allocb(1, BPRI_MED)) == NULL) {
807*0Sstevel@tonic-gate 					freemsg(pckt_msgp);
808*0Sstevel@tonic-gate 					miocnak(q, mp, 0, EAGAIN);
809*0Sstevel@tonic-gate 					break;
810*0Sstevel@tonic-gate 				}
811*0Sstevel@tonic-gate 				/*
812*0Sstevel@tonic-gate 				 * XXX:	Is EAGAIN really the right response to
813*0Sstevel@tonic-gate 				 *	flow control blockage?
814*0Sstevel@tonic-gate 				 */
815*0Sstevel@tonic-gate 				if (!bcanputnext(RD(q), mp->b_band)) {
816*0Sstevel@tonic-gate 					freemsg(bp);
817*0Sstevel@tonic-gate 					freemsg(pckt_msgp);
818*0Sstevel@tonic-gate 					miocnak(q, mp, 0, EAGAIN);
819*0Sstevel@tonic-gate 					break;
820*0Sstevel@tonic-gate 				}
821*0Sstevel@tonic-gate 				*bp->b_wptr++ = *mp->b_cont->b_rptr;
822*0Sstevel@tonic-gate 				qreply(q, bp);
823*0Sstevel@tonic-gate 			}
824*0Sstevel@tonic-gate 
825*0Sstevel@tonic-gate 			putnext(q, pckt_msgp);
826*0Sstevel@tonic-gate 			mioc2ack(mp, NULL, 0, 0);
827*0Sstevel@tonic-gate 			qreply(q, mp);
828*0Sstevel@tonic-gate 			break;
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 		case PTSSTTY:
831*0Sstevel@tonic-gate 			if (ntp->state & IS_PTSTTY) {
832*0Sstevel@tonic-gate 				miocnak(q, mp, 0, EEXIST);
833*0Sstevel@tonic-gate 			} else {
834*0Sstevel@tonic-gate 				ntp->state |= IS_PTSTTY;
835*0Sstevel@tonic-gate 				mioc2ack(mp, NULL, 0, 0);
836*0Sstevel@tonic-gate 				qreply(q, mp);
837*0Sstevel@tonic-gate 			}
838*0Sstevel@tonic-gate 			break;
839*0Sstevel@tonic-gate 
840*0Sstevel@tonic-gate 		default:
841*0Sstevel@tonic-gate 			/*
842*0Sstevel@tonic-gate 			 * End of the line.  The slave driver doesn't see any
843*0Sstevel@tonic-gate 			 * ioctls that we don't explicitly pass along to it.
844*0Sstevel@tonic-gate 			 */
845*0Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
846*0Sstevel@tonic-gate 			break;
847*0Sstevel@tonic-gate 		}
848*0Sstevel@tonic-gate 		break;
849*0Sstevel@tonic-gate 
850*0Sstevel@tonic-gate 	case M_DELAY: /* tty delays not supported */
851*0Sstevel@tonic-gate 		freemsg(mp);
852*0Sstevel@tonic-gate 		break;
853*0Sstevel@tonic-gate 
854*0Sstevel@tonic-gate 	case M_DATA:
855*0Sstevel@tonic-gate 		if ((mp->b_wptr - mp->b_rptr) < 0) {
856*0Sstevel@tonic-gate 			/*
857*0Sstevel@tonic-gate 			 * Free all bad length messages.
858*0Sstevel@tonic-gate 			 */
859*0Sstevel@tonic-gate 			freemsg(mp);
860*0Sstevel@tonic-gate 			break;
861*0Sstevel@tonic-gate 		} else if ((mp->b_wptr - mp->b_rptr) == 0) {
862*0Sstevel@tonic-gate 			if (!(ntp->state & IS_PTSTTY)) {
863*0Sstevel@tonic-gate 				freemsg(mp);
864*0Sstevel@tonic-gate 				break;
865*0Sstevel@tonic-gate 			}
866*0Sstevel@tonic-gate 		}
867*0Sstevel@tonic-gate 		if (ntp->state & OFLOW_CTL)
868*0Sstevel@tonic-gate 			return (0);
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate 	default:
871*0Sstevel@tonic-gate 		putnext(q, mp);
872*0Sstevel@tonic-gate 		break;
873*0Sstevel@tonic-gate 
874*0Sstevel@tonic-gate 	}
875*0Sstevel@tonic-gate 
876*0Sstevel@tonic-gate 	return (1);
877*0Sstevel@tonic-gate }
878*0Sstevel@tonic-gate 
879*0Sstevel@tonic-gate /*
880*0Sstevel@tonic-gate  * Message must be of type M_IOCTL or M_IOCDATA for this routine to be called.
881*0Sstevel@tonic-gate  */
882*0Sstevel@tonic-gate static void
ptioc(queue_t * q,mblk_t * mp,int qside)883*0Sstevel@tonic-gate ptioc(queue_t *q, mblk_t *mp, int qside)
884*0Sstevel@tonic-gate {
885*0Sstevel@tonic-gate 	struct ptem *tp;
886*0Sstevel@tonic-gate 	struct iocblk *iocp;
887*0Sstevel@tonic-gate 	struct winsize *wb;
888*0Sstevel@tonic-gate 	struct jwinsize *jwb;
889*0Sstevel@tonic-gate 	mblk_t *tmp;
890*0Sstevel@tonic-gate 	mblk_t *pckt_msgp;	/* message sent to the PCKT module */
891*0Sstevel@tonic-gate 	int error;
892*0Sstevel@tonic-gate 
893*0Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
894*0Sstevel@tonic-gate 	tp = (struct ptem *)q->q_ptr;
895*0Sstevel@tonic-gate 
896*0Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
897*0Sstevel@tonic-gate 
898*0Sstevel@tonic-gate 	case JWINSIZE:
899*0Sstevel@tonic-gate 		/*
900*0Sstevel@tonic-gate 		 * For compatibility:  If all zeros, NAK the message for dumb
901*0Sstevel@tonic-gate 		 * terminals.
902*0Sstevel@tonic-gate 		 */
903*0Sstevel@tonic-gate 		if ((tp->wsz.ws_row == 0) && (tp->wsz.ws_col == 0) &&
904*0Sstevel@tonic-gate 		    (tp->wsz.ws_xpixel == 0) && (tp->wsz.ws_ypixel == 0)) {
905*0Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
906*0Sstevel@tonic-gate 			return;
907*0Sstevel@tonic-gate 		}
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 		tmp = allocb(sizeof (struct jwinsize), BPRI_MED);
910*0Sstevel@tonic-gate 		if (tmp == NULL) {
911*0Sstevel@tonic-gate 			miocnak(q, mp, 0, EAGAIN);
912*0Sstevel@tonic-gate 			return;
913*0Sstevel@tonic-gate 		}
914*0Sstevel@tonic-gate 
915*0Sstevel@tonic-gate 		if (iocp->ioc_count == TRANSPARENT)
916*0Sstevel@tonic-gate 			mcopyout(mp, NULL, sizeof (struct jwinsize), NULL, tmp);
917*0Sstevel@tonic-gate 		else
918*0Sstevel@tonic-gate 			mioc2ack(mp, tmp, sizeof (struct jwinsize), 0);
919*0Sstevel@tonic-gate 
920*0Sstevel@tonic-gate 		jwb = (struct jwinsize *)mp->b_cont->b_rptr;
921*0Sstevel@tonic-gate 		jwb->bytesx = tp->wsz.ws_col;
922*0Sstevel@tonic-gate 		jwb->bytesy = tp->wsz.ws_row;
923*0Sstevel@tonic-gate 		jwb->bitsx = tp->wsz.ws_xpixel;
924*0Sstevel@tonic-gate 		jwb->bitsy = tp->wsz.ws_ypixel;
925*0Sstevel@tonic-gate 
926*0Sstevel@tonic-gate 		qreply(q, mp);
927*0Sstevel@tonic-gate 		return;
928*0Sstevel@tonic-gate 
929*0Sstevel@tonic-gate 	case TIOCGWINSZ:
930*0Sstevel@tonic-gate 		/*
931*0Sstevel@tonic-gate 		 * If all zeros NAK the message for dumb terminals.
932*0Sstevel@tonic-gate 		 */
933*0Sstevel@tonic-gate 		if ((tp->wsz.ws_row == 0) && (tp->wsz.ws_col == 0) &&
934*0Sstevel@tonic-gate 		    (tp->wsz.ws_xpixel == 0) && (tp->wsz.ws_ypixel == 0)) {
935*0Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
936*0Sstevel@tonic-gate 			return;
937*0Sstevel@tonic-gate 		}
938*0Sstevel@tonic-gate 
939*0Sstevel@tonic-gate 		tmp = allocb(sizeof (struct winsize), BPRI_MED);
940*0Sstevel@tonic-gate 		if (tmp == NULL) {
941*0Sstevel@tonic-gate 			miocnak(q, mp, 0, EAGAIN);
942*0Sstevel@tonic-gate 			return;
943*0Sstevel@tonic-gate 		}
944*0Sstevel@tonic-gate 
945*0Sstevel@tonic-gate 		mioc2ack(mp, tmp, sizeof (struct winsize), 0);
946*0Sstevel@tonic-gate 
947*0Sstevel@tonic-gate 		wb = (struct winsize *)mp->b_cont->b_rptr;
948*0Sstevel@tonic-gate 		wb->ws_row = tp->wsz.ws_row;
949*0Sstevel@tonic-gate 		wb->ws_col = tp->wsz.ws_col;
950*0Sstevel@tonic-gate 		wb->ws_xpixel = tp->wsz.ws_xpixel;
951*0Sstevel@tonic-gate 		wb->ws_ypixel = tp->wsz.ws_ypixel;
952*0Sstevel@tonic-gate 
953*0Sstevel@tonic-gate 		qreply(q, mp);
954*0Sstevel@tonic-gate 		return;
955*0Sstevel@tonic-gate 
956*0Sstevel@tonic-gate 	case TIOCSWINSZ:
957*0Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct winsize));
958*0Sstevel@tonic-gate 		if (error != 0) {
959*0Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
960*0Sstevel@tonic-gate 			return;
961*0Sstevel@tonic-gate 		}
962*0Sstevel@tonic-gate 
963*0Sstevel@tonic-gate 		wb = (struct winsize *)mp->b_cont->b_rptr;
964*0Sstevel@tonic-gate 		/*
965*0Sstevel@tonic-gate 		 * Send a SIGWINCH signal if the row/col information has
966*0Sstevel@tonic-gate 		 * changed.
967*0Sstevel@tonic-gate 		 */
968*0Sstevel@tonic-gate 		if ((tp->wsz.ws_row != wb->ws_row) ||
969*0Sstevel@tonic-gate 		    (tp->wsz.ws_col != wb->ws_col) ||
970*0Sstevel@tonic-gate 		    (tp->wsz.ws_xpixel != wb->ws_xpixel) ||
971*0Sstevel@tonic-gate 		    (tp->wsz.ws_ypixel != wb->ws_xpixel)) {
972*0Sstevel@tonic-gate 			/*
973*0Sstevel@tonic-gate 			 * SIGWINCH is always sent upstream.
974*0Sstevel@tonic-gate 			 */
975*0Sstevel@tonic-gate 			if (qside == WRSIDE)
976*0Sstevel@tonic-gate 				(void) putnextctl1(RD(q), M_SIG, SIGWINCH);
977*0Sstevel@tonic-gate 			else if (qside == RDSIDE)
978*0Sstevel@tonic-gate 				(void) putnextctl1(q, M_SIG, SIGWINCH);
979*0Sstevel@tonic-gate 			/*
980*0Sstevel@tonic-gate 			 * Message may have come in as an M_IOCDATA; pass it
981*0Sstevel@tonic-gate 			 * to the master side as an M_IOCTL.
982*0Sstevel@tonic-gate 			 */
983*0Sstevel@tonic-gate 			mp->b_datap->db_type = M_IOCTL;
984*0Sstevel@tonic-gate 			if (qside == WRSIDE) {
985*0Sstevel@tonic-gate 				/*
986*0Sstevel@tonic-gate 				 * Need a copy of this message to pass on to
987*0Sstevel@tonic-gate 				 * the PCKT module, only if the M_IOCTL
988*0Sstevel@tonic-gate 				 * orginated from the slave side.
989*0Sstevel@tonic-gate 				 */
990*0Sstevel@tonic-gate 				if ((pckt_msgp = copymsg(mp)) == NULL) {
991*0Sstevel@tonic-gate 					miocnak(q, mp, 0, EAGAIN);
992*0Sstevel@tonic-gate 					return;
993*0Sstevel@tonic-gate 				}
994*0Sstevel@tonic-gate 				putnext(q, pckt_msgp);
995*0Sstevel@tonic-gate 			}
996*0Sstevel@tonic-gate 			tp->wsz.ws_row = wb->ws_row;
997*0Sstevel@tonic-gate 			tp->wsz.ws_col = wb->ws_col;
998*0Sstevel@tonic-gate 			tp->wsz.ws_xpixel = wb->ws_xpixel;
999*0Sstevel@tonic-gate 			tp->wsz.ws_ypixel = wb->ws_ypixel;
1000*0Sstevel@tonic-gate 		}
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 		mioc2ack(mp, NULL, 0, 0);
1003*0Sstevel@tonic-gate 		qreply(q, mp);
1004*0Sstevel@tonic-gate 		return;
1005*0Sstevel@tonic-gate 
1006*0Sstevel@tonic-gate 	case TIOCSIGNAL: {
1007*0Sstevel@tonic-gate 		/*
1008*0Sstevel@tonic-gate 		 * This ioctl can emanate from the master side in remote
1009*0Sstevel@tonic-gate 		 * mode only.
1010*0Sstevel@tonic-gate 		 */
1011*0Sstevel@tonic-gate 		int	sig;
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 		if (DB_TYPE(mp) == M_IOCTL && iocp->ioc_count != TRANSPARENT) {
1014*0Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
1015*0Sstevel@tonic-gate 			if (error != 0) {
1016*0Sstevel@tonic-gate 				miocnak(q, mp, 0, error);
1017*0Sstevel@tonic-gate 				return;
1018*0Sstevel@tonic-gate 			}
1019*0Sstevel@tonic-gate 		}
1020*0Sstevel@tonic-gate 
1021*0Sstevel@tonic-gate 		if (DB_TYPE(mp) == M_IOCDATA || iocp->ioc_count != TRANSPARENT)
1022*0Sstevel@tonic-gate 			sig = *(int *)mp->b_cont->b_rptr;
1023*0Sstevel@tonic-gate 		else
1024*0Sstevel@tonic-gate 			sig = (int)*(intptr_t *)mp->b_cont->b_rptr;
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 		if (sig < 1 || sig >= NSIG) {
1027*0Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
1028*0Sstevel@tonic-gate 			return;
1029*0Sstevel@tonic-gate 		}
1030*0Sstevel@tonic-gate 
1031*0Sstevel@tonic-gate 		/*
1032*0Sstevel@tonic-gate 		 * Send an M_PCSIG message up the slave's read side and
1033*0Sstevel@tonic-gate 		 * respond back to the master with an ACK or NAK as
1034*0Sstevel@tonic-gate 		 * appropriate.
1035*0Sstevel@tonic-gate 		 */
1036*0Sstevel@tonic-gate 		if (putnextctl1(q, M_PCSIG, sig) == 0) {
1037*0Sstevel@tonic-gate 			miocnak(q, mp, 0, EAGAIN);
1038*0Sstevel@tonic-gate 			return;
1039*0Sstevel@tonic-gate 		}
1040*0Sstevel@tonic-gate 
1041*0Sstevel@tonic-gate 		mioc2ack(mp, NULL, 0, 0);
1042*0Sstevel@tonic-gate 		qreply(q, mp);
1043*0Sstevel@tonic-gate 		return;
1044*0Sstevel@tonic-gate 	    }
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate 	case TIOCREMOTE: {
1047*0Sstevel@tonic-gate 		int	onoff;
1048*0Sstevel@tonic-gate 		mblk_t	*mctlp;
1049*0Sstevel@tonic-gate 
1050*0Sstevel@tonic-gate 		if (DB_TYPE(mp) == M_IOCTL) {
1051*0Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (int));
1052*0Sstevel@tonic-gate 			if (error != 0) {
1053*0Sstevel@tonic-gate 				miocnak(q, mp, 0, error);
1054*0Sstevel@tonic-gate 				return;
1055*0Sstevel@tonic-gate 			}
1056*0Sstevel@tonic-gate 		}
1057*0Sstevel@tonic-gate 
1058*0Sstevel@tonic-gate 		onoff = *(int *)mp->b_cont->b_rptr;
1059*0Sstevel@tonic-gate 
1060*0Sstevel@tonic-gate 		/*
1061*0Sstevel@tonic-gate 		 * Send M_CTL up using the iocblk format.
1062*0Sstevel@tonic-gate 		 */
1063*0Sstevel@tonic-gate 		mctlp = mkiocb(onoff ? MC_NO_CANON : MC_DO_CANON);
1064*0Sstevel@tonic-gate 		if (mctlp == NULL) {
1065*0Sstevel@tonic-gate 			miocnak(q, mp, 0, EAGAIN);
1066*0Sstevel@tonic-gate 			return;
1067*0Sstevel@tonic-gate 		}
1068*0Sstevel@tonic-gate 		mctlp->b_datap->db_type = M_CTL;
1069*0Sstevel@tonic-gate 		putnext(q, mctlp);
1070*0Sstevel@tonic-gate 
1071*0Sstevel@tonic-gate 		/*
1072*0Sstevel@tonic-gate 		 * ACK the ioctl.
1073*0Sstevel@tonic-gate 		 */
1074*0Sstevel@tonic-gate 		mioc2ack(mp, NULL, 0, 0);
1075*0Sstevel@tonic-gate 		qreply(q, mp);
1076*0Sstevel@tonic-gate 
1077*0Sstevel@tonic-gate 		/*
1078*0Sstevel@tonic-gate 		 * Record state change.
1079*0Sstevel@tonic-gate 		 */
1080*0Sstevel@tonic-gate 		if (onoff)
1081*0Sstevel@tonic-gate 			tp->state |= REMOTEMODE;
1082*0Sstevel@tonic-gate 		else
1083*0Sstevel@tonic-gate 			tp->state &= ~REMOTEMODE;
1084*0Sstevel@tonic-gate 		return;
1085*0Sstevel@tonic-gate 	    }
1086*0Sstevel@tonic-gate 
1087*0Sstevel@tonic-gate 	default:
1088*0Sstevel@tonic-gate 		putnext(q, mp);
1089*0Sstevel@tonic-gate 		return;
1090*0Sstevel@tonic-gate 	}
1091*0Sstevel@tonic-gate }
1092