pty.c revision 27770
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
3427770Sjmg * $Id: tty_pty.c,v 1.43 1997/03/24 12:03:06 bde 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>
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>
521541Srgrimes#include <sys/uio.h>
531541Srgrimes#include <sys/kernel.h>
541541Srgrimes#include <sys/vnode.h>
553308Sphk#include <sys/signalvar.h>
561541Srgrimes
5712517Sjulian#ifdef DEVFS
5812517Sjulian#include <sys/devfsext.h>
5912517Sjulian#endif /*DEVFS*/
6012517Sjulian
6116322Sgpalmer#ifdef notyet
6212819Sphkstatic void ptyattach __P((int n));
6316322Sgpalmer#endif
6412819Sphkstatic void ptsstart __P((struct tty *tp));
6512819Sphkstatic void ptcwakeup __P((struct tty *tp, int flag));
6611789Sbde
6712675Sjulianstatic	d_open_t	ptsopen;
6812675Sjulianstatic	d_close_t	ptsclose;
6912675Sjulianstatic	d_read_t	ptsread;
7012675Sjulianstatic	d_write_t	ptswrite;
7112675Sjulianstatic	d_ioctl_t	ptyioctl;
7212675Sjulianstatic	d_stop_t	ptsstop;
7312731Sbdestatic	d_devtotty_t	ptydevtotty;
7412675Sjulianstatic	d_open_t	ptcopen;
7512675Sjulianstatic	d_close_t	ptcclose;
7612675Sjulianstatic	d_read_t	ptcread;
7712675Sjulianstatic	d_write_t	ptcwrite;
7812675Sjulianstatic	d_select_t	ptcselect;
7912675Sjulian
8012675Sjulian#define CDEV_MAJOR_S 5
8112675Sjulian#define CDEV_MAJOR_C 6
8212678Sphkstatic struct cdevsw pts_cdevsw =
8312675Sjulian	{ ptsopen,	ptsclose,	ptsread,	ptswrite,	/*5*/
8412675Sjulian	  ptyioctl,	ptsstop,	nullreset,	ptydevtotty,/* ttyp */
8512675Sjulian	  ttselect,	nommap,		NULL,	"pts",	NULL,	-1 };
8612675Sjulian
8712678Sphkstatic struct cdevsw ptc_cdevsw =
8812675Sjulian	{ ptcopen,	ptcclose,	ptcread,	ptcwrite,	/*6*/
8912675Sjulian	  ptyioctl,	nullstop,	nullreset,	ptydevtotty,/* ptyp */
9012675Sjulian	  ptcselect,	nommap,		NULL,	"ptc",	NULL,	-1 };
9112675Sjulian
9212675Sjulian
931541Srgrimes#if NPTY == 1
941541Srgrimes#undef NPTY
951541Srgrimes#define	NPTY	32		/* crude XXX */
9627770Sjmg#warning	You have only one pty defined, redefining to 32.
971541Srgrimes#endif
981541Srgrimes
9927770Sjmg#ifdef DEVFS
10027770Sjmg#define MAXUNITS (8 * 32)
10127770Sjmgstatic	void	*devfs_token_pts[MAXUNITS];
10227770Sjmgstatic	void	*devfs_token_ptc[MAXUNITS];
10327770Sjmgstatic  const	char jnames[] = "pqrsPQRS";
10427770Sjmg#if NPTY > MAXUNITS
10527770Sjmg#undef NPTY
10627770Sjmg#define NPTY MAXUNITS
10727770Sjmg#warning	Can't have more than 256 pty's with DEVFS defined.
10827770Sjmg#endif
10927770Sjmg#endif
11027770Sjmg
1111541Srgrimes#define BUFSIZ 100		/* Chunk size iomoved to/from user */
1121541Srgrimes
1131541Srgrimes/*
11412564Sjulian * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
11512564Sjulian * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
1161541Srgrimes */
11712819Sphkstatic struct	tty pt_tty[NPTY];	/* XXX */
11812819Sphkstatic struct	pt_ioctl {
1191541Srgrimes	int	pt_flags;
1201541Srgrimes	struct	selinfo pt_selr, pt_selw;
1211541Srgrimes	u_char	pt_send;
1221541Srgrimes	u_char	pt_ucntl;
1231541Srgrimes} pt_ioctl[NPTY];		/* XXX */
12412819Sphkstatic int	npty = NPTY;		/* for pstat -t */
1251541Srgrimes
1261541Srgrimes#define	PF_PKT		0x08		/* packet mode */
1271541Srgrimes#define	PF_STOPPED	0x10		/* user told stopped */
1281541Srgrimes#define	PF_REMOTE	0x20		/* remote and flow controlled input */
1291541Srgrimes#define	PF_NOSTOP	0x40
1301541Srgrimes#define PF_UCNTL	0x80		/* user control mode */
1311541Srgrimes
13216322Sgpalmer#ifdef notyet
1331541Srgrimes/*
1341541Srgrimes * Establish n (or default if n is 1) ptys in the system.
1351541Srgrimes *
1361541Srgrimes * XXX cdevsw & pstat require the array `pty[]' to be an array
1371541Srgrimes */
13812819Sphkstatic void
1391541Srgrimesptyattach(n)
1401541Srgrimes	int n;
1411541Srgrimes{
1421541Srgrimes	char *mem;
1431541Srgrimes	register u_long ntb;
1441541Srgrimes#define	DEFAULT_NPTY	32
1451541Srgrimes
1461541Srgrimes	/* maybe should allow 0 => none? */
1471541Srgrimes	if (n <= 1)
1481541Srgrimes		n = DEFAULT_NPTY;
1491541Srgrimes	ntb = n * sizeof(struct tty);
1501541Srgrimes	mem = malloc(ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl),
1511541Srgrimes	    M_DEVBUF, M_WAITOK);
1521541Srgrimes	pt_tty = (struct tty *)mem;
1531541Srgrimes	mem = (char *)ALIGN(mem + ntb);
1541541Srgrimes	pt_ioctl = (struct pt_ioctl *)mem;
1551541Srgrimes	npty = n;
15616322Sgpalmer}
1571541Srgrimes#endif
1581541Srgrimes
1591541Srgrimes/*ARGSUSED*/
16012675Sjulianstatic	int
1611541Srgrimesptsopen(dev, flag, devtype, p)
1621541Srgrimes	dev_t dev;
1631541Srgrimes	int flag, devtype;
1641541Srgrimes	struct proc *p;
1651541Srgrimes{
1661541Srgrimes	register struct tty *tp;
1671541Srgrimes	int error;
1681541Srgrimes
1691541Srgrimes	if (minor(dev) >= npty)
1701541Srgrimes		return (ENXIO);
1711541Srgrimes	tp = &pt_tty[minor(dev)];
1721541Srgrimes	if ((tp->t_state & TS_ISOPEN) == 0) {
1731541Srgrimes		ttychars(tp);		/* Set up default chars */
1741541Srgrimes		tp->t_iflag = TTYDEF_IFLAG;
1751541Srgrimes		tp->t_oflag = TTYDEF_OFLAG;
1761541Srgrimes		tp->t_lflag = TTYDEF_LFLAG;
1771541Srgrimes		tp->t_cflag = TTYDEF_CFLAG;
1781541Srgrimes		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
1791541Srgrimes		ttsetwater(tp);		/* would be done in xxparam() */
1801541Srgrimes	} else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
1811541Srgrimes		return (EBUSY);
1821541Srgrimes	if (tp->t_oproc)			/* Ctrlr still around. */
1839824Sbde		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
1841541Srgrimes	while ((tp->t_state & TS_CARR_ON) == 0) {
1851541Srgrimes		if (flag&FNONBLOCK)
1861541Srgrimes			break;
1879639Sbde		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
1889624Sbde				 "ptsopn", 0);
1893308Sphk		if (error)
1901541Srgrimes			return (error);
1911541Srgrimes	}
1921541Srgrimes	error = (*linesw[tp->t_line].l_open)(dev, tp);
1937724Sache	if (error == 0)
1947724Sache		ptcwakeup(tp, FREAD|FWRITE);
1951541Srgrimes	return (error);
1961541Srgrimes}
1971541Srgrimes
19812675Sjulianstatic	int
1991541Srgrimesptsclose(dev, flag, mode, p)
2001541Srgrimes	dev_t dev;
2011541Srgrimes	int flag, mode;
2021541Srgrimes	struct proc *p;
2031541Srgrimes{
2041541Srgrimes	register struct tty *tp;
2051541Srgrimes	int err;
2061541Srgrimes
2071541Srgrimes	tp = &pt_tty[minor(dev)];
2081541Srgrimes	err = (*linesw[tp->t_line].l_close)(tp, flag);
2097730Sache	ptsstop(tp, FREAD|FWRITE);
2107724Sache	(void) ttyclose(tp);
2111541Srgrimes	return (err);
2121541Srgrimes}
2131541Srgrimes
21412675Sjulianstatic	int
2151541Srgrimesptsread(dev, uio, flag)
2161541Srgrimes	dev_t dev;
2171541Srgrimes	struct uio *uio;
2181541Srgrimes	int flag;
2191541Srgrimes{
2201541Srgrimes	struct proc *p = curproc;
2211541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
2221541Srgrimes	register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
2231541Srgrimes	int error = 0;
2241541Srgrimes
2251541Srgrimesagain:
2261541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
2271541Srgrimes		while (isbackground(p, tp)) {
2281541Srgrimes			if ((p->p_sigignore & sigmask(SIGTTIN)) ||
2291541Srgrimes			    (p->p_sigmask & sigmask(SIGTTIN)) ||
2301541Srgrimes			    p->p_pgrp->pg_jobc == 0 ||
2311541Srgrimes			    p->p_flag & P_PPWAIT)
2321541Srgrimes				return (EIO);
2331541Srgrimes			pgsignal(p->p_pgrp, SIGTTIN, 1);
2349624Sbde			error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
2359624Sbde					 0);
2363308Sphk			if (error)
2371541Srgrimes				return (error);
2381541Srgrimes		}
2391541Srgrimes		if (tp->t_canq.c_cc == 0) {
2401541Srgrimes			if (flag & IO_NDELAY)
2411541Srgrimes				return (EWOULDBLOCK);
2429639Sbde			error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
2439624Sbde					 "ptsin", 0);
2443308Sphk			if (error)
2451541Srgrimes				return (error);
2461541Srgrimes			goto again;
2471541Srgrimes		}
2481541Srgrimes		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
2491541Srgrimes			if (ureadc(getc(&tp->t_canq), uio) < 0) {
2501541Srgrimes				error = EFAULT;
2511541Srgrimes				break;
2521541Srgrimes			}
2531541Srgrimes		if (tp->t_canq.c_cc == 1)
2541541Srgrimes			(void) getc(&tp->t_canq);
2551541Srgrimes		if (tp->t_canq.c_cc)
2561541Srgrimes			return (error);
2571541Srgrimes	} else
2581541Srgrimes		if (tp->t_oproc)
2591541Srgrimes			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
2601541Srgrimes	ptcwakeup(tp, FWRITE);
2611541Srgrimes	return (error);
2621541Srgrimes}
2631541Srgrimes
2641541Srgrimes/*
2651541Srgrimes * Write to pseudo-tty.
2661541Srgrimes * Wakeups of controlling tty will happen
2671541Srgrimes * indirectly, when tty driver calls ptsstart.
2681541Srgrimes */
26912675Sjulianstatic	int
2701541Srgrimesptswrite(dev, uio, flag)
2711541Srgrimes	dev_t dev;
2721541Srgrimes	struct uio *uio;
2731541Srgrimes	int flag;
2741541Srgrimes{
2751541Srgrimes	register struct tty *tp;
2761541Srgrimes
2771541Srgrimes	tp = &pt_tty[minor(dev)];
2781541Srgrimes	if (tp->t_oproc == 0)
2791541Srgrimes		return (EIO);
2801541Srgrimes	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
2811541Srgrimes}
2821541Srgrimes
2831541Srgrimes/*
2841541Srgrimes * Start output on pseudo-tty.
2851541Srgrimes * Wake up process selecting or sleeping for input from controlling tty.
2861541Srgrimes */
28712819Sphkstatic void
2881541Srgrimesptsstart(tp)
2891541Srgrimes	struct tty *tp;
2901541Srgrimes{
2911541Srgrimes	register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
2921541Srgrimes
2931541Srgrimes	if (tp->t_state & TS_TTSTOP)
2941541Srgrimes		return;
2951541Srgrimes	if (pti->pt_flags & PF_STOPPED) {
2961541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
2971541Srgrimes		pti->pt_send = TIOCPKT_START;
2981541Srgrimes	}
2991541Srgrimes	ptcwakeup(tp, FREAD);
3001541Srgrimes}
3011541Srgrimes
30212819Sphkstatic void
3031541Srgrimesptcwakeup(tp, flag)
3041541Srgrimes	struct tty *tp;
3051541Srgrimes	int flag;
3061541Srgrimes{
3071541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
3081541Srgrimes
3091541Srgrimes	if (flag & FREAD) {
3101541Srgrimes		selwakeup(&pti->pt_selr);
3119639Sbde		wakeup(TSA_PTC_READ(tp));
3121541Srgrimes	}
3131541Srgrimes	if (flag & FWRITE) {
3141541Srgrimes		selwakeup(&pti->pt_selw);
3159639Sbde		wakeup(TSA_PTC_WRITE(tp));
3161541Srgrimes	}
3171541Srgrimes}
3181541Srgrimes
31912675Sjulianstatic	int
3201541Srgrimesptcopen(dev, flag, devtype, p)
3211541Srgrimes	dev_t dev;
3221541Srgrimes	int flag, devtype;
3231541Srgrimes	struct proc *p;
3241541Srgrimes{
3251541Srgrimes	register struct tty *tp;
3261541Srgrimes	struct pt_ioctl *pti;
3271541Srgrimes
3281541Srgrimes	if (minor(dev) >= npty)
3291541Srgrimes		return (ENXIO);
3301541Srgrimes	tp = &pt_tty[minor(dev)];
3311541Srgrimes	if (tp->t_oproc)
3321541Srgrimes		return (EIO);
3331541Srgrimes	tp->t_oproc = ptsstart;
3341541Srgrimes#ifdef sun4c
3351541Srgrimes	tp->t_stop = ptsstop;
3361541Srgrimes#endif
3371541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
3381541Srgrimes	tp->t_lflag &= ~EXTPROC;
3391541Srgrimes	pti = &pt_ioctl[minor(dev)];
3401541Srgrimes	pti->pt_flags = 0;
3411541Srgrimes	pti->pt_send = 0;
3421541Srgrimes	pti->pt_ucntl = 0;
3431541Srgrimes	return (0);
3441541Srgrimes}
3451541Srgrimes
34612675Sjulianstatic	int
34710624Sbdeptcclose(dev, flags, fmt, p)
3481541Srgrimes	dev_t dev;
34910624Sbde	int flags;
35010624Sbde	int fmt;
35110624Sbde	struct proc *p;
3521541Srgrimes{
3531541Srgrimes	register struct tty *tp;
3541541Srgrimes
3551541Srgrimes	tp = &pt_tty[minor(dev)];
3561541Srgrimes	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
3579824Sbde
3589824Sbde	/*
3599824Sbde	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
3609824Sbde	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
3619824Sbde	 * l_modem()s that ignore carrier drop make no sense for ptys but
3629824Sbde	 * may be in use because other parts of the line discipline make
3639824Sbde	 * sense for ptys.  Recover by doing everything that a normal
3649824Sbde	 * ttymodem() would have done except for sending a SIGHUP.
3659824Sbde	 */
3669850Sbde	if (tp->t_state & TS_ISOPEN) {
3679850Sbde		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
3689850Sbde		tp->t_state |= TS_ZOMBIE;
3699850Sbde		ttyflush(tp, FREAD | FWRITE);
3709850Sbde	}
3719824Sbde
3721541Srgrimes	tp->t_oproc = 0;		/* mark closed */
3731541Srgrimes	return (0);
3741541Srgrimes}
3751541Srgrimes
37612675Sjulianstatic	int
3771541Srgrimesptcread(dev, uio, flag)
3781541Srgrimes	dev_t dev;
3791541Srgrimes	struct uio *uio;
3801541Srgrimes	int flag;
3811541Srgrimes{
3821541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
3831541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
3841541Srgrimes	char buf[BUFSIZ];
3851541Srgrimes	int error = 0, cc;
3861541Srgrimes
3871541Srgrimes	/*
3881541Srgrimes	 * We want to block until the slave
3891541Srgrimes	 * is open, and there's something to read;
3901541Srgrimes	 * but if we lost the slave or we're NBIO,
3911541Srgrimes	 * then return the appropriate error instead.
3921541Srgrimes	 */
3931541Srgrimes	for (;;) {
3941541Srgrimes		if (tp->t_state&TS_ISOPEN) {
3951541Srgrimes			if (pti->pt_flags&PF_PKT && pti->pt_send) {
3961541Srgrimes				error = ureadc((int)pti->pt_send, uio);
3971541Srgrimes				if (error)
3981541Srgrimes					return (error);
3991541Srgrimes				if (pti->pt_send & TIOCPKT_IOCTL) {
4001541Srgrimes					cc = min(uio->uio_resid,
4011541Srgrimes						sizeof(tp->t_termios));
4022807Sbde					uiomove((caddr_t)&tp->t_termios, cc,
4032807Sbde						uio);
4041541Srgrimes				}
4051541Srgrimes				pti->pt_send = 0;
4061541Srgrimes				return (0);
4071541Srgrimes			}
4081541Srgrimes			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
4091541Srgrimes				error = ureadc((int)pti->pt_ucntl, uio);
4101541Srgrimes				if (error)
4111541Srgrimes					return (error);
4121541Srgrimes				pti->pt_ucntl = 0;
4131541Srgrimes				return (0);
4141541Srgrimes			}
4151541Srgrimes			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
4161541Srgrimes				break;
4171541Srgrimes		}
4189824Sbde		if ((tp->t_state & TS_CONNECTED) == 0)
4191541Srgrimes			return (0);	/* EOF */
4201541Srgrimes		if (flag & IO_NDELAY)
4211541Srgrimes			return (EWOULDBLOCK);
4229639Sbde		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
4233308Sphk		if (error)
4241541Srgrimes			return (error);
4251541Srgrimes	}
4261541Srgrimes	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
4271541Srgrimes		error = ureadc(0, uio);
4281541Srgrimes	while (uio->uio_resid > 0 && error == 0) {
4291541Srgrimes		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
4301541Srgrimes		if (cc <= 0)
4311541Srgrimes			break;
4321541Srgrimes		error = uiomove(buf, cc, uio);
4331541Srgrimes	}
4349626Sbde	ttwwakeup(tp);
4351541Srgrimes	return (error);
4361541Srgrimes}
4371541Srgrimes
43812675Sjulianstatic	void
4391541Srgrimesptsstop(tp, flush)
4401541Srgrimes	register struct tty *tp;
4411541Srgrimes	int flush;
4421541Srgrimes{
4431541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
4441541Srgrimes	int flag;
4451541Srgrimes
4461541Srgrimes	/* note: FLUSHREAD and FLUSHWRITE already ok */
4471541Srgrimes	if (flush == 0) {
4481541Srgrimes		flush = TIOCPKT_STOP;
4491541Srgrimes		pti->pt_flags |= PF_STOPPED;
4501541Srgrimes	} else
4511541Srgrimes		pti->pt_flags &= ~PF_STOPPED;
4521541Srgrimes	pti->pt_send |= flush;
4531541Srgrimes	/* change of perspective */
4541541Srgrimes	flag = 0;
4551541Srgrimes	if (flush & FREAD)
4561541Srgrimes		flag |= FWRITE;
4571541Srgrimes	if (flush & FWRITE)
4581541Srgrimes		flag |= FREAD;
4591541Srgrimes	ptcwakeup(tp, flag);
4601541Srgrimes}
4611541Srgrimes
46212675Sjulianstatic	int
4631541Srgrimesptcselect(dev, rw, p)
4641541Srgrimes	dev_t dev;
4651541Srgrimes	int rw;
4661541Srgrimes	struct proc *p;
4671541Srgrimes{
4681541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
4691541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
4701541Srgrimes	int s;
4711541Srgrimes
4729824Sbde	if ((tp->t_state & TS_CONNECTED) == 0)
4731541Srgrimes		return (1);
4741541Srgrimes	switch (rw) {
4751541Srgrimes
4761541Srgrimes	case FREAD:
4771541Srgrimes		/*
4781541Srgrimes		 * Need to block timeouts (ttrstart).
4791541Srgrimes		 */
4801541Srgrimes		s = spltty();
4811541Srgrimes		if ((tp->t_state&TS_ISOPEN) &&
4821541Srgrimes		     tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
4831541Srgrimes			splx(s);
4841541Srgrimes			return (1);
4851541Srgrimes		}
4861541Srgrimes		splx(s);
4871541Srgrimes		/* FALLTHROUGH */
4881541Srgrimes
4891541Srgrimes	case 0:					/* exceptional */
4901541Srgrimes		if ((tp->t_state&TS_ISOPEN) &&
4913308Sphk		    ((pti->pt_flags&PF_PKT && pti->pt_send) ||
4923308Sphk		     (pti->pt_flags&PF_UCNTL && pti->pt_ucntl)))
4931541Srgrimes			return (1);
4941541Srgrimes		selrecord(p, &pti->pt_selr);
4951541Srgrimes		break;
4961541Srgrimes
4971541Srgrimes
4981541Srgrimes	case FWRITE:
4991541Srgrimes		if (tp->t_state&TS_ISOPEN) {
5001541Srgrimes			if (pti->pt_flags & PF_REMOTE) {
5011541Srgrimes			    if (tp->t_canq.c_cc == 0)
5021541Srgrimes				return (1);
5031541Srgrimes			} else {
5041541Srgrimes			    if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2)
5051541Srgrimes				    return (1);
5061541Srgrimes			    if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON))
5071541Srgrimes				    return (1);
5081541Srgrimes			}
5091541Srgrimes		}
5101541Srgrimes		selrecord(p, &pti->pt_selw);
5111541Srgrimes		break;
5121541Srgrimes
5131541Srgrimes	}
5141541Srgrimes	return (0);
5151541Srgrimes}
5161541Srgrimes
51712675Sjulianstatic	int
5181541Srgrimesptcwrite(dev, uio, flag)
5191541Srgrimes	dev_t dev;
5201541Srgrimes	register struct uio *uio;
5211541Srgrimes	int flag;
5221541Srgrimes{
5231541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
5241549Srgrimes	register u_char *cp = 0;
5251541Srgrimes	register int cc = 0;
5261541Srgrimes	u_char locbuf[BUFSIZ];
5271541Srgrimes	int cnt = 0;
5281541Srgrimes	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
5291541Srgrimes	int error = 0;
5301541Srgrimes
5311541Srgrimesagain:
5321541Srgrimes	if ((tp->t_state&TS_ISOPEN) == 0)
5331541Srgrimes		goto block;
5341541Srgrimes	if (pti->pt_flags & PF_REMOTE) {
5351541Srgrimes		if (tp->t_canq.c_cc)
5361541Srgrimes			goto block;
53711789Sbde		while ((uio->uio_resid > 0 || cc > 0) &&
53811789Sbde		       tp->t_canq.c_cc < TTYHOG - 1) {
5391541Srgrimes			if (cc == 0) {
5401541Srgrimes				cc = min(uio->uio_resid, BUFSIZ);
5411541Srgrimes				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
5421541Srgrimes				cp = locbuf;
5431541Srgrimes				error = uiomove((caddr_t)cp, cc, uio);
5441541Srgrimes				if (error)
5451541Srgrimes					return (error);
5461541Srgrimes				/* check again for safety */
54711789Sbde				if ((tp->t_state & TS_ISOPEN) == 0) {
54811789Sbde					/* adjust as usual */
54911789Sbde					uio->uio_resid += cc;
5501541Srgrimes					return (EIO);
55111789Sbde				}
5521541Srgrimes			}
55315199Sbde			if (cc > 0) {
55415199Sbde				cc = b_to_q((char *)cp, cc, &tp->t_canq);
55515199Sbde				/*
55615199Sbde				 * XXX we don't guarantee that the canq size
55715199Sbde				 * is >= TTYHOG, so the above b_to_q() may
55815199Sbde				 * leave some bytes uncopied.  However, space
55915199Sbde				 * is guaranteed for the null terminator if
56015199Sbde				 * we don't fail here since (TTYHOG - 1) is
56115199Sbde				 * not a multiple of CBSIZE.
56215199Sbde				 */
56315199Sbde				if (cc > 0)
56415199Sbde					break;
56515199Sbde			}
5661541Srgrimes		}
56711789Sbde		/* adjust for data copied in but not written */
56811789Sbde		uio->uio_resid += cc;
5691541Srgrimes		(void) putc(0, &tp->t_canq);
5701541Srgrimes		ttwakeup(tp);
5719639Sbde		wakeup(TSA_PTS_READ(tp));
5721541Srgrimes		return (0);
5731541Srgrimes	}
57411789Sbde	while (uio->uio_resid > 0 || cc > 0) {
5751541Srgrimes		if (cc == 0) {
5761541Srgrimes			cc = min(uio->uio_resid, BUFSIZ);
5771541Srgrimes			cp = locbuf;
5781541Srgrimes			error = uiomove((caddr_t)cp, cc, uio);
5791541Srgrimes			if (error)
5801541Srgrimes				return (error);
5811541Srgrimes			/* check again for safety */
58211789Sbde			if ((tp->t_state & TS_ISOPEN) == 0) {
58311789Sbde				/* adjust for data copied in but not written */
58411789Sbde				uio->uio_resid += cc;
5851541Srgrimes				return (EIO);
58611789Sbde			}
5871541Srgrimes		}
5881541Srgrimes		while (cc > 0) {
5891541Srgrimes			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
5901541Srgrimes			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
5919824Sbde				wakeup(TSA_HUP_OR_INPUT(tp));
5921541Srgrimes				goto block;
5931541Srgrimes			}
5941541Srgrimes			(*linesw[tp->t_line].l_rint)(*cp++, tp);
5951541Srgrimes			cnt++;
5961541Srgrimes			cc--;
5971541Srgrimes		}
5981541Srgrimes		cc = 0;
5991541Srgrimes	}
6001541Srgrimes	return (0);
6011541Srgrimesblock:
6021541Srgrimes	/*
6031541Srgrimes	 * Come here to wait for slave to open, for space
60415199Sbde	 * in outq, or space in rawq, or an empty canq.
6051541Srgrimes	 */
60611789Sbde	if ((tp->t_state & TS_CONNECTED) == 0) {
60711789Sbde		/* adjust for data copied in but not written */
60811789Sbde		uio->uio_resid += cc;
6091541Srgrimes		return (EIO);
61011789Sbde	}
6111541Srgrimes	if (flag & IO_NDELAY) {
6121541Srgrimes		/* adjust for data copied in but not written */
6131541Srgrimes		uio->uio_resid += cc;
6141541Srgrimes		if (cnt == 0)
6151541Srgrimes			return (EWOULDBLOCK);
6161541Srgrimes		return (0);
6171541Srgrimes	}
6189639Sbde	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
6193308Sphk	if (error) {
6201541Srgrimes		/* adjust for data copied in but not written */
6211541Srgrimes		uio->uio_resid += cc;
6221541Srgrimes		return (error);
6231541Srgrimes	}
6241541Srgrimes	goto again;
6251541Srgrimes}
6261541Srgrimes
62712675Sjulianstatic	struct tty *
6286712Spstptydevtotty(dev)
6296712Spst	dev_t		dev;
6306712Spst{
6316712Spst	if (minor(dev) >= npty)
6326712Spst		return (NULL);
6336712Spst
6346712Spst	return &pt_tty[minor(dev)];
6356712Spst}
6366712Spst
6371541Srgrimes/*ARGSUSED*/
63812675Sjulianstatic	int
6391541Srgrimesptyioctl(dev, cmd, data, flag, p)
6401541Srgrimes	dev_t dev;
6411541Srgrimes	int cmd;
6421541Srgrimes	caddr_t data;
6431541Srgrimes	int flag;
6441541Srgrimes	struct proc *p;
6451541Srgrimes{
6461541Srgrimes	register struct tty *tp = &pt_tty[minor(dev)];
6471541Srgrimes	register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
6481541Srgrimes	register u_char *cc = tp->t_cc;
6491541Srgrimes	int stop, error;
6501541Srgrimes
6511541Srgrimes	/*
6521541Srgrimes	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
6531541Srgrimes	 * ttywflush(tp) will hang if there are characters in the outq.
6541541Srgrimes	 */
6551541Srgrimes	if (cmd == TIOCEXT) {
6561541Srgrimes		/*
6571541Srgrimes		 * When the EXTPROC bit is being toggled, we need
6581541Srgrimes		 * to send an TIOCPKT_IOCTL if the packet driver
6591541Srgrimes		 * is turned on.
6601541Srgrimes		 */
6611541Srgrimes		if (*(int *)data) {
6621541Srgrimes			if (pti->pt_flags & PF_PKT) {
6631541Srgrimes				pti->pt_send |= TIOCPKT_IOCTL;
6641541Srgrimes				ptcwakeup(tp, FREAD);
6651541Srgrimes			}
6661541Srgrimes			tp->t_lflag |= EXTPROC;
6671541Srgrimes		} else {
66814511Shsu			if ((tp->t_lflag & EXTPROC) &&
6691541Srgrimes			    (pti->pt_flags & PF_PKT)) {
6701541Srgrimes				pti->pt_send |= TIOCPKT_IOCTL;
6711541Srgrimes				ptcwakeup(tp, FREAD);
6721541Srgrimes			}
6731541Srgrimes			tp->t_lflag &= ~EXTPROC;
6741541Srgrimes		}
6751541Srgrimes		return(0);
6761541Srgrimes	} else
67712813Sjulian	if (cdevsw[major(dev)]->d_open == ptcopen)
6781541Srgrimes		switch (cmd) {
6791541Srgrimes
6801541Srgrimes		case TIOCGPGRP:
6811541Srgrimes			/*
6821541Srgrimes			 * We aviod calling ttioctl on the controller since,
6831541Srgrimes			 * in that case, tp must be the controlling terminal.
6841541Srgrimes			 */
6851541Srgrimes			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
6861541Srgrimes			return (0);
6871541Srgrimes
6881541Srgrimes		case TIOCPKT:
6891541Srgrimes			if (*(int *)data) {
6901541Srgrimes				if (pti->pt_flags & PF_UCNTL)
6911541Srgrimes					return (EINVAL);
6921541Srgrimes				pti->pt_flags |= PF_PKT;
6931541Srgrimes			} else
6941541Srgrimes				pti->pt_flags &= ~PF_PKT;
6951541Srgrimes			return (0);
6961541Srgrimes
6971541Srgrimes		case TIOCUCNTL:
6981541Srgrimes			if (*(int *)data) {
6991541Srgrimes				if (pti->pt_flags & PF_PKT)
7001541Srgrimes					return (EINVAL);
7011541Srgrimes				pti->pt_flags |= PF_UCNTL;
7021541Srgrimes			} else
7031541Srgrimes				pti->pt_flags &= ~PF_UCNTL;
7041541Srgrimes			return (0);
7051541Srgrimes
7061541Srgrimes		case TIOCREMOTE:
7071541Srgrimes			if (*(int *)data)
7081541Srgrimes				pti->pt_flags |= PF_REMOTE;
7091541Srgrimes			else
7101541Srgrimes				pti->pt_flags &= ~PF_REMOTE;
7111541Srgrimes			ttyflush(tp, FREAD|FWRITE);
7121541Srgrimes			return (0);
7131541Srgrimes
7141541Srgrimes#ifdef COMPAT_43
7158876Srgrimes		case TIOCSETP:
7161541Srgrimes		case TIOCSETN:
7171541Srgrimes#endif
7181541Srgrimes		case TIOCSETD:
7191541Srgrimes		case TIOCSETA:
7201541Srgrimes		case TIOCSETAW:
7219858Sache		case TIOCSETAF:
7221541Srgrimes			ndflush(&tp->t_outq, tp->t_outq.c_cc);
7231541Srgrimes			break;
7241541Srgrimes
7251541Srgrimes		case TIOCSIG:
72627770Sjmg			if (*(unsigned int *)data >= NSIG ||
72727770Sjmg			    *(unsigned int *)data == 0)
7281541Srgrimes				return(EINVAL);
7291541Srgrimes			if ((tp->t_lflag&NOFLSH) == 0)
7301541Srgrimes				ttyflush(tp, FREAD|FWRITE);
7311541Srgrimes			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
7321541Srgrimes			if ((*(unsigned int *)data == SIGINFO) &&
7331541Srgrimes			    ((tp->t_lflag&NOKERNINFO) == 0))
7341541Srgrimes				ttyinfo(tp);
7351541Srgrimes			return(0);
7361541Srgrimes		}
7371541Srgrimes	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
7381541Srgrimes	if (error < 0)
7391541Srgrimes		 error = ttioctl(tp, cmd, data, flag);
7401541Srgrimes	if (error < 0) {
7411541Srgrimes		if (pti->pt_flags & PF_UCNTL &&
7421541Srgrimes		    (cmd & ~0xff) == UIOCCMD(0)) {
7431541Srgrimes			if (cmd & 0xff) {
7441541Srgrimes				pti->pt_ucntl = (u_char)cmd;
7451541Srgrimes				ptcwakeup(tp, FREAD);
7461541Srgrimes			}
7471541Srgrimes			return (0);
7481541Srgrimes		}
7491541Srgrimes		error = ENOTTY;
7501541Srgrimes	}
7511541Srgrimes	/*
7521541Srgrimes	 * If external processing and packet mode send ioctl packet.
7531541Srgrimes	 */
7541541Srgrimes	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
7551541Srgrimes		switch(cmd) {
7561541Srgrimes		case TIOCSETA:
7571541Srgrimes		case TIOCSETAW:
7581541Srgrimes		case TIOCSETAF:
7591541Srgrimes#ifdef COMPAT_43
7601541Srgrimes		case TIOCSETP:
7611541Srgrimes		case TIOCSETN:
7621541Srgrimes#endif
7631541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
7641541Srgrimes		case TIOCSETC:
7651541Srgrimes		case TIOCSLTC:
7661541Srgrimes		case TIOCLBIS:
7671541Srgrimes		case TIOCLBIC:
7681541Srgrimes		case TIOCLSET:
7691541Srgrimes#endif
7701541Srgrimes			pti->pt_send |= TIOCPKT_IOCTL;
7711541Srgrimes			ptcwakeup(tp, FREAD);
7721541Srgrimes		default:
7731541Srgrimes			break;
7741541Srgrimes		}
7751541Srgrimes	}
7768876Srgrimes	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
7771541Srgrimes		&& CCEQ(cc[VSTART], CTRL('q'));
7781541Srgrimes	if (pti->pt_flags & PF_NOSTOP) {
7791541Srgrimes		if (stop) {
7801541Srgrimes			pti->pt_send &= ~TIOCPKT_NOSTOP;
7811541Srgrimes			pti->pt_send |= TIOCPKT_DOSTOP;
7821541Srgrimes			pti->pt_flags &= ~PF_NOSTOP;
7831541Srgrimes			ptcwakeup(tp, FREAD);
7841541Srgrimes		}
7851541Srgrimes	} else {
7861541Srgrimes		if (!stop) {
7871541Srgrimes			pti->pt_send &= ~TIOCPKT_DOSTOP;
7881541Srgrimes			pti->pt_send |= TIOCPKT_NOSTOP;
7891541Srgrimes			pti->pt_flags |= PF_NOSTOP;
7901541Srgrimes			ptcwakeup(tp, FREAD);
7911541Srgrimes		}
7921541Srgrimes	}
7931541Srgrimes	return (error);
7941541Srgrimes}
79512517Sjulian
79612517Sjulianstatic ptc_devsw_installed = 0;
79712517Sjulian
79812675Sjulianstatic void
79912675Sjulianptc_drvinit(void *unused)
80012517Sjulian{
80112517Sjulian#ifdef DEVFS
80212567Sjulian	int i,j,k;
80312517Sjulian#endif
80412517Sjulian	dev_t dev;
80512517Sjulian
80612517Sjulian	if( ! ptc_devsw_installed ) {
80712675Sjulian		dev = makedev(CDEV_MAJOR_S, 0);
80812675Sjulian		cdevsw_add(&dev, &pts_cdevsw, NULL);
80912675Sjulian		dev = makedev(CDEV_MAJOR_C, 0);
81012675Sjulian		cdevsw_add(&dev, &ptc_cdevsw, NULL);
81112517Sjulian		ptc_devsw_installed = 1;
81212517Sjulian#ifdef DEVFS
81312521Sjulian		for ( i = 0 ; i<NPTY ; i++ ) {
81412564Sjulian			j = i / 32;
81512564Sjulian			k = i % 32;
81612675Sjulian			devfs_token_pts[i] =
81713630Sphk				devfs_add_devswf(&pts_cdevsw,i,
81813630Sphk						DV_CHR,0,0,0666,
81913639Sphk						"tty%c%n",jnames[j],k);
82012675Sjulian			devfs_token_ptc[i] =
82113630Sphk				devfs_add_devswf(&ptc_cdevsw,i,
82213630Sphk						DV_CHR,0,0,0666,
82313639Sphk						"pty%c%n",jnames[j],k);
82412521Sjulian		}
82512521Sjulian#endif
82612517Sjulian    	}
82712517Sjulian}
82812517Sjulian
82912517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
830