1 /* tty_chu.c,v 3.1 1993/07/06 01:07:30 jbj Exp
2 * tty_chu.c - CHU line driver
8 #include "../h/param.h"
9 #include "../h/types.h"
10 #include "../h/systm.h"
12 #include "../h/user.h"
13 #include "../h/ioctl.h"
15 #include "../h/proc.h"
16 #include "../h/file.h"
17 #include "../h/conf.h"
21 #include "../h/chudefs.h"
24 * Line discipline for receiving CHU time codes.
25 * Does elementary noise elimination, takes time stamps after
26 * the arrival of each character, returns a buffer full of the
27 * received 10 character code and the associated time stamps.
32 u_char used; /* Set to 1 when structure in use */
33 u_char lastindex; /* least recently used buffer */
34 u_char curindex; /* buffer to use */
35 u_char sleeping; /* set to 1 when we're sleeping on a buffer */
36 struct chucode chubuf[NUMCHUBUFS];
40 * Number of microseconds we allow between
41 * character arrivals. The speed is 300 baud
42 * so this should be somewhat more than 30 msec
44 #define CHUMAXUSEC (50*1000) /* 50 msec */
49 * Open as CHU time discipline. Called when discipline changed
50 * with ioctl, and changes the interpretation of the information
51 * in the tty structure.
56 register struct tty *tp;
58 register struct chudata *chu;
61 * Don't allow multiple opens. This will also protect us
62 * from someone opening /dev/tty
64 if (tp->t_line == CHULDISC)
67 for (chu = chu_data; chu < &chu_data[NCHU]; chu++)
70 if (chu >= &chu[NCHU])
73 chu->lastindex = chu->curindex = 0;
75 chu->chubuf[0].ncodechars = 0;
76 tp->T_LINEP = (caddr_t) chu;
81 * Break down... called when discipline changed or from device
85 register struct tty *tp;
87 register int s = spl5();
89 ((struct chudata *) tp->T_LINEP)->used = 0;
92 tp->t_rawq.c_cc = 0; /* clear queues -- paranoid */
94 tp->t_line = 0; /* paranoid: avoid races */
99 * Read a CHU buffer. Sleep on the current buffer
102 register struct tty *tp;
105 register struct chudata *chu;
106 register struct chucode *chucode;
109 if ((tp->t_state&TS_CARR_ON)==0)
112 chu = (struct chudata *) (tp->T_LINEP);
115 chucode = &(chu->chubuf[chu->lastindex]);
116 while (chu->curindex == chu->lastindex) {
118 sleep((caddr_t)chucode, TTIPRI);
121 if (++(chu->lastindex) >= NUMCHUBUFS)
125 return (uiomove((caddr_t)chucode, sizeof(*chucode), UIO_READ, uio));
129 * Low level character input routine.
130 * If the character looks okay, grab a time stamp. If the stuff in
131 * the buffer is too old, dump it and start fresh. If the character is
132 * non-BCDish, everything in the buffer too.
136 register struct tty *tp;
138 register struct chudata *chu = (struct chudata *) tp->T_LINEP;
139 register struct chucode *chuc;
145 * Do a check on the BSDness of the character. This delays
146 * the time stamp a bit but saves a fair amount of overhead
147 * when the static is bad.
149 if (((c) & 0xf) > 9 || (((c)>>4) & 0xf) > 9) {
150 chuc = &(chu->chubuf[chu->curindex]);
151 chuc->ncodechars = 0; /* blow all previous away */
156 * Call microtime() to get the current time of day
161 * Compute the difference in this character's time stamp
162 * and the last. If it exceeds the margin, blow away all
163 * the characters currently in the buffer.
165 chuc = &(chu->chubuf[chu->curindex]);
166 i = (int)chuc->ncodechars;
168 sec = tv.tv_sec - chuc->codetimes[i-1].tv_sec;
169 usec = tv.tv_usec - chuc->codetimes[i-1].tv_usec;
174 if (sec != 0 || usec > CHUMAXUSEC) {
176 chuc->ncodechars = 0;
181 * Store the character. If we're done, have to tell someone
183 chuc->codechars[i] = (u_char)c;
184 chuc->codetimes[i] = tv;
186 if (++i < NCHUCHARS) {
188 * Not much to do here. Save the count and wait
189 * for another character.
191 chuc->ncodechars = (u_char)i;
194 * Mark this buffer full and point at next. If the
195 * next buffer is full we overwrite it by bumping the
198 chuc->ncodechars = NCHUCHARS;
199 if (++(chu->curindex) >= NUMCHUBUFS)
201 if (chu->curindex == chu->lastindex)
202 if (++(chu->lastindex) >= NUMCHUBUFS)
204 chu->chubuf[chu->curindex].ncodechars = 0;
207 * Wake up anyone sleeping on this. Also wake up
208 * selectors and/or deliver a SIGIO as required.
211 selwakeup(tp->t_rsel, tp->t_state&TS_RCOLL);
212 tp->t_state &= ~TS_RCOLL;
215 if (tp->t_state & TS_ASYNC)
216 gsignal(tp->t_pgrp, SIGIO);
218 (void) wakeup((caddr_t)chuc);
223 * Handle ioctls. We reject all tty-style except those that
224 * change the line discipline.
226 chuioctl(tp, cmd, data, flag)
242 return (ENOTTY); /* not quite appropriate */
250 register struct tty *tp = &cdevsw[major(dev)].d_ttys[minor(dev)];
254 chu = (struct chudata *) (tp->T_LINEP);
259 if (chu->curindex != chu->lastindex)
261 if (tp->t_rsel && tp->t_rsel->p_wchan == (caddr_t)&selwait)
262 tp->t_state |= TS_RCOLL;
264 tp->t_rsel = u.u_procp;