NetBSD/sys/kern/tty_tb.c

403 lines
10 KiB
C
Raw Normal View History

2000-11-02 09:57:01 +03:00
/* $NetBSD: tty_tb.c,v 1.24 2000/11/02 06:57:01 enami Exp $ */
1993-03-21 12:45:37 +03:00
/*-
1994-05-12 07:25:27 +04:00
* Copyright (c) 1982, 1986, 1991, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
1998-03-01 05:20:01 +03:00
* @(#)tty_tb.c 8.2 (Berkeley) 1/9/95
1993-03-21 12:45:37 +03:00
*/
#include "tb.h"
/*
* Line discipline for RS232 tablets;
* supplies binary coordinate data.
*/
1993-12-18 07:21:37 +03:00
#include <sys/param.h>
#include <sys/tablet.h>
1996-02-04 05:15:01 +03:00
#include <sys/systm.h>
2000-11-02 09:57:01 +03:00
#include <sys/conf.h>
#include <sys/ioctl.h>
#include <sys/ioctl_compat.h>
1993-12-18 07:21:37 +03:00
#include <sys/tty.h>
#include <sys/proc.h>
1993-03-21 12:45:37 +03:00
1996-02-04 05:15:01 +03:00
union tbpos {
struct hitpos hitpos;
struct gtcopos gtcopos;
struct polpos polpos;
};
1993-03-21 12:45:37 +03:00
/*
* Tablet configuration table.
*/
struct tbconf {
short tbc_recsize; /* input record size in bytes */
short tbc_uiosize; /* size of data record returned user */
int tbc_sync; /* mask for finding sync byte/bit */
1996-02-04 05:15:01 +03:00
/* decoding routine */
void (*tbc_decode) __P((struct tbconf *, char *, union tbpos *));
1994-12-13 23:03:13 +03:00
u_char *tbc_run; /* enter run mode sequence */
u_char *tbc_point; /* enter point mode sequence */
u_char *tbc_stop; /* stop sequence */
u_char *tbc_start; /* start/restart sequence */
1993-03-21 12:45:37 +03:00
int tbc_flags;
#define TBF_POL 0x1 /* polhemus hack */
#define TBF_INPROX 0x2 /* tablet has proximity info */
};
1996-02-04 05:15:01 +03:00
static void gtcodecode __P((struct tbconf *, char *, union tbpos *));
static void tbolddecode __P((struct tbconf *, char *, union tbpos *));
static void tblresdecode __P((struct tbconf *, char *, union tbpos *));
static void tbhresdecode __P((struct tbconf *, char *, union tbpos *));
static void poldecode __P((struct tbconf *, char *, union tbpos *));
1993-03-21 12:45:37 +03:00
struct tbconf tbconf[TBTYPE] = {
{ 0 },
{ 5, sizeof(struct hitpos), 0200, tbolddecode, "6", "4" },
{ 5, sizeof(struct hitpos), 0200, tbolddecode, "\1CN", "\1RT", "\2", "\4" },
{ 8, sizeof(struct gtcopos), 0200, gtcodecode },
{17, sizeof(struct polpos), 0200, poldecode, 0, 0, "\21", "\5\22\2\23",
1993-03-21 12:45:37 +03:00
TBF_POL },
{ 5, sizeof(struct hitpos), 0100, tblresdecode, "\1CN", "\1PT", "\2", "\4",
1993-03-21 12:45:37 +03:00
TBF_INPROX },
{ 6, sizeof(struct hitpos), 0200, tbhresdecode, "\1CN", "\1PT", "\2", "\4",
1993-03-21 12:45:37 +03:00
TBF_INPROX },
{ 5, sizeof(struct hitpos), 0100, tblresdecode, "\1CL\33", "\1PT\33", 0, 0},
{ 6, sizeof(struct hitpos), 0200, tbhresdecode, "\1CL\33", "\1PT\33", 0, 0},
1993-03-21 12:45:37 +03:00
};
/*
* Tablet state
*/
struct tb {
int tbflags; /* mode & type bits */
#define TBMAXREC 17 /* max input record size */
char cbuf[TBMAXREC]; /* input buffer */
1994-05-12 07:25:27 +04:00
int tbinbuf;
char *tbcp;
1996-02-04 05:15:01 +03:00
union tbpos tbpos;
} tb[NTB];
1993-03-21 12:45:37 +03:00
1996-02-04 05:15:01 +03:00
int tbopen __P((dev_t, struct tty *));
void tbclose __P((struct tty *));
int tbread __P((struct tty *, struct uio *));
void tbinput __P((int, struct tty *));
int tbtioctl __P((struct tty *, u_long, caddr_t, int, struct proc *));
void tbattach __P((int));
1993-03-21 12:45:37 +03:00
/*
* Open as tablet discipline; called on discipline change.
*/
/*ARGSUSED*/
1996-02-04 05:15:01 +03:00
int
1993-03-21 12:45:37 +03:00
tbopen(dev, tp)
dev_t dev;
2000-03-30 13:27:11 +04:00
struct tty *tp;
1993-03-21 12:45:37 +03:00
{
2000-03-30 13:27:11 +04:00
struct tb *tbp;
1993-03-21 12:45:37 +03:00
2000-11-02 09:57:01 +03:00
if (tp->t_linesw && (tp->t_linesw->l_no == TABLDISC))
1994-05-12 07:25:27 +04:00
return (ENODEV);
1993-03-21 12:45:37 +03:00
ttywflush(tp);
for (tbp = tb; tbp < &tb[NTB]; tbp++)
1993-03-21 12:45:37 +03:00
if (tbp->tbflags == 0)
break;
if (tbp >= &tb[NTB])
1993-03-21 12:45:37 +03:00
return (EBUSY);
tbp->tbflags = TBTIGER|TBPOINT; /* default */
tbp->tbcp = tbp->cbuf;
tbp->tbinbuf = 0;
memset((caddr_t)&tbp->tbpos, 0, sizeof(tbp->tbpos));
tp->t_sc = (caddr_t)tbp;
1993-03-21 12:45:37 +03:00
tp->t_flags |= LITOUT;
return (0);
}
/*
* Line discipline change or last device close.
*/
1996-02-04 05:15:01 +03:00
void
1993-03-21 12:45:37 +03:00
tbclose(tp)
2000-03-30 13:27:11 +04:00
struct tty *tp;
1993-03-21 12:45:37 +03:00
{
int modebits = TBPOINT|TBSTOP;
1996-02-04 05:15:01 +03:00
tbtioctl(tp, BIOSMODE, (caddr_t) &modebits, 0, curproc);
1993-03-21 12:45:37 +03:00
}
/*
* Read from a tablet line.
* Characters have been buffered in a buffer and decoded.
*/
1996-02-04 05:15:01 +03:00
int
1993-03-21 12:45:37 +03:00
tbread(tp, uio)
2000-03-30 13:27:11 +04:00
struct tty *tp;
1993-03-21 12:45:37 +03:00
struct uio *uio;
{
2000-03-30 13:27:11 +04:00
struct tb *tbp = (struct tb *)tp->t_sc;
struct tbconf *tc = &tbconf[tbp->tbflags & TBTYPE];
1993-03-21 12:45:37 +03:00
int ret;
if ((tp->t_state&TS_CARR_ON) == 0)
return (EIO);
1996-02-04 05:15:01 +03:00
ret = uiomove((caddr_t) &tbp->tbpos, tc->tbc_uiosize, uio);
1993-03-21 12:45:37 +03:00
if (tc->tbc_flags&TBF_POL)
1996-02-04 05:15:01 +03:00
tbp->tbpos.polpos.p_key = ' ';
1993-03-21 12:45:37 +03:00
return (ret);
}
/*
* Low level character input routine.
* Stuff the character in the buffer, and decode
* if all the chars are there.
*
* This routine could be expanded in-line in the receiver
* interrupt routine to make it run as fast as possible.
*/
1996-02-04 05:15:01 +03:00
void
1993-03-21 12:45:37 +03:00
tbinput(c, tp)
2000-03-30 13:27:11 +04:00
int c;
struct tty *tp;
1993-03-21 12:45:37 +03:00
{
2000-03-30 13:27:11 +04:00
struct tb *tbp = (struct tb *)tp->t_sc;
struct tbconf *tc = &tbconf[tbp->tbflags & TBTYPE];
1993-03-21 12:45:37 +03:00
if (tc->tbc_recsize == 0 || tc->tbc_decode == 0) /* paranoid? */
return;
/*
* Locate sync bit/byte or reset input buffer.
*/
if (c&tc->tbc_sync || tbp->tbinbuf == tc->tbc_recsize) {
tbp->tbcp = tbp->cbuf;
tbp->tbinbuf = 0;
1993-03-21 12:45:37 +03:00
}
*tbp->tbcp++ = c&0177;
1993-03-21 12:45:37 +03:00
/*
* Call decode routine only if a full record has been collected.
*/
if (++tbp->tbinbuf == tc->tbc_recsize)
1996-02-04 05:15:01 +03:00
(*tc->tbc_decode)(tc, tbp->cbuf, &tbp->tbpos);
1993-03-21 12:45:37 +03:00
}
/*
* Decode GTCO 8 byte format (high res, tilt, and pressure).
*/
1996-02-04 05:15:01 +03:00
static void
gtcodecode(tc, cp, u)
1993-03-21 12:45:37 +03:00
struct tbconf *tc;
2000-03-30 13:27:11 +04:00
char *cp;
union tbpos *u;
1993-03-21 12:45:37 +03:00
{
1996-02-04 05:15:01 +03:00
struct gtcopos *pos = &u->gtcopos;
pos->pressure = *cp >> 2;
pos->status = (pos->pressure > 16) | TBINPROX; /* half way down */
pos->xpos = (*cp++ & 03) << 14;
pos->xpos |= *cp++ << 7;
pos->xpos |= *cp++;
pos->ypos = (*cp++ & 03) << 14;
pos->ypos |= *cp++ << 7;
pos->ypos |= *cp++;
pos->xtilt = *cp++;
pos->ytilt = *cp++;
pos->scount++;
1993-03-21 12:45:37 +03:00
}
/*
* Decode old Hitachi 5 byte format (low res).
*/
1996-02-04 05:15:01 +03:00
static void
tbolddecode(tc, cp, u)
1993-03-21 12:45:37 +03:00
struct tbconf *tc;
2000-03-30 13:27:11 +04:00
char *cp;
union tbpos *u;
1993-03-21 12:45:37 +03:00
{
1996-02-04 05:15:01 +03:00
struct hitpos *pos = &u->hitpos;
2000-03-30 13:27:11 +04:00
char byte;
1993-03-21 12:45:37 +03:00
byte = *cp++;
1996-02-04 05:15:01 +03:00
pos->status = (byte&0100) ? TBINPROX : 0;
1993-03-21 12:45:37 +03:00
byte &= ~0100;
if (byte > 036)
1996-02-04 05:15:01 +03:00
pos->status |= 1 << ((byte-040)/2);
pos->xpos = *cp++ << 7;
pos->xpos |= *cp++;
if (pos->xpos < 256) /* tablet wraps around at 256 */
pos->status &= ~TBINPROX; /* make it out of proximity */
pos->ypos = *cp++ << 7;
pos->ypos |= *cp++;
pos->scount++;
1993-03-21 12:45:37 +03:00
}
/*
* Decode new Hitach 5-byte format (low res).
*/
1996-02-04 05:15:01 +03:00
static void
tblresdecode(tc, cp, u)
1993-03-21 12:45:37 +03:00
struct tbconf *tc;
2000-03-30 13:27:11 +04:00
char *cp;
union tbpos *u;
1993-03-21 12:45:37 +03:00
{
1996-02-04 05:15:01 +03:00
struct hitpos *pos = &u->hitpos;
1993-03-21 12:45:37 +03:00
*cp &= ~0100; /* mask sync bit */
1996-02-04 05:15:01 +03:00
pos->status = (*cp++ >> 2) | TBINPROX;
if (tc->tbc_flags&TBF_INPROX && pos->status&020)
pos->status &= ~(020|TBINPROX);
pos->xpos = *cp++;
pos->xpos |= *cp++ << 6;
pos->ypos = *cp++;
pos->ypos |= *cp++ << 6;
pos->scount++;
1993-03-21 12:45:37 +03:00
}
/*
* Decode new Hitach 6-byte format (high res).
*/
1996-02-04 05:15:01 +03:00
static void
tbhresdecode(tc, cp, u)
1993-03-21 12:45:37 +03:00
struct tbconf *tc;
2000-03-30 13:27:11 +04:00
char *cp;
union tbpos *u;
1993-03-21 12:45:37 +03:00
{
1996-02-04 05:15:01 +03:00
struct hitpos *pos = &u->hitpos;
1993-03-21 12:45:37 +03:00
char byte;
byte = *cp++;
1996-02-04 05:15:01 +03:00
pos->xpos = (byte & 03) << 14;
pos->xpos |= *cp++ << 7;
pos->xpos |= *cp++;
pos->ypos = *cp++ << 14;
pos->ypos |= *cp++ << 7;
pos->ypos |= *cp++;
pos->status = (byte >> 2) | TBINPROX;
if (tc->tbc_flags&TBF_INPROX && pos->status&020)
pos->status &= ~(020|TBINPROX);
pos->scount++;
1993-03-21 12:45:37 +03:00
}
/*
* Polhemus decode.
*/
1996-02-04 05:15:01 +03:00
static void
poldecode(tc, cp, u)
1993-03-21 12:45:37 +03:00
struct tbconf *tc;
2000-03-30 13:27:11 +04:00
char *cp;
union tbpos *u;
1993-03-21 12:45:37 +03:00
{
1996-02-04 05:15:01 +03:00
struct polpos *pos = &u->polpos;
pos->p_x = cp[4] | cp[3]<<7 | (cp[9] & 0x03) << 14;
pos->p_y = cp[6] | cp[5]<<7 | (cp[9] & 0x0c) << 12;
pos->p_z = cp[8] | cp[7]<<7 | (cp[9] & 0x30) << 10;
pos->p_azi = cp[11] | cp[10]<<7 | (cp[16] & 0x03) << 14;
pos->p_pit = cp[13] | cp[12]<<7 | (cp[16] & 0x0c) << 12;
pos->p_rol = cp[15] | cp[14]<<7 | (cp[16] & 0x30) << 10;
pos->p_stat = cp[1] | cp[0]<<7;
1993-03-21 12:45:37 +03:00
if (cp[2] != ' ')
1996-02-04 05:15:01 +03:00
pos->p_key = cp[2];
1993-03-21 12:45:37 +03:00
}
/*ARGSUSED*/
1996-02-04 05:15:01 +03:00
int
tbtioctl(tp, cmd, data, flag, p)
1993-03-21 12:45:37 +03:00
struct tty *tp;
u_long cmd;
1993-03-21 12:45:37 +03:00
caddr_t data;
int flag;
struct proc *p;
1993-03-21 12:45:37 +03:00
{
2000-03-30 13:27:11 +04:00
struct tb *tbp = (struct tb *)tp->t_sc;
1993-03-21 12:45:37 +03:00
switch (cmd) {
case BIOGMODE:
*(int *)data = tbp->tbflags & TBMODE;
break;
case BIOSTYPE:
if (tbconf[*(int *)data & TBTYPE].tbc_recsize == 0 ||
tbconf[*(int *)data & TBTYPE].tbc_decode == 0)
return (EINVAL);
tbp->tbflags &= ~TBTYPE;
tbp->tbflags |= *(int *)data & TBTYPE;
/* fall thru... to set mode bits */
case BIOSMODE: {
2000-03-30 13:27:11 +04:00
struct tbconf *tc;
1994-12-13 23:03:13 +03:00
u_char *c;
1993-03-21 12:45:37 +03:00
tbp->tbflags &= ~TBMODE;
tbp->tbflags |= *(int *)data & TBMODE;
tc = &tbconf[tbp->tbflags & TBTYPE];
1994-12-13 23:03:13 +03:00
if (tbp->tbflags & TBSTOP) {
1993-03-21 12:45:37 +03:00
if (tc->tbc_stop)
for (c = tc->tbc_stop; *c != '\0'; c++)
1994-12-13 23:03:13 +03:00
ttyoutput(*c, tp);
1993-03-21 12:45:37 +03:00
} else if (tc->tbc_start)
for (c = tc->tbc_start; *c != '\0'; c++)
1994-12-13 23:03:13 +03:00
ttyoutput(*c, tp);
if (tbp->tbflags & TBPOINT) {
1993-03-21 12:45:37 +03:00
if (tc->tbc_point)
for (c = tc->tbc_point; *c != '\0'; c++)
1994-12-13 23:03:13 +03:00
ttyoutput(*c, tp);
1993-03-21 12:45:37 +03:00
} else if (tc->tbc_run)
for (c = tc->tbc_run; *c != '\0'; c++)
1994-12-13 23:03:13 +03:00
ttyoutput(*c, tp);
1993-03-21 12:45:37 +03:00
ttstart(tp);
break;
}
case BIOGTYPE:
*(int *)data = tbp->tbflags & TBTYPE;
break;
case TIOCSETD:
case TIOCGETD:
case TIOCGETP:
case TIOCGETC:
return (-1); /* pass thru... */
default:
return (ENOTTY);
}
return (0);
}
1996-02-04 05:15:01 +03:00
void
tbattach(dummy)
int dummy;
{
/* stub to handle side effect of new config */
}