pty.c revision 59818
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
3314511Shsu *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
3450477Speter * $FreeBSD: head/sys/kern/tty_pty.c 59818 2000-05-01 10:24:21Z ache $
351541Srgrimes */
361541Srgrimes
371541Srgrimes/*
381541Srgrimes * Pseudo-teletype Driver
391541Srgrimes * (Actually two drivers, requiring two entries in 'cdevsw')
401541Srgrimes */
411541Srgrimes#include "pty.h"		/* XXX */
4231778Seivind#include "opt_compat.h"
431541Srgrimes#include <sys/param.h>
441541Srgrimes#include <sys/systm.h>
4524207Sbde#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
4624207Sbde#include <sys/ioctl_compat.h>
4724207Sbde#endif
481541Srgrimes#include <sys/proc.h>
491541Srgrimes#include <sys/tty.h>
501541Srgrimes#include <sys/conf.h>
5124131Sbde#include <sys/fcntl.h>
5229354Speter#include <sys/poll.h>
531541Srgrimes#include <sys/kernel.h>
541541Srgrimes#include <sys/vnode.h>
553308Sphk#include <sys/signalvar.h>
5649536Sphk#include <sys/malloc.h>
571541Srgrimes
5849536SphkMALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
5912517Sjulian
6012819Sphkstatic void ptsstart __P((struct tty *tp));
6151654Sphkstatic void ptsstop __P((struct tty *tp, int rw));
6212819Sphkstatic void ptcwakeup __P((struct tty *tp, int flag));
6349536Sphkstatic void ptyinit __P((int n));
6411789Sbde
6512675Sjulianstatic	d_open_t	ptsopen;
6612675Sjulianstatic	d_close_t	ptsclose;
6712675Sjulianstatic	d_read_t	ptsread;
6812675Sjulianstatic	d_write_t	ptswrite;
6912675Sjulianstatic	d_ioctl_t	ptyioctl;
7012675Sjulianstatic	d_open_t	ptcopen;
7112675Sjulianstatic	d_close_t	ptcclose;
7212675Sjulianstatic	d_read_t	ptcread;
7312675Sjulianstatic	d_write_t	ptcwrite;
7429354Speterstatic	d_poll_t	ptcpoll;
7512675Sjulian
7638485Sbde#define	CDEV_MAJOR_S	5
7747625Sphkstatic struct cdevsw pts_cdevsw = {
7847625Sphk	/* open */	ptsopen,
7947625Sphk	/* close */	ptsclose,
8047625Sphk	/* read */	ptsread,
8147625Sphk	/* write */	ptswrite,
8247625Sphk	/* ioctl */	ptyioctl,
8351654Sphk	/* poll */	ttypoll,
8447625Sphk	/* mmap */	nommap,
8547625Sphk	/* strategy */	nostrategy,
8647625Sphk	/* name */	"pts",
8747625Sphk	/* maj */	CDEV_MAJOR_S,
8847625Sphk	/* dump */	nodump,
8947625Sphk	/* psize */	nopsize,
9047625Sphk	/* flags */	D_TTY,
9147625Sphk	/* bmaj */	-1
9238485Sbde};
9312675Sjulian
9438485Sbde#define	CDEV_MAJOR_C	6
9547625Sphkstatic struct cdevsw ptc_cdevsw = {
9647625Sphk	/* open */	ptcopen,
9747625Sphk	/* close */	ptcclose,
9847625Sphk	/* read */	ptcread,
9947625Sphk	/* write */	ptcwrite,
10047625Sphk	/* ioctl */	ptyioctl,
10147625Sphk	/* poll */	ptcpoll,
10247625Sphk	/* mmap */	nommap,
10347625Sphk	/* strategy */	nostrategy,
10447625Sphk	/* name */	"ptc",
10547625Sphk	/* maj */	CDEV_MAJOR_C,
10647625Sphk	/* dump */	nodump,
10747625Sphk	/* psize */	nopsize,
10847625Sphk	/* flags */	D_TTY,
10947625Sphk	/* bmaj */	-1
11038485Sbde};
11112675Sjulian
1121541Srgrimes#define BUFSIZ 100		/* Chunk size iomoved to/from user */
1131541Srgrimes
11449536Sphkstruct	pt_ioctl {
1151541Srgrimes	int	pt_flags;
1161541Srgrimes	struct	selinfo pt_selr, pt_selw;
1171541Srgrimes	u_char	pt_send;
1181541Srgrimes	u_char	pt_ucntl;
11949536Sphk	struct tty pt_tty;
12050092Sjulian	dev_t	devs, devc;
12157070Srwatson	struct	prison *pt_prison;
12249536Sphk};
1231541Srgrimes
1241541Srgrimes#define	PF_PKT		0x08		/* packet mode */
1251541Srgrimes#define	PF_STOPPED	0x10		/* user told stopped */
1261541Srgrimes#define	PF_REMOTE	0x20		/* remote and flow controlled input */
1271541Srgrimes#define	PF_NOSTOP	0x40
1281541Srgrimes#define PF_UCNTL	0x80		/* user control mode */
1291541Srgrimes
1301541Srgrimes/*
13149536Sphk * This function creates and initializes a pts/ptc pair
1321541Srgrimes *
13349536Sphk * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
13449536Sphk * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
13549536Sphk *
13649536Sphk * XXX: define and add mapping of upper minor bits to allow more
13749536Sphk *      than 256 ptys.
1381541Srgrimes */
13912819Sphkstatic void
14049536Sphkptyinit(n)
1411541Srgrimes	int n;
1421541Srgrimes{
14349536Sphk	dev_t devs, devc;
14449536Sphk	char *names = "pqrsPQRS";
14549536Sphk	struct pt_ioctl *pt;
1461541Srgrimes
14749536Sphk	/* For now we only map the lower 8 bits of the minor */
14849536Sphk	if (n & ~0xff)
14949536Sphk		return;
15049536Sphk
15150092Sjulian	pt = malloc(sizeof(*pt), M_PTY, M_WAITOK);
15250092Sjulian	bzero(pt, sizeof(*pt));
15350092Sjulian	pt->devs = devs = make_dev(&pts_cdevsw, n,
15449536Sphk	    0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
15550092Sjulian	pt->devc = devc = make_dev(&ptc_cdevsw, n,
15649536Sphk	    0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
15749536Sphk
15849536Sphk	devs->si_drv1 = devc->si_drv1 = pt;
15950652Sphk	devs->si_tty = devc->si_tty = &pt->pt_tty;
16049540Sphk	ttyregister(&pt->pt_tty);
16116322Sgpalmer}
1621541Srgrimes
1631541Srgrimes/*ARGSUSED*/
16412675Sjulianstatic	int
1651541Srgrimesptsopen(dev, flag, devtype, p)
1661541Srgrimes	dev_t dev;
1671541Srgrimes	int flag, devtype;
1681541Srgrimes	struct proc *p;
1691541Srgrimes{
1701541Srgrimes	register struct tty *tp;
1711541Srgrimes	int error;
17249992Sjulian	int minr;
17349992Sjulian	dev_t nextdev;
17457070Srwatson	struct pt_ioctl *pti;
1751541Srgrimes
17649992Sjulian	/*
17750254Sphk	 * XXX: Gross hack for DEVFS:
17849992Sjulian	 * If we openned this device, ensure we have the
17950254Sphk	 * next one too, so people can open it.
18049992Sjulian	 */
18149992Sjulian	minr = lminor(dev);
18249992Sjulian	if (minr < 255) {
18349992Sjulian		nextdev = makedev(major(dev), minr + 1);
18449992Sjulian		if (!nextdev->si_drv1) {
18549992Sjulian			ptyinit(minr + 1);
18649992Sjulian		}
18749992Sjulian	}
18849536Sphk	if (!dev->si_drv1)
18949536Sphk		ptyinit(minor(dev));
19049536Sphk	if (!dev->si_drv1)
19149536Sphk		return(ENXIO);
19257070Srwatson	pti = dev->si_drv1;
19350652Sphk	tp = dev->si_tty;
1941541Srgrimes	if ((tp->t_state & TS_ISOPEN) == 0) {
1951541Srgrimes		ttychars(tp);		/* Set up default chars */
1961541Srgrimes		tp->t_iflag = TTYDEF_IFLAG;
1971541Srgrimes		tp->t_oflag = TTYDEF_OFLAG;
1981541Srgrimes		tp->t_lflag = TTYDEF_LFLAG;
1991541Srgrimes		tp->t_cflag = TTYDEF_CFLAG;
2001541Srgrimes		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
20157070Srwatson	} else if (tp->t_state & TS_XCLUDE && suser(p)) {
2021541Srgrimes		return (EBUSY);
20357070Srwatson	} else if (pti->pt_prison != p->p_prison) {
20457070Srwatson		return (EBUSY);
20557070Srwatson	}
2061541Srgrimes	if (tp->t_oproc)			/* Ctrlr still around. */
2079824Sbde		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
2081541Srgrimes	while ((tp->t_state & TS_CARR_ON) == 0) {
2091541Srgrimes		if (flag&FNONBLOCK)
2101541Srgrimes			break;
2119639Sbde		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
2129624Sbde				 "ptsopn", 0);
2133308Sphk		if (error)
2141541Srgrimes			return (error);
2151541Srgrimes	}
2161541Srgrimes	error = (*linesw[tp->t_line].l_open)(dev, tp);
2177724Sache	if (error == 0)
2187724Sache		ptcwakeup(tp, FREAD|FWRITE);
2191541Srgrimes	return (error);
2201541Srgrimes}
2211541Srgrimes
22212675Sjulianstatic	int
2231541Srgrimesptsclose(dev, flag, mode, p)
2241541Srgrimes	dev_t dev;
2251541Srgrimes	int flag, mode;
2261541Srgrimes	struct proc *p;
2271541Srgrimes{
2281541Srgrimes	register struct tty *tp;
2291541Srgrimes	int err;
2301541Srgrimes
23150652Sphk	tp = dev->si_tty;
2321541Srgrimes	err = (*linesw[tp->t_line].l_close)(tp, flag);
2337730Sache	ptsstop(tp, FREAD|FWRITE);
2347724Sache	(void) ttyclose(tp);
2351541Srgrimes	return (err);
2361541Srgrimes}
2371541Srgrimes
23812675Sjulianstatic	int
2391541Srgrimesptsread(dev, uio, flag)
2401541Srgrimes	dev_t dev;
2411541Srgrimes	struct uio *uio;
2421541Srgrimes	int flag;
2431541Srgrimes{
2441541Srgrimes	struct proc *p = curproc;
24550652Sphk	register struct tty *tp = dev->si_tty;
24649536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
2471541Srgrimes	int error = 0;
2481541Srgrimes
2491541Srgrimesagain:
2501541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
2511541Srgrimes		while (isbackground(p, tp)) {
25251791Smarcel			if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
25351791Smarcel			    SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
25451791Smarcel			    p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
2551541Srgrimes				return (EIO);
2561541Srgrimes			pgsignal(p->p_pgrp, SIGTTIN, 1);
2579624Sbde			error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
2589624Sbde					 0);
2593308Sphk			if (error)
2601541Srgrimes				return (error);
2611541Srgrimes		}
2621541Srgrimes		if (tp->t_canq.c_cc == 0) {
2631541Srgrimes			if (flag & IO_NDELAY)
2641541Srgrimes				return (EWOULDBLOCK);
2659639Sbde			error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
2669624Sbde					 "ptsin", 0);
2673308Sphk			if (error)
2681541Srgrimes				return (error);
2691541Srgrimes			goto again;
2701541Srgrimes		}
2711541Srgrimes		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
2721541Srgrimes			if (ureadc(getc(&tp->t_canq), uio) < 0) {
2731541Srgrimes				error = EFAULT;
2741541Srgrimes				break;
2751541Srgrimes			}
2761541Srgrimes		if (tp->t_canq.c_cc == 1)
2771541Srgrimes			(void) getc(&tp->t_canq);
2781541Srgrimes		if (tp->t_canq.c_cc)
2791541Srgrimes			return (error);
2801541Srgrimes	} else
2811541Srgrimes		if (tp->t_oproc)
2821541Srgrimes			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
2831541Srgrimes	ptcwakeup(tp, FWRITE);
2841541Srgrimes	return (error);
2851541Srgrimes}
2861541Srgrimes
2871541Srgrimes/*
2881541Srgrimes * Write to pseudo-tty.
2891541Srgrimes * Wakeups of controlling tty will happen
2901541Srgrimes * indirectly, when tty driver calls ptsstart.
2911541Srgrimes */
29212675Sjulianstatic	int
2931541Srgrimesptswrite(dev, uio, flag)
2941541Srgrimes	dev_t dev;
2951541Srgrimes	struct uio *uio;
2961541Srgrimes	int flag;
2971541Srgrimes{
2981541Srgrimes	register struct tty *tp;
2991541Srgrimes
30050652Sphk	tp = dev->si_tty;
3011541Srgrimes	if (tp->t_oproc == 0)
3021541Srgrimes		return (EIO);
3031541Srgrimes	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
3041541Srgrimes}
3051541Srgrimes
3061541Srgrimes/*
3071541Srgrimes * Start output on pseudo-tty.
3081541Srgrimes * Wake up process selecting or sleeping for input from controlling tty.
3091541Srgrimes */
31012819Sphkstatic void
3111541Srgrimesptsstart(tp)
3121541Srgrimes	struct tty *tp;
3131541Srgrimes{
31449536Sphk	register struct pt_ioctl *pti = tp->t_dev->si_drv1;
3151541Srgrimes
3161541Srgrimes	if (tp->t_state & TS_TTSTOP)
3171541Srgrimes		return;
3181541Srgrimes	if (pti->pt_flags & PF_STOPPED) {
3191541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
3201541Srgrimes		pti->pt_send = TIOCPKT_START;
3211541Srgrimes	}
3221541Srgrimes	ptcwakeup(tp, FREAD);
3231541Srgrimes}
3241541Srgrimes
32512819Sphkstatic void
3261541Srgrimesptcwakeup(tp, flag)
3271541Srgrimes	struct tty *tp;
3281541Srgrimes	int flag;
3291541Srgrimes{
33049536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
3311541Srgrimes
3321541Srgrimes	if (flag & FREAD) {
3331541Srgrimes		selwakeup(&pti->pt_selr);
3349639Sbde		wakeup(TSA_PTC_READ(tp));
3351541Srgrimes	}
3361541Srgrimes	if (flag & FWRITE) {
3371541Srgrimes		selwakeup(&pti->pt_selw);
3389639Sbde		wakeup(TSA_PTC_WRITE(tp));
3391541Srgrimes	}
3401541Srgrimes}
3411541Srgrimes
34212675Sjulianstatic	int
3431541Srgrimesptcopen(dev, flag, devtype, p)
3441541Srgrimes	dev_t dev;
3451541Srgrimes	int flag, devtype;
3461541Srgrimes	struct proc *p;
3471541Srgrimes{
3481541Srgrimes	register struct tty *tp;
3491541Srgrimes	struct pt_ioctl *pti;
3501541Srgrimes
35149536Sphk	if (!dev->si_drv1)
35249536Sphk		ptyinit(minor(dev));
35349536Sphk	if (!dev->si_drv1)
35449536Sphk		return(ENXIO);
35550652Sphk	tp = dev->si_tty;
3561541Srgrimes	if (tp->t_oproc)
3571541Srgrimes		return (EIO);
35859818Sache	tp->t_timeout = -1;
3591541Srgrimes	tp->t_oproc = ptsstart;
36051654Sphk	tp->t_stop = ptsstop;
3611541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
3621541Srgrimes	tp->t_lflag &= ~EXTPROC;
36349536Sphk	pti = dev->si_drv1;
36457070Srwatson	pti->pt_prison = p->p_prison;
3651541Srgrimes	pti->pt_flags = 0;
3661541Srgrimes	pti->pt_send = 0;
3671541Srgrimes	pti->pt_ucntl = 0;
3681541Srgrimes	return (0);
3691541Srgrimes}
3701541Srgrimes
37112675Sjulianstatic	int
37210624Sbdeptcclose(dev, flags, fmt, p)
3731541Srgrimes	dev_t dev;
37410624Sbde	int flags;
37510624Sbde	int fmt;
37610624Sbde	struct proc *p;
3771541Srgrimes{
3781541Srgrimes	register struct tty *tp;
3791541Srgrimes
38050652Sphk	tp = dev->si_tty;
3811541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
3829824Sbde
3839824Sbde	/*
3849824Sbde	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
3859824Sbde	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
3869824Sbde	 * l_modem()s that ignore carrier drop make no sense for ptys but
3879824Sbde	 * may be in use because other parts of the line discipline make
3889824Sbde	 * sense for ptys.  Recover by doing everything that a normal
3899824Sbde	 * ttymodem() would have done except for sending a SIGHUP.
3909824Sbde	 */
3919850Sbde	if (tp->t_state & TS_ISOPEN) {
3929850Sbde		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
3939850Sbde		tp->t_state |= TS_ZOMBIE;
3949850Sbde		ttyflush(tp, FREAD | FWRITE);
3959850Sbde	}
3969824Sbde
3971541Srgrimes	tp->t_oproc = 0;		/* mark closed */
3981541Srgrimes	return (0);
3991541Srgrimes}
4001541Srgrimes
40112675Sjulianstatic	int
4021541Srgrimesptcread(dev, uio, flag)
4031541Srgrimes	dev_t dev;
4041541Srgrimes	struct uio *uio;
4051541Srgrimes	int flag;
4061541Srgrimes{
40750652Sphk	register struct tty *tp = dev->si_tty;
40849536Sphk	struct pt_ioctl *pti = dev->si_drv1;
4091541Srgrimes	char buf[BUFSIZ];
4101541Srgrimes	int error = 0, cc;
4111541Srgrimes
4121541Srgrimes	/*
4131541Srgrimes	 * We want to block until the slave
4141541Srgrimes	 * is open, and there's something to read;
4151541Srgrimes	 * but if we lost the slave or we're NBIO,
4161541Srgrimes	 * then return the appropriate error instead.
4171541Srgrimes	 */
4181541Srgrimes	for (;;) {
4191541Srgrimes		if (tp->t_state&TS_ISOPEN) {
4201541Srgrimes			if (pti->pt_flags&PF_PKT && pti->pt_send) {
4211541Srgrimes				error = ureadc((int)pti->pt_send, uio);
4221541Srgrimes				if (error)
4231541Srgrimes					return (error);
4241541Srgrimes				if (pti->pt_send & TIOCPKT_IOCTL) {
4251541Srgrimes					cc = min(uio->uio_resid,
4261541Srgrimes						sizeof(tp->t_termios));
4272807Sbde					uiomove((caddr_t)&tp->t_termios, cc,
4282807Sbde						uio);
4291541Srgrimes				}
4301541Srgrimes				pti->pt_send = 0;
4311541Srgrimes				return (0);
4321541Srgrimes			}
4331541Srgrimes			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
4341541Srgrimes				error = ureadc((int)pti->pt_ucntl, uio);
4351541Srgrimes				if (error)
4361541Srgrimes					return (error);
4371541Srgrimes				pti->pt_ucntl = 0;
4381541Srgrimes				return (0);
4391541Srgrimes			}
4401541Srgrimes			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
4411541Srgrimes				break;
4421541Srgrimes		}
4439824Sbde		if ((tp->t_state & TS_CONNECTED) == 0)
4441541Srgrimes			return (0);	/* EOF */
4451541Srgrimes		if (flag & IO_NDELAY)
4461541Srgrimes			return (EWOULDBLOCK);
4479639Sbde		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
4483308Sphk		if (error)
4491541Srgrimes			return (error);
4501541Srgrimes	}
4511541Srgrimes	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
4521541Srgrimes		error = ureadc(0, uio);
4531541Srgrimes	while (uio->uio_resid > 0 && error == 0) {
4541541Srgrimes		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
4551541Srgrimes		if (cc <= 0)
4561541Srgrimes			break;
4571541Srgrimes		error = uiomove(buf, cc, uio);
4581541Srgrimes	}
4599626Sbde	ttwwakeup(tp);
4601541Srgrimes	return (error);
4611541Srgrimes}
4621541Srgrimes
46312675Sjulianstatic	void
4641541Srgrimesptsstop(tp, flush)
4651541Srgrimes	register struct tty *tp;
4661541Srgrimes	int flush;
4671541Srgrimes{
46849536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
4691541Srgrimes	int flag;
4701541Srgrimes
4711541Srgrimes	/* note: FLUSHREAD and FLUSHWRITE already ok */
4721541Srgrimes	if (flush == 0) {
4731541Srgrimes		flush = TIOCPKT_STOP;
4741541Srgrimes		pti->pt_flags |= PF_STOPPED;
4751541Srgrimes	} else
4761541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
4771541Srgrimes	pti->pt_send |= flush;
4781541Srgrimes	/* change of perspective */
4791541Srgrimes	flag = 0;
4801541Srgrimes	if (flush & FREAD)
4811541Srgrimes		flag |= FWRITE;
4821541Srgrimes	if (flush & FWRITE)
4831541Srgrimes		flag |= FREAD;
4841541Srgrimes	ptcwakeup(tp, flag);
4851541Srgrimes}
4861541Srgrimes
48712675Sjulianstatic	int
48829354Speterptcpoll(dev, events, p)
4891541Srgrimes	dev_t dev;
49029354Speter	int events;
4911541Srgrimes	struct proc *p;
4921541Srgrimes{
49350652Sphk	register struct tty *tp = dev->si_tty;
49449536Sphk	struct pt_ioctl *pti = dev->si_drv1;
49529354Speter	int revents = 0;
4961541Srgrimes	int s;
4971541Srgrimes
4989824Sbde	if ((tp->t_state & TS_CONNECTED) == 0)
49929354Speter		return (seltrue(dev, events, p) | POLLHUP);
5001541Srgrimes
50129354Speter	/*
50229354Speter	 * Need to block timeouts (ttrstart).
50329354Speter	 */
50429354Speter	s = spltty();
5051541Srgrimes
50629354Speter	if (events & (POLLIN | POLLRDNORM))
50729354Speter		if ((tp->t_state & TS_ISOPEN) &&
50829354Speter		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
50929354Speter		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
51029354Speter		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
51129354Speter			revents |= events & (POLLIN | POLLRDNORM);
5121541Srgrimes
51329354Speter	if (events & (POLLOUT | POLLWRNORM))
51429354Speter		if (tp->t_state & TS_ISOPEN &&
51529354Speter		    ((pti->pt_flags & PF_REMOTE) ?
51629354Speter		     (tp->t_canq.c_cc == 0) :
51729354Speter		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
51829354Speter		      (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON)))))
51929354Speter			revents |= events & (POLLOUT | POLLWRNORM);
5201541Srgrimes
52129354Speter	if (events & POLLHUP)
52229354Speter		if ((tp->t_state & TS_CARR_ON) == 0)
52329354Speter			revents |= POLLHUP;
5241541Srgrimes
52529354Speter	if (revents == 0) {
52629354Speter		if (events & (POLLIN | POLLRDNORM))
52729354Speter			selrecord(p, &pti->pt_selr);
52829354Speter
52929354Speter		if (events & (POLLOUT | POLLWRNORM))
53029354Speter			selrecord(p, &pti->pt_selw);
5311541Srgrimes	}
53229354Speter	splx(s);
53329354Speter
53429354Speter	return (revents);
5351541Srgrimes}
5361541Srgrimes
53712675Sjulianstatic	int
5381541Srgrimesptcwrite(dev, uio, flag)
5391541Srgrimes	dev_t dev;
5401541Srgrimes	register struct uio *uio;
5411541Srgrimes	int flag;
5421541Srgrimes{
54350652Sphk	register struct tty *tp = dev->si_tty;
5441549Srgrimes	register u_char *cp = 0;
5451541Srgrimes	register int cc = 0;
5461541Srgrimes	u_char locbuf[BUFSIZ];
5471541Srgrimes	int cnt = 0;
54849536Sphk	struct pt_ioctl *pti = dev->si_drv1;
5491541Srgrimes	int error = 0;
5501541Srgrimes
5511541Srgrimesagain:
5521541Srgrimes	if ((tp->t_state&TS_ISOPEN) == 0)
5531541Srgrimes		goto block;
5541541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
5551541Srgrimes		if (tp->t_canq.c_cc)
5561541Srgrimes			goto block;
55711789Sbde		while ((uio->uio_resid > 0 || cc > 0) &&
55811789Sbde		       tp->t_canq.c_cc < TTYHOG - 1) {
5591541Srgrimes			if (cc == 0) {
5601541Srgrimes				cc = min(uio->uio_resid, BUFSIZ);
5611541Srgrimes				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
5621541Srgrimes				cp = locbuf;
5631541Srgrimes				error = uiomove((caddr_t)cp, cc, uio);
5641541Srgrimes				if (error)
5651541Srgrimes					return (error);
5661541Srgrimes				/* check again for safety */
56711789Sbde				if ((tp->t_state & TS_ISOPEN) == 0) {
56811789Sbde					/* adjust as usual */
56911789Sbde					uio->uio_resid += cc;
5701541Srgrimes					return (EIO);
57111789Sbde				}
5721541Srgrimes			}
57315199Sbde			if (cc > 0) {
57415199Sbde				cc = b_to_q((char *)cp, cc, &tp->t_canq);
57515199Sbde				/*
57615199Sbde				 * XXX we don't guarantee that the canq size
57715199Sbde				 * is >= TTYHOG, so the above b_to_q() may
57815199Sbde				 * leave some bytes uncopied.  However, space
57915199Sbde				 * is guaranteed for the null terminator if
58015199Sbde				 * we don't fail here since (TTYHOG - 1) is
58115199Sbde				 * not a multiple of CBSIZE.
58215199Sbde				 */
58315199Sbde				if (cc > 0)
58415199Sbde					break;
58515199Sbde			}
5861541Srgrimes		}
58711789Sbde		/* adjust for data copied in but not written */
58811789Sbde		uio->uio_resid += cc;
5891541Srgrimes		(void) putc(0, &tp->t_canq);
5901541Srgrimes		ttwakeup(tp);
5919639Sbde		wakeup(TSA_PTS_READ(tp));
5921541Srgrimes		return (0);
5931541Srgrimes	}
59411789Sbde	while (uio->uio_resid > 0 || cc > 0) {
5951541Srgrimes		if (cc == 0) {
5961541Srgrimes			cc = min(uio->uio_resid, BUFSIZ);
5971541Srgrimes			cp = locbuf;
5981541Srgrimes			error = uiomove((caddr_t)cp, cc, uio);
5991541Srgrimes			if (error)
6001541Srgrimes				return (error);
6011541Srgrimes			/* check again for safety */
60211789Sbde			if ((tp->t_state & TS_ISOPEN) == 0) {
60311789Sbde				/* adjust for data copied in but not written */
60411789Sbde				uio->uio_resid += cc;
6051541Srgrimes				return (EIO);
60611789Sbde			}
6071541Srgrimes		}
6081541Srgrimes		while (cc > 0) {
6091541Srgrimes			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
6101541Srgrimes			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
6119824Sbde				wakeup(TSA_HUP_OR_INPUT(tp));
6121541Srgrimes				goto block;
6131541Srgrimes			}
6141541Srgrimes			(*linesw[tp->t_line].l_rint)(*cp++, tp);
6151541Srgrimes			cnt++;
6161541Srgrimes			cc--;
6171541Srgrimes		}
6181541Srgrimes		cc = 0;
6191541Srgrimes	}
6201541Srgrimes	return (0);
6211541Srgrimesblock:
6221541Srgrimes	/*
6231541Srgrimes	 * Come here to wait for slave to open, for space
62415199Sbde	 * in outq, or space in rawq, or an empty canq.
6251541Srgrimes	 */
62611789Sbde	if ((tp->t_state & TS_CONNECTED) == 0) {
62711789Sbde		/* adjust for data copied in but not written */
62811789Sbde		uio->uio_resid += cc;
6291541Srgrimes		return (EIO);
63011789Sbde	}
6311541Srgrimes	if (flag & IO_NDELAY) {
6321541Srgrimes		/* adjust for data copied in but not written */
6331541Srgrimes		uio->uio_resid += cc;
6341541Srgrimes		if (cnt == 0)
6351541Srgrimes			return (EWOULDBLOCK);
6361541Srgrimes		return (0);
6371541Srgrimes	}
6389639Sbde	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
6393308Sphk	if (error) {
6401541Srgrimes		/* adjust for data copied in but not written */
6411541Srgrimes		uio->uio_resid += cc;
6421541Srgrimes		return (error);
6431541Srgrimes	}
6441541Srgrimes	goto again;
6451541Srgrimes}
6461541Srgrimes
6471541Srgrimes/*ARGSUSED*/
64812675Sjulianstatic	int
6491541Srgrimesptyioctl(dev, cmd, data, flag, p)
6501541Srgrimes	dev_t dev;
65136735Sdfr	u_long cmd;
6521541Srgrimes	caddr_t data;
6531541Srgrimes	int flag;
6541541Srgrimes	struct proc *p;
6551541Srgrimes{
65650652Sphk	register struct tty *tp = dev->si_tty;
65749536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
6581541Srgrimes	register u_char *cc = tp->t_cc;
6591541Srgrimes	int stop, error;
6601541Srgrimes
66147301Sluoqi	if (devsw(dev)->d_open == ptcopen) {
6621541Srgrimes		switch (cmd) {
6631541Srgrimes
6641541Srgrimes		case TIOCGPGRP:
6651541Srgrimes			/*
66633826Sbde			 * We avoid calling ttioctl on the controller since,
6671541Srgrimes			 * in that case, tp must be the controlling terminal.
6681541Srgrimes			 */
6691541Srgrimes			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
6701541Srgrimes			return (0);
6711541Srgrimes
6721541Srgrimes		case TIOCPKT:
6731541Srgrimes			if (*(int *)data) {
6741541Srgrimes				if (pti->pt_flags & PF_UCNTL)
6751541Srgrimes					return (EINVAL);
6761541Srgrimes				pti->pt_flags |= PF_PKT;
6771541Srgrimes			} else
6781541Srgrimes				pti->pt_flags &= ~PF_PKT;
6791541Srgrimes			return (0);
6801541Srgrimes
6811541Srgrimes		case TIOCUCNTL:
6821541Srgrimes			if (*(int *)data) {
6831541Srgrimes				if (pti->pt_flags & PF_PKT)
6841541Srgrimes					return (EINVAL);
6851541Srgrimes				pti->pt_flags |= PF_UCNTL;
6861541Srgrimes			} else
6871541Srgrimes				pti->pt_flags &= ~PF_UCNTL;
6881541Srgrimes			return (0);
6891541Srgrimes
6901541Srgrimes		case TIOCREMOTE:
6911541Srgrimes			if (*(int *)data)
6921541Srgrimes				pti->pt_flags |= PF_REMOTE;
6931541Srgrimes			else
6941541Srgrimes				pti->pt_flags &= ~PF_REMOTE;
6951541Srgrimes			ttyflush(tp, FREAD|FWRITE);
6961541Srgrimes			return (0);
69747203Sluoqi		}
6981541Srgrimes
69947203Sluoqi		/*
70047203Sluoqi		 * The rest of the ioctls shouldn't be called until
70147301Sluoqi		 * the slave is open.
70247203Sluoqi		 */
70347203Sluoqi		if ((tp->t_state & TS_ISOPEN) == 0)
70447301Sluoqi			return (EAGAIN);
70547203Sluoqi
70647203Sluoqi		switch (cmd) {
7071541Srgrimes#ifdef COMPAT_43
7088876Srgrimes		case TIOCSETP:
7091541Srgrimes		case TIOCSETN:
7101541Srgrimes#endif
7111541Srgrimes		case TIOCSETD:
7121541Srgrimes		case TIOCSETA:
7131541Srgrimes		case TIOCSETAW:
7149858Sache		case TIOCSETAF:
71547301Sluoqi			/*
71647301Sluoqi			 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
71747301Sluoqi			 * ttywflush(tp) will hang if there are characters in
71847301Sluoqi			 * the outq.
71947301Sluoqi			 */
7201541Srgrimes			ndflush(&tp->t_outq, tp->t_outq.c_cc);
7211541Srgrimes			break;
7221541Srgrimes
7231541Srgrimes		case TIOCSIG:
72427770Sjmg			if (*(unsigned int *)data >= NSIG ||
72527770Sjmg			    *(unsigned int *)data == 0)
7261541Srgrimes				return(EINVAL);
7271541Srgrimes			if ((tp->t_lflag&NOFLSH) == 0)
7281541Srgrimes				ttyflush(tp, FREAD|FWRITE);
7291541Srgrimes			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
7301541Srgrimes			if ((*(unsigned int *)data == SIGINFO) &&
7311541Srgrimes			    ((tp->t_lflag&NOKERNINFO) == 0))
7321541Srgrimes				ttyinfo(tp);
7331541Srgrimes			return(0);
7341541Srgrimes		}
73547203Sluoqi	}
73647301Sluoqi	if (cmd == TIOCEXT) {
73747301Sluoqi		/*
73847301Sluoqi		 * When the EXTPROC bit is being toggled, we need
73947301Sluoqi		 * to send an TIOCPKT_IOCTL if the packet driver
74047301Sluoqi		 * is turned on.
74147301Sluoqi		 */
74247301Sluoqi		if (*(int *)data) {
74347301Sluoqi			if (pti->pt_flags & PF_PKT) {
74447301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
74547301Sluoqi				ptcwakeup(tp, FREAD);
74647301Sluoqi			}
74747301Sluoqi			tp->t_lflag |= EXTPROC;
74847301Sluoqi		} else {
74947301Sluoqi			if ((tp->t_lflag & EXTPROC) &&
75047301Sluoqi			    (pti->pt_flags & PF_PKT)) {
75147301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
75247301Sluoqi				ptcwakeup(tp, FREAD);
75347301Sluoqi			}
75447301Sluoqi			tp->t_lflag &= ~EXTPROC;
75547301Sluoqi		}
75647301Sluoqi		return(0);
75747301Sluoqi	}
7581541Srgrimes	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
75931577Sbde	if (error == ENOIOCTL)
7601541Srgrimes		 error = ttioctl(tp, cmd, data, flag);
76131577Sbde	if (error == ENOIOCTL) {
7621541Srgrimes		if (pti->pt_flags & PF_UCNTL &&
7631541Srgrimes		    (cmd & ~0xff) == UIOCCMD(0)) {
7641541Srgrimes			if (cmd & 0xff) {
7651541Srgrimes				pti->pt_ucntl = (u_char)cmd;
7661541Srgrimes				ptcwakeup(tp, FREAD);
7671541Srgrimes			}
7681541Srgrimes			return (0);
7691541Srgrimes		}
7701541Srgrimes		error = ENOTTY;
7711541Srgrimes	}
7721541Srgrimes	/*
7731541Srgrimes	 * If external processing and packet mode send ioctl packet.
7741541Srgrimes	 */
7751541Srgrimes	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
7761541Srgrimes		switch(cmd) {
7771541Srgrimes		case TIOCSETA:
7781541Srgrimes		case TIOCSETAW:
7791541Srgrimes		case TIOCSETAF:
7801541Srgrimes#ifdef COMPAT_43
7811541Srgrimes		case TIOCSETP:
7821541Srgrimes		case TIOCSETN:
7831541Srgrimes#endif
7841541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
7851541Srgrimes		case TIOCSETC:
7861541Srgrimes		case TIOCSLTC:
7871541Srgrimes		case TIOCLBIS:
7881541Srgrimes		case TIOCLBIC:
7891541Srgrimes		case TIOCLSET:
7901541Srgrimes#endif
7911541Srgrimes			pti->pt_send |= TIOCPKT_IOCTL;
7921541Srgrimes			ptcwakeup(tp, FREAD);
7931541Srgrimes		default:
7941541Srgrimes			break;
7951541Srgrimes		}
7961541Srgrimes	}
7978876Srgrimes	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
7981541Srgrimes		&& CCEQ(cc[VSTART], CTRL('q'));
7991541Srgrimes	if (pti->pt_flags & PF_NOSTOP) {
8001541Srgrimes		if (stop) {
8011541Srgrimes			pti->pt_send &= ~TIOCPKT_NOSTOP;
8021541Srgrimes			pti->pt_send |= TIOCPKT_DOSTOP;
8031541Srgrimes			pti->pt_flags &= ~PF_NOSTOP;
8041541Srgrimes			ptcwakeup(tp, FREAD);
8051541Srgrimes		}
8061541Srgrimes	} else {
8071541Srgrimes		if (!stop) {
8081541Srgrimes			pti->pt_send &= ~TIOCPKT_DOSTOP;
8091541Srgrimes			pti->pt_send |= TIOCPKT_NOSTOP;
8101541Srgrimes			pti->pt_flags |= PF_NOSTOP;
8111541Srgrimes			ptcwakeup(tp, FREAD);
8121541Srgrimes		}
8131541Srgrimes	}
8141541Srgrimes	return (error);
8151541Srgrimes}
81612517Sjulian
81712517Sjulian
81829506Sbdestatic void ptc_drvinit __P((void *unused));
81949536Sphk
82012675Sjulianstatic void
82129506Sbdeptc_drvinit(unused)
82229506Sbde	void *unused;
82312517Sjulian{
82453483Sjkh	cdevsw_add(&pts_cdevsw);
82553483Sjkh	cdevsw_add(&ptc_cdevsw);
82650254Sphk	/* XXX: Gross hack for DEVFS */
82750254Sphk	ptyinit(0);
82812517Sjulian}
82912517Sjulian
83012517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
831