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