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