]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/tty_tb.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / bsd / kern / tty_tb.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
1c79356b
A
29 */
30/* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
31/*-
32 * Copyright (c) 1982, 1986, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * @(#)tty_tb.c 8.1 (Berkeley) 6/10/93
36 */
37
38#include "tb.h"
39#if NTB > 0
40
41/*
42 * Line discipline for RS232 tablets;
43 * supplies binary coordinate data.
44 */
45#include <sys/param.h>
46#include <sys/tablet.h>
47#include <sys/tty.h>
48#if NeXT
91447636 49#include <sys/proc_internal.h>
1c79356b
A
50#endif
51
52/*
53 * Tablet configuration table.
54 */
55struct tbconf {
56 short tbc_recsize; /* input record size in bytes */
57 short tbc_uiosize; /* size of data record returned user */
58 int tbc_sync; /* mask for finding sync byte/bit */
59 int (*tbc_decode)();/* decoding routine */
60 char *tbc_run; /* enter run mode sequence */
61 char *tbc_point; /* enter point mode sequence */
62 char *tbc_stop; /* stop sequence */
63 char *tbc_start; /* start/restart sequence */
64 int tbc_flags;
65#define TBF_POL 0x1 /* polhemus hack */
66#define TBF_INPROX 0x2 /* tablet has proximity info */
67};
68
69static int tbdecode(), gtcodecode(), poldecode();
70static int tblresdecode(), tbhresdecode();
71
72struct tbconf tbconf[TBTYPE] = {
73{ 0 },
74{ 5, sizeof (struct tbpos), 0200, tbdecode, "6", "4" },
75{ 5, sizeof (struct tbpos), 0200, tbdecode, "\1CN", "\1RT", "\2", "\4" },
76{ 8, sizeof (struct gtcopos), 0200, gtcodecode },
77{17, sizeof (struct polpos), 0200, poldecode, 0, 0, "\21", "\5\22\2\23",
78 TBF_POL },
79{ 5, sizeof (struct tbpos), 0100, tblresdecode, "\1CN", "\1PT", "\2", "\4",
80 TBF_INPROX },
81{ 6, sizeof (struct tbpos), 0200, tbhresdecode, "\1CN", "\1PT", "\2", "\4",
82 TBF_INPROX },
83{ 5, sizeof (struct tbpos), 0100, tblresdecode, "\1CL\33", "\1PT\33", 0, 0},
84{ 6, sizeof (struct tbpos), 0200, tbhresdecode, "\1CL\33", "\1PT\33", 0, 0},
85};
86
87/*
88 * Tablet state
89 */
90struct tb {
91 int tbflags; /* mode & type bits */
92#define TBMAXREC 17 /* max input record size */
93 char cbuf[TBMAXREC]; /* input buffer */
94 union {
95 struct tbpos tbpos;
96 struct gtcopos gtcopos;
97 struct polpos polpos;
98 } rets; /* processed state */
99#define NTBS 16
100} tb[NTBS];
101
102/*
103 * Open as tablet discipline; called on discipline change.
104 */
105/*ARGSUSED*/
106tbopen(dev, tp)
107 dev_t dev;
108 register struct tty *tp;
109{
110 register struct tb *tbp;
111
112 if (tp->t_line == TABLDISC)
113 return (ENODEV);
114 ttywflush(tp);
115 for (tbp = tb; tbp < &tb[NTBS]; tbp++)
116 if (tbp->tbflags == 0)
117 break;
118 if (tbp >= &tb[NTBS])
119 return (EBUSY);
120 tbp->tbflags = TBTIGER|TBPOINT; /* default */
121 tp->t_cp = tbp->cbuf;
122 tp->t_inbuf = 0;
123 bzero((caddr_t)&tbp->rets, sizeof (tbp->rets));
124 tp->T_LINEP = (caddr_t)tbp;
125 tp->t_flags |= LITOUT;
126 return (0);
127}
128
129/*
130 * Line discipline change or last device close.
131 */
132tbclose(tp)
133 register struct tty *tp;
134{
135 register int s;
136 int modebits = TBPOINT|TBSTOP;
137
138#ifndef NeXT
139 tbioctl(tp, BIOSMODE, &modebits, 0);
140#else
141 tbioctl(tp, BIOSMODE, &modebits, 0, current_proc());
142#endif
143 s = spltty();
144 ((struct tb *)tp->T_LINEP)->tbflags = 0;
145 tp->t_cp = 0;
146 tp->t_inbuf = 0;
147 tp->t_rawq.c_cc = 0; /* clear queues -- paranoid */
148 tp->t_canq.c_cc = 0;
149 tp->t_line = 0; /* paranoid: avoid races */
150 splx(s);
151}
152
153/*
154 * Read from a tablet line.
155 * Characters have been buffered in a buffer and decoded.
156 */
157tbread(tp, uio)
158 register struct tty *tp;
159 struct uio *uio;
160{
161 register struct tb *tbp = (struct tb *)tp->T_LINEP;
162 register struct tbconf *tc = &tbconf[tbp->tbflags & TBTYPE];
163 int ret;
164
165 if ((tp->t_state&TS_CARR_ON) == 0)
166 return (EIO);
167 ret = uiomove(&tbp->rets, tc->tbc_uiosize, uio);
168 if (tc->tbc_flags&TBF_POL)
169 tbp->rets.polpos.p_key = ' ';
170 return (ret);
171}
172
173/*
174 * Low level character input routine.
175 * Stuff the character in the buffer, and decode
176 * if all the chars are there.
177 *
178 * This routine could be expanded in-line in the receiver
179 * interrupt routine to make it run as fast as possible.
180 */
181tbinput(c, tp)
182 register int c;
183 register struct tty *tp;
184{
185 register struct tb *tbp = (struct tb *)tp->T_LINEP;
186 register struct tbconf *tc = &tbconf[tbp->tbflags & TBTYPE];
187
188 if (tc->tbc_recsize == 0 || tc->tbc_decode == 0) /* paranoid? */
189 return;
190 /*
191 * Locate sync bit/byte or reset input buffer.
192 */
193 if (c&tc->tbc_sync || tp->t_inbuf == tc->tbc_recsize) {
194 tp->t_cp = tbp->cbuf;
195 tp->t_inbuf = 0;
196 }
197 *tp->t_cp++ = c&0177;
198 /*
199 * Call decode routine only if a full record has been collected.
200 */
201 if (++tp->t_inbuf == tc->tbc_recsize)
202 (*tc->tbc_decode)(tc, tbp->cbuf, &tbp->rets);
203}
204
205/*
206 * Decode GTCO 8 byte format (high res, tilt, and pressure).
207 */
208static
209gtcodecode(tc, cp, tbpos)
210 struct tbconf *tc;
211 register char *cp;
212 register struct gtcopos *tbpos;
213{
214
215 tbpos->pressure = *cp >> 2;
216 tbpos->status = (tbpos->pressure > 16) | TBINPROX; /* half way down */
217 tbpos->xpos = (*cp++ & 03) << 14;
218 tbpos->xpos |= *cp++ << 7;
219 tbpos->xpos |= *cp++;
220 tbpos->ypos = (*cp++ & 03) << 14;
221 tbpos->ypos |= *cp++ << 7;
222 tbpos->ypos |= *cp++;
223 tbpos->xtilt = *cp++;
224 tbpos->ytilt = *cp++;
225 tbpos->scount++;
226}
227
228/*
229 * Decode old Hitachi 5 byte format (low res).
230 */
231static
232tbdecode(tc, cp, tbpos)
233 struct tbconf *tc;
234 register char *cp;
235 register struct tbpos *tbpos;
236{
237 register char byte;
238
239 byte = *cp++;
240 tbpos->status = (byte&0100) ? TBINPROX : 0;
241 byte &= ~0100;
242 if (byte > 036)
243 tbpos->status |= 1 << ((byte-040)/2);
244 tbpos->xpos = *cp++ << 7;
245 tbpos->xpos |= *cp++;
246 if (tbpos->xpos < 256) /* tablet wraps around at 256 */
247 tbpos->status &= ~TBINPROX; /* make it out of proximity */
248 tbpos->ypos = *cp++ << 7;
249 tbpos->ypos |= *cp++;
250 tbpos->scount++;
251}
252
253/*
254 * Decode new Hitach 5-byte format (low res).
255 */
256static
257tblresdecode(tc, cp, tbpos)
258 struct tbconf *tc;
259 register char *cp;
260 register struct tbpos *tbpos;
261{
262
263 *cp &= ~0100; /* mask sync bit */
264 tbpos->status = (*cp++ >> 2) | TBINPROX;
265 if (tc->tbc_flags&TBF_INPROX && tbpos->status&020)
266 tbpos->status &= ~(020|TBINPROX);
267 tbpos->xpos = *cp++;
268 tbpos->xpos |= *cp++ << 6;
269 tbpos->ypos = *cp++;
270 tbpos->ypos |= *cp++ << 6;
271 tbpos->scount++;
272}
273
274/*
275 * Decode new Hitach 6-byte format (high res).
276 */
277static
278tbhresdecode(tc, cp, tbpos)
279 struct tbconf *tc;
280 register char *cp;
281 register struct tbpos *tbpos;
282{
283 char byte;
284
285 byte = *cp++;
286 tbpos->xpos = (byte & 03) << 14;
287 tbpos->xpos |= *cp++ << 7;
288 tbpos->xpos |= *cp++;
289 tbpos->ypos = *cp++ << 14;
290 tbpos->ypos |= *cp++ << 7;
291 tbpos->ypos |= *cp++;
292 tbpos->status = (byte >> 2) | TBINPROX;
293 if (tc->tbc_flags&TBF_INPROX && tbpos->status&020)
294 tbpos->status &= ~(020|TBINPROX);
295 tbpos->scount++;
296}
297
298/*
299 * Polhemus decode.
300 */
301static
302poldecode(tc, cp, polpos)
303 struct tbconf *tc;
304 register char *cp;
305 register struct polpos *polpos;
306{
307
308 polpos->p_x = cp[4] | cp[3]<<7 | (cp[9] & 0x03) << 14;
309 polpos->p_y = cp[6] | cp[5]<<7 | (cp[9] & 0x0c) << 12;
310 polpos->p_z = cp[8] | cp[7]<<7 | (cp[9] & 0x30) << 10;
311 polpos->p_azi = cp[11] | cp[10]<<7 | (cp[16] & 0x03) << 14;
312 polpos->p_pit = cp[13] | cp[12]<<7 | (cp[16] & 0x0c) << 12;
313 polpos->p_rol = cp[15] | cp[14]<<7 | (cp[16] & 0x30) << 10;
314 polpos->p_stat = cp[1] | cp[0]<<7;
315 if (cp[2] != ' ')
316 polpos->p_key = cp[2];
317}
318
319/*ARGSUSED*/
320#ifndef NeXT
321tbioctl(tp, cmd, data, flag)
322 struct tty *tp;
323 caddr_t data;
324#else
325tbtioctl(tp, cmd, data, flag, p)
326 struct tty *tp;
327 u_long cmd;
328 caddr_t data;
329 int flag;
330 struct proc *p;
331#endif /* !NeXT */
332{
333 register struct tb *tbp = (struct tb *)tp->T_LINEP;
334
335 switch (cmd) {
336
337 case BIOGMODE:
338 *(int *)data = tbp->tbflags & TBMODE;
339 break;
340
341 case BIOSTYPE:
342 if (tbconf[*(int *)data & TBTYPE].tbc_recsize == 0 ||
343 tbconf[*(int *)data & TBTYPE].tbc_decode == 0)
344 return (EINVAL);
345 tbp->tbflags &= ~TBTYPE;
346 tbp->tbflags |= *(int *)data & TBTYPE;
347 /* fall thru... to set mode bits */
348
349 case BIOSMODE: {
350 register struct tbconf *tc;
351
352 tbp->tbflags &= ~TBMODE;
353 tbp->tbflags |= *(int *)data & TBMODE;
354 tc = &tbconf[tbp->tbflags & TBTYPE];
355 if (tbp->tbflags&TBSTOP) {
356 if (tc->tbc_stop)
357 ttyout(tc->tbc_stop, tp);
358 } else if (tc->tbc_start)
359 ttyout(tc->tbc_start, tp);
360 if (tbp->tbflags&TBPOINT) {
361 if (tc->tbc_point)
362 ttyout(tc->tbc_point, tp);
363 } else if (tc->tbc_run)
364 ttyout(tc->tbc_run, tp);
365 ttstart(tp);
366 break;
367 }
368
369 case BIOGTYPE:
370 *(int *)data = tbp->tbflags & TBTYPE;
371 break;
372
373 case TIOCSETD:
374 case TIOCGETD:
375 case TIOCGETP:
376 case TIOCGETC:
377 return (-1); /* pass thru... */
378
379 default:
380 return (ENOTTY);
381 }
382 return (0);
383}
384#endif /* NTB > 0 */