pty.c revision 51654
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 51654 1999-09-25 16:21:39Z phk $
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	/* stop */	nostop,
8447625Sphk	/* reset */	noreset,
8551654Sphk	/* devtotty */	nodevtotty,
8651654Sphk	/* poll */	ttypoll,
8747625Sphk	/* mmap */	nommap,
8847625Sphk	/* strategy */	nostrategy,
8947625Sphk	/* name */	"pts",
9047625Sphk	/* parms */	noparms,
9147625Sphk	/* maj */	CDEV_MAJOR_S,
9247625Sphk	/* dump */	nodump,
9347625Sphk	/* psize */	nopsize,
9447625Sphk	/* flags */	D_TTY,
9547625Sphk	/* maxio */	0,
9647625Sphk	/* bmaj */	-1
9738485Sbde};
9812675Sjulian
9938485Sbde#define	CDEV_MAJOR_C	6
10047625Sphkstatic struct cdevsw ptc_cdevsw = {
10147625Sphk	/* open */	ptcopen,
10247625Sphk	/* close */	ptcclose,
10347625Sphk	/* read */	ptcread,
10447625Sphk	/* write */	ptcwrite,
10547625Sphk	/* ioctl */	ptyioctl,
10647625Sphk	/* stop */	nostop,
10747625Sphk	/* reset */	noreset,
10851654Sphk	/* devtotty */	nodevtotty,
10947625Sphk	/* poll */	ptcpoll,
11047625Sphk	/* mmap */	nommap,
11147625Sphk	/* strategy */	nostrategy,
11247625Sphk	/* name */	"ptc",
11347625Sphk	/* parms */	noparms,
11447625Sphk	/* maj */	CDEV_MAJOR_C,
11547625Sphk	/* dump */	nodump,
11647625Sphk	/* psize */	nopsize,
11747625Sphk	/* flags */	D_TTY,
11847625Sphk	/* maxio */	0,
11947625Sphk	/* bmaj */	-1
12038485Sbde};
12112675Sjulian
1221541Srgrimes#define BUFSIZ 100		/* Chunk size iomoved to/from user */
1231541Srgrimes
12449536Sphkstruct	pt_ioctl {
1251541Srgrimes	int	pt_flags;
1261541Srgrimes	struct	selinfo pt_selr, pt_selw;
1271541Srgrimes	u_char	pt_send;
1281541Srgrimes	u_char	pt_ucntl;
12949536Sphk	struct tty pt_tty;
13050092Sjulian	dev_t	devs, devc;
13149536Sphk};
1321541Srgrimes
1331541Srgrimes#define	PF_PKT		0x08		/* packet mode */
1341541Srgrimes#define	PF_STOPPED	0x10		/* user told stopped */
1351541Srgrimes#define	PF_REMOTE	0x20		/* remote and flow controlled input */
1361541Srgrimes#define	PF_NOSTOP	0x40
1371541Srgrimes#define PF_UCNTL	0x80		/* user control mode */
1381541Srgrimes
1391541Srgrimes/*
14049536Sphk * This function creates and initializes a pts/ptc pair
1411541Srgrimes *
14249536Sphk * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
14349536Sphk * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
14449536Sphk *
14549536Sphk * XXX: define and add mapping of upper minor bits to allow more
14649536Sphk *      than 256 ptys.
1471541Srgrimes */
14812819Sphkstatic void
14949536Sphkptyinit(n)
1501541Srgrimes	int n;
1511541Srgrimes{
15249536Sphk	dev_t devs, devc;
15349536Sphk	char *names = "pqrsPQRS";
15449536Sphk	struct pt_ioctl *pt;
1551541Srgrimes
15649536Sphk	/* For now we only map the lower 8 bits of the minor */
15749536Sphk	if (n & ~0xff)
15849536Sphk		return;
15949536Sphk
16050092Sjulian	pt = malloc(sizeof(*pt), M_PTY, M_WAITOK);
16150092Sjulian	bzero(pt, sizeof(*pt));
16250092Sjulian	pt->devs = devs = make_dev(&pts_cdevsw, n,
16349536Sphk	    0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
16450092Sjulian	pt->devc = devc = make_dev(&ptc_cdevsw, n,
16549536Sphk	    0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
16649536Sphk
16749536Sphk	devs->si_drv1 = devc->si_drv1 = pt;
16850652Sphk	devs->si_tty = devc->si_tty = &pt->pt_tty;
16949540Sphk	ttyregister(&pt->pt_tty);
17016322Sgpalmer}
1711541Srgrimes
1721541Srgrimes/*ARGSUSED*/
17312675Sjulianstatic	int
1741541Srgrimesptsopen(dev, flag, devtype, p)
1751541Srgrimes	dev_t dev;
1761541Srgrimes	int flag, devtype;
1771541Srgrimes	struct proc *p;
1781541Srgrimes{
1791541Srgrimes	register struct tty *tp;
1801541Srgrimes	int error;
18149992Sjulian	int minr;
18249992Sjulian	dev_t nextdev;
1831541Srgrimes
18449992Sjulian	/*
18550254Sphk	 * XXX: Gross hack for DEVFS:
18649992Sjulian	 * If we openned this device, ensure we have the
18750254Sphk	 * next one too, so people can open it.
18849992Sjulian	 */
18949992Sjulian	minr = lminor(dev);
19049992Sjulian	if (minr < 255) {
19149992Sjulian		nextdev = makedev(major(dev), minr + 1);
19249992Sjulian		if (!nextdev->si_drv1) {
19349992Sjulian			ptyinit(minr + 1);
19449992Sjulian		}
19549992Sjulian	}
19649536Sphk	if (!dev->si_drv1)
19749536Sphk		ptyinit(minor(dev));
19849536Sphk	if (!dev->si_drv1)
19949536Sphk		return(ENXIO);
20050652Sphk	tp = dev->si_tty;
2011541Srgrimes	if ((tp->t_state & TS_ISOPEN) == 0) {
2021541Srgrimes		ttychars(tp);		/* Set up default chars */
2031541Srgrimes		tp->t_iflag = TTYDEF_IFLAG;
2041541Srgrimes		tp->t_oflag = TTYDEF_OFLAG;
2051541Srgrimes		tp->t_lflag = TTYDEF_LFLAG;
2061541Srgrimes		tp->t_cflag = TTYDEF_CFLAG;
2071541Srgrimes		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
20846112Sphk	} else if (tp->t_state & TS_XCLUDE && suser(p))
2091541Srgrimes		return (EBUSY);
2101541Srgrimes	if (tp->t_oproc)			/* Ctrlr still around. */
2119824Sbde		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
2121541Srgrimes	while ((tp->t_state & TS_CARR_ON) == 0) {
2131541Srgrimes		if (flag&FNONBLOCK)
2141541Srgrimes			break;
2159639Sbde		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
2169624Sbde				 "ptsopn", 0);
2173308Sphk		if (error)
2181541Srgrimes			return (error);
2191541Srgrimes	}
2201541Srgrimes	error = (*linesw[tp->t_line].l_open)(dev, tp);
2217724Sache	if (error == 0)
2227724Sache		ptcwakeup(tp, FREAD|FWRITE);
2231541Srgrimes	return (error);
2241541Srgrimes}
2251541Srgrimes
22612675Sjulianstatic	int
2271541Srgrimesptsclose(dev, flag, mode, p)
2281541Srgrimes	dev_t dev;
2291541Srgrimes	int flag, mode;
2301541Srgrimes	struct proc *p;
2311541Srgrimes{
2321541Srgrimes	register struct tty *tp;
2331541Srgrimes	int err;
2341541Srgrimes
23550652Sphk	tp = dev->si_tty;
2361541Srgrimes	err = (*linesw[tp->t_line].l_close)(tp, flag);
2377730Sache	ptsstop(tp, FREAD|FWRITE);
2387724Sache	(void) ttyclose(tp);
2391541Srgrimes	return (err);
2401541Srgrimes}
2411541Srgrimes
24212675Sjulianstatic	int
2431541Srgrimesptsread(dev, uio, flag)
2441541Srgrimes	dev_t dev;
2451541Srgrimes	struct uio *uio;
2461541Srgrimes	int flag;
2471541Srgrimes{
2481541Srgrimes	struct proc *p = curproc;
24950652Sphk	register struct tty *tp = dev->si_tty;
25049536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
2511541Srgrimes	int error = 0;
2521541Srgrimes
2531541Srgrimesagain:
2541541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
2551541Srgrimes		while (isbackground(p, tp)) {
2561541Srgrimes			if ((p->p_sigignore & sigmask(SIGTTIN)) ||
2571541Srgrimes			    (p->p_sigmask & sigmask(SIGTTIN)) ||
2581541Srgrimes			    p->p_pgrp->pg_jobc == 0 ||
2591541Srgrimes			    p->p_flag & P_PPWAIT)
2601541Srgrimes				return (EIO);
2611541Srgrimes			pgsignal(p->p_pgrp, SIGTTIN, 1);
2629624Sbde			error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
2639624Sbde					 0);
2643308Sphk			if (error)
2651541Srgrimes				return (error);
2661541Srgrimes		}
2671541Srgrimes		if (tp->t_canq.c_cc == 0) {
2681541Srgrimes			if (flag & IO_NDELAY)
2691541Srgrimes				return (EWOULDBLOCK);
2709639Sbde			error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
2719624Sbde					 "ptsin", 0);
2723308Sphk			if (error)
2731541Srgrimes				return (error);
2741541Srgrimes			goto again;
2751541Srgrimes		}
2761541Srgrimes		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
2771541Srgrimes			if (ureadc(getc(&tp->t_canq), uio) < 0) {
2781541Srgrimes				error = EFAULT;
2791541Srgrimes				break;
2801541Srgrimes			}
2811541Srgrimes		if (tp->t_canq.c_cc == 1)
2821541Srgrimes			(void) getc(&tp->t_canq);
2831541Srgrimes		if (tp->t_canq.c_cc)
2841541Srgrimes			return (error);
2851541Srgrimes	} else
2861541Srgrimes		if (tp->t_oproc)
2871541Srgrimes			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
2881541Srgrimes	ptcwakeup(tp, FWRITE);
2891541Srgrimes	return (error);
2901541Srgrimes}
2911541Srgrimes
2921541Srgrimes/*
2931541Srgrimes * Write to pseudo-tty.
2941541Srgrimes * Wakeups of controlling tty will happen
2951541Srgrimes * indirectly, when tty driver calls ptsstart.
2961541Srgrimes */
29712675Sjulianstatic	int
2981541Srgrimesptswrite(dev, uio, flag)
2991541Srgrimes	dev_t dev;
3001541Srgrimes	struct uio *uio;
3011541Srgrimes	int flag;
3021541Srgrimes{
3031541Srgrimes	register struct tty *tp;
3041541Srgrimes
30550652Sphk	tp = dev->si_tty;
3061541Srgrimes	if (tp->t_oproc == 0)
3071541Srgrimes		return (EIO);
3081541Srgrimes	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
3091541Srgrimes}
3101541Srgrimes
3111541Srgrimes/*
3121541Srgrimes * Start output on pseudo-tty.
3131541Srgrimes * Wake up process selecting or sleeping for input from controlling tty.
3141541Srgrimes */
31512819Sphkstatic void
3161541Srgrimesptsstart(tp)
3171541Srgrimes	struct tty *tp;
3181541Srgrimes{
31949536Sphk	register struct pt_ioctl *pti = tp->t_dev->si_drv1;
3201541Srgrimes
3211541Srgrimes	if (tp->t_state & TS_TTSTOP)
3221541Srgrimes		return;
3231541Srgrimes	if (pti->pt_flags & PF_STOPPED) {
3241541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
3251541Srgrimes		pti->pt_send = TIOCPKT_START;
3261541Srgrimes	}
3271541Srgrimes	ptcwakeup(tp, FREAD);
3281541Srgrimes}
3291541Srgrimes
33012819Sphkstatic void
3311541Srgrimesptcwakeup(tp, flag)
3321541Srgrimes	struct tty *tp;
3331541Srgrimes	int flag;
3341541Srgrimes{
33549536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
3361541Srgrimes
3371541Srgrimes	if (flag & FREAD) {
3381541Srgrimes		selwakeup(&pti->pt_selr);
3399639Sbde		wakeup(TSA_PTC_READ(tp));
3401541Srgrimes	}
3411541Srgrimes	if (flag & FWRITE) {
3421541Srgrimes		selwakeup(&pti->pt_selw);
3439639Sbde		wakeup(TSA_PTC_WRITE(tp));
3441541Srgrimes	}
3451541Srgrimes}
3461541Srgrimes
34712675Sjulianstatic	int
3481541Srgrimesptcopen(dev, flag, devtype, p)
3491541Srgrimes	dev_t dev;
3501541Srgrimes	int flag, devtype;
3511541Srgrimes	struct proc *p;
3521541Srgrimes{
3531541Srgrimes	register struct tty *tp;
3541541Srgrimes	struct pt_ioctl *pti;
3551541Srgrimes
35649536Sphk	if (!dev->si_drv1)
35749536Sphk		ptyinit(minor(dev));
35849536Sphk	if (!dev->si_drv1)
35949536Sphk		return(ENXIO);
36050652Sphk	tp = dev->si_tty;
3611541Srgrimes	if (tp->t_oproc)
3621541Srgrimes		return (EIO);
3631541Srgrimes	tp->t_oproc = ptsstart;
36451654Sphk	tp->t_stop = ptsstop;
3651541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
3661541Srgrimes	tp->t_lflag &= ~EXTPROC;
36749536Sphk	pti = dev->si_drv1;
3681541Srgrimes	pti->pt_flags = 0;
3691541Srgrimes	pti->pt_send = 0;
3701541Srgrimes	pti->pt_ucntl = 0;
3711541Srgrimes	return (0);
3721541Srgrimes}
3731541Srgrimes
37412675Sjulianstatic	int
37510624Sbdeptcclose(dev, flags, fmt, p)
3761541Srgrimes	dev_t dev;
37710624Sbde	int flags;
37810624Sbde	int fmt;
37910624Sbde	struct proc *p;
3801541Srgrimes{
3811541Srgrimes	register struct tty *tp;
3821541Srgrimes
38350652Sphk	tp = dev->si_tty;
3841541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
3859824Sbde
3869824Sbde	/*
3879824Sbde	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
3889824Sbde	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
3899824Sbde	 * l_modem()s that ignore carrier drop make no sense for ptys but
3909824Sbde	 * may be in use because other parts of the line discipline make
3919824Sbde	 * sense for ptys.  Recover by doing everything that a normal
3929824Sbde	 * ttymodem() would have done except for sending a SIGHUP.
3939824Sbde	 */
3949850Sbde	if (tp->t_state & TS_ISOPEN) {
3959850Sbde		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
3969850Sbde		tp->t_state |= TS_ZOMBIE;
3979850Sbde		ttyflush(tp, FREAD | FWRITE);
3989850Sbde	}
3999824Sbde
4001541Srgrimes	tp->t_oproc = 0;		/* mark closed */
4011541Srgrimes	return (0);
4021541Srgrimes}
4031541Srgrimes
40412675Sjulianstatic	int
4051541Srgrimesptcread(dev, uio, flag)
4061541Srgrimes	dev_t dev;
4071541Srgrimes	struct uio *uio;
4081541Srgrimes	int flag;
4091541Srgrimes{
41050652Sphk	register struct tty *tp = dev->si_tty;
41149536Sphk	struct pt_ioctl *pti = dev->si_drv1;
4121541Srgrimes	char buf[BUFSIZ];
4131541Srgrimes	int error = 0, cc;
4141541Srgrimes
4151541Srgrimes	/*
4161541Srgrimes	 * We want to block until the slave
4171541Srgrimes	 * is open, and there's something to read;
4181541Srgrimes	 * but if we lost the slave or we're NBIO,
4191541Srgrimes	 * then return the appropriate error instead.
4201541Srgrimes	 */
4211541Srgrimes	for (;;) {
4221541Srgrimes		if (tp->t_state&TS_ISOPEN) {
4231541Srgrimes			if (pti->pt_flags&PF_PKT && pti->pt_send) {
4241541Srgrimes				error = ureadc((int)pti->pt_send, uio);
4251541Srgrimes				if (error)
4261541Srgrimes					return (error);
4271541Srgrimes				if (pti->pt_send & TIOCPKT_IOCTL) {
4281541Srgrimes					cc = min(uio->uio_resid,
4291541Srgrimes						sizeof(tp->t_termios));
4302807Sbde					uiomove((caddr_t)&tp->t_termios, cc,
4312807Sbde						uio);
4321541Srgrimes				}
4331541Srgrimes				pti->pt_send = 0;
4341541Srgrimes				return (0);
4351541Srgrimes			}
4361541Srgrimes			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
4371541Srgrimes				error = ureadc((int)pti->pt_ucntl, uio);
4381541Srgrimes				if (error)
4391541Srgrimes					return (error);
4401541Srgrimes				pti->pt_ucntl = 0;
4411541Srgrimes				return (0);
4421541Srgrimes			}
4431541Srgrimes			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
4441541Srgrimes				break;
4451541Srgrimes		}
4469824Sbde		if ((tp->t_state & TS_CONNECTED) == 0)
4471541Srgrimes			return (0);	/* EOF */
4481541Srgrimes		if (flag & IO_NDELAY)
4491541Srgrimes			return (EWOULDBLOCK);
4509639Sbde		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
4513308Sphk		if (error)
4521541Srgrimes			return (error);
4531541Srgrimes	}
4541541Srgrimes	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
4551541Srgrimes		error = ureadc(0, uio);
4561541Srgrimes	while (uio->uio_resid > 0 && error == 0) {
4571541Srgrimes		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
4581541Srgrimes		if (cc <= 0)
4591541Srgrimes			break;
4601541Srgrimes		error = uiomove(buf, cc, uio);
4611541Srgrimes	}
4629626Sbde	ttwwakeup(tp);
4631541Srgrimes	return (error);
4641541Srgrimes}
4651541Srgrimes
46612675Sjulianstatic	void
4671541Srgrimesptsstop(tp, flush)
4681541Srgrimes	register struct tty *tp;
4691541Srgrimes	int flush;
4701541Srgrimes{
47149536Sphk	struct pt_ioctl *pti = tp->t_dev->si_drv1;
4721541Srgrimes	int flag;
4731541Srgrimes
4741541Srgrimes	/* note: FLUSHREAD and FLUSHWRITE already ok */
4751541Srgrimes	if (flush == 0) {
4761541Srgrimes		flush = TIOCPKT_STOP;
4771541Srgrimes		pti->pt_flags |= PF_STOPPED;
4781541Srgrimes	} else
4791541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
4801541Srgrimes	pti->pt_send |= flush;
4811541Srgrimes	/* change of perspective */
4821541Srgrimes	flag = 0;
4831541Srgrimes	if (flush & FREAD)
4841541Srgrimes		flag |= FWRITE;
4851541Srgrimes	if (flush & FWRITE)
4861541Srgrimes		flag |= FREAD;
4871541Srgrimes	ptcwakeup(tp, flag);
4881541Srgrimes}
4891541Srgrimes
49012675Sjulianstatic	int
49129354Speterptcpoll(dev, events, p)
4921541Srgrimes	dev_t dev;
49329354Speter	int events;
4941541Srgrimes	struct proc *p;
4951541Srgrimes{
49650652Sphk	register struct tty *tp = dev->si_tty;
49749536Sphk	struct pt_ioctl *pti = dev->si_drv1;
49829354Speter	int revents = 0;
4991541Srgrimes	int s;
5001541Srgrimes
5019824Sbde	if ((tp->t_state & TS_CONNECTED) == 0)
50229354Speter		return (seltrue(dev, events, p) | POLLHUP);
5031541Srgrimes
50429354Speter	/*
50529354Speter	 * Need to block timeouts (ttrstart).
50629354Speter	 */
50729354Speter	s = spltty();
5081541Srgrimes
50929354Speter	if (events & (POLLIN | POLLRDNORM))
51029354Speter		if ((tp->t_state & TS_ISOPEN) &&
51129354Speter		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
51229354Speter		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
51329354Speter		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
51429354Speter			revents |= events & (POLLIN | POLLRDNORM);
5151541Srgrimes
51629354Speter	if (events & (POLLOUT | POLLWRNORM))
51729354Speter		if (tp->t_state & TS_ISOPEN &&
51829354Speter		    ((pti->pt_flags & PF_REMOTE) ?
51929354Speter		     (tp->t_canq.c_cc == 0) :
52029354Speter		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
52129354Speter		      (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON)))))
52229354Speter			revents |= events & (POLLOUT | POLLWRNORM);
5231541Srgrimes
52429354Speter	if (events & POLLHUP)
52529354Speter		if ((tp->t_state & TS_CARR_ON) == 0)
52629354Speter			revents |= POLLHUP;
5271541Srgrimes
52829354Speter	if (revents == 0) {
52929354Speter		if (events & (POLLIN | POLLRDNORM))
53029354Speter			selrecord(p, &pti->pt_selr);
53129354Speter
53229354Speter		if (events & (POLLOUT | POLLWRNORM))
53329354Speter			selrecord(p, &pti->pt_selw);
5341541Srgrimes	}
53529354Speter	splx(s);
53629354Speter
53729354Speter	return (revents);
5381541Srgrimes}
5391541Srgrimes
54012675Sjulianstatic	int
5411541Srgrimesptcwrite(dev, uio, flag)
5421541Srgrimes	dev_t dev;
5431541Srgrimes	register struct uio *uio;
5441541Srgrimes	int flag;
5451541Srgrimes{
54650652Sphk	register struct tty *tp = dev->si_tty;
5471549Srgrimes	register u_char *cp = 0;
5481541Srgrimes	register int cc = 0;
5491541Srgrimes	u_char locbuf[BUFSIZ];
5501541Srgrimes	int cnt = 0;
55149536Sphk	struct pt_ioctl *pti = dev->si_drv1;
5521541Srgrimes	int error = 0;
5531541Srgrimes
5541541Srgrimesagain:
5551541Srgrimes	if ((tp->t_state&TS_ISOPEN) == 0)
5561541Srgrimes		goto block;
5571541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
5581541Srgrimes		if (tp->t_canq.c_cc)
5591541Srgrimes			goto block;
56011789Sbde		while ((uio->uio_resid > 0 || cc > 0) &&
56111789Sbde		       tp->t_canq.c_cc < TTYHOG - 1) {
5621541Srgrimes			if (cc == 0) {
5631541Srgrimes				cc = min(uio->uio_resid, BUFSIZ);
5641541Srgrimes				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
5651541Srgrimes				cp = locbuf;
5661541Srgrimes				error = uiomove((caddr_t)cp, cc, uio);
5671541Srgrimes				if (error)
5681541Srgrimes					return (error);
5691541Srgrimes				/* check again for safety */
57011789Sbde				if ((tp->t_state & TS_ISOPEN) == 0) {
57111789Sbde					/* adjust as usual */
57211789Sbde					uio->uio_resid += cc;
5731541Srgrimes					return (EIO);
57411789Sbde				}
5751541Srgrimes			}
57615199Sbde			if (cc > 0) {
57715199Sbde				cc = b_to_q((char *)cp, cc, &tp->t_canq);
57815199Sbde				/*
57915199Sbde				 * XXX we don't guarantee that the canq size
58015199Sbde				 * is >= TTYHOG, so the above b_to_q() may
58115199Sbde				 * leave some bytes uncopied.  However, space
58215199Sbde				 * is guaranteed for the null terminator if
58315199Sbde				 * we don't fail here since (TTYHOG - 1) is
58415199Sbde				 * not a multiple of CBSIZE.
58515199Sbde				 */
58615199Sbde				if (cc > 0)
58715199Sbde					break;
58815199Sbde			}
5891541Srgrimes		}
59011789Sbde		/* adjust for data copied in but not written */
59111789Sbde		uio->uio_resid += cc;
5921541Srgrimes		(void) putc(0, &tp->t_canq);
5931541Srgrimes		ttwakeup(tp);
5949639Sbde		wakeup(TSA_PTS_READ(tp));
5951541Srgrimes		return (0);
5961541Srgrimes	}
59711789Sbde	while (uio->uio_resid > 0 || cc > 0) {
5981541Srgrimes		if (cc == 0) {
5991541Srgrimes			cc = min(uio->uio_resid, BUFSIZ);
6001541Srgrimes			cp = locbuf;
6011541Srgrimes			error = uiomove((caddr_t)cp, cc, uio);
6021541Srgrimes			if (error)
6031541Srgrimes				return (error);
6041541Srgrimes			/* check again for safety */
60511789Sbde			if ((tp->t_state & TS_ISOPEN) == 0) {
60611789Sbde				/* adjust for data copied in but not written */
60711789Sbde				uio->uio_resid += cc;
6081541Srgrimes				return (EIO);
60911789Sbde			}
6101541Srgrimes		}
6111541Srgrimes		while (cc > 0) {
6121541Srgrimes			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
6131541Srgrimes			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
6149824Sbde				wakeup(TSA_HUP_OR_INPUT(tp));
6151541Srgrimes				goto block;
6161541Srgrimes			}
6171541Srgrimes			(*linesw[tp->t_line].l_rint)(*cp++, tp);
6181541Srgrimes			cnt++;
6191541Srgrimes			cc--;
6201541Srgrimes		}
6211541Srgrimes		cc = 0;
6221541Srgrimes	}
6231541Srgrimes	return (0);
6241541Srgrimesblock:
6251541Srgrimes	/*
6261541Srgrimes	 * Come here to wait for slave to open, for space
62715199Sbde	 * in outq, or space in rawq, or an empty canq.
6281541Srgrimes	 */
62911789Sbde	if ((tp->t_state & TS_CONNECTED) == 0) {
63011789Sbde		/* adjust for data copied in but not written */
63111789Sbde		uio->uio_resid += cc;
6321541Srgrimes		return (EIO);
63311789Sbde	}
6341541Srgrimes	if (flag & IO_NDELAY) {
6351541Srgrimes		/* adjust for data copied in but not written */
6361541Srgrimes		uio->uio_resid += cc;
6371541Srgrimes		if (cnt == 0)
6381541Srgrimes			return (EWOULDBLOCK);
6391541Srgrimes		return (0);
6401541Srgrimes	}
6419639Sbde	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
6423308Sphk	if (error) {
6431541Srgrimes		/* adjust for data copied in but not written */
6441541Srgrimes		uio->uio_resid += cc;
6451541Srgrimes		return (error);
6461541Srgrimes	}
6471541Srgrimes	goto again;
6481541Srgrimes}
6491541Srgrimes
6501541Srgrimes/*ARGSUSED*/
65112675Sjulianstatic	int
6521541Srgrimesptyioctl(dev, cmd, data, flag, p)
6531541Srgrimes	dev_t dev;
65436735Sdfr	u_long cmd;
6551541Srgrimes	caddr_t data;
6561541Srgrimes	int flag;
6571541Srgrimes	struct proc *p;
6581541Srgrimes{
65950652Sphk	register struct tty *tp = dev->si_tty;
66049536Sphk	register struct pt_ioctl *pti = dev->si_drv1;
6611541Srgrimes	register u_char *cc = tp->t_cc;
6621541Srgrimes	int stop, error;
6631541Srgrimes
66447301Sluoqi	if (devsw(dev)->d_open == ptcopen) {
6651541Srgrimes		switch (cmd) {
6661541Srgrimes
6671541Srgrimes		case TIOCGPGRP:
6681541Srgrimes			/*
66933826Sbde			 * We avoid calling ttioctl on the controller since,
6701541Srgrimes			 * in that case, tp must be the controlling terminal.
6711541Srgrimes			 */
6721541Srgrimes			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
6731541Srgrimes			return (0);
6741541Srgrimes
6751541Srgrimes		case TIOCPKT:
6761541Srgrimes			if (*(int *)data) {
6771541Srgrimes				if (pti->pt_flags & PF_UCNTL)
6781541Srgrimes					return (EINVAL);
6791541Srgrimes				pti->pt_flags |= PF_PKT;
6801541Srgrimes			} else
6811541Srgrimes				pti->pt_flags &= ~PF_PKT;
6821541Srgrimes			return (0);
6831541Srgrimes
6841541Srgrimes		case TIOCUCNTL:
6851541Srgrimes			if (*(int *)data) {
6861541Srgrimes				if (pti->pt_flags & PF_PKT)
6871541Srgrimes					return (EINVAL);
6881541Srgrimes				pti->pt_flags |= PF_UCNTL;
6891541Srgrimes			} else
6901541Srgrimes				pti->pt_flags &= ~PF_UCNTL;
6911541Srgrimes			return (0);
6921541Srgrimes
6931541Srgrimes		case TIOCREMOTE:
6941541Srgrimes			if (*(int *)data)
6951541Srgrimes				pti->pt_flags |= PF_REMOTE;
6961541Srgrimes			else
6971541Srgrimes				pti->pt_flags &= ~PF_REMOTE;
6981541Srgrimes			ttyflush(tp, FREAD|FWRITE);
6991541Srgrimes			return (0);
70047203Sluoqi		}
7011541Srgrimes
70247203Sluoqi		/*
70347203Sluoqi		 * The rest of the ioctls shouldn't be called until
70447301Sluoqi		 * the slave is open.
70547203Sluoqi		 */
70647203Sluoqi		if ((tp->t_state & TS_ISOPEN) == 0)
70747301Sluoqi			return (EAGAIN);
70847203Sluoqi
70947203Sluoqi		switch (cmd) {
7101541Srgrimes#ifdef COMPAT_43
7118876Srgrimes		case TIOCSETP:
7121541Srgrimes		case TIOCSETN:
7131541Srgrimes#endif
7141541Srgrimes		case TIOCSETD:
7151541Srgrimes		case TIOCSETA:
7161541Srgrimes		case TIOCSETAW:
7179858Sache		case TIOCSETAF:
71847301Sluoqi			/*
71947301Sluoqi			 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
72047301Sluoqi			 * ttywflush(tp) will hang if there are characters in
72147301Sluoqi			 * the outq.
72247301Sluoqi			 */
7231541Srgrimes			ndflush(&tp->t_outq, tp->t_outq.c_cc);
7241541Srgrimes			break;
7251541Srgrimes
7261541Srgrimes		case TIOCSIG:
72727770Sjmg			if (*(unsigned int *)data >= NSIG ||
72827770Sjmg			    *(unsigned int *)data == 0)
7291541Srgrimes				return(EINVAL);
7301541Srgrimes			if ((tp->t_lflag&NOFLSH) == 0)
7311541Srgrimes				ttyflush(tp, FREAD|FWRITE);
7321541Srgrimes			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
7331541Srgrimes			if ((*(unsigned int *)data == SIGINFO) &&
7341541Srgrimes			    ((tp->t_lflag&NOKERNINFO) == 0))
7351541Srgrimes				ttyinfo(tp);
7361541Srgrimes			return(0);
7371541Srgrimes		}
73847203Sluoqi	}
73947301Sluoqi	if (cmd == TIOCEXT) {
74047301Sluoqi		/*
74147301Sluoqi		 * When the EXTPROC bit is being toggled, we need
74247301Sluoqi		 * to send an TIOCPKT_IOCTL if the packet driver
74347301Sluoqi		 * is turned on.
74447301Sluoqi		 */
74547301Sluoqi		if (*(int *)data) {
74647301Sluoqi			if (pti->pt_flags & PF_PKT) {
74747301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
74847301Sluoqi				ptcwakeup(tp, FREAD);
74947301Sluoqi			}
75047301Sluoqi			tp->t_lflag |= EXTPROC;
75147301Sluoqi		} else {
75247301Sluoqi			if ((tp->t_lflag & EXTPROC) &&
75347301Sluoqi			    (pti->pt_flags & PF_PKT)) {
75447301Sluoqi				pti->pt_send |= TIOCPKT_IOCTL;
75547301Sluoqi				ptcwakeup(tp, FREAD);
75647301Sluoqi			}
75747301Sluoqi			tp->t_lflag &= ~EXTPROC;
75847301Sluoqi		}
75947301Sluoqi		return(0);
76047301Sluoqi	}
7611541Srgrimes	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
76231577Sbde	if (error == ENOIOCTL)
7631541Srgrimes		 error = ttioctl(tp, cmd, data, flag);
76431577Sbde	if (error == ENOIOCTL) {
7651541Srgrimes		if (pti->pt_flags & PF_UCNTL &&
7661541Srgrimes		    (cmd & ~0xff) == UIOCCMD(0)) {
7671541Srgrimes			if (cmd & 0xff) {
7681541Srgrimes				pti->pt_ucntl = (u_char)cmd;
7691541Srgrimes				ptcwakeup(tp, FREAD);
7701541Srgrimes			}
7711541Srgrimes			return (0);
7721541Srgrimes		}
7731541Srgrimes		error = ENOTTY;
7741541Srgrimes	}
7751541Srgrimes	/*
7761541Srgrimes	 * If external processing and packet mode send ioctl packet.
7771541Srgrimes	 */
7781541Srgrimes	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
7791541Srgrimes		switch(cmd) {
7801541Srgrimes		case TIOCSETA:
7811541Srgrimes		case TIOCSETAW:
7821541Srgrimes		case TIOCSETAF:
7831541Srgrimes#ifdef COMPAT_43
7841541Srgrimes		case TIOCSETP:
7851541Srgrimes		case TIOCSETN:
7861541Srgrimes#endif
7871541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
7881541Srgrimes		case TIOCSETC:
7891541Srgrimes		case TIOCSLTC:
7901541Srgrimes		case TIOCLBIS:
7911541Srgrimes		case TIOCLBIC:
7921541Srgrimes		case TIOCLSET:
7931541Srgrimes#endif
7941541Srgrimes			pti->pt_send |= TIOCPKT_IOCTL;
7951541Srgrimes			ptcwakeup(tp, FREAD);
7961541Srgrimes		default:
7971541Srgrimes			break;
7981541Srgrimes		}
7991541Srgrimes	}
8008876Srgrimes	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
8011541Srgrimes		&& CCEQ(cc[VSTART], CTRL('q'));
8021541Srgrimes	if (pti->pt_flags & PF_NOSTOP) {
8031541Srgrimes		if (stop) {
8041541Srgrimes			pti->pt_send &= ~TIOCPKT_NOSTOP;
8051541Srgrimes			pti->pt_send |= TIOCPKT_DOSTOP;
8061541Srgrimes			pti->pt_flags &= ~PF_NOSTOP;
8071541Srgrimes			ptcwakeup(tp, FREAD);
8081541Srgrimes		}
8091541Srgrimes	} else {
8101541Srgrimes		if (!stop) {
8111541Srgrimes			pti->pt_send &= ~TIOCPKT_DOSTOP;
8121541Srgrimes			pti->pt_send |= TIOCPKT_NOSTOP;
8131541Srgrimes			pti->pt_flags |= PF_NOSTOP;
8141541Srgrimes			ptcwakeup(tp, FREAD);
8151541Srgrimes		}
8161541Srgrimes	}
8171541Srgrimes	return (error);
8181541Srgrimes}
81912517Sjulian
82012517Sjulian
82129506Sbdestatic void ptc_drvinit __P((void *unused));
82249536Sphk
82312675Sjulianstatic void
82429506Sbdeptc_drvinit(unused)
82529506Sbde	void *unused;
82612517Sjulian{
82750254Sphk	cdevsw_add(&pts_cdevsw);
82850254Sphk	cdevsw_add(&ptc_cdevsw);
82950254Sphk	/* XXX: Gross hack for DEVFS */
83050254Sphk	ptyinit(0);
83112517Sjulian}
83212517Sjulian
83312517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
834