Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.37
      1 /*	$NetBSD: tty_pty.c,v 1.37 1996/09/05 15:31:40 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)tty_pty.c	8.2 (Berkeley) 9/23/93
     36  */
     37 
     38 /*
     39  * Pseudo-teletype Driver
     40  * (Actually two drivers, requiring two entries in 'cdevsw')
     41  */
     42 #include "pty.h"		/* XXX */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/proc.h>
     48 #include <sys/tty.h>
     49 #include <sys/file.h>
     50 #include <sys/uio.h>
     51 #include <sys/kernel.h>
     52 #include <sys/vnode.h>
     53 #include <sys/signalvar.h>
     54 #include <sys/uio.h>
     55 #include <sys/conf.h>
     56 
     57 
     58 
     59 #if NPTY == 1
     60 #undef NPTY
     61 #define	NPTY	32		/* crude XXX */
     62 #endif
     63 
     64 /* Macros to clear/set/test flags. */
     65 #define	SET(t, f)	(t) |= (f)
     66 #define	CLR(t, f)	(t) &= ~((unsigned)(f))
     67 #define	ISSET(t, f)	((t) & (f))
     68 
     69 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
     70 
     71 /*
     72  * pts == /dev/tty[pqrs]?
     73  * ptc == /dev/pty[pqrs]?
     74  */
     75 struct	pt_softc {
     76 	struct	tty *pt_tty;
     77 	int	pt_flags;
     78 	struct	selinfo pt_selr, pt_selw;
     79 	u_char	pt_send;
     80 	u_char	pt_ucntl;
     81 } pt_softc[NPTY];		/* XXX */
     82 int	npty = NPTY;		/* for pstat -t */
     83 
     84 #define	PF_PKT		0x08		/* packet mode */
     85 #define	PF_STOPPED	0x10		/* user told stopped */
     86 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
     87 #define	PF_NOSTOP	0x40
     88 #define PF_UCNTL	0x80		/* user control mode */
     89 
     90 void	ptyattach __P((int));
     91 void	ptcwakeup __P((struct tty *, int));
     92 struct tty *ptytty __P((dev_t));
     93 void	ptsstart __P((struct tty *));
     94 
     95 /*
     96  * Establish n (or default if n is 1) ptys in the system.
     97  */
     98 void
     99 ptyattach(n)
    100 	int n;
    101 {
    102 #ifdef notyet
    103 #define	DEFAULT_NPTY	32
    104 
    105 	/* maybe should allow 0 => none? */
    106 	if (n <= 1)
    107 		n = DEFAULT_NPTY;
    108 	pt_softc = malloc(n * sizeof(struct pt_softc), M_DEVBUF, M_WAITOK);
    109 	npty = n;
    110 #endif
    111 }
    112 
    113 /*ARGSUSED*/
    114 int
    115 ptsopen(dev, flag, devtype, p)
    116 	dev_t dev;
    117 	int flag, devtype;
    118 	struct proc *p;
    119 {
    120 	struct pt_softc *pti;
    121 	register struct tty *tp;
    122 	int error;
    123 
    124 	if (minor(dev) >= npty)
    125 		return (ENXIO);
    126 	pti = &pt_softc[minor(dev)];
    127 	if (!pti->pt_tty) {
    128 		tp = pti->pt_tty = ttymalloc();
    129 		tty_attach(tp);
    130 	}
    131 	else
    132 		tp = pti->pt_tty;
    133 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    134 		SET(tp->t_state, TS_WOPEN);
    135 		ttychars(tp);		/* Set up default chars */
    136 		tp->t_iflag = TTYDEF_IFLAG;
    137 		tp->t_oflag = TTYDEF_OFLAG;
    138 		tp->t_lflag = TTYDEF_LFLAG;
    139 		tp->t_cflag = TTYDEF_CFLAG;
    140 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    141 		ttsetwater(tp);		/* would be done in xxparam() */
    142 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    143 		return (EBUSY);
    144 	if (tp->t_oproc)			/* Ctrlr still around. */
    145 		SET(tp->t_state, TS_CARR_ON);
    146 	while (!ISSET(tp->t_state, TS_CARR_ON)) {
    147 		SET(tp->t_state, TS_WOPEN);
    148 		if (flag&FNONBLOCK)
    149 			break;
    150 		error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
    151 				 ttopen, 0);
    152 		if (error)
    153 			return (error);
    154 	}
    155 	error = (*linesw[tp->t_line].l_open)(dev, tp);
    156 	ptcwakeup(tp, FREAD|FWRITE);
    157 	return (error);
    158 }
    159 
    160 int
    161 ptsclose(dev, flag, mode, p)
    162 	dev_t dev;
    163 	int flag, mode;
    164 	struct proc *p;
    165 {
    166 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    167 	register struct tty *tp = pti->pt_tty;
    168 	int error;
    169 
    170 	error = (*linesw[tp->t_line].l_close)(tp, flag);
    171 	error |= ttyclose(tp);
    172 	ptcwakeup(tp, FREAD|FWRITE);
    173 	return (error);
    174 }
    175 
    176 int
    177 ptsread(dev, uio, flag)
    178 	dev_t dev;
    179 	struct uio *uio;
    180 	int flag;
    181 {
    182 	struct proc *p = curproc;
    183 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    184 	register struct tty *tp = pti->pt_tty;
    185 	int error = 0;
    186 
    187 again:
    188 	if (pti->pt_flags & PF_REMOTE) {
    189 		while (isbackground(p, tp)) {
    190 			if ((p->p_sigignore & sigmask(SIGTTIN)) ||
    191 			    (p->p_sigmask & sigmask(SIGTTIN)) ||
    192 			    p->p_pgrp->pg_jobc == 0 ||
    193 			    p->p_flag & P_PPWAIT)
    194 				return (EIO);
    195 			pgsignal(p->p_pgrp, SIGTTIN, 1);
    196 			error = ttysleep(tp, (caddr_t)&lbolt,
    197 					 TTIPRI | PCATCH, ttybg, 0);
    198 			if (error)
    199 				return (error);
    200 		}
    201 		if (tp->t_canq.c_cc == 0) {
    202 			if (flag & IO_NDELAY)
    203 				return (EWOULDBLOCK);
    204 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
    205 					 TTIPRI | PCATCH, ttyin, 0);
    206 			if (error)
    207 				return (error);
    208 			goto again;
    209 		}
    210 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
    211 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
    212 				error = EFAULT;
    213 				break;
    214 			}
    215 		if (tp->t_canq.c_cc == 1)
    216 			(void) getc(&tp->t_canq);
    217 		if (tp->t_canq.c_cc)
    218 			return (error);
    219 	} else
    220 		if (tp->t_oproc)
    221 			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
    222 	ptcwakeup(tp, FWRITE);
    223 	return (error);
    224 }
    225 
    226 /*
    227  * Write to pseudo-tty.
    228  * Wakeups of controlling tty will happen
    229  * indirectly, when tty driver calls ptsstart.
    230  */
    231 int
    232 ptswrite(dev, uio, flag)
    233 	dev_t dev;
    234 	struct uio *uio;
    235 	int flag;
    236 {
    237 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    238 	register struct tty *tp = pti->pt_tty;
    239 
    240 	if (tp->t_oproc == 0)
    241 		return (EIO);
    242 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    243 }
    244 
    245 /*
    246  * Start output on pseudo-tty.
    247  * Wake up process selecting or sleeping for input from controlling tty.
    248  */
    249 void
    250 ptsstart(tp)
    251 	struct tty *tp;
    252 {
    253 	register struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    254 
    255 	if (ISSET(tp->t_state, TS_TTSTOP))
    256 		return;
    257 	if (pti->pt_flags & PF_STOPPED) {
    258 		pti->pt_flags &= ~PF_STOPPED;
    259 		pti->pt_send = TIOCPKT_START;
    260 	}
    261 	ptcwakeup(tp, FREAD);
    262 }
    263 
    264 void
    265 ptsstop(tp, flush)
    266 	register struct tty *tp;
    267 	int flush;
    268 {
    269 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    270 	int flag;
    271 
    272 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    273 	if (flush == 0) {
    274 		flush = TIOCPKT_STOP;
    275 		pti->pt_flags |= PF_STOPPED;
    276 	} else
    277 		pti->pt_flags &= ~PF_STOPPED;
    278 	pti->pt_send |= flush;
    279 	/* change of perspective */
    280 	flag = 0;
    281 	if (flush & FREAD)
    282 		flag |= FWRITE;
    283 	if (flush & FWRITE)
    284 		flag |= FREAD;
    285 	ptcwakeup(tp, flag);
    286 }
    287 
    288 void
    289 ptcwakeup(tp, flag)
    290 	struct tty *tp;
    291 	int flag;
    292 {
    293 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
    294 
    295 	if (flag & FREAD) {
    296 		selwakeup(&pti->pt_selr);
    297 		wakeup((caddr_t)&tp->t_outq.c_cf);
    298 	}
    299 	if (flag & FWRITE) {
    300 		selwakeup(&pti->pt_selw);
    301 		wakeup((caddr_t)&tp->t_rawq.c_cf);
    302 	}
    303 }
    304 
    305 int ptcopen __P((dev_t, int, int, struct proc *));
    306 
    307 /*ARGSUSED*/
    308 int
    309 ptcopen(dev, flag, devtype, p)
    310 	dev_t dev;
    311 	int flag, devtype;
    312 	struct proc *p;
    313 {
    314 	struct pt_softc *pti;
    315 	register struct tty *tp;
    316 
    317 	if (minor(dev) >= npty)
    318 		return (ENXIO);
    319 	pti = &pt_softc[minor(dev)];
    320 	if (!pti->pt_tty) {
    321 		tp = pti->pt_tty = ttymalloc();
    322 		tty_attach(tp);
    323 	}
    324 	else
    325 		tp = pti->pt_tty;
    326 	if (tp->t_oproc)
    327 		return (EIO);
    328 	tp->t_oproc = ptsstart;
    329 	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
    330 	CLR(tp->t_lflag, EXTPROC);
    331 	pti->pt_flags = 0;
    332 	pti->pt_send = 0;
    333 	pti->pt_ucntl = 0;
    334 	return (0);
    335 }
    336 
    337 /*ARGSUSED*/
    338 int
    339 ptcclose(dev, flag, devtype, p)
    340 	dev_t dev;
    341 	int flag, devtype;
    342 	struct proc *p;
    343 {
    344 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    345 	register struct tty *tp = pti->pt_tty;
    346 
    347 	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
    348 	CLR(tp->t_state, TS_CARR_ON);
    349 	tp->t_oproc = 0;		/* mark closed */
    350 	return (0);
    351 }
    352 
    353 int
    354 ptcread(dev, uio, flag)
    355 	dev_t dev;
    356 	struct uio *uio;
    357 	int flag;
    358 {
    359 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    360 	register struct tty *tp = pti->pt_tty;
    361 	char buf[BUFSIZ];
    362 	int error = 0, cc;
    363 
    364 	/*
    365 	 * We want to block until the slave
    366 	 * is open, and there's something to read;
    367 	 * but if we lost the slave or we're NBIO,
    368 	 * then return the appropriate error instead.
    369 	 */
    370 	for (;;) {
    371 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    372 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
    373 				error = ureadc((int)pti->pt_send, uio);
    374 				if (error)
    375 					return (error);
    376 				if (pti->pt_send & TIOCPKT_IOCTL) {
    377 					cc = min(uio->uio_resid,
    378 						sizeof(tp->t_termios));
    379 					uiomove((caddr_t) &tp->t_termios,
    380 						cc, uio);
    381 				}
    382 				pti->pt_send = 0;
    383 				return (0);
    384 			}
    385 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
    386 				error = ureadc((int)pti->pt_ucntl, uio);
    387 				if (error)
    388 					return (error);
    389 				pti->pt_ucntl = 0;
    390 				return (0);
    391 			}
    392 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
    393 				break;
    394 		}
    395 		if (!ISSET(tp->t_state, TS_CARR_ON))
    396 			return (0);	/* EOF */
    397 		if (flag & IO_NDELAY)
    398 			return (EWOULDBLOCK);
    399 		error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
    400 			       ttyin, 0);
    401 		if (error)
    402 			return (error);
    403 	}
    404 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
    405 		error = ureadc(0, uio);
    406 	while (uio->uio_resid > 0 && error == 0) {
    407 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
    408 		if (cc <= 0)
    409 			break;
    410 		error = uiomove(buf, cc, uio);
    411 	}
    412 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    413 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    414 			CLR(tp->t_state, TS_ASLEEP);
    415 			wakeup((caddr_t)&tp->t_outq);
    416 		}
    417 		selwakeup(&tp->t_wsel);
    418 	}
    419 	return (error);
    420 }
    421 
    422 
    423 int
    424 ptcwrite(dev, uio, flag)
    425 	dev_t dev;
    426 	register struct uio *uio;
    427 	int flag;
    428 {
    429 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    430 	register struct tty *tp = pti->pt_tty;
    431 	register u_char *cp = NULL;
    432 	register int cc = 0;
    433 	u_char locbuf[BUFSIZ];
    434 	int cnt = 0;
    435 	int error = 0;
    436 
    437 again:
    438 	if (!ISSET(tp->t_state, TS_ISOPEN))
    439 		goto block;
    440 	if (pti->pt_flags & PF_REMOTE) {
    441 		if (tp->t_canq.c_cc)
    442 			goto block;
    443 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
    444 			if (cc == 0) {
    445 				cc = min(uio->uio_resid, BUFSIZ);
    446 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
    447 				cp = locbuf;
    448 				error = uiomove((caddr_t)cp, cc, uio);
    449 				if (error)
    450 					return (error);
    451 				/* check again for safety */
    452 				if (!ISSET(tp->t_state, TS_ISOPEN))
    453 					return (EIO);
    454 			}
    455 			if (cc)
    456 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
    457 			cc = 0;
    458 		}
    459 		(void) putc(0, &tp->t_canq);
    460 		ttwakeup(tp);
    461 		wakeup((caddr_t)&tp->t_canq);
    462 		return (0);
    463 	}
    464 	while (uio->uio_resid > 0) {
    465 		if (cc == 0) {
    466 			cc = min(uio->uio_resid, BUFSIZ);
    467 			cp = locbuf;
    468 			error = uiomove((caddr_t)cp, cc, uio);
    469 			if (error)
    470 				return (error);
    471 			/* check again for safety */
    472 			if (!ISSET(tp->t_state, TS_ISOPEN))
    473 				return (EIO);
    474 		}
    475 		while (cc > 0) {
    476 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
    477 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_iflag, ICANON))) {
    478 				wakeup((caddr_t)&tp->t_rawq);
    479 				goto block;
    480 			}
    481 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
    482 			cnt++;
    483 			cc--;
    484 		}
    485 		cc = 0;
    486 	}
    487 	return (0);
    488 block:
    489 	/*
    490 	 * Come here to wait for slave to open, for space
    491 	 * in outq, or space in rawq.
    492 	 */
    493 	if (!ISSET(tp->t_state, TS_CARR_ON))
    494 		return (EIO);
    495 	if (flag & IO_NDELAY) {
    496 		/* adjust for data copied in but not written */
    497 		uio->uio_resid += cc;
    498 		if (cnt == 0)
    499 			return (EWOULDBLOCK);
    500 		return (0);
    501 	}
    502 	error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
    503 		       ttyout, 0);
    504 	if (error) {
    505 		/* adjust for data copied in but not written */
    506 		uio->uio_resid += cc;
    507 		return (error);
    508 	}
    509 	goto again;
    510 }
    511 
    512 int
    513 ptcselect(dev, rw, p)
    514 	dev_t dev;
    515 	int rw;
    516 	struct proc *p;
    517 {
    518 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    519 	register struct tty *tp = pti->pt_tty;
    520 	int s;
    521 
    522 	if (!ISSET(tp->t_state, TS_CARR_ON))
    523 		return (1);
    524 	switch (rw) {
    525 
    526 	case FREAD:
    527 		/*
    528 		 * Need to block timeouts (ttrstart).
    529 		 */
    530 		s = spltty();
    531 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    532 		     tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP)) {
    533 			splx(s);
    534 			return (1);
    535 		}
    536 		splx(s);
    537 		/* FALLTHROUGH */
    538 
    539 	case 0:					/* exceptional */
    540 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    541 		    (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    542 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    543 			return (1);
    544 		selrecord(p, &pti->pt_selr);
    545 		break;
    546 
    547 
    548 	case FWRITE:
    549 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    550 			if (pti->pt_flags & PF_REMOTE) {
    551 			    if (tp->t_canq.c_cc == 0)
    552 				return (1);
    553 			} else {
    554 			    if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2)
    555 				    return (1);
    556 			    if (tp->t_canq.c_cc == 0 && ISSET(tp->t_iflag, ICANON))
    557 				    return (1);
    558 			}
    559 		}
    560 		selrecord(p, &pti->pt_selw);
    561 		break;
    562 
    563 	}
    564 	return (0);
    565 }
    566 
    567 
    568 struct tty *
    569 ptytty(dev)
    570 	dev_t dev;
    571 {
    572 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    573 	register struct tty *tp = pti->pt_tty;
    574 
    575 	return (tp);
    576 }
    577 
    578 /*ARGSUSED*/
    579 int
    580 ptyioctl(dev, cmd, data, flag, p)
    581 	dev_t dev;
    582 	u_long cmd;
    583 	caddr_t data;
    584 	int flag;
    585 	struct proc *p;
    586 {
    587 	register struct pt_softc *pti = &pt_softc[minor(dev)];
    588 	register struct tty *tp = pti->pt_tty;
    589 	register u_char *cc = tp->t_cc;
    590 	int stop, error;
    591 
    592 	/*
    593 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
    594 	 * ttywflush(tp) will hang if there are characters in the outq.
    595 	 */
    596 	if (cmd == TIOCEXT) {
    597 		/*
    598 		 * When the EXTPROC bit is being toggled, we need
    599 		 * to send an TIOCPKT_IOCTL if the packet driver
    600 		 * is turned on.
    601 		 */
    602 		if (*(int *)data) {
    603 			if (pti->pt_flags & PF_PKT) {
    604 				pti->pt_send |= TIOCPKT_IOCTL;
    605 				ptcwakeup(tp, FREAD);
    606 			}
    607 			SET(tp->t_lflag, EXTPROC);
    608 		} else {
    609 			if (ISSET(tp->t_state, EXTPROC) &&
    610 			    (pti->pt_flags & PF_PKT)) {
    611 				pti->pt_send |= TIOCPKT_IOCTL;
    612 				ptcwakeup(tp, FREAD);
    613 			}
    614 			CLR(tp->t_lflag, EXTPROC);
    615 		}
    616 		return(0);
    617 	} else
    618 	if (cdevsw[major(dev)].d_open == ptcopen)
    619 		switch (cmd) {
    620 
    621 		case TIOCGPGRP:
    622 #ifdef COMPAT_SUNOS
    623 			{
    624 			/*
    625 			 * I'm not sure about SunOS TIOCGPGRP semantics
    626 			 * on PTYs, but it's something like this:
    627 			 */
    628 			extern struct emul emul_sunos;
    629 			if (p->p_emul == &emul_sunos && tp->t_pgrp == 0)
    630 				return (EIO);
    631 			*(int *)data = tp->t_pgrp->pg_id;
    632 			return (0);
    633 			}
    634 #endif
    635 			/*
    636 			 * We avoid calling ttioctl on the controller since,
    637 			 * in that case, tp must be the controlling terminal.
    638 			 */
    639 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
    640 			return (0);
    641 
    642 		case TIOCPKT:
    643 			if (*(int *)data) {
    644 				if (pti->pt_flags & PF_UCNTL)
    645 					return (EINVAL);
    646 				pti->pt_flags |= PF_PKT;
    647 			} else
    648 				pti->pt_flags &= ~PF_PKT;
    649 			return (0);
    650 
    651 		case TIOCUCNTL:
    652 			if (*(int *)data) {
    653 				if (pti->pt_flags & PF_PKT)
    654 					return (EINVAL);
    655 				pti->pt_flags |= PF_UCNTL;
    656 			} else
    657 				pti->pt_flags &= ~PF_UCNTL;
    658 			return (0);
    659 
    660 		case TIOCREMOTE:
    661 			if (*(int *)data)
    662 				pti->pt_flags |= PF_REMOTE;
    663 			else
    664 				pti->pt_flags &= ~PF_REMOTE;
    665 			ttyflush(tp, FREAD|FWRITE);
    666 			return (0);
    667 
    668 #ifdef COMPAT_OLDTTY
    669 		case TIOCSETP:
    670 		case TIOCSETN:
    671 #endif
    672 		case TIOCSETD:
    673 		case TIOCSETA:
    674 		case TIOCSETAW:
    675 		case TIOCSETAF:
    676 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
    677 			break;
    678 
    679 		case TIOCSIG:
    680 			if (*(unsigned int *)data >= NSIG)
    681 				return(EINVAL);
    682 			if (!ISSET(tp->t_lflag, NOFLSH))
    683 				ttyflush(tp, FREAD|FWRITE);
    684 			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
    685 			if ((*(unsigned int *)data == SIGINFO) &&
    686 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
    687 				ttyinfo(tp);
    688 			return(0);
    689 		}
    690 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    691 	if (error < 0)
    692 		 error = ttioctl(tp, cmd, data, flag, p);
    693 	if (error < 0) {
    694 		if (pti->pt_flags & PF_UCNTL &&
    695 		    (cmd & ~0xff) == UIOCCMD(0)) {
    696 			if (cmd & 0xff) {
    697 				pti->pt_ucntl = (u_char)cmd;
    698 				ptcwakeup(tp, FREAD);
    699 			}
    700 			return (0);
    701 		}
    702 		error = ENOTTY;
    703 	}
    704 	/*
    705 	 * If external processing and packet mode send ioctl packet.
    706 	 */
    707 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
    708 		switch(cmd) {
    709 		case TIOCSETA:
    710 		case TIOCSETAW:
    711 		case TIOCSETAF:
    712 #ifdef COMPAT_OLDTTY
    713 		case TIOCSETP:
    714 		case TIOCSETN:
    715 		case TIOCSETC:
    716 		case TIOCSLTC:
    717 		case TIOCLBIS:
    718 		case TIOCLBIC:
    719 		case TIOCLSET:
    720 #endif
    721 			pti->pt_send |= TIOCPKT_IOCTL;
    722 			ptcwakeup(tp, FREAD);
    723 		default:
    724 			break;
    725 		}
    726 	}
    727 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
    728 		&& CCEQ(cc[VSTART], CTRL('q'));
    729 	if (pti->pt_flags & PF_NOSTOP) {
    730 		if (stop) {
    731 			pti->pt_send &= ~TIOCPKT_NOSTOP;
    732 			pti->pt_send |= TIOCPKT_DOSTOP;
    733 			pti->pt_flags &= ~PF_NOSTOP;
    734 			ptcwakeup(tp, FREAD);
    735 		}
    736 	} else {
    737 		if (!stop) {
    738 			pti->pt_send &= ~TIOCPKT_DOSTOP;
    739 			pti->pt_send |= TIOCPKT_NOSTOP;
    740 			pti->pt_flags |= PF_NOSTOP;
    741 			ptcwakeup(tp, FREAD);
    742 		}
    743 	}
    744 	return (error);
    745 }
    746