Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
0880b18e | 2 | * Copyright (c) 1982, 1986 Regents of the University of California. |
da7c5cc6 KM |
3 | * All rights reserved. The Berkeley software License Agreement |
4 | * specifies the terms and conditions for redistribution. | |
5 | * | |
adc2dce6 | 6 | * @(#)tty_tb.c 7.6 (Berkeley) %G% |
da7c5cc6 | 7 | */ |
4f9a21f0 SL |
8 | |
9 | #include "tb.h" | |
10 | #if NTB > 0 | |
11 | ||
a5fcb39b SL |
12 | /* |
13 | * Line discipline for RS232 tablets; | |
14 | * supplies binary coordinate data. | |
15 | */ | |
94368568 | 16 | #include "param.h" |
a5fcb39b | 17 | #include "tablet.h" |
94368568 | 18 | #include "tty.h" |
4f9a21f0 SL |
19 | |
20 | /* | |
a5fcb39b | 21 | * Tablet configuration table. |
4f9a21f0 | 22 | */ |
a5fcb39b SL |
23 | struct tbconf { |
24 | short tbc_recsize; /* input record size in bytes */ | |
25 | short tbc_uiosize; /* size of data record returned user */ | |
26 | int tbc_sync; /* mask for finding sync byte/bit */ | |
27 | int (*tbc_decode)();/* decoding routine */ | |
28 | char *tbc_run; /* enter run mode sequence */ | |
29 | char *tbc_point; /* enter point mode sequence */ | |
30 | char *tbc_stop; /* stop sequence */ | |
31 | char *tbc_start; /* start/restart sequence */ | |
32 | int tbc_flags; | |
729ea429 SL |
33 | #define TBF_POL 0x1 /* polhemus hack */ |
34 | #define TBF_INPROX 0x2 /* tablet has proximity info */ | |
a5fcb39b | 35 | }; |
4f9a21f0 | 36 | |
a5fcb39b SL |
37 | static int tbdecode(), gtcodecode(), poldecode(); |
38 | static int tblresdecode(), tbhresdecode(); | |
13b9086b | 39 | |
a5fcb39b SL |
40 | struct tbconf tbconf[TBTYPE] = { |
41 | { 0 }, | |
42 | { 5, sizeof (struct tbpos), 0200, tbdecode, "6", "4" }, | |
43 | { 5, sizeof (struct tbpos), 0200, tbdecode, "\1CN", "\1RT", "\2", "\4" }, | |
44 | { 8, sizeof (struct gtcopos), 0200, gtcodecode }, | |
45 | {17, sizeof (struct polpos), 0200, poldecode, 0, 0, "\21", "\5\22\2\23", | |
729ea429 SL |
46 | TBF_POL }, |
47 | { 5, sizeof (struct tbpos), 0100, tblresdecode, "\1CN", "\1PT", "\2", "\4", | |
48 | TBF_INPROX }, | |
49 | { 6, sizeof (struct tbpos), 0200, tbhresdecode, "\1CN", "\1PT", "\2", "\4", | |
50 | TBF_INPROX }, | |
51 | { 5, sizeof (struct tbpos), 0100, tblresdecode, "\1CL\33", "\1PT\33", 0, 0}, | |
52 | { 6, sizeof (struct tbpos), 0200, tbhresdecode, "\1CL\33", "\1PT\33", 0, 0}, | |
a5fcb39b SL |
53 | }; |
54 | ||
55 | /* | |
56 | * Tablet state | |
57 | */ | |
13b9086b | 58 | struct tb { |
a5fcb39b SL |
59 | int tbflags; /* mode & type bits */ |
60 | #define TBMAXREC 17 /* max input record size */ | |
61 | char cbuf[TBMAXREC]; /* input buffer */ | |
62 | union { | |
63 | struct tbpos tbpos; | |
64 | struct gtcopos gtcopos; | |
65 | struct polpos polpos; | |
66 | } rets; /* processed state */ | |
67 | #define NTBS 16 | |
13b9086b | 68 | } tb[NTBS]; |
4f9a21f0 SL |
69 | |
70 | /* | |
a5fcb39b | 71 | * Open as tablet discipline; called on discipline change. |
4f9a21f0 SL |
72 | */ |
73 | /*ARGSUSED*/ | |
74 | tbopen(dev, tp) | |
942f05a9 SL |
75 | dev_t dev; |
76 | register struct tty *tp; | |
4f9a21f0 | 77 | { |
13b9086b | 78 | register struct tb *tbp; |
4f9a21f0 | 79 | |
a5fcb39b | 80 | if (tp->t_line == TABLDISC) |
13b9086b | 81 | return (ENODEV); |
88a7a62a | 82 | ttywflush(tp); |
13b9086b | 83 | for (tbp = tb; tbp < &tb[NTBS]; tbp++) |
a5fcb39b | 84 | if (tbp->tbflags == 0) |
13b9086b SL |
85 | break; |
86 | if (tbp >= &tb[NTBS]) | |
87 | return (EBUSY); | |
a5fcb39b | 88 | tbp->tbflags = TBTIGER|TBPOINT; /* default */ |
13b9086b | 89 | tp->t_cp = tbp->cbuf; |
4f9a21f0 | 90 | tp->t_inbuf = 0; |
a5fcb39b SL |
91 | bzero((caddr_t)&tbp->rets, sizeof (tbp->rets)); |
92 | tp->T_LINEP = (caddr_t)tbp; | |
93 | tp->t_flags |= LITOUT; | |
830bbc16 | 94 | return (0); |
4f9a21f0 SL |
95 | } |
96 | ||
97 | /* | |
a5fcb39b | 98 | * Line discipline change or last device close. |
4f9a21f0 SL |
99 | */ |
100 | tbclose(tp) | |
830bbc16 | 101 | register struct tty *tp; |
4f9a21f0 | 102 | { |
a5fcb39b SL |
103 | register int s; |
104 | int modebits = TBPOINT|TBSTOP; | |
4f9a21f0 | 105 | |
a5fcb39b | 106 | tbioctl(tp, BIOSMODE, &modebits, 0); |
fb1db32c | 107 | s = spltty(); |
a5fcb39b | 108 | ((struct tb *)tp->T_LINEP)->tbflags = 0; |
4f9a21f0 SL |
109 | tp->t_cp = 0; |
110 | tp->t_inbuf = 0; | |
111 | tp->t_rawq.c_cc = 0; /* clear queues -- paranoid */ | |
112 | tp->t_canq.c_cc = 0; | |
4a5e6198 | 113 | tp->t_line = 0; /* paranoid: avoid races */ |
4f9a21f0 SL |
114 | splx(s); |
115 | } | |
116 | ||
117 | /* | |
118 | * Read from a tablet line. | |
a5fcb39b | 119 | * Characters have been buffered in a buffer and decoded. |
4f9a21f0 | 120 | */ |
740e4029 BJ |
121 | tbread(tp, uio) |
122 | register struct tty *tp; | |
123 | struct uio *uio; | |
4f9a21f0 | 124 | { |
a5fcb39b SL |
125 | register struct tb *tbp = (struct tb *)tp->T_LINEP; |
126 | register struct tbconf *tc = &tbconf[tbp->tbflags & TBTYPE]; | |
127 | int ret; | |
4f9a21f0 | 128 | |
a5fcb39b | 129 | if ((tp->t_state&TS_CARR_ON) == 0) |
840510a3 | 130 | return (EIO); |
c4ec2128 | 131 | ret = uiomove(&tbp->rets, tc->tbc_uiosize, uio); |
a5fcb39b SL |
132 | if (tc->tbc_flags&TBF_POL) |
133 | tbp->rets.polpos.p_key = ' '; | |
134 | return (ret); | |
4f9a21f0 SL |
135 | } |
136 | ||
137 | /* | |
138 | * Low level character input routine. | |
a5fcb39b | 139 | * Stuff the character in the buffer, and decode |
4f9a21f0 SL |
140 | * if all the chars are there. |
141 | * | |
142 | * This routine could be expanded in-line in the receiver | |
a5fcb39b | 143 | * interrupt routine to make it run as fast as possible. |
4f9a21f0 | 144 | */ |
4f9a21f0 | 145 | tbinput(c, tp) |
840510a3 BJ |
146 | register int c; |
147 | register struct tty *tp; | |
4f9a21f0 | 148 | { |
a5fcb39b SL |
149 | register struct tb *tbp = (struct tb *)tp->T_LINEP; |
150 | register struct tbconf *tc = &tbconf[tbp->tbflags & TBTYPE]; | |
151 | ||
152 | if (tc->tbc_recsize == 0 || tc->tbc_decode == 0) /* paranoid? */ | |
153 | return; | |
154 | /* | |
155 | * Locate sync bit/byte or reset input buffer. | |
156 | */ | |
157 | if (c&tc->tbc_sync || tp->t_inbuf == tc->tbc_recsize) { | |
158 | tp->t_cp = tbp->cbuf; | |
159 | tp->t_inbuf = 0; | |
4f9a21f0 | 160 | } |
a5fcb39b SL |
161 | *tp->t_cp++ = c&0177; |
162 | /* | |
163 | * Call decode routine only if a full record has been collected. | |
164 | */ | |
165 | if (++tp->t_inbuf == tc->tbc_recsize) | |
729ea429 | 166 | (*tc->tbc_decode)(tc, tbp->cbuf, &tbp->rets); |
4f9a21f0 SL |
167 | } |
168 | ||
169 | /* | |
a5fcb39b | 170 | * Decode GTCO 8 byte format (high res, tilt, and pressure). |
4f9a21f0 | 171 | */ |
a5fcb39b | 172 | static |
729ea429 SL |
173 | gtcodecode(tc, cp, tbpos) |
174 | struct tbconf *tc; | |
4f9a21f0 | 175 | register char *cp; |
a5fcb39b | 176 | register struct gtcopos *tbpos; |
4f9a21f0 SL |
177 | { |
178 | ||
a5fcb39b SL |
179 | tbpos->pressure = *cp >> 2; |
180 | tbpos->status = (tbpos->pressure > 16) | TBINPROX; /* half way down */ | |
181 | tbpos->xpos = (*cp++ & 03) << 14; | |
182 | tbpos->xpos |= *cp++ << 7; | |
183 | tbpos->xpos |= *cp++; | |
184 | tbpos->ypos = (*cp++ & 03) << 14; | |
185 | tbpos->ypos |= *cp++ << 7; | |
186 | tbpos->ypos |= *cp++; | |
187 | tbpos->xtilt = *cp++; | |
188 | tbpos->ytilt = *cp++; | |
13b9086b | 189 | tbpos->scount++; |
4f9a21f0 SL |
190 | } |
191 | ||
192 | /* | |
a5fcb39b | 193 | * Decode old Hitachi 5 byte format (low res). |
4f9a21f0 | 194 | */ |
a5fcb39b | 195 | static |
729ea429 SL |
196 | tbdecode(tc, cp, tbpos) |
197 | struct tbconf *tc; | |
4f9a21f0 | 198 | register char *cp; |
13b9086b | 199 | register struct tbpos *tbpos; |
4f9a21f0 | 200 | { |
4f9a21f0 SL |
201 | register char byte; |
202 | ||
203 | byte = *cp++; | |
a5fcb39b | 204 | tbpos->status = (byte&0100) ? TBINPROX : 0; |
4f9a21f0 | 205 | byte &= ~0100; |
840510a3 | 206 | if (byte > 036) |
a5fcb39b SL |
207 | tbpos->status |= 1 << ((byte-040)/2); |
208 | tbpos->xpos = *cp++ << 7; | |
209 | tbpos->xpos |= *cp++; | |
210 | if (tbpos->xpos < 256) /* tablet wraps around at 256 */ | |
211 | tbpos->status &= ~TBINPROX; /* make it out of proximity */ | |
212 | tbpos->ypos = *cp++ << 7; | |
213 | tbpos->ypos |= *cp++; | |
214 | tbpos->scount++; | |
215 | } | |
216 | ||
217 | /* | |
218 | * Decode new Hitach 5-byte format (low res). | |
219 | */ | |
220 | static | |
729ea429 SL |
221 | tblresdecode(tc, cp, tbpos) |
222 | struct tbconf *tc; | |
a5fcb39b SL |
223 | register char *cp; |
224 | register struct tbpos *tbpos; | |
225 | { | |
226 | ||
227 | *cp &= ~0100; /* mask sync bit */ | |
228 | tbpos->status = (*cp++ >> 2) | TBINPROX; | |
729ea429 SL |
229 | if (tc->tbc_flags&TBF_INPROX && tbpos->status&020) |
230 | tbpos->status &= ~(020|TBINPROX); | |
a5fcb39b SL |
231 | tbpos->xpos = *cp++; |
232 | tbpos->xpos |= *cp++ << 6; | |
233 | tbpos->ypos = *cp++; | |
234 | tbpos->ypos |= *cp++ << 6; | |
235 | tbpos->scount++; | |
236 | } | |
237 | ||
238 | /* | |
239 | * Decode new Hitach 6-byte format (high res). | |
240 | */ | |
241 | static | |
729ea429 SL |
242 | tbhresdecode(tc, cp, tbpos) |
243 | struct tbconf *tc; | |
a5fcb39b SL |
244 | register char *cp; |
245 | register struct tbpos *tbpos; | |
246 | { | |
247 | char byte; | |
248 | ||
249 | byte = *cp++; | |
250 | tbpos->xpos = (byte & 03) << 14; | |
251 | tbpos->xpos |= *cp++ << 7; | |
252 | tbpos->xpos |= *cp++; | |
253 | tbpos->ypos = *cp++ << 14; | |
254 | tbpos->ypos |= *cp++ << 7; | |
255 | tbpos->ypos |= *cp++; | |
256 | tbpos->status = (byte >> 2) | TBINPROX; | |
729ea429 SL |
257 | if (tc->tbc_flags&TBF_INPROX && tbpos->status&020) |
258 | tbpos->status &= ~(020|TBINPROX); | |
13b9086b | 259 | tbpos->scount++; |
4f9a21f0 SL |
260 | } |
261 | ||
262 | /* | |
a5fcb39b | 263 | * Polhemus decode. |
4f9a21f0 | 264 | */ |
a5fcb39b | 265 | static |
729ea429 SL |
266 | poldecode(tc, cp, polpos) |
267 | struct tbconf *tc; | |
a5fcb39b SL |
268 | register char *cp; |
269 | register struct polpos *polpos; | |
270 | { | |
271 | ||
272 | polpos->p_x = cp[4] | cp[3]<<7 | (cp[9] & 0x03) << 14; | |
273 | polpos->p_y = cp[6] | cp[5]<<7 | (cp[9] & 0x0c) << 12; | |
274 | polpos->p_z = cp[8] | cp[7]<<7 | (cp[9] & 0x30) << 10; | |
275 | polpos->p_azi = cp[11] | cp[10]<<7 | (cp[16] & 0x03) << 14; | |
276 | polpos->p_pit = cp[13] | cp[12]<<7 | (cp[16] & 0x0c) << 12; | |
277 | polpos->p_rol = cp[15] | cp[14]<<7 | (cp[16] & 0x30) << 10; | |
278 | polpos->p_stat = cp[1] | cp[0]<<7; | |
279 | if (cp[2] != ' ') | |
280 | polpos->p_key = cp[2]; | |
281 | } | |
282 | ||
4f9a21f0 | 283 | /*ARGSUSED*/ |
942f05a9 SL |
284 | tbioctl(tp, cmd, data, flag) |
285 | struct tty *tp; | |
286 | caddr_t data; | |
4f9a21f0 | 287 | { |
a5fcb39b | 288 | register struct tb *tbp = (struct tb *)tp->T_LINEP; |
4f9a21f0 | 289 | |
4f9a21f0 SL |
290 | switch (cmd) { |
291 | ||
a5fcb39b SL |
292 | case BIOGMODE: |
293 | *(int *)data = tbp->tbflags & TBMODE; | |
294 | break; | |
295 | ||
296 | case BIOSTYPE: | |
297 | if (tbconf[*(int *)data & TBTYPE].tbc_recsize == 0 || | |
298 | tbconf[*(int *)data & TBTYPE].tbc_decode == 0) | |
299 | return (EINVAL); | |
300 | tbp->tbflags &= ~TBTYPE; | |
301 | tbp->tbflags |= *(int *)data & TBTYPE; | |
302 | /* fall thru... to set mode bits */ | |
303 | ||
304 | case BIOSMODE: { | |
305 | register struct tbconf *tc; | |
306 | ||
307 | tbp->tbflags &= ~TBMODE; | |
308 | tbp->tbflags |= *(int *)data & TBMODE; | |
309 | tc = &tbconf[tbp->tbflags & TBTYPE]; | |
310 | if (tbp->tbflags&TBSTOP) { | |
311 | if (tc->tbc_stop) | |
312 | ttyout(tc->tbc_stop, tp); | |
313 | } else if (tc->tbc_start) | |
314 | ttyout(tc->tbc_start, tp); | |
315 | if (tbp->tbflags&TBPOINT) { | |
316 | if (tc->tbc_point) | |
317 | ttyout(tc->tbc_point, tp); | |
318 | } else if (tc->tbc_run) | |
319 | ttyout(tc->tbc_run, tp); | |
320 | ttstart(tp); | |
321 | break; | |
322 | } | |
323 | ||
324 | case BIOGTYPE: | |
325 | *(int *)data = tbp->tbflags & TBTYPE; | |
326 | break; | |
327 | ||
4f9a21f0 SL |
328 | case TIOCSETD: |
329 | case TIOCGETD: | |
330 | case TIOCGETP: | |
331 | case TIOCGETC: | |
a5fcb39b SL |
332 | return (-1); /* pass thru... */ |
333 | ||
334 | default: | |
335 | return (ENOTTY); | |
4f9a21f0 | 336 | } |
4f9a21f0 SL |
337 | return (0); |
338 | } | |
339 | #endif |