pty.c revision 14511
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
3414511Shsu * $Id: tty_pty.c,v 1.38 1996/02/29 00:18:11 hsu Exp $
351541Srgrimes */
361541Srgrimes
371541Srgrimes/*
381541Srgrimes * Pseudo-teletype Driver
391541Srgrimes * (Actually two drivers, requiring two entries in 'cdevsw')
401541Srgrimes */
411541Srgrimes#include "pty.h"		/* XXX */
421541Srgrimes
431541Srgrimes#include <sys/param.h>
441541Srgrimes#include <sys/systm.h>
451541Srgrimes#include <sys/ioctl.h>
461541Srgrimes#include <sys/proc.h>
471541Srgrimes#include <sys/tty.h>
481541Srgrimes#include <sys/conf.h>
491541Srgrimes#include <sys/file.h>
501541Srgrimes#include <sys/uio.h>
511541Srgrimes#include <sys/kernel.h>
521541Srgrimes#include <sys/vnode.h>
533308Sphk#include <sys/signalvar.h>
541541Srgrimes
5512517Sjulian#ifdef DEVFS
5612517Sjulian#include <sys/devfsext.h>
5712517Sjulian#endif /*DEVFS*/
5812517Sjulian
5912819Sphkstatic void ptyattach __P((int n));
6012819Sphkstatic void ptsstart __P((struct tty *tp));
6112819Sphkstatic void ptcwakeup __P((struct tty *tp, int flag));
6211789Sbde
6312675Sjulianstatic	d_open_t	ptsopen;
6412675Sjulianstatic	d_close_t	ptsclose;
6512675Sjulianstatic	d_read_t	ptsread;
6612675Sjulianstatic	d_write_t	ptswrite;
6712675Sjulianstatic	d_ioctl_t	ptyioctl;
6812675Sjulianstatic	d_stop_t	ptsstop;
6912731Sbdestatic	d_devtotty_t	ptydevtotty;
7012675Sjulianstatic	d_open_t	ptcopen;
7112675Sjulianstatic	d_close_t	ptcclose;
7212675Sjulianstatic	d_read_t	ptcread;
7312675Sjulianstatic	d_write_t	ptcwrite;
7412675Sjulianstatic	d_ioctl_t	ptcioctl;
7512675Sjulianstatic	d_select_t	ptcselect;
7612675Sjulian
7712675Sjulian#define CDEV_MAJOR_S 5
7812675Sjulian#define CDEV_MAJOR_C 6
7912678Sphkstatic struct cdevsw pts_cdevsw =
8012675Sjulian	{ ptsopen,	ptsclose,	ptsread,	ptswrite,	/*5*/
8112675Sjulian	  ptyioctl,	ptsstop,	nullreset,	ptydevtotty,/* ttyp */
8212675Sjulian	  ttselect,	nommap,		NULL,	"pts",	NULL,	-1 };
8312675Sjulian
8412678Sphkstatic struct cdevsw ptc_cdevsw =
8512675Sjulian	{ ptcopen,	ptcclose,	ptcread,	ptcwrite,	/*6*/
8612675Sjulian	  ptyioctl,	nullstop,	nullreset,	ptydevtotty,/* ptyp */
8712675Sjulian	  ptcselect,	nommap,		NULL,	"ptc",	NULL,	-1 };
8812675Sjulian
8912675Sjulian
901541Srgrimes#if NPTY == 1
911541Srgrimes#undef NPTY
921541Srgrimes#define	NPTY	32		/* crude XXX */
931541Srgrimes#endif
941541Srgrimes
951541Srgrimes#define BUFSIZ 100		/* Chunk size iomoved to/from user */
961541Srgrimes
971541Srgrimes/*
9812564Sjulian * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
9912564Sjulian * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
1001541Srgrimes */
10112819Sphkstatic struct	tty pt_tty[NPTY];	/* XXX */
10212819Sphkstatic struct	pt_ioctl {
1031541Srgrimes	int	pt_flags;
1041541Srgrimes	struct	selinfo pt_selr, pt_selw;
1051541Srgrimes	u_char	pt_send;
1061541Srgrimes	u_char	pt_ucntl;
1071541Srgrimes} pt_ioctl[NPTY];		/* XXX */
10812819Sphkstatic int	npty = NPTY;		/* for pstat -t */
1091541Srgrimes
1101541Srgrimes#define	PF_PKT		0x08		/* packet mode */
1111541Srgrimes#define	PF_STOPPED	0x10		/* user told stopped */
1121541Srgrimes#define	PF_REMOTE	0x20		/* remote and flow controlled input */
1131541Srgrimes#define	PF_NOSTOP	0x40
1141541Srgrimes#define PF_UCNTL	0x80		/* user control mode */
1151541Srgrimes
1161541Srgrimes/*
1171541Srgrimes * Establish n (or default if n is 1) ptys in the system.
1181541Srgrimes *
1191541Srgrimes * XXX cdevsw & pstat require the array `pty[]' to be an array
1201541Srgrimes */
12112819Sphkstatic void
1221541Srgrimesptyattach(n)
1231541Srgrimes	int n;
1241541Srgrimes{
1251541Srgrimes#ifdef notyet
1261541Srgrimes	char *mem;
1271541Srgrimes	register u_long ntb;
1281541Srgrimes#define	DEFAULT_NPTY	32
1291541Srgrimes
1301541Srgrimes	/* maybe should allow 0 => none? */
1311541Srgrimes	if (n <= 1)
1321541Srgrimes		n = DEFAULT_NPTY;
1331541Srgrimes	ntb = n * sizeof(struct tty);
1341541Srgrimes	mem = malloc(ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl),
1351541Srgrimes	    M_DEVBUF, M_WAITOK);
1361541Srgrimes	pt_tty = (struct tty *)mem;
1371541Srgrimes	mem = (char *)ALIGN(mem + ntb);
1381541Srgrimes	pt_ioctl = (struct pt_ioctl *)mem;
1391541Srgrimes	npty = n;
1401541Srgrimes#endif
1411541Srgrimes}
1421541Srgrimes
1431541Srgrimes/*ARGSUSED*/
14412675Sjulianstatic	int
1451541Srgrimesptsopen(dev, flag, devtype, p)
1461541Srgrimes	dev_t dev;
1471541Srgrimes	int flag, devtype;
1481541Srgrimes	struct proc *p;
1491541Srgrimes{
1501541Srgrimes	register struct tty *tp;
1511541Srgrimes	int error;
1521541Srgrimes
1531541Srgrimes	if (minor(dev) >= npty)
1541541Srgrimes		return (ENXIO);
1551541Srgrimes	tp = &pt_tty[minor(dev)];
1561541Srgrimes	if ((tp->t_state & TS_ISOPEN) == 0) {
1571541Srgrimes		ttychars(tp);		/* Set up default chars */
1581541Srgrimes		tp->t_iflag = TTYDEF_IFLAG;
1591541Srgrimes		tp->t_oflag = TTYDEF_OFLAG;
1601541Srgrimes		tp->t_lflag = TTYDEF_LFLAG;
1611541Srgrimes		tp->t_cflag = TTYDEF_CFLAG;
1621541Srgrimes		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
1631541Srgrimes		ttsetwater(tp);		/* would be done in xxparam() */
1641541Srgrimes	} else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
1651541Srgrimes		return (EBUSY);
1661541Srgrimes	if (tp->t_oproc)			/* Ctrlr still around. */
1679824Sbde		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
1681541Srgrimes	while ((tp->t_state & TS_CARR_ON) == 0) {
1691541Srgrimes		if (flag&FNONBLOCK)
1701541Srgrimes			break;
1719639Sbde		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
1729624Sbde				 "ptsopn", 0);
1733308Sphk		if (error)
1741541Srgrimes			return (error);
1751541Srgrimes	}
1761541Srgrimes	error = (*linesw[tp->t_line].l_open)(dev, tp);
1777724Sache	if (error == 0)
1787724Sache		ptcwakeup(tp, FREAD|FWRITE);
1791541Srgrimes	return (error);
1801541Srgrimes}
1811541Srgrimes
18212675Sjulianstatic	int
1831541Srgrimesptsclose(dev, flag, mode, p)
1841541Srgrimes	dev_t dev;
1851541Srgrimes	int flag, mode;
1861541Srgrimes	struct proc *p;
1871541Srgrimes{
1881541Srgrimes	register struct tty *tp;
1891541Srgrimes	int err;
1901541Srgrimes
1911541Srgrimes	tp = &pt_tty[minor(dev)];
1921541Srgrimes	err = (*linesw[tp->t_line].l_close)(tp, flag);
1937730Sache	ptsstop(tp, FREAD|FWRITE);
1947724Sache	(void) ttyclose(tp);
1951541Srgrimes	return (err);
1961541Srgrimes}
1971541Srgrimes
19812675Sjulianstatic	int
1991541Srgrimesptsread(dev, uio, flag)
2001541Srgrimes	dev_t dev;
2011541Srgrimes	struct uio *uio;
2021541Srgrimes	int flag;
2031541Srgrimes{
2041541Srgrimes	struct proc *p = curproc;
2051541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
2061541Srgrimes	register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
2071541Srgrimes	int error = 0;
2081541Srgrimes
2091541Srgrimesagain:
2101541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
2111541Srgrimes		while (isbackground(p, tp)) {
2121541Srgrimes			if ((p->p_sigignore & sigmask(SIGTTIN)) ||
2131541Srgrimes			    (p->p_sigmask & sigmask(SIGTTIN)) ||
2141541Srgrimes			    p->p_pgrp->pg_jobc == 0 ||
2151541Srgrimes			    p->p_flag & P_PPWAIT)
2161541Srgrimes				return (EIO);
2171541Srgrimes			pgsignal(p->p_pgrp, SIGTTIN, 1);
2189624Sbde			error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
2199624Sbde					 0);
2203308Sphk			if (error)
2211541Srgrimes				return (error);
2221541Srgrimes		}
2231541Srgrimes		if (tp->t_canq.c_cc == 0) {
2241541Srgrimes			if (flag & IO_NDELAY)
2251541Srgrimes				return (EWOULDBLOCK);
2269639Sbde			error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
2279624Sbde					 "ptsin", 0);
2283308Sphk			if (error)
2291541Srgrimes				return (error);
2301541Srgrimes			goto again;
2311541Srgrimes		}
2321541Srgrimes		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
2331541Srgrimes			if (ureadc(getc(&tp->t_canq), uio) < 0) {
2341541Srgrimes				error = EFAULT;
2351541Srgrimes				break;
2361541Srgrimes			}
2371541Srgrimes		if (tp->t_canq.c_cc == 1)
2381541Srgrimes			(void) getc(&tp->t_canq);
2391541Srgrimes		if (tp->t_canq.c_cc)
2401541Srgrimes			return (error);
2411541Srgrimes	} else
2421541Srgrimes		if (tp->t_oproc)
2431541Srgrimes			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
2441541Srgrimes	ptcwakeup(tp, FWRITE);
2451541Srgrimes	return (error);
2461541Srgrimes}
2471541Srgrimes
2481541Srgrimes/*
2491541Srgrimes * Write to pseudo-tty.
2501541Srgrimes * Wakeups of controlling tty will happen
2511541Srgrimes * indirectly, when tty driver calls ptsstart.
2521541Srgrimes */
25312675Sjulianstatic	int
2541541Srgrimesptswrite(dev, uio, flag)
2551541Srgrimes	dev_t dev;
2561541Srgrimes	struct uio *uio;
2571541Srgrimes	int flag;
2581541Srgrimes{
2591541Srgrimes	register struct tty *tp;
2601541Srgrimes
2611541Srgrimes	tp = &pt_tty[minor(dev)];
2621541Srgrimes	if (tp->t_oproc == 0)
2631541Srgrimes		return (EIO);
2641541Srgrimes	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
2651541Srgrimes}
2661541Srgrimes
2671541Srgrimes/*
2681541Srgrimes * Start output on pseudo-tty.
2691541Srgrimes * Wake up process selecting or sleeping for input from controlling tty.
2701541Srgrimes */
27112819Sphkstatic void
2721541Srgrimesptsstart(tp)
2731541Srgrimes	struct tty *tp;
2741541Srgrimes{
2751541Srgrimes	register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
2761541Srgrimes
2771541Srgrimes	if (tp->t_state & TS_TTSTOP)
2781541Srgrimes		return;
2791541Srgrimes	if (pti->pt_flags & PF_STOPPED) {
2801541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
2811541Srgrimes		pti->pt_send = TIOCPKT_START;
2821541Srgrimes	}
2831541Srgrimes	ptcwakeup(tp, FREAD);
2841541Srgrimes}
2851541Srgrimes
28612819Sphkstatic void
2871541Srgrimesptcwakeup(tp, flag)
2881541Srgrimes	struct tty *tp;
2891541Srgrimes	int flag;
2901541Srgrimes{
2911541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
2921541Srgrimes
2931541Srgrimes	if (flag & FREAD) {
2941541Srgrimes		selwakeup(&pti->pt_selr);
2959639Sbde		wakeup(TSA_PTC_READ(tp));
2961541Srgrimes	}
2971541Srgrimes	if (flag & FWRITE) {
2981541Srgrimes		selwakeup(&pti->pt_selw);
2999639Sbde		wakeup(TSA_PTC_WRITE(tp));
3001541Srgrimes	}
3011541Srgrimes}
3021541Srgrimes
30312675Sjulianstatic	int
3041541Srgrimesptcopen(dev, flag, devtype, p)
3051541Srgrimes	dev_t dev;
3061541Srgrimes	int flag, devtype;
3071541Srgrimes	struct proc *p;
3081541Srgrimes{
3091541Srgrimes	register struct tty *tp;
3101541Srgrimes	struct pt_ioctl *pti;
3111541Srgrimes
3121541Srgrimes	if (minor(dev) >= npty)
3131541Srgrimes		return (ENXIO);
3141541Srgrimes	tp = &pt_tty[minor(dev)];
3151541Srgrimes	if (tp->t_oproc)
3161541Srgrimes		return (EIO);
3171541Srgrimes	tp->t_oproc = ptsstart;
3181541Srgrimes#ifdef sun4c
3191541Srgrimes	tp->t_stop = ptsstop;
3201541Srgrimes#endif
3211541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
3221541Srgrimes	tp->t_lflag &= ~EXTPROC;
3231541Srgrimes	pti = &pt_ioctl[minor(dev)];
3241541Srgrimes	pti->pt_flags = 0;
3251541Srgrimes	pti->pt_send = 0;
3261541Srgrimes	pti->pt_ucntl = 0;
3271541Srgrimes	return (0);
3281541Srgrimes}
3291541Srgrimes
33012675Sjulianstatic	int
33110624Sbdeptcclose(dev, flags, fmt, p)
3321541Srgrimes	dev_t dev;
33310624Sbde	int flags;
33410624Sbde	int fmt;
33510624Sbde	struct proc *p;
3361541Srgrimes{
3371541Srgrimes	register struct tty *tp;
3381541Srgrimes
3391541Srgrimes	tp = &pt_tty[minor(dev)];
3401541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
3419824Sbde
3429824Sbde	/*
3439824Sbde	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
3449824Sbde	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
3459824Sbde	 * l_modem()s that ignore carrier drop make no sense for ptys but
3469824Sbde	 * may be in use because other parts of the line discipline make
3479824Sbde	 * sense for ptys.  Recover by doing everything that a normal
3489824Sbde	 * ttymodem() would have done except for sending a SIGHUP.
3499824Sbde	 */
3509850Sbde	if (tp->t_state & TS_ISOPEN) {
3519850Sbde		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
3529850Sbde		tp->t_state |= TS_ZOMBIE;
3539850Sbde		ttyflush(tp, FREAD | FWRITE);
3549850Sbde	}
3559824Sbde
3561541Srgrimes	tp->t_oproc = 0;		/* mark closed */
3571541Srgrimes	return (0);
3581541Srgrimes}
3591541Srgrimes
36012675Sjulianstatic	int
3611541Srgrimesptcread(dev, uio, flag)
3621541Srgrimes	dev_t dev;
3631541Srgrimes	struct uio *uio;
3641541Srgrimes	int flag;
3651541Srgrimes{
3661541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
3671541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
3681541Srgrimes	char buf[BUFSIZ];
3691541Srgrimes	int error = 0, cc;
3701541Srgrimes
3711541Srgrimes	/*
3721541Srgrimes	 * We want to block until the slave
3731541Srgrimes	 * is open, and there's something to read;
3741541Srgrimes	 * but if we lost the slave or we're NBIO,
3751541Srgrimes	 * then return the appropriate error instead.
3761541Srgrimes	 */
3771541Srgrimes	for (;;) {
3781541Srgrimes		if (tp->t_state&TS_ISOPEN) {
3791541Srgrimes			if (pti->pt_flags&PF_PKT && pti->pt_send) {
3801541Srgrimes				error = ureadc((int)pti->pt_send, uio);
3811541Srgrimes				if (error)
3821541Srgrimes					return (error);
3831541Srgrimes				if (pti->pt_send & TIOCPKT_IOCTL) {
3841541Srgrimes					cc = min(uio->uio_resid,
3851541Srgrimes						sizeof(tp->t_termios));
3862807Sbde					uiomove((caddr_t)&tp->t_termios, cc,
3872807Sbde						uio);
3881541Srgrimes				}
3891541Srgrimes				pti->pt_send = 0;
3901541Srgrimes				return (0);
3911541Srgrimes			}
3921541Srgrimes			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
3931541Srgrimes				error = ureadc((int)pti->pt_ucntl, uio);
3941541Srgrimes				if (error)
3951541Srgrimes					return (error);
3961541Srgrimes				pti->pt_ucntl = 0;
3971541Srgrimes				return (0);
3981541Srgrimes			}
3991541Srgrimes			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
4001541Srgrimes				break;
4011541Srgrimes		}
4029824Sbde		if ((tp->t_state & TS_CONNECTED) == 0)
4031541Srgrimes			return (0);	/* EOF */
4041541Srgrimes		if (flag & IO_NDELAY)
4051541Srgrimes			return (EWOULDBLOCK);
4069639Sbde		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
4073308Sphk		if (error)
4081541Srgrimes			return (error);
4091541Srgrimes	}
4101541Srgrimes	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
4111541Srgrimes		error = ureadc(0, uio);
4121541Srgrimes	while (uio->uio_resid > 0 && error == 0) {
4131541Srgrimes		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
4141541Srgrimes		if (cc <= 0)
4151541Srgrimes			break;
4161541Srgrimes		error = uiomove(buf, cc, uio);
4171541Srgrimes	}
4189626Sbde	ttwwakeup(tp);
4191541Srgrimes	return (error);
4201541Srgrimes}
4211541Srgrimes
42212675Sjulianstatic	void
4231541Srgrimesptsstop(tp, flush)
4241541Srgrimes	register struct tty *tp;
4251541Srgrimes	int flush;
4261541Srgrimes{
4271541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
4281541Srgrimes	int flag;
4291541Srgrimes
4301541Srgrimes	/* note: FLUSHREAD and FLUSHWRITE already ok */
4311541Srgrimes	if (flush == 0) {
4321541Srgrimes		flush = TIOCPKT_STOP;
4331541Srgrimes		pti->pt_flags |= PF_STOPPED;
4341541Srgrimes	} else
4351541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
4361541Srgrimes	pti->pt_send |= flush;
4371541Srgrimes	/* change of perspective */
4381541Srgrimes	flag = 0;
4391541Srgrimes	if (flush & FREAD)
4401541Srgrimes		flag |= FWRITE;
4411541Srgrimes	if (flush & FWRITE)
4421541Srgrimes		flag |= FREAD;
4431541Srgrimes	ptcwakeup(tp, flag);
4441541Srgrimes}
4451541Srgrimes
44612675Sjulianstatic	int
4471541Srgrimesptcselect(dev, rw, p)
4481541Srgrimes	dev_t dev;
4491541Srgrimes	int rw;
4501541Srgrimes	struct proc *p;
4511541Srgrimes{
4521541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
4531541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
4541541Srgrimes	int s;
4551541Srgrimes
4569824Sbde	if ((tp->t_state & TS_CONNECTED) == 0)
4571541Srgrimes		return (1);
4581541Srgrimes	switch (rw) {
4591541Srgrimes
4601541Srgrimes	case FREAD:
4611541Srgrimes		/*
4621541Srgrimes		 * Need to block timeouts (ttrstart).
4631541Srgrimes		 */
4641541Srgrimes		s = spltty();
4651541Srgrimes		if ((tp->t_state&TS_ISOPEN) &&
4661541Srgrimes		     tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
4671541Srgrimes			splx(s);
4681541Srgrimes			return (1);
4691541Srgrimes		}
4701541Srgrimes		splx(s);
4711541Srgrimes		/* FALLTHROUGH */
4721541Srgrimes
4731541Srgrimes	case 0:					/* exceptional */
4741541Srgrimes		if ((tp->t_state&TS_ISOPEN) &&
4753308Sphk		    ((pti->pt_flags&PF_PKT && pti->pt_send) ||
4763308Sphk		     (pti->pt_flags&PF_UCNTL && pti->pt_ucntl)))
4771541Srgrimes			return (1);
4781541Srgrimes		selrecord(p, &pti->pt_selr);
4791541Srgrimes		break;
4801541Srgrimes
4811541Srgrimes
4821541Srgrimes	case FWRITE:
4831541Srgrimes		if (tp->t_state&TS_ISOPEN) {
4841541Srgrimes			if (pti->pt_flags & PF_REMOTE) {
4851541Srgrimes			    if (tp->t_canq.c_cc == 0)
4861541Srgrimes				return (1);
4871541Srgrimes			} else {
4881541Srgrimes			    if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2)
4891541Srgrimes				    return (1);
4901541Srgrimes			    if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON))
4911541Srgrimes				    return (1);
4921541Srgrimes			}
4931541Srgrimes		}
4941541Srgrimes		selrecord(p, &pti->pt_selw);
4951541Srgrimes		break;
4961541Srgrimes
4971541Srgrimes	}
4981541Srgrimes	return (0);
4991541Srgrimes}
5001541Srgrimes
50112675Sjulianstatic	int
5021541Srgrimesptcwrite(dev, uio, flag)
5031541Srgrimes	dev_t dev;
5041541Srgrimes	register struct uio *uio;
5051541Srgrimes	int flag;
5061541Srgrimes{
5071541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
5081549Srgrimes	register u_char *cp = 0;
5091541Srgrimes	register int cc = 0;
5101541Srgrimes	u_char locbuf[BUFSIZ];
5111541Srgrimes	int cnt = 0;
5121541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
5131541Srgrimes	int error = 0;
5141541Srgrimes
5151541Srgrimesagain:
5161541Srgrimes	if ((tp->t_state&TS_ISOPEN) == 0)
5171541Srgrimes		goto block;
5181541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
5191541Srgrimes		if (tp->t_canq.c_cc)
5201541Srgrimes			goto block;
52111789Sbde		while ((uio->uio_resid > 0 || cc > 0) &&
52211789Sbde		       tp->t_canq.c_cc < TTYHOG - 1) {
5231541Srgrimes			if (cc == 0) {
5241541Srgrimes				cc = min(uio->uio_resid, BUFSIZ);
5251541Srgrimes				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
5261541Srgrimes				cp = locbuf;
5271541Srgrimes				error = uiomove((caddr_t)cp, cc, uio);
5281541Srgrimes				if (error)
5291541Srgrimes					return (error);
5301541Srgrimes				/* check again for safety */
53111789Sbde				if ((tp->t_state & TS_ISOPEN) == 0) {
53211789Sbde					/* adjust as usual */
53311789Sbde					uio->uio_resid += cc;
5341541Srgrimes					return (EIO);
53511789Sbde				}
5361541Srgrimes			}
5371541Srgrimes			if (cc)
53811789Sbde				cc -= b_to_q((char *)cp, cc, &tp->t_canq);
5391541Srgrimes		}
54011789Sbde		/* adjust for data copied in but not written */
54111789Sbde		uio->uio_resid += cc;
5421541Srgrimes		(void) putc(0, &tp->t_canq);
5431541Srgrimes		ttwakeup(tp);
5449639Sbde		wakeup(TSA_PTS_READ(tp));
5451541Srgrimes		return (0);
5461541Srgrimes	}
54711789Sbde	while (uio->uio_resid > 0 || cc > 0) {
5481541Srgrimes		if (cc == 0) {
5491541Srgrimes			cc = min(uio->uio_resid, BUFSIZ);
5501541Srgrimes			cp = locbuf;
5511541Srgrimes			error = uiomove((caddr_t)cp, cc, uio);
5521541Srgrimes			if (error)
5531541Srgrimes				return (error);
5541541Srgrimes			/* check again for safety */
55511789Sbde			if ((tp->t_state & TS_ISOPEN) == 0) {
55611789Sbde				/* adjust for data copied in but not written */
55711789Sbde				uio->uio_resid += cc;
5581541Srgrimes				return (EIO);
55911789Sbde			}
5601541Srgrimes		}
5611541Srgrimes		while (cc > 0) {
5621541Srgrimes			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
5631541Srgrimes			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
5649824Sbde				wakeup(TSA_HUP_OR_INPUT(tp));
5651541Srgrimes				goto block;
5661541Srgrimes			}
5671541Srgrimes			(*linesw[tp->t_line].l_rint)(*cp++, tp);
5681541Srgrimes			cnt++;
5691541Srgrimes			cc--;
5701541Srgrimes		}
5711541Srgrimes		cc = 0;
5721541Srgrimes	}
5731541Srgrimes	return (0);
5741541Srgrimesblock:
5751541Srgrimes	/*
5761541Srgrimes	 * Come here to wait for slave to open, for space
5771541Srgrimes	 * in outq, or space in rawq.
5781541Srgrimes	 */
57911789Sbde	if ((tp->t_state & TS_CONNECTED) == 0) {
58011789Sbde		/* adjust for data copied in but not written */
58111789Sbde		uio->uio_resid += cc;
5821541Srgrimes		return (EIO);
58311789Sbde	}
5841541Srgrimes	if (flag & IO_NDELAY) {
5851541Srgrimes		/* adjust for data copied in but not written */
5861541Srgrimes		uio->uio_resid += cc;
5871541Srgrimes		if (cnt == 0)
5881541Srgrimes			return (EWOULDBLOCK);
5891541Srgrimes		return (0);
5901541Srgrimes	}
5919639Sbde	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
5923308Sphk	if (error) {
5931541Srgrimes		/* adjust for data copied in but not written */
5941541Srgrimes		uio->uio_resid += cc;
5951541Srgrimes		return (error);
5961541Srgrimes	}
5971541Srgrimes	goto again;
5981541Srgrimes}
5991541Srgrimes
60012675Sjulianstatic	struct tty *
6016712Spstptydevtotty(dev)
6026712Spst	dev_t		dev;
6036712Spst{
6046712Spst	if (minor(dev) >= npty)
6056712Spst		return (NULL);
6066712Spst
6076712Spst	return &pt_tty[minor(dev)];
6086712Spst}
6096712Spst
6101541Srgrimes/*ARGSUSED*/
61112675Sjulianstatic	int
6121541Srgrimesptyioctl(dev, cmd, data, flag, p)
6131541Srgrimes	dev_t dev;
6141541Srgrimes	int cmd;
6151541Srgrimes	caddr_t data;
6161541Srgrimes	int flag;
6171541Srgrimes	struct proc *p;
6181541Srgrimes{
6191541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
6201541Srgrimes	register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
6211541Srgrimes	register u_char *cc = tp->t_cc;
6221541Srgrimes	int stop, error;
6231541Srgrimes
6241541Srgrimes	/*
6251541Srgrimes	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
6261541Srgrimes	 * ttywflush(tp) will hang if there are characters in the outq.
6271541Srgrimes	 */
6281541Srgrimes	if (cmd == TIOCEXT) {
6291541Srgrimes		/*
6301541Srgrimes		 * When the EXTPROC bit is being toggled, we need
6311541Srgrimes		 * to send an TIOCPKT_IOCTL if the packet driver
6321541Srgrimes		 * is turned on.
6331541Srgrimes		 */
6341541Srgrimes		if (*(int *)data) {
6351541Srgrimes			if (pti->pt_flags & PF_PKT) {
6361541Srgrimes				pti->pt_send |= TIOCPKT_IOCTL;
6371541Srgrimes				ptcwakeup(tp, FREAD);
6381541Srgrimes			}
6391541Srgrimes			tp->t_lflag |= EXTPROC;
6401541Srgrimes		} else {
64114511Shsu			if ((tp->t_lflag & EXTPROC) &&
6421541Srgrimes			    (pti->pt_flags & PF_PKT)) {
6431541Srgrimes				pti->pt_send |= TIOCPKT_IOCTL;
6441541Srgrimes				ptcwakeup(tp, FREAD);
6451541Srgrimes			}
6461541Srgrimes			tp->t_lflag &= ~EXTPROC;
6471541Srgrimes		}
6481541Srgrimes		return(0);
6491541Srgrimes	} else
65012813Sjulian	if (cdevsw[major(dev)]->d_open == ptcopen)
6511541Srgrimes		switch (cmd) {
6521541Srgrimes
6531541Srgrimes		case TIOCGPGRP:
6541541Srgrimes			/*
6551541Srgrimes			 * We aviod calling ttioctl on the controller since,
6561541Srgrimes			 * in that case, tp must be the controlling terminal.
6571541Srgrimes			 */
6581541Srgrimes			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
6591541Srgrimes			return (0);
6601541Srgrimes
6611541Srgrimes		case TIOCPKT:
6621541Srgrimes			if (*(int *)data) {
6631541Srgrimes				if (pti->pt_flags & PF_UCNTL)
6641541Srgrimes					return (EINVAL);
6651541Srgrimes				pti->pt_flags |= PF_PKT;
6661541Srgrimes			} else
6671541Srgrimes				pti->pt_flags &= ~PF_PKT;
6681541Srgrimes			return (0);
6691541Srgrimes
6701541Srgrimes		case TIOCUCNTL:
6711541Srgrimes			if (*(int *)data) {
6721541Srgrimes				if (pti->pt_flags & PF_PKT)
6731541Srgrimes					return (EINVAL);
6741541Srgrimes				pti->pt_flags |= PF_UCNTL;
6751541Srgrimes			} else
6761541Srgrimes				pti->pt_flags &= ~PF_UCNTL;
6771541Srgrimes			return (0);
6781541Srgrimes
6791541Srgrimes		case TIOCREMOTE:
6801541Srgrimes			if (*(int *)data)
6811541Srgrimes				pti->pt_flags |= PF_REMOTE;
6821541Srgrimes			else
6831541Srgrimes				pti->pt_flags &= ~PF_REMOTE;
6841541Srgrimes			ttyflush(tp, FREAD|FWRITE);
6851541Srgrimes			return (0);
6861541Srgrimes
6871541Srgrimes#ifdef COMPAT_43
6888876Srgrimes		case TIOCSETP:
6891541Srgrimes		case TIOCSETN:
6901541Srgrimes#endif
6911541Srgrimes		case TIOCSETD:
6921541Srgrimes		case TIOCSETA:
6931541Srgrimes		case TIOCSETAW:
6949858Sache		case TIOCSETAF:
6951541Srgrimes			ndflush(&tp->t_outq, tp->t_outq.c_cc);
6961541Srgrimes			break;
6971541Srgrimes
6981541Srgrimes		case TIOCSIG:
6991541Srgrimes			if (*(unsigned int *)data >= NSIG)
7001541Srgrimes				return(EINVAL);
7011541Srgrimes			if ((tp->t_lflag&NOFLSH) == 0)
7021541Srgrimes				ttyflush(tp, FREAD|FWRITE);
7031541Srgrimes			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
7041541Srgrimes			if ((*(unsigned int *)data == SIGINFO) &&
7051541Srgrimes			    ((tp->t_lflag&NOKERNINFO) == 0))
7061541Srgrimes				ttyinfo(tp);
7071541Srgrimes			return(0);
7081541Srgrimes		}
7091541Srgrimes	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
7101541Srgrimes	if (error < 0)
7111541Srgrimes		 error = ttioctl(tp, cmd, data, flag);
7121541Srgrimes	if (error < 0) {
7131541Srgrimes		if (pti->pt_flags & PF_UCNTL &&
7141541Srgrimes		    (cmd & ~0xff) == UIOCCMD(0)) {
7151541Srgrimes			if (cmd & 0xff) {
7161541Srgrimes				pti->pt_ucntl = (u_char)cmd;
7171541Srgrimes				ptcwakeup(tp, FREAD);
7181541Srgrimes			}
7191541Srgrimes			return (0);
7201541Srgrimes		}
7211541Srgrimes		error = ENOTTY;
7221541Srgrimes	}
7231541Srgrimes	/*
7241541Srgrimes	 * If external processing and packet mode send ioctl packet.
7251541Srgrimes	 */
7261541Srgrimes	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
7271541Srgrimes		switch(cmd) {
7281541Srgrimes		case TIOCSETA:
7291541Srgrimes		case TIOCSETAW:
7301541Srgrimes		case TIOCSETAF:
7311541Srgrimes#ifdef COMPAT_43
7321541Srgrimes		case TIOCSETP:
7331541Srgrimes		case TIOCSETN:
7341541Srgrimes#endif
7351541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
7361541Srgrimes		case TIOCSETC:
7371541Srgrimes		case TIOCSLTC:
7381541Srgrimes		case TIOCLBIS:
7391541Srgrimes		case TIOCLBIC:
7401541Srgrimes		case TIOCLSET:
7411541Srgrimes#endif
7421541Srgrimes			pti->pt_send |= TIOCPKT_IOCTL;
7431541Srgrimes			ptcwakeup(tp, FREAD);
7441541Srgrimes		default:
7451541Srgrimes			break;
7461541Srgrimes		}
7471541Srgrimes	}
7488876Srgrimes	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
7491541Srgrimes		&& CCEQ(cc[VSTART], CTRL('q'));
7501541Srgrimes	if (pti->pt_flags & PF_NOSTOP) {
7511541Srgrimes		if (stop) {
7521541Srgrimes			pti->pt_send &= ~TIOCPKT_NOSTOP;
7531541Srgrimes			pti->pt_send |= TIOCPKT_DOSTOP;
7541541Srgrimes			pti->pt_flags &= ~PF_NOSTOP;
7551541Srgrimes			ptcwakeup(tp, FREAD);
7561541Srgrimes		}
7571541Srgrimes	} else {
7581541Srgrimes		if (!stop) {
7591541Srgrimes			pti->pt_send &= ~TIOCPKT_DOSTOP;
7601541Srgrimes			pti->pt_send |= TIOCPKT_NOSTOP;
7611541Srgrimes			pti->pt_flags |= PF_NOSTOP;
7621541Srgrimes			ptcwakeup(tp, FREAD);
7631541Srgrimes		}
7641541Srgrimes	}
7651541Srgrimes	return (error);
7661541Srgrimes}
76712517Sjulian
76812517Sjulianstatic ptc_devsw_installed = 0;
76912675Sjulian#ifdef DEVFS
77012675Sjulian#define MAXUNITS (8 * 32)
77112675Sjulianstatic	void	*devfs_token_pts[MAXUNITS];
77212675Sjulianstatic	void	*devfs_token_ptc[MAXUNITS];
77312819Sphkstatic  const	char jnames[] = "pqrsPQRS";
77412675Sjulian#endif
77512517Sjulian
77612675Sjulianstatic void
77712675Sjulianptc_drvinit(void *unused)
77812517Sjulian{
77912517Sjulian#ifdef DEVFS
78012567Sjulian	int i,j,k;
78112675Sjulian	char name[16];
78212517Sjulian#endif
78312517Sjulian	dev_t dev;
78412517Sjulian
78512517Sjulian	if( ! ptc_devsw_installed ) {
78612675Sjulian		dev = makedev(CDEV_MAJOR_S, 0);
78712675Sjulian		cdevsw_add(&dev, &pts_cdevsw, NULL);
78812675Sjulian		dev = makedev(CDEV_MAJOR_C, 0);
78912675Sjulian		cdevsw_add(&dev, &ptc_cdevsw, NULL);
79012517Sjulian		ptc_devsw_installed = 1;
79112517Sjulian#ifdef DEVFS
79212517Sjulian/*XXX*/
79312517Sjulian#if NPTY > MAXUNITS
79412517Sjulian#undef NPTY
79512517Sjulian#define NPTY MAXUNITS
79612517Sjulian#endif
79712521Sjulian		for ( i = 0 ; i<NPTY ; i++ ) {
79812675Sjulian			void *x;
79912517Sjulian
80012564Sjulian			j = i / 32;
80112564Sjulian			k = i % 32;
80212675Sjulian			devfs_token_pts[i] =
80313630Sphk				devfs_add_devswf(&pts_cdevsw,i,
80413630Sphk						DV_CHR,0,0,0666,
80513639Sphk						"tty%c%n",jnames[j],k);
80612675Sjulian			devfs_token_ptc[i] =
80713630Sphk				devfs_add_devswf(&ptc_cdevsw,i,
80813630Sphk						DV_CHR,0,0,0666,
80913639Sphk						"pty%c%n",jnames[j],k);
81012521Sjulian		}
81112521Sjulian#endif
81212517Sjulian    	}
81312517Sjulian}
81412517Sjulian
81512517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
81612517Sjulian
81712517Sjulian
818