Annotation of Net2/kern/tty_tb.c, revision 1.1.1.2

1.1       root        1: /*-
                      2:  * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. All advertising materials mentioning features or use of this software
                     14:  *    must display the following acknowledgement:
                     15:  *     This product includes software developed by the University of
                     16:  *     California, Berkeley and its contributors.
                     17:  * 4. Neither the name of the University nor the names of its contributors
                     18:  *    may be used to endorse or promote products derived from this software
                     19:  *    without specific prior written permission.
                     20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     22:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     23:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     25:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     31:  * SUCH DAMAGE.
                     32:  *
                     33:  *     @(#)tty_tb.c    7.7 (Berkeley) 5/9/91
                     34:  */
                     35: 
                     36: #include "tb.h"
                     37: #if NTB > 0
                     38: 
                     39: /*
                     40:  * Line discipline for RS232 tablets;
                     41:  * supplies binary coordinate data.
                     42:  */
1.1.1.2 ! root       43: 
1.1       root       44: #include "param.h"
1.1.1.2 ! root       45: #include "ioctl.h"
1.1       root       46: #include "tablet.h"
                     47: #include "tty.h"
                     48: 
                     49: /*
                     50:  * Tablet configuration table.
                     51:  */
                     52: struct tbconf {
                     53:        short   tbc_recsize;    /* input record size in bytes */
                     54:        short   tbc_uiosize;    /* size of data record returned user */
                     55:        int     tbc_sync;       /* mask for finding sync byte/bit */
                     56:        int     (*tbc_decode)();/* decoding routine */
                     57:        char    *tbc_run;       /* enter run mode sequence */
                     58:        char    *tbc_point;     /* enter point mode sequence */
                     59:        char    *tbc_stop;      /* stop sequence */
                     60:        char    *tbc_start;     /* start/restart sequence */
                     61:        int     tbc_flags;
                     62: #define        TBF_POL         0x1     /* polhemus hack */
                     63: #define        TBF_INPROX      0x2     /* tablet has proximity info */
                     64: };
                     65: 
                     66: static int tbdecode(), gtcodecode(), poldecode();
                     67: static int tblresdecode(), tbhresdecode();
                     68: 
                     69: struct tbconf tbconf[TBTYPE] = {
                     70: { 0 },
                     71: { 5, sizeof (struct tbpos), 0200, tbdecode, "6", "4" },
                     72: { 5, sizeof (struct tbpos), 0200, tbdecode, "\1CN", "\1RT", "\2", "\4" },
                     73: { 8, sizeof (struct gtcopos), 0200, gtcodecode },
                     74: {17, sizeof (struct polpos), 0200, poldecode, 0, 0, "\21", "\5\22\2\23",
                     75:   TBF_POL },
                     76: { 5, sizeof (struct tbpos), 0100, tblresdecode, "\1CN", "\1PT", "\2", "\4",
                     77:   TBF_INPROX },
                     78: { 6, sizeof (struct tbpos), 0200, tbhresdecode, "\1CN", "\1PT", "\2", "\4",
                     79:   TBF_INPROX },
                     80: { 5, sizeof (struct tbpos), 0100, tblresdecode, "\1CL\33", "\1PT\33", 0, 0},
                     81: { 6, sizeof (struct tbpos), 0200, tbhresdecode, "\1CL\33", "\1PT\33", 0, 0},
                     82: };
                     83: 
                     84: /*
                     85:  * Tablet state
                     86:  */
                     87: struct tb {
                     88:        int     tbflags;                /* mode & type bits */
                     89: #define        TBMAXREC        17      /* max input record size */
                     90:        char    cbuf[TBMAXREC];         /* input buffer */
                     91:        union {
                     92:                struct  tbpos tbpos;
                     93:                struct  gtcopos gtcopos;
                     94:                struct  polpos polpos;
                     95:        } rets;                         /* processed state */
                     96: #define NTBS   16
                     97: } tb[NTBS];
                     98: 
                     99: /*
                    100:  * Open as tablet discipline; called on discipline change.
                    101:  */
                    102: /*ARGSUSED*/
                    103: tbopen(dev, tp)
                    104:        dev_t dev;
                    105:        register struct tty *tp;
                    106: {
                    107:        register struct tb *tbp;
                    108: 
                    109:        if (tp->t_line == TABLDISC)
                    110:                return (ENODEV);
                    111:        ttywflush(tp);
                    112:        for (tbp = tb; tbp < &tb[NTBS]; tbp++)
                    113:                if (tbp->tbflags == 0)
                    114:                        break;
                    115:        if (tbp >= &tb[NTBS])
                    116:                return (EBUSY);
                    117:        tbp->tbflags = TBTIGER|TBPOINT;         /* default */
                    118:        tp->t_cp = tbp->cbuf;
                    119:        tp->t_inbuf = 0;
                    120:        bzero((caddr_t)&tbp->rets, sizeof (tbp->rets));
                    121:        tp->T_LINEP = (caddr_t)tbp;
                    122:        tp->t_flags |= LITOUT;
                    123:        return (0);
                    124: }
                    125: 
                    126: /*
                    127:  * Line discipline change or last device close.
                    128:  */
                    129: tbclose(tp)
                    130:        register struct tty *tp;
                    131: {
                    132:        register int s;
                    133:        int modebits = TBPOINT|TBSTOP;
                    134: 
                    135:        tbioctl(tp, BIOSMODE, &modebits, 0);
                    136:        s = spltty();
                    137:        ((struct tb *)tp->T_LINEP)->tbflags = 0;
                    138:        tp->t_cp = 0;
                    139:        tp->t_inbuf = 0;
                    140:        tp->t_rawq.c_cc = 0;            /* clear queues -- paranoid */
                    141:        tp->t_canq.c_cc = 0;
                    142:        tp->t_line = 0;                 /* paranoid: avoid races */
                    143:        splx(s);
                    144: }
                    145: 
                    146: /*
                    147:  * Read from a tablet line.
                    148:  * Characters have been buffered in a buffer and decoded.
                    149:  */
                    150: tbread(tp, uio)
                    151:        register struct tty *tp;
                    152:        struct uio *uio;
                    153: {
                    154:        register struct tb *tbp = (struct tb *)tp->T_LINEP;
                    155:        register struct tbconf *tc = &tbconf[tbp->tbflags & TBTYPE];
                    156:        int ret;
                    157: 
                    158:        if ((tp->t_state&TS_CARR_ON) == 0)
                    159:                return (EIO);
                    160:        ret = uiomove(&tbp->rets, tc->tbc_uiosize, uio);
                    161:        if (tc->tbc_flags&TBF_POL)
                    162:                tbp->rets.polpos.p_key = ' ';
                    163:        return (ret);
                    164: }
                    165: 
                    166: /*
                    167:  * Low level character input routine.
                    168:  * Stuff the character in the buffer, and decode
                    169:  * if all the chars are there.
                    170:  *
                    171:  * This routine could be expanded in-line in the receiver
                    172:  * interrupt routine to make it run as fast as possible.
                    173:  */
                    174: tbinput(c, tp)
                    175:        register int c;
                    176:        register struct tty *tp;
                    177: {
                    178:        register struct tb *tbp = (struct tb *)tp->T_LINEP;
                    179:        register struct tbconf *tc = &tbconf[tbp->tbflags & TBTYPE];
                    180: 
                    181:        if (tc->tbc_recsize == 0 || tc->tbc_decode == 0)        /* paranoid? */
                    182:                return;
                    183:        /*
                    184:         * Locate sync bit/byte or reset input buffer.
                    185:         */
                    186:        if (c&tc->tbc_sync || tp->t_inbuf == tc->tbc_recsize) {
                    187:                tp->t_cp = tbp->cbuf;
                    188:                tp->t_inbuf = 0;
                    189:        }
                    190:        *tp->t_cp++ = c&0177;
                    191:        /*
                    192:         * Call decode routine only if a full record has been collected.
                    193:         */
                    194:        if (++tp->t_inbuf == tc->tbc_recsize)
                    195:                (*tc->tbc_decode)(tc, tbp->cbuf, &tbp->rets);
                    196: }
                    197: 
                    198: /*
                    199:  * Decode GTCO 8 byte format (high res, tilt, and pressure).
                    200:  */
                    201: static
                    202: gtcodecode(tc, cp, tbpos)
                    203:        struct tbconf *tc;
                    204:        register char *cp;
                    205:        register struct gtcopos *tbpos;
                    206: {
                    207: 
                    208:        tbpos->pressure = *cp >> 2;
                    209:        tbpos->status = (tbpos->pressure > 16) | TBINPROX; /* half way down */
                    210:        tbpos->xpos = (*cp++ & 03) << 14;
                    211:        tbpos->xpos |= *cp++ << 7;
                    212:        tbpos->xpos |= *cp++;
                    213:        tbpos->ypos = (*cp++ & 03) << 14;
                    214:        tbpos->ypos |= *cp++ << 7;
                    215:        tbpos->ypos |= *cp++;
                    216:        tbpos->xtilt = *cp++;
                    217:        tbpos->ytilt = *cp++;
                    218:        tbpos->scount++;
                    219: }
                    220: 
                    221: /*
                    222:  * Decode old Hitachi 5 byte format (low res).
                    223:  */
                    224: static
                    225: tbdecode(tc, cp, tbpos)
                    226:        struct tbconf *tc;
                    227:        register char *cp;
                    228:        register struct tbpos *tbpos;
                    229: {
                    230:        register char byte;
                    231: 
                    232:        byte = *cp++;
                    233:        tbpos->status = (byte&0100) ? TBINPROX : 0;
                    234:        byte &= ~0100;
                    235:        if (byte > 036)
                    236:                tbpos->status |= 1 << ((byte-040)/2);
                    237:        tbpos->xpos = *cp++ << 7;
                    238:        tbpos->xpos |= *cp++;
                    239:        if (tbpos->xpos < 256)                  /* tablet wraps around at 256 */
                    240:                tbpos->status &= ~TBINPROX;     /* make it out of proximity */
                    241:        tbpos->ypos = *cp++ << 7;
                    242:        tbpos->ypos |= *cp++;
                    243:        tbpos->scount++;
                    244: }
                    245: 
                    246: /*
                    247:  * Decode new Hitach 5-byte format (low res).
                    248:  */
                    249: static
                    250: tblresdecode(tc, cp, tbpos)
                    251:        struct tbconf *tc;
                    252:        register char *cp;
                    253:        register struct tbpos *tbpos;
                    254: {
                    255: 
                    256:        *cp &= ~0100;           /* mask sync bit */
                    257:        tbpos->status = (*cp++ >> 2) | TBINPROX;
                    258:        if (tc->tbc_flags&TBF_INPROX && tbpos->status&020)
                    259:                tbpos->status &= ~(020|TBINPROX);
                    260:        tbpos->xpos = *cp++;
                    261:        tbpos->xpos |= *cp++ << 6;
                    262:        tbpos->ypos = *cp++;
                    263:        tbpos->ypos |= *cp++ << 6;
                    264:        tbpos->scount++;
                    265: }
                    266: 
                    267: /*
                    268:  * Decode new Hitach 6-byte format (high res).
                    269:  */
                    270: static
                    271: tbhresdecode(tc, cp, tbpos)
                    272:        struct tbconf *tc;
                    273:        register char *cp;
                    274:        register struct tbpos *tbpos;
                    275: {
                    276:        char byte;
                    277: 
                    278:        byte = *cp++;
                    279:        tbpos->xpos = (byte & 03) << 14;
                    280:        tbpos->xpos |= *cp++ << 7;
                    281:        tbpos->xpos |= *cp++;
                    282:        tbpos->ypos = *cp++ << 14;
                    283:        tbpos->ypos |= *cp++ << 7;
                    284:        tbpos->ypos |= *cp++;
                    285:        tbpos->status = (byte >> 2) | TBINPROX;
                    286:        if (tc->tbc_flags&TBF_INPROX && tbpos->status&020)
                    287:                tbpos->status &= ~(020|TBINPROX);
                    288:        tbpos->scount++;
                    289: }
                    290: 
                    291: /*
                    292:  * Polhemus decode.
                    293:  */
                    294: static
                    295: poldecode(tc, cp, polpos)
                    296:        struct tbconf *tc;
                    297:        register char *cp;
                    298:        register struct polpos *polpos;
                    299: {
                    300: 
                    301:        polpos->p_x = cp[4] | cp[3]<<7 | (cp[9] & 0x03) << 14;
                    302:        polpos->p_y = cp[6] | cp[5]<<7 | (cp[9] & 0x0c) << 12;
                    303:        polpos->p_z = cp[8] | cp[7]<<7 | (cp[9] & 0x30) << 10;
                    304:        polpos->p_azi = cp[11] | cp[10]<<7 | (cp[16] & 0x03) << 14;
                    305:        polpos->p_pit = cp[13] | cp[12]<<7 | (cp[16] & 0x0c) << 12;
                    306:        polpos->p_rol = cp[15] | cp[14]<<7 | (cp[16] & 0x30) << 10;
                    307:        polpos->p_stat = cp[1] | cp[0]<<7;
                    308:        if (cp[2] != ' ')
                    309:                polpos->p_key = cp[2];
                    310: }
                    311: 
                    312: /*ARGSUSED*/
                    313: tbioctl(tp, cmd, data, flag)
                    314:        struct tty *tp;
                    315:        caddr_t data;
                    316: {
                    317:        register struct tb *tbp = (struct tb *)tp->T_LINEP;
                    318: 
                    319:        switch (cmd) {
                    320: 
                    321:        case BIOGMODE:
                    322:                *(int *)data = tbp->tbflags & TBMODE;
                    323:                break;
                    324: 
                    325:        case BIOSTYPE:
                    326:                if (tbconf[*(int *)data & TBTYPE].tbc_recsize == 0 ||
                    327:                    tbconf[*(int *)data & TBTYPE].tbc_decode == 0)
                    328:                        return (EINVAL);
                    329:                tbp->tbflags &= ~TBTYPE;
                    330:                tbp->tbflags |= *(int *)data & TBTYPE;
                    331:                /* fall thru... to set mode bits */
                    332: 
                    333:        case BIOSMODE: {
                    334:                register struct tbconf *tc;
                    335: 
                    336:                tbp->tbflags &= ~TBMODE;
                    337:                tbp->tbflags |= *(int *)data & TBMODE;
                    338:                tc = &tbconf[tbp->tbflags & TBTYPE];
                    339:                if (tbp->tbflags&TBSTOP) {
                    340:                        if (tc->tbc_stop)
                    341:                                ttyout(tc->tbc_stop, tp);
                    342:                } else if (tc->tbc_start)
                    343:                        ttyout(tc->tbc_start, tp);
                    344:                if (tbp->tbflags&TBPOINT) {
                    345:                        if (tc->tbc_point)
                    346:                                ttyout(tc->tbc_point, tp);
                    347:                } else if (tc->tbc_run)
                    348:                        ttyout(tc->tbc_run, tp);
                    349:                ttstart(tp);
                    350:                break;
                    351:        }
                    352: 
                    353:        case BIOGTYPE:
                    354:                *(int *)data = tbp->tbflags & TBTYPE;
                    355:                break;
                    356: 
                    357:        case TIOCSETD:
                    358:        case TIOCGETD:
                    359:        case TIOCGETP:
                    360:        case TIOCGETC:
                    361:                return (-1);            /* pass thru... */
                    362: 
                    363:        default:
                    364:                return (ENOTTY);
                    365:        }
                    366:        return (0);
                    367: }
                    368: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.