pty.c revision 57070
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 57070 2000-02-09 03:32:11Z rwatson $
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);
3581541Srgrimes	tp->t_oproc = ptsstart;
35951654Sphk	tp->t_stop = ptsstop;
3601541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
3611541Srgrimes	tp->t_lflag &= ~EXTPROC;
36249536Sphk	pti = dev->si_drv1;
36357070Srwatson	pti->pt_prison = p->p_prison;
3641541Srgrimes	pti->pt_flags = 0;
3651541Srgrimes	pti->pt_send = 0;
3661541Srgrimes	pti->pt_ucntl = 0;
3671541Srgrimes	return (0);
3681541Srgrimes}
3691541Srgrimes
37012675Sjulianstatic	int
37110624Sbdeptcclose(dev, flags, fmt, p)
3721541Srgrimes	dev_t dev;
37310624Sbde	int flags;
37410624Sbde	int fmt;
37510624Sbde	struct proc *p;
3761541Srgrimes{
3771541Srgrimes	register struct tty *tp;
3781541Srgrimes
37950652Sphk	tp = dev->si_tty;
3801541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
3819824Sbde
3829824Sbde	/*
3839824Sbde	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
3849824Sbde	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
3859824Sbde	 * l_modem()s that ignore carrier drop make no sense for ptys but
3869824Sbde	 * may be in use because other parts of the line discipline make
3879824Sbde	 * sense for ptys.  Recover by doing everything that a normal
3889824Sbde	 * ttymodem() would have done except for sending a SIGHUP.
3899824Sbde	 */
3909850Sbde	if (tp->t_state & TS_ISOPEN) {
3919850Sbde		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
3929850Sbde		tp->t_state |= TS_ZOMBIE;
3939850Sbde		ttyflush(tp, FREAD | FWRITE);
3949850Sbde	}
3959824Sbde
3961541Srgrimes	tp->t_oproc = 0;		/* mark closed */
3971541Srgrimes	return (0);
3981541Srgrimes}
3991541Srgrimes
40012675Sjulianstatic	int
4011541Srgrimesptcread(dev, uio, flag)
4021541Srgrimes	dev_t dev;
4031541Srgrimes	struct uio *uio;
4041541Srgrimes	int flag;
4051541Srgrimes{
40650652Sphk	register struct tty *tp = dev->si_tty;
40749536Sphk	struct pt_ioctl *pti = dev->si_drv1;
4081541Srgrimes	char buf[BUFSIZ];
4091541Srgrimes	int error = 0, cc;
4101541Srgrimes
4111541Srgrimes	/*
4121541Srgrimes	 * We want to block until the slave
4131541Srgrimes	 * is open, and there's something to read;
4141541Srgrimes	 * but if we lost the slave or we're NBIO,
4151541Srgrimes	 * then return the appropriate error instead.
4161541Srgrimes	 */
4171541Srgrimes	for (;;) {
4181541Srgrimes		if (tp->t_state&TS_ISOPEN) {
4191541Srgrimes			if (pti->pt_flags&PF_PKT && pti->pt_send) {
4201541Srgrimes				error = ureadc((int)pti->pt_send, uio);
4211541Srgrimes				if (error)
4221541Srgrimes					return (error);
4231541Srgrimes				if (pti->pt_send & TIOCPKT_IOCTL) {
4241541Srgrimes					cc = min(uio->uio_resid,
4251541Srgrimes						sizeof(tp->t_termios));
4262807Sbde					uiomove((caddr_t)&tp->t_termios, cc,
4272807Sbde						uio);
4281541Srgrimes				}
4291541Srgrimes				pti->pt_send = 0;
4301541Srgrimes				return (0);
4311541Srgrimes			}
4321541Srgrimes			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
4331541Srgrimes				error = ureadc((int)pti->pt_ucntl, uio);
4341541Srgrimes				if (error)
4351541Srgrimes					return (error);
4361541Srgrimes				pti->pt_ucntl = 0;
4371541Srgrimes				return (0);
4381541Srgrimes			}
4391541Srgrimes			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
4401541Srgrimes				break;
4411541Srgrimes		}
4429824Sbde		if ((tp->t_state & TS_CONNECTED) == 0)
4431541Srgrimes			return (0);	/* EOF */
4441541Srgrimes		if (flag & IO_NDELAY)
4451541Srgrimes			return (EWOULDBLOCK);
4469639Sbde		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
4473308Sphk		if (error)
4481541Srgrimes			return (error);
4491541Srgrimes	}
4501541Srgrimes	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
4511541Srgrimes		error = ureadc(0, uio);
4521541Srgrimes	while (uio->uio_resid > 0 && error == 0) {
4531541Srgrimes		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
4541541Srgrimes		if (cc <= 0)
4551541Srgrimes			break;
4561541Srgrimes		error = uiomove(buf, cc, uio);
4571541Srgrimes	}
4589626Sbde	ttwwakeup(tp);
4591541Srgrimes	return (error);
4601541Srgrimes}
4611541Srgrimes
46212675Sjulianstatic	void
4631541Srgrimesptsstop(tp, flush)
4641541Srgrimes	register struct tty *tp;
4651541Srgrimes	int flush;
4661541Srgrimes{
46749536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
4681541Srgrimes	int flag;
4691541Srgrimes
4701541Srgrimes	/* note: FLUSHREAD and FLUSHWRITE already ok */
4711541Srgrimes	if (flush == 0) {
4721541Srgrimes		flush = TIOCPKT_STOP;
4731541Srgrimes		pti->pt_flags |= PF_STOPPED;
4741541Srgrimes	} else
4751541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
4761541Srgrimes	pti->pt_send |= flush;
4771541Srgrimes	/* change of perspective */
4781541Srgrimes	flag = 0;
4791541Srgrimes	if (flush & FREAD)
4801541Srgrimes		flag |= FWRITE;
4811541Srgrimes	if (flush & FWRITE)
4821541Srgrimes		flag |= FREAD;
4831541Srgrimes	ptcwakeup(tp, flag);
4841541Srgrimes}
4851541Srgrimes
48612675Sjulianstatic	int
48729354Speterptcpoll(dev, events, p)
4881541Srgrimes	dev_t dev;
48929354Speter	int events;
4901541Srgrimes	struct proc *p;
4911541Srgrimes{
49250652Sphk	register struct tty *tp = dev->si_tty;
49349536Sphk	struct pt_ioctl *pti = dev->si_drv1;
49429354Speter	int revents = 0;
4951541Srgrimes	int s;
4961541Srgrimes
4979824Sbde	if ((tp->t_state & TS_CONNECTED) == 0)
49829354Speter		return (seltrue(dev, events, p) | POLLHUP);
4991541Srgrimes
50029354Speter	/*
50129354Speter	 * Need to block timeouts (ttrstart).
50229354Speter	 */
50329354Speter	s = spltty();
5041541Srgrimes
50529354Speter	if (events & (POLLIN | POLLRDNORM))
50629354Speter		if ((tp->t_state & TS_ISOPEN) &&
50729354Speter		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
50829354Speter		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
50929354Speter		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
51029354Speter			revents |= events & (POLLIN | POLLRDNORM);
5111541Srgrimes
51229354Speter	if (events & (POLLOUT | POLLWRNORM))
51329354Speter		if (tp->t_state & TS_ISOPEN &&
51429354Speter		    ((pti->pt_flags & PF_REMOTE) ?
51529354Speter		     (tp->t_canq.c_cc == 0) :
51629354Speter		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
51729354Speter		      (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON)))))
51829354Speter			revents |= events & (POLLOUT | POLLWRNORM);
5191541Srgrimes
52029354Speter	if (events & POLLHUP)
52129354Speter		if ((tp->t_state & TS_CARR_ON) == 0)
52229354Speter			revents |= POLLHUP;
5231541Srgrimes
52429354Speter	if (revents == 0) {
52529354Speter		if (events & (POLLIN | POLLRDNORM))
52629354Speter			selrecord(p, &pti->pt_selr);
52729354Speter
52829354Speter		if (events & (POLLOUT | POLLWRNORM))
52929354Speter			selrecord(p, &pti->pt_selw);
5301541Srgrimes	}
53129354Speter	splx(s);
53229354Speter
53329354Speter	return (revents);
5341541Srgrimes}
5351541Srgrimes
53612675Sjulianstatic	int
5371541Srgrimesptcwrite(dev, uio, flag)
5381541Srgrimes	dev_t dev;
5391541Srgrimes	register struct uio *uio;
5401541Srgrimes	int flag;
5411541Srgrimes{
54250652Sphk	register struct tty *tp = dev->si_tty;
5431549Srgrimes	register u_char *cp = 0;
5441541Srgrimes	register int cc = 0;
5451541Srgrimes	u_char locbuf[BUFSIZ];
5461541Srgrimes	int cnt = 0;
54749536Sphk	struct pt_ioctl *pti = dev->si_drv1;
5481541Srgrimes	int error = 0;
5491541Srgrimes
5501541Srgrimesagain:
5511541Srgrimes	if ((tp->t_state&TS_ISOPEN) == 0)
5521541Srgrimes		goto block;
5531541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
5541541Srgrimes		if (tp->t_canq.c_cc)
5551541Srgrimes			goto block;
55611789Sbde		while ((uio->uio_resid > 0 || cc > 0) &&
55711789Sbde		       tp->t_canq.c_cc < TTYHOG - 1) {
5581541Srgrimes			if (cc == 0) {
5591541Srgrimes				cc = min(uio->uio_resid, BUFSIZ);
5601541Srgrimes				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
5611541Srgrimes				cp = locbuf;
5621541Srgrimes				error = uiomove((caddr_t)cp, cc, uio);
5631541Srgrimes				if (error)
5641541Srgrimes					return (error);
5651541Srgrimes				/* check again for safety */
56611789Sbde				if ((tp->t_state & TS_ISOPEN) == 0) {
56711789Sbde					/* adjust as usual */
56811789Sbde					uio->uio_resid += cc;
5691541Srgrimes					return (EIO);
57011789Sbde				}
5711541Srgrimes			}
57215199Sbde			if (cc > 0) {
57315199Sbde				cc = b_to_q((char *)cp, cc, &tp->t_canq);
57415199Sbde				/*
57515199Sbde				 * XXX we don't guarantee that the canq size
57615199Sbde				 * is >= TTYHOG, so the above b_to_q() may
57715199Sbde				 * leave some bytes uncopied.  However, space
57815199Sbde				 * is guaranteed for the null terminator if
57915199Sbde				 * we don't fail here since (TTYHOG - 1) is
58015199Sbde				 * not a multiple of CBSIZE.
58115199Sbde				 */
58215199Sbde				if (cc > 0)
58315199Sbde					break;
58415199Sbde			}
5851541Srgrimes		}
58611789Sbde		/* adjust for data copied in but not written */
58711789Sbde		uio->uio_resid += cc;
5881541Srgrimes		(void) putc(0, &tp->t_canq);
5891541Srgrimes		ttwakeup(tp);
5909639Sbde		wakeup(TSA_PTS_READ(tp));
5911541Srgrimes		return (0);
5921541Srgrimes	}
59311789Sbde	while (uio->uio_resid > 0 || cc > 0) {
5941541Srgrimes		if (cc == 0) {
5951541Srgrimes			cc = min(uio->uio_resid, BUFSIZ);
5961541Srgrimes			cp = locbuf;
5971541Srgrimes			error = uiomove((caddr_t)cp, cc, uio);
5981541Srgrimes			if (error)
5991541Srgrimes				return (error);
6001541Srgrimes			/* check again for safety */
60111789Sbde			if ((tp->t_state & TS_ISOPEN) == 0) {
60211789Sbde				/* adjust for data copied in but not written */
60311789Sbde				uio->uio_resid += cc;
6041541Srgrimes				return (EIO);
60511789Sbde			}
6061541Srgrimes		}
6071541Srgrimes		while (cc > 0) {
6081541Srgrimes			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
6091541Srgrimes			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
6109824Sbde				wakeup(TSA_HUP_OR_INPUT(tp));
6111541Srgrimes				goto block;
6121541Srgrimes			}
6131541Srgrimes			(*linesw[tp->t_line].l_rint)(*cp++, tp);
6141541Srgrimes			cnt++;
6151541Srgrimes			cc--;
6161541Srgrimes		}
6171541Srgrimes		cc = 0;
6181541Srgrimes	}
6191541Srgrimes	return (0);
6201541Srgrimesblock:
6211541Srgrimes	/*
6221541Srgrimes	 * Come here to wait for slave to open, for space
62315199Sbde	 * in outq, or space in rawq, or an empty canq.
6241541Srgrimes	 */
62511789Sbde	if ((tp->t_state & TS_CONNECTED) == 0) {
62611789Sbde		/* adjust for data copied in but not written */
62711789Sbde		uio->uio_resid += cc;
6281541Srgrimes		return (EIO);
62911789Sbde	}
6301541Srgrimes	if (flag & IO_NDELAY) {
6311541Srgrimes		/* adjust for data copied in but not written */
6321541Srgrimes		uio->uio_resid += cc;
6331541Srgrimes		if (cnt == 0)
6341541Srgrimes			return (EWOULDBLOCK);
6351541Srgrimes		return (0);
6361541Srgrimes	}
6379639Sbde	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
6383308Sphk	if (error) {
6391541Srgrimes		/* adjust for data copied in but not written */
6401541Srgrimes		uio->uio_resid += cc;
6411541Srgrimes		return (error);
6421541Srgrimes	}
6431541Srgrimes	goto again;
6441541Srgrimes}
6451541Srgrimes
6461541Srgrimes/*ARGSUSED*/
64712675Sjulianstatic	int
6481541Srgrimesptyioctl(dev, cmd, data, flag, p)
6491541Srgrimes	dev_t dev;
65036735Sdfr	u_long cmd;
6511541Srgrimes	caddr_t data;
6521541Srgrimes	int flag;
6531541Srgrimes	struct proc *p;
6541541Srgrimes{
65550652Sphk	register struct tty *tp = dev->si_tty;
65649536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
6571541Srgrimes	register u_char *cc = tp->t_cc;
6581541Srgrimes	int stop, error;
6591541Srgrimes
66047301Sluoqi	if (devsw(dev)->d_open == ptcopen) {
6611541Srgrimes		switch (cmd) {
6621541Srgrimes
6631541Srgrimes		case TIOCGPGRP:
6641541Srgrimes			/*
66533826Sbde			 * We avoid calling ttioctl on the controller since,
6661541Srgrimes			 * in that case, tp must be the controlling terminal.
6671541Srgrimes			 */
6681541Srgrimes			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
6691541Srgrimes			return (0);
6701541Srgrimes
6711541Srgrimes		case TIOCPKT:
6721541Srgrimes			if (*(int *)data) {
6731541Srgrimes				if (pti->pt_flags & PF_UCNTL)
6741541Srgrimes					return (EINVAL);
6751541Srgrimes				pti->pt_flags |= PF_PKT;
6761541Srgrimes			} else
6771541Srgrimes				pti->pt_flags &= ~PF_PKT;
6781541Srgrimes			return (0);
6791541Srgrimes
6801541Srgrimes		case TIOCUCNTL:
6811541Srgrimes			if (*(int *)data) {
6821541Srgrimes				if (pti->pt_flags & PF_PKT)
6831541Srgrimes					return (EINVAL);
6841541Srgrimes				pti->pt_flags |= PF_UCNTL;
6851541Srgrimes			} else
6861541Srgrimes				pti->pt_flags &= ~PF_UCNTL;
6871541Srgrimes			return (0);
6881541Srgrimes
6891541Srgrimes		case TIOCREMOTE:
6901541Srgrimes			if (*(int *)data)
6911541Srgrimes				pti->pt_flags |= PF_REMOTE;
6921541Srgrimes			else
6931541Srgrimes				pti->pt_flags &= ~PF_REMOTE;
6941541Srgrimes			ttyflush(tp, FREAD|FWRITE);
6951541Srgrimes			return (0);
69647203Sluoqi		}
6971541Srgrimes
69847203Sluoqi		/*
69947203Sluoqi		 * The rest of the ioctls shouldn't be called until
70047301Sluoqi		 * the slave is open.
70147203Sluoqi		 */
70247203Sluoqi		if ((tp->t_state & TS_ISOPEN) == 0)
70347301Sluoqi			return (EAGAIN);
70447203Sluoqi
70547203Sluoqi		switch (cmd) {
7061541Srgrimes#ifdef COMPAT_43
7078876Srgrimes		case TIOCSETP:
7081541Srgrimes		case TIOCSETN:
7091541Srgrimes#endif
7101541Srgrimes		case TIOCSETD:
7111541Srgrimes		case TIOCSETA:
7121541Srgrimes		case TIOCSETAW:
7139858Sache		case TIOCSETAF:
71447301Sluoqi			/*
71547301Sluoqi			 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
71647301Sluoqi			 * ttywflush(tp) will hang if there are characters in
71747301Sluoqi			 * the outq.
71847301Sluoqi			 */
7191541Srgrimes			ndflush(&tp->t_outq, tp->t_outq.c_cc);
7201541Srgrimes			break;
7211541Srgrimes
7221541Srgrimes		case TIOCSIG:
72327770Sjmg			if (*(unsigned int *)data >= NSIG ||
72427770Sjmg			    *(unsigned int *)data == 0)
7251541Srgrimes				return(EINVAL);
7261541Srgrimes			if ((tp->t_lflag&NOFLSH) == 0)
7271541Srgrimes				ttyflush(tp, FREAD|FWRITE);
7281541Srgrimes			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
7291541Srgrimes			if ((*(unsigned int *)data == SIGINFO) &&
7301541Srgrimes			    ((tp->t_lflag&NOKERNINFO) == 0))
7311541Srgrimes				ttyinfo(tp);
7321541Srgrimes			return(0);
7331541Srgrimes		}
73447203Sluoqi	}
73547301Sluoqi	if (cmd == TIOCEXT) {
73647301Sluoqi		/*
73747301Sluoqi		 * When the EXTPROC bit is being toggled, we need
73847301Sluoqi		 * to send an TIOCPKT_IOCTL if the packet driver
73947301Sluoqi		 * is turned on.
74047301Sluoqi		 */
74147301Sluoqi		if (*(int *)data) {
74247301Sluoqi			if (pti->pt_flags & PF_PKT) {
74347301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
74447301Sluoqi				ptcwakeup(tp, FREAD);
74547301Sluoqi			}
74647301Sluoqi			tp->t_lflag |= EXTPROC;
74747301Sluoqi		} else {
74847301Sluoqi			if ((tp->t_lflag & EXTPROC) &&
74947301Sluoqi			    (pti->pt_flags & PF_PKT)) {
75047301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
75147301Sluoqi				ptcwakeup(tp, FREAD);
75247301Sluoqi			}
75347301Sluoqi			tp->t_lflag &= ~EXTPROC;
75447301Sluoqi		}
75547301Sluoqi		return(0);
75647301Sluoqi	}
7571541Srgrimes	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
75831577Sbde	if (error == ENOIOCTL)
7591541Srgrimes		 error = ttioctl(tp, cmd, data, flag);
76031577Sbde	if (error == ENOIOCTL) {
7611541Srgrimes		if (pti->pt_flags & PF_UCNTL &&
7621541Srgrimes		    (cmd & ~0xff) == UIOCCMD(0)) {
7631541Srgrimes			if (cmd & 0xff) {
7641541Srgrimes				pti->pt_ucntl = (u_char)cmd;
7651541Srgrimes				ptcwakeup(tp, FREAD);
7661541Srgrimes			}
7671541Srgrimes			return (0);
7681541Srgrimes		}
7691541Srgrimes		error = ENOTTY;
7701541Srgrimes	}
7711541Srgrimes	/*
7721541Srgrimes	 * If external processing and packet mode send ioctl packet.
7731541Srgrimes	 */
7741541Srgrimes	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
7751541Srgrimes		switch(cmd) {
7761541Srgrimes		case TIOCSETA:
7771541Srgrimes		case TIOCSETAW:
7781541Srgrimes		case TIOCSETAF:
7791541Srgrimes#ifdef COMPAT_43
7801541Srgrimes		case TIOCSETP:
7811541Srgrimes		case TIOCSETN:
7821541Srgrimes#endif
7831541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
7841541Srgrimes		case TIOCSETC:
7851541Srgrimes		case TIOCSLTC:
7861541Srgrimes		case TIOCLBIS:
7871541Srgrimes		case TIOCLBIC:
7881541Srgrimes		case TIOCLSET:
7891541Srgrimes#endif
7901541Srgrimes			pti->pt_send |= TIOCPKT_IOCTL;
7911541Srgrimes			ptcwakeup(tp, FREAD);
7921541Srgrimes		default:
7931541Srgrimes			break;
7941541Srgrimes		}
7951541Srgrimes	}
7968876Srgrimes	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
7971541Srgrimes		&& CCEQ(cc[VSTART], CTRL('q'));
7981541Srgrimes	if (pti->pt_flags & PF_NOSTOP) {
7991541Srgrimes		if (stop) {
8001541Srgrimes			pti->pt_send &= ~TIOCPKT_NOSTOP;
8011541Srgrimes			pti->pt_send |= TIOCPKT_DOSTOP;
8021541Srgrimes			pti->pt_flags &= ~PF_NOSTOP;
8031541Srgrimes			ptcwakeup(tp, FREAD);
8041541Srgrimes		}
8051541Srgrimes	} else {
8061541Srgrimes		if (!stop) {
8071541Srgrimes			pti->pt_send &= ~TIOCPKT_DOSTOP;
8081541Srgrimes			pti->pt_send |= TIOCPKT_NOSTOP;
8091541Srgrimes			pti->pt_flags |= PF_NOSTOP;
8101541Srgrimes			ptcwakeup(tp, FREAD);
8111541Srgrimes		}
8121541Srgrimes	}
8131541Srgrimes	return (error);
8141541Srgrimes}
81512517Sjulian
81612517Sjulian
81729506Sbdestatic void ptc_drvinit __P((void *unused));
81849536Sphk
81912675Sjulianstatic void
82029506Sbdeptc_drvinit(unused)
82129506Sbde	void *unused;
82212517Sjulian{
82353483Sjkh	cdevsw_add(&pts_cdevsw);
82453483Sjkh	cdevsw_add(&ptc_cdevsw);
82550254Sphk	/* XXX: Gross hack for DEVFS */
82650254Sphk	ptyinit(0);
82712517Sjulian}
82812517Sjulian
82912517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
830