xref: /csrg-svn/sys/kern/tty_bk.c (revision 29108)
123388Smckusick /*
2*29108Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323388Smckusick  * All rights reserved.  The Berkeley software License Agreement
423388Smckusick  * specifies the terms and conditions for redistribution.
523388Smckusick  *
6*29108Smckusick  *	@(#)tty_bk.c	7.1 (Berkeley) 06/05/86
723388Smckusick  */
87385Sroot 
97385Sroot #include "bk.h"
107385Sroot 
117385Sroot #if NBK > 0
1217096Sbloom #include "param.h"
1317096Sbloom #include "systm.h"
1417096Sbloom #include "dir.h"
1517096Sbloom #include "user.h"
1617096Sbloom #include "ioctl.h"
1717096Sbloom #include "tty.h"
1817096Sbloom #include "proc.h"
1917096Sbloom #include "inode.h"
2017096Sbloom #include "file.h"
2117096Sbloom #include "conf.h"
2217096Sbloom #include "buf.h"
2317096Sbloom #include "uio.h"
247385Sroot 
257385Sroot /*
267385Sroot  * Line discipline for Berkeley network.
277385Sroot  *
287385Sroot  * This supplies single lines to a user level program
297385Sroot  * with a minimum of fuss.  Lines are newline terminated.
307385Sroot  *
317385Sroot  * This discipline requires that tty device drivers call
327385Sroot  * the line specific l_ioctl routine from their ioctl routines,
337385Sroot  * assigning the result to cmd so that we can refuse most tty specific
347385Sroot  * ioctls which are unsafe because we have ambushed the
357385Sroot  * teletype input queues, overlaying them with other information.
367385Sroot  */
377385Sroot 
387385Sroot /*
397385Sroot  * Open as networked discipline.  Called when discipline changed
407385Sroot  * with ioctl, this assigns a buffer to the line for input, and
417385Sroot  * changing the interpretation of the information in the tty structure.
427385Sroot  */
437385Sroot /*ARGSUSED*/
447385Sroot bkopen(dev, tp)
457629Ssam 	dev_t dev;
467629Ssam 	register struct tty *tp;
477385Sroot {
487385Sroot 	register struct buf *bp;
497385Sroot 
508561Sroot 	if (tp->t_line == NETLDISC)
518561Sroot 		return (EBUSY);	/* sometimes the network opens /dev/tty */
527385Sroot 	bp = geteblk(1024);
5312753Ssam 	ttyflush(tp, FREAD|FWRITE);
547385Sroot 	tp->t_bufp = bp;
557385Sroot 	tp->t_cp = (char *)bp->b_un.b_addr;
567385Sroot 	tp->t_inbuf = 0;
577385Sroot 	tp->t_rec = 0;
588561Sroot 	return (0);
597385Sroot }
607385Sroot 
617385Sroot /*
627385Sroot  * Break down... called when discipline changed or from device
637385Sroot  * close routine.
647385Sroot  */
657385Sroot bkclose(tp)
668561Sroot 	register struct tty *tp;
677385Sroot {
688561Sroot 	register int s;
697385Sroot 
707385Sroot 	s = spl5();
717385Sroot 	wakeup((caddr_t)&tp->t_rawq);
727385Sroot 	if (tp->t_bufp) {
737385Sroot 		brelse(tp->t_bufp);
747385Sroot 		tp->t_bufp = 0;
757385Sroot 	} else
767385Sroot 		printf("bkclose: no buf\n");
777385Sroot 	tp->t_cp = 0;
787385Sroot 	tp->t_inbuf = 0;
797385Sroot 	tp->t_rec = 0;
807385Sroot 	tp->t_line = 0;		/* paranoid: avoid races */
817385Sroot 	splx(s);
827385Sroot }
837385Sroot 
847385Sroot /*
857385Sroot  * Read from a network line.
867385Sroot  * Characters have been buffered in a system buffer and are
877385Sroot  * now dumped back to the user in one fell swoop, and with a
887385Sroot  * minimum of fuss.  Note that no input is accepted when a record
897385Sroot  * is waiting.  Our clearing tp->t_rec here allows further input
907385Sroot  * to accumulate.
917385Sroot  */
927725Sroot bkread(tp, uio)
937725Sroot 	register struct tty *tp;
947725Sroot 	struct uio *uio;
957385Sroot {
968590Sroot 	register int s;
978521Sroot 	int error;
987385Sroot 
997385Sroot 	if ((tp->t_state&TS_CARR_ON)==0)
1007385Sroot 		return (-1);
1017385Sroot 	s = spl5();
1027385Sroot 	while (tp->t_rec == 0 && tp->t_line == NETLDISC)
1037385Sroot 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
1047385Sroot 	splx(s);
1057385Sroot 	if (tp->t_line != NETLDISC)
1067385Sroot 		return (-1);
1078521Sroot 	error = uiomove(tp->t_bufp->b_un.b_addr, tp->t_inbuf, UIO_READ, uio);
1087385Sroot 	tp->t_cp = (char *)tp->t_bufp->b_un.b_addr;
1097385Sroot 	tp->t_inbuf = 0;
1107385Sroot 	tp->t_rec = 0;
1118521Sroot 	return (error);
1127385Sroot }
1137385Sroot 
1147385Sroot /*
1157385Sroot  * Low level character input routine.
1167385Sroot  * Stuff the character in the buffer, and wake up the top
1177385Sroot  * half after setting t_rec if this completes the record
1187385Sroot  * or if the buffer is (ick!) full.
1197385Sroot  *
1207385Sroot  * Thisis where the formatting should get done to allow
1217385Sroot  * 8 character data paths through escapes.
1227385Sroot  *
1237385Sroot  * This rutine should be expanded in-line in the receiver
1247385Sroot  * interrupt routine of the dh-11 to make it run as fast as possible.
1257385Sroot  */
1267385Sroot bkinput(c, tp)
1277385Sroot register c;
1287385Sroot register struct tty *tp;
1297385Sroot {
1307385Sroot 
1317385Sroot 	if (tp->t_rec)
1327385Sroot 		return;
1337385Sroot 	*tp->t_cp++ = c;
1347385Sroot 	if (++tp->t_inbuf == 1024 || c == '\n') {
1357385Sroot 		tp->t_rec = 1;
1367385Sroot 		wakeup((caddr_t)&tp->t_rawq);
1377385Sroot 	}
1387385Sroot }
1397385Sroot 
1407385Sroot /*
1417385Sroot  * This routine is called whenever a ioctl is about to be performed
1427385Sroot  * and gets a chance to reject the ioctl.  We reject all teletype
1437385Sroot  * oriented ioctl's except those which set the discipline, and
1447385Sroot  * those which get parameters (gtty and get special characters).
1457385Sroot  */
1467385Sroot /*ARGSUSED*/
1477629Ssam bkioctl(tp, cmd, data, flag)
1487629Ssam 	struct tty *tp;
1497629Ssam 	caddr_t data;
1507385Sroot {
1517385Sroot 
1527385Sroot 	if ((cmd>>8) != 't')
1538561Sroot 		return (-1);
1547385Sroot 	switch (cmd) {
1557385Sroot 
1567385Sroot 	case TIOCSETD:
1577385Sroot 	case TIOCGETD:
1587385Sroot 	case TIOCGETP:
1597385Sroot 	case TIOCGETC:
1608561Sroot 		return (-1);
1617385Sroot 	}
1628561Sroot 	return (ENOTTY);
1637385Sroot }
1647385Sroot #endif
165