xref: /csrg-svn/sys/kern/tty_tb.c (revision 68171)
149594Sbostic /*-
263180Sbostic  * Copyright (c) 1982, 1986, 1991, 1993
363180Sbostic  *	The Regents of the University of California.  All rights reserved.
423392Smckusick  *
549594Sbostic  * %sccs.include.redist.c%
649594Sbostic  *
7*68171Scgd  *	@(#)tty_tb.c	8.2 (Berkeley) 01/09/95
823392Smckusick  */
97297Ssam 
107297Ssam #include "tb.h"
117297Ssam #if NTB > 0
127297Ssam 
1325412Ssam /*
1425412Ssam  * Line discipline for RS232 tablets;
1525412Ssam  * supplies binary coordinate data.
1625412Ssam  */
1756517Sbostic #include <sys/param.h>
1856517Sbostic #include <sys/tablet.h>
1956517Sbostic #include <sys/tty.h>
207297Ssam 
217297Ssam /*
2225412Ssam  * Tablet configuration table.
237297Ssam  */
2425412Ssam struct	tbconf {
2525412Ssam 	short	tbc_recsize;	/* input record size in bytes */
2625412Ssam 	short	tbc_uiosize;	/* size of data record returned user */
2725412Ssam 	int	tbc_sync;	/* mask for finding sync byte/bit */
2825412Ssam 	int	(*tbc_decode)();/* decoding routine */
2925412Ssam 	char	*tbc_run;	/* enter run mode sequence */
3025412Ssam 	char	*tbc_point;	/* enter point mode sequence */
3125412Ssam 	char	*tbc_stop;	/* stop sequence */
3225412Ssam 	char	*tbc_start;	/* start/restart sequence */
3325412Ssam 	int	tbc_flags;
3430289Ssam #define	TBF_POL		0x1	/* polhemus hack */
3530289Ssam #define	TBF_INPROX	0x2	/* tablet has proximity info */
3625412Ssam };
377297Ssam 
3825412Ssam static	int tbdecode(), gtcodecode(), poldecode();
3925412Ssam static	int tblresdecode(), tbhresdecode();
408522Sroot 
4125412Ssam struct	tbconf tbconf[TBTYPE] = {
4225412Ssam { 0 },
4325412Ssam { 5, sizeof (struct tbpos), 0200, tbdecode, "6", "4" },
4425412Ssam { 5, sizeof (struct tbpos), 0200, tbdecode, "\1CN", "\1RT", "\2", "\4" },
4525412Ssam { 8, sizeof (struct gtcopos), 0200, gtcodecode },
4625412Ssam {17, sizeof (struct polpos), 0200, poldecode, 0, 0, "\21", "\5\22\2\23",
4730289Ssam   TBF_POL },
4830289Ssam { 5, sizeof (struct tbpos), 0100, tblresdecode, "\1CN", "\1PT", "\2", "\4",
4930289Ssam   TBF_INPROX },
5030289Ssam { 6, sizeof (struct tbpos), 0200, tbhresdecode, "\1CN", "\1PT", "\2", "\4",
5130289Ssam   TBF_INPROX },
5230289Ssam { 5, sizeof (struct tbpos), 0100, tblresdecode, "\1CL\33", "\1PT\33", 0, 0},
5330289Ssam { 6, sizeof (struct tbpos), 0200, tbhresdecode, "\1CL\33", "\1PT\33", 0, 0},
5425412Ssam };
5525412Ssam 
5625412Ssam /*
5725412Ssam  * Tablet state
5825412Ssam  */
5914596Ssam struct tb {
6025412Ssam 	int	tbflags;		/* mode & type bits */
6125412Ssam #define	TBMAXREC	17	/* max input record size */
6225412Ssam 	char	cbuf[TBMAXREC];		/* input buffer */
6325412Ssam 	union {
6425412Ssam 		struct	tbpos tbpos;
6525412Ssam 		struct	gtcopos gtcopos;
6625412Ssam 		struct	polpos polpos;
6725412Ssam 	} rets;				/* processed state */
6825412Ssam #define NTBS	16
6914596Ssam } tb[NTBS];
707297Ssam 
717297Ssam /*
7225412Ssam  * Open as tablet discipline; called on discipline change.
737297Ssam  */
747297Ssam /*ARGSUSED*/
tbopen(dev,tp)757297Ssam tbopen(dev, tp)
767632Ssam 	dev_t dev;
777632Ssam 	register struct tty *tp;
787297Ssam {
7914596Ssam 	register struct tb *tbp;
807297Ssam 
8125412Ssam 	if (tp->t_line == TABLDISC)
8214596Ssam 		return (ENODEV);
8314596Ssam 	ttywflush(tp);
8414596Ssam 	for (tbp = tb; tbp < &tb[NTBS]; tbp++)
8525412Ssam 		if (tbp->tbflags == 0)
8614596Ssam 			break;
8714596Ssam 	if (tbp >= &tb[NTBS])
888557Sroot 		return (EBUSY);
8925412Ssam 	tbp->tbflags = TBTIGER|TBPOINT;		/* default */
9014596Ssam 	tp->t_cp = tbp->cbuf;
917297Ssam 	tp->t_inbuf = 0;
9225412Ssam 	bzero((caddr_t)&tbp->rets, sizeof (tbp->rets));
9325412Ssam 	tp->T_LINEP = (caddr_t)tbp;
9425412Ssam 	tp->t_flags |= LITOUT;
958557Sroot 	return (0);
967297Ssam }
977297Ssam 
987297Ssam /*
9925412Ssam  * Line discipline change or last device close.
1007297Ssam  */
tbclose(tp)1017297Ssam tbclose(tp)
1028557Sroot 	register struct tty *tp;
1037297Ssam {
10425412Ssam 	register int s;
10525412Ssam 	int modebits = TBPOINT|TBSTOP;
1067297Ssam 
10725412Ssam 	tbioctl(tp, BIOSMODE, &modebits, 0);
10829947Skarels 	s = spltty();
10925412Ssam 	((struct tb *)tp->T_LINEP)->tbflags = 0;
1107297Ssam 	tp->t_cp = 0;
1117297Ssam 	tp->t_inbuf = 0;
1127297Ssam 	tp->t_rawq.c_cc = 0;		/* clear queues -- paranoid */
1137297Ssam 	tp->t_canq.c_cc = 0;
11415044Smckusick 	tp->t_line = 0;			/* paranoid: avoid races */
1157297Ssam 	splx(s);
1167297Ssam }
1177297Ssam 
1187297Ssam /*
1197297Ssam  * Read from a tablet line.
12025412Ssam  * Characters have been buffered in a buffer and decoded.
1217297Ssam  */
tbread(tp,uio)1227732Sroot tbread(tp, uio)
1237732Sroot 	register struct tty *tp;
1247732Sroot 	struct uio *uio;
1257297Ssam {
12625412Ssam 	register struct tb *tbp = (struct tb *)tp->T_LINEP;
12725412Ssam 	register struct tbconf *tc = &tbconf[tbp->tbflags & TBTYPE];
12825412Ssam 	int ret;
1297297Ssam 
13025412Ssam 	if ((tp->t_state&TS_CARR_ON) == 0)
1318522Sroot 		return (EIO);
13237728Smckusick 	ret = uiomove(&tbp->rets, tc->tbc_uiosize, uio);
13325412Ssam 	if (tc->tbc_flags&TBF_POL)
13425412Ssam 		tbp->rets.polpos.p_key = ' ';
13525412Ssam 	return (ret);
1367297Ssam }
1377297Ssam 
1387297Ssam /*
1397297Ssam  * Low level character input routine.
14025412Ssam  * Stuff the character in the buffer, and decode
1417297Ssam  * if all the chars are there.
1427297Ssam  *
1437297Ssam  * This routine could be expanded in-line in the receiver
14425412Ssam  * interrupt routine to make it run as fast as possible.
1457297Ssam  */
tbinput(c,tp)1467297Ssam tbinput(c, tp)
1478522Sroot 	register int c;
1488522Sroot 	register struct tty *tp;
1497297Ssam {
15025412Ssam 	register struct tb *tbp = (struct tb *)tp->T_LINEP;
15125412Ssam 	register struct tbconf *tc = &tbconf[tbp->tbflags & TBTYPE];
1527297Ssam 
15325412Ssam 	if (tc->tbc_recsize == 0 || tc->tbc_decode == 0)	/* paranoid? */
15425412Ssam 		return;
15525412Ssam 	/*
15625412Ssam 	 * Locate sync bit/byte or reset input buffer.
15725412Ssam 	 */
15825412Ssam 	if (c&tc->tbc_sync || tp->t_inbuf == tc->tbc_recsize) {
15925412Ssam 		tp->t_cp = tbp->cbuf;
16025412Ssam 		tp->t_inbuf = 0;
1617297Ssam 	}
16225412Ssam 	*tp->t_cp++ = c&0177;
16325412Ssam 	/*
16425412Ssam 	 * Call decode routine only if a full record has been collected.
16525412Ssam 	 */
16625412Ssam 	if (++tp->t_inbuf == tc->tbc_recsize)
16730289Ssam 		(*tc->tbc_decode)(tc, tbp->cbuf, &tbp->rets);
1687297Ssam }
1697297Ssam 
1707297Ssam /*
17125412Ssam  * Decode GTCO 8 byte format (high res, tilt, and pressure).
1727297Ssam  */
17325412Ssam static
17430289Ssam gtcodecode(tc, cp, tbpos)
17530289Ssam 	struct tbconf *tc;
1767297Ssam 	register char *cp;
17725412Ssam 	register struct gtcopos *tbpos;
1787297Ssam {
1797297Ssam 
18025412Ssam 	tbpos->pressure = *cp >> 2;
18125412Ssam 	tbpos->status = (tbpos->pressure > 16) | TBINPROX; /* half way down */
18225412Ssam 	tbpos->xpos = (*cp++ & 03) << 14;
18325412Ssam 	tbpos->xpos |= *cp++ << 7;
18425412Ssam 	tbpos->xpos |= *cp++;
18525412Ssam 	tbpos->ypos = (*cp++ & 03) << 14;
18625412Ssam 	tbpos->ypos |= *cp++ << 7;
18725412Ssam 	tbpos->ypos |= *cp++;
18825412Ssam 	tbpos->xtilt = *cp++;
18925412Ssam 	tbpos->ytilt = *cp++;
19014596Ssam 	tbpos->scount++;
1917297Ssam }
1927297Ssam 
1937297Ssam /*
19425412Ssam  * Decode old Hitachi 5 byte format (low res).
1957297Ssam  */
19625412Ssam static
19730289Ssam tbdecode(tc, cp, tbpos)
19830289Ssam 	struct tbconf *tc;
1997297Ssam 	register char *cp;
20014596Ssam 	register struct tbpos *tbpos;
2017297Ssam {
2027297Ssam 	register char byte;
2037297Ssam 
2047297Ssam 	byte = *cp++;
20525412Ssam 	tbpos->status = (byte&0100) ? TBINPROX : 0;
2067297Ssam 	byte &= ~0100;
2078522Sroot 	if (byte > 036)
20825412Ssam 		tbpos->status |= 1 << ((byte-040)/2);
20925412Ssam 	tbpos->xpos = *cp++ << 7;
21025412Ssam 	tbpos->xpos |= *cp++;
21125412Ssam 	if (tbpos->xpos < 256)			/* tablet wraps around at 256 */
21225412Ssam 		tbpos->status &= ~TBINPROX;	/* make it out of proximity */
21325412Ssam 	tbpos->ypos = *cp++ << 7;
21425412Ssam 	tbpos->ypos |= *cp++;
21514596Ssam 	tbpos->scount++;
2167297Ssam }
2177297Ssam 
2187297Ssam /*
21925412Ssam  * Decode new Hitach 5-byte format (low res).
2207297Ssam  */
22125412Ssam static
22230289Ssam tblresdecode(tc, cp, tbpos)
22330289Ssam 	struct tbconf *tc;
22425412Ssam 	register char *cp;
22525412Ssam 	register struct tbpos *tbpos;
22625412Ssam {
22725412Ssam 
22825412Ssam 	*cp &= ~0100;		/* mask sync bit */
22925412Ssam 	tbpos->status = (*cp++ >> 2) | TBINPROX;
23030289Ssam 	if (tc->tbc_flags&TBF_INPROX && tbpos->status&020)
23130289Ssam 		tbpos->status &= ~(020|TBINPROX);
23225412Ssam 	tbpos->xpos = *cp++;
23325412Ssam 	tbpos->xpos |= *cp++ << 6;
23425412Ssam 	tbpos->ypos = *cp++;
23525412Ssam 	tbpos->ypos |= *cp++ << 6;
23625412Ssam 	tbpos->scount++;
23725412Ssam }
23825412Ssam 
23925412Ssam /*
24025412Ssam  * Decode new Hitach 6-byte format (high res).
24125412Ssam  */
24225412Ssam static
24330289Ssam tbhresdecode(tc, cp, tbpos)
24430289Ssam 	struct tbconf *tc;
24525412Ssam 	register char *cp;
24625412Ssam 	register struct tbpos *tbpos;
24725412Ssam {
24825412Ssam 	char byte;
24925412Ssam 
25025412Ssam 	byte = *cp++;
25125412Ssam 	tbpos->xpos = (byte & 03) << 14;
25225412Ssam 	tbpos->xpos |= *cp++ << 7;
25325412Ssam 	tbpos->xpos |= *cp++;
25425412Ssam 	tbpos->ypos = *cp++ << 14;
25525412Ssam 	tbpos->ypos |= *cp++ << 7;
25625412Ssam 	tbpos->ypos |= *cp++;
25725412Ssam 	tbpos->status = (byte >> 2) | TBINPROX;
25830289Ssam 	if (tc->tbc_flags&TBF_INPROX && tbpos->status&020)
25930289Ssam 		tbpos->status &= ~(020|TBINPROX);
26025412Ssam 	tbpos->scount++;
26125412Ssam }
26225412Ssam 
26325412Ssam /*
26425412Ssam  * Polhemus decode.
26525412Ssam  */
26625412Ssam static
26730289Ssam poldecode(tc, cp, polpos)
26830289Ssam 	struct tbconf *tc;
26925412Ssam 	register char *cp;
27025412Ssam 	register struct polpos *polpos;
27125412Ssam {
27225412Ssam 
27325412Ssam 	polpos->p_x = cp[4] | cp[3]<<7 | (cp[9] & 0x03) << 14;
27425412Ssam 	polpos->p_y = cp[6] | cp[5]<<7 | (cp[9] & 0x0c) << 12;
27525412Ssam 	polpos->p_z = cp[8] | cp[7]<<7 | (cp[9] & 0x30) << 10;
27625412Ssam 	polpos->p_azi = cp[11] | cp[10]<<7 | (cp[16] & 0x03) << 14;
27725412Ssam 	polpos->p_pit = cp[13] | cp[12]<<7 | (cp[16] & 0x0c) << 12;
27825412Ssam 	polpos->p_rol = cp[15] | cp[14]<<7 | (cp[16] & 0x30) << 10;
27925412Ssam 	polpos->p_stat = cp[1] | cp[0]<<7;
28025412Ssam 	if (cp[2] != ' ')
28125412Ssam 		polpos->p_key = cp[2];
28225412Ssam }
28325412Ssam 
2847297Ssam /*ARGSUSED*/
2857632Ssam tbioctl(tp, cmd, data, flag)
2867632Ssam 	struct tty *tp;
287*68171Scgd 	u_long cmd;
2887632Ssam 	caddr_t data;
289*68171Scgd 	int flag;
2907297Ssam {
29125412Ssam 	register struct tb *tbp = (struct tb *)tp->T_LINEP;
2927297Ssam 
2937297Ssam 	switch (cmd) {
2947297Ssam 
29525412Ssam 	case BIOGMODE:
29625412Ssam 		*(int *)data = tbp->tbflags & TBMODE;
29725412Ssam 		break;
29825412Ssam 
29925412Ssam 	case BIOSTYPE:
30025412Ssam 		if (tbconf[*(int *)data & TBTYPE].tbc_recsize == 0 ||
30125412Ssam 		    tbconf[*(int *)data & TBTYPE].tbc_decode == 0)
30225412Ssam 			return (EINVAL);
30325412Ssam 		tbp->tbflags &= ~TBTYPE;
30425412Ssam 		tbp->tbflags |= *(int *)data & TBTYPE;
30525412Ssam 		/* fall thru... to set mode bits */
30625412Ssam 
30725412Ssam 	case BIOSMODE: {
30825412Ssam 		register struct tbconf *tc;
30925412Ssam 
31025412Ssam 		tbp->tbflags &= ~TBMODE;
31125412Ssam 		tbp->tbflags |= *(int *)data & TBMODE;
31225412Ssam 		tc = &tbconf[tbp->tbflags & TBTYPE];
31325412Ssam 		if (tbp->tbflags&TBSTOP) {
31425412Ssam 			if (tc->tbc_stop)
31525412Ssam 				ttyout(tc->tbc_stop, tp);
31625412Ssam 		} else if (tc->tbc_start)
31725412Ssam 			ttyout(tc->tbc_start, tp);
31825412Ssam 		if (tbp->tbflags&TBPOINT) {
31925412Ssam 			if (tc->tbc_point)
32025412Ssam 				ttyout(tc->tbc_point, tp);
32125412Ssam 		} else if (tc->tbc_run)
32225412Ssam 			ttyout(tc->tbc_run, tp);
32325412Ssam 		ttstart(tp);
32425412Ssam 		break;
32525412Ssam 	}
32625412Ssam 
32725412Ssam 	case BIOGTYPE:
32825412Ssam 		*(int *)data = tbp->tbflags & TBTYPE;
32925412Ssam 		break;
33025412Ssam 
3317297Ssam 	case TIOCSETD:
3327297Ssam 	case TIOCGETD:
3337297Ssam 	case TIOCGETP:
3347297Ssam 	case TIOCGETC:
33525412Ssam 		return (-1);		/* pass thru... */
33625412Ssam 
33725412Ssam 	default:
33825412Ssam 		return (ENOTTY);
3397297Ssam 	}
3407297Ssam 	return (0);
3417297Ssam }
3427297Ssam #endif
343