2 * CHU STREAMS module for SunOS
6 * Copyright 1991-1994, Nick Sayer
8 * Special thanks to Greg Onufer for his debug assists.
9 * Special thanks to Matthias Urlichs for the 4.1.x loadable driver support
11 * Special wet-noodle whippings to Sun for not properly documenting
12 * ANYTHING that makes this stuff at all possible.
14 * Should be PUSHed directly on top of a serial I/O channel.
15 * Provides complete chucode structures to user space.
20 * To make a SunOS 4.1.x compatable loadable module (from the ntp kernel
23 * % cc -c -I../include -DLOADABLE tty_chu_STREAMS.c
25 * The resulting .o file is the loadable module. Modload it
28 * % modload tty_chu_STREAMS.o -entry _chuinit
30 * When none of the instances are pushed in a STREAM, you can
31 * modunload the driver in the usual manner if you wish.
33 * As an alternative to loading it dynamically you can compile it
34 * directly into the kernel by hacking str_conf.c. See the README
35 * file for more details on doing it the old fashioned way.
38 * To make a Solaris 2.x compatable module (from the ntp kernel
41 * % {gcc,cc} -c -I../include -DSOLARIS2 tty_chu_STREAMS.c
42 * % ld -r -o /usr/kernel/strmod/chu tty_chu_STREAMS.o
43 * % chmod 755 /usr/kernel/strmod/chu
45 * The OS will load it for you automagically when it is first pushed.
47 * If you get syntax errors from <sys/timer.h> (really references
48 * to types that weren't typedef'd in gcc's version of types.h),
49 * add -D_SYS_TIMER_H to blot out the miscreants.
51 * Under Solaris 2.2 and previous, do not attempt to modunload the
52 * module unless you're SURE it's not in use. I haven't tried it, but
53 * I've been told it won't do the right thing. Under Solaris 2.3 (and
54 * presumably future revs) an attempt to unload the module when it's in
55 * use will properly refuse with a "busy" message.
60 * v2.6 - Mutexed the per-instance chucode just to be safe.
61 * v2.5 - Fixed show-stopper bug in Solaris 2.x - qprocson().
62 * v2.4 - Added dynamic allocation support for Solaris 2.x.
63 * v2.3 - Added support for Solaris 2.x.
64 * v2.2 - Added SERVICE IMMEDIATE hack.
65 * v2.1 - Added 'sixth byte' heuristics.
66 * v2.0 - first version with an actual version number.
67 * Added support for new CHU 'second 31' data format.
68 * Deleted PEDANTIC and ANAL_RETENTIVE.
77 #elif defined(LOADABLE)
89 * Number of microseconds we allow between
90 * character arrivals. The speed is 300 baud
91 * so this should be somewhat more than 30 msec
93 #define CHUMAXUSEC (60*1000) /* 60 msec */
95 #include <sys/types.h>
96 #include <sys/stream.h>
97 #include <sys/param.h>
99 #include <sys/errno.h>
100 #include <sys/user.h>
104 #include <sys/chudefs.h>
108 #include <sys/ksynch.h>
109 #include <sys/kmem.h>
110 #include <sys/cmn_err.h>
111 #include <sys/conf.h>
112 #include <sys/strtty.h>
113 #include <sys/modctl.h>
115 #include <sys/sunddi.h>
121 #include <sys/kernel.h>
122 #include <sys/conf.h>
124 #include <sundev/mbvar.h>
125 #include <sun/autoconf.h>
126 #include <sun/vddrv.h>
131 static struct module_info rminfo = { 0, "chu", 0, INFPSZ, 0, 0 };
132 static struct module_info wminfo = { 0, "chu", 0, INFPSZ, 0, 0 };
133 static int chuopen(), churput(), chuwput(), chuclose();
135 static struct qinit rinit = { churput, NULL, chuopen, chuclose, NULL,
138 static struct qinit winit = { chuwput, NULL, NULL, NULL, NULL,
141 struct streamtab chuinfo = { &rinit, &winit, NULL, NULL };
144 * Here's our private data type and structs
149 kmutex_t chucode_mutex;
153 struct chucode chu_struct;
157 struct priv_data our_priv_data[NCHU];
162 static struct fmodsw fsw =
169 extern struct mod_ops mod_strmodops;
171 static struct modlstrmod modlstrmod =
174 "CHU timecode decoder v2.6",
178 static struct modlinkage modlinkage =
187 return mod_install(&modlinkage);
193 return mod_info(&modlinkage,foo);
198 return mod_remove(&modlinkage);
201 #endif /* SOLARIS2 */
207 static struct vdldrv vd =
211 NULL, NULL, NULL, 0, 0, NULL, NULL, 0, 0,
214 static struct fmodsw *chu_fmod;
217 chuinit (fc, vdp, vdi, vds)
228 /* Find free entry in fmodsw */
229 for (dev = 0; dev < fmodcnt; dev++) {
230 if (fmodsw[dev].f_str == NULL)
235 chu_fmod = &fmodsw[dev];
237 /* If you think a kernel would have strcpy() you're mistaken. */
238 for (i = 0; i <= FMNAMESZ; i++)
239 chu_fmod->f_name[i] = wminfo.mi_idname[i];
241 chu_fmod->f_str = &chuinfo;
243 vdp->vdd_vdtab = (struct vdlinkage *) & vd;
248 for (i=0; i<NCHU; i++)
249 our_priv_data[i].in_use=0;
257 for (dev = 0; dev < NCHU; dev++)
258 if (our_priv_data[dev].in_use) {
259 /* One of the modules is still open */
263 chu_fmod->f_name[0] = '\0';
264 chu_fmod->f_str = NULL;
275 #endif /* LOADABLE */
277 #if !defined(LOADABLE) && !defined(SOLARIS2)
279 char chu_first_open=1;
284 static int chuopen(q, dev, flag, sflag)
292 #if !defined(LOADABLE) && !defined(SOLARIS2)
298 our_priv_data[i].in_use=0;
303 /* According to the docs, calling with KM_SLEEP can never
306 q->q_ptr = kmem_alloc( sizeof(struct priv_data), KM_SLEEP );
307 ((struct priv_data *) q->q_ptr)->chu_struct.ncodechars = 0;
309 mutex_init(&((struct priv_data *) q->q_ptr)->chucode_mutex,"Chucode Mutex",MUTEX_DRIVER,NULL);
312 if (!putnextctl1(WR(q), M_CTL, MC_SERVICEIMM))
315 mutex_destroy(&((struct priv_data *)q->q_ptr)->chucode_mutex);
316 kmem_free(q->q_ptr, sizeof(struct chucode) );
324 if (!our_priv_data[i].in_use)
326 ((struct priv_data *) (q->q_ptr))=&(our_priv_data[i]);
327 our_priv_data[i].in_use++;
328 our_priv_data[i].chu_struct.ncodechars = 0;
329 if (!putctl1(WR(q)->q_next, M_CTL, MC_SERVICEIMM))
331 our_priv_data[i].in_use=0;
345 static int chuclose(q, flag)
351 mutex_destroy(&((struct priv_data *)q->q_ptr)->chucode_mutex);
352 kmem_free(q->q_ptr, sizeof(struct chucode) );
354 ((struct priv_data *) (q->q_ptr))->in_use=0;
360 * Now the crux of the biscuit.
362 * We will be passed data from the man downstairs. If it's not a data
363 * packet, it must be important, so pass it along unmunged. If, however,
364 * it is a data packet, we're gonna do special stuff to it. We're going
365 * to pass each character we get to the old line discipline code we
366 * include below for just such an occasion. When the old ldisc code
367 * gets a full chucode struct, we'll hand it back upstairs.
369 * chuinput takes a single character and q (as quickly as possible).
370 * passback takes a pointer to a chucode struct and q and sends it upstream.
376 static int churput(q, mp)
382 switch(mp->b_datap->db_type)
385 for(bp=mp; bp!=NULL; bp=bp->b_cont)
387 while(bp->b_rptr < bp->b_wptr)
388 chuinput( ((u_char)*(bp->b_rptr++)) , q );
400 * Writing to a chu device doesn't make sense, but we'll pass them
401 * through in case they're important.
404 static int chuwput(q, mp)
412 * Take a pointer to a filled chucode struct and a queue and
413 * send the chucode stuff upstream
416 void passback(outdata,q)
417 struct chucode *outdata;
423 mp=(mblk_t*) allocb(sizeof(struct chucode),BPRI_LO);
428 cmn_err(CE_WARN,"chu module couldn't allocate message block");
430 log(LOG_ERR,"chu: cannot allocate message");
435 for(j=0;j<sizeof(struct chucode); j++)
436 *mp->b_wptr++ = *( ((char*)outdata) + j );
442 * This routine was copied nearly verbatim from the old line discipline.
448 register struct chucode *chuc;
454 * Quick, Batman, get a timestamp! We need to do this
455 * right away. The time between the end of the stop bit
456 * and this point is critical, and should be as nearly
457 * constant and as short as possible. (Un)fortunately,
458 * the Sun's clock granularity is so big this isn't a
461 * uniqtime() is totally undocumented, but there you are.
466 mutex_enter(&((struct priv_data *)q->q_ptr)->chucode_mutex);
470 * Now, locate the chu struct once so we don't have to do it
473 chuc=&(((struct priv_data *) (q->q_ptr))->chu_struct);
476 * Compute the difference in this character's time stamp
477 * and the last. If it exceeds the margin, blow away all
478 * the characters currently in the buffer.
480 i = (int)chuc->ncodechars;
483 sec = tv.tv_sec - chuc->codetimes[i-1].tv_sec;
484 usec = tv.tv_usec - chuc->codetimes[i-1].tv_usec;
490 if (sec != 0 || usec > CHUMAXUSEC)
493 chuc->ncodechars = 0;
498 * Store the character.
500 chuc->codechars[i] = (u_char)c;
501 chuc->codetimes[i] = tv;
504 * Now we perform the 'sixth byte' heuristics.
506 * This is a long story.
508 * We used to be able to count on the first byte of the code
509 * having a '6' in the LSD. This prevented most code framing
510 * errors (garbage before the first byte wouldn't typically
511 * have a 6 in the LSD). That's no longer the case.
513 * We can get around this, however, by noting that the 6th byte
514 * must be either equal to or one's complement of the first.
515 * If we get a sixth byte that ISN'T like that, then it may
516 * well be that the first byte is garbage. The right thing
517 * to do is to left-shift the whole buffer one count and
518 * continue to wait for the sixth byte.
520 if (i == NCHUCHARS/2)
522 register u_char temp_byte;
524 temp_byte=chuc->codechars[i] ^ chuc->codechars[0];
526 if ( (temp_byte) && (temp_byte!=0xff) )
530 * No match. Left-shift the buffer and try again
532 for(t=0;t<=NCHUCHARS/2;t++)
534 chuc->codechars[t]=chuc->codechars[t+1];
535 chuc->codetimes[t]=chuc->codetimes[t+1];
538 i--; /* This is because of the ++i immediately following */
548 * We're not done. Not much to do here. Save the count and wait
549 * for another character.
551 chuc->ncodechars = (u_char)i;
556 * We are done. Mark this buffer full and pass it along.
558 chuc->ncodechars = NCHUCHARS;
561 * Now we have a choice. Either the front half and back half
562 * have to match, or be one's complement of each other.
564 * So let's try the first byte and see
567 if(chuc->codechars[0] == chuc->codechars[NCHUCHARS/2])
569 chuc->chutype = CHU_TIME;
570 for( i=0; i<(NCHUCHARS/2); i++)
571 if (chuc->codechars[i] != chuc->codechars[i+(NCHUCHARS/2)])
573 chuc->ncodechars = 0;
575 mutex_exit(&((struct priv_data *)q->q_ptr)->chucode_mutex);
582 chuc->chutype = CHU_YEAR;
583 for( i=0; i<(NCHUCHARS/2); i++)
584 if (((chuc->codechars[i] ^ chuc->codechars[i+(NCHUCHARS/2)]) & 0xff)
587 chuc->ncodechars = 0;
589 mutex_exit(&((struct priv_data *)q->q_ptr)->chucode_mutex);
595 passback(chuc,q); /* We're done! */
596 chuc->ncodechars = 0; /* Start all over again! */
599 mutex_exit(&((struct priv_data *)q->q_ptr)->chucode_mutex);
603 #endif /* NCHU > 0 */