Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / ntpd / refclock_tt560.c
blobc25a2a398535aa81f90528bb390bab6e5bc0d651
1 /* $NetBSD$ */
3 /*
4 * refclock_tt560 - clock driver for the TrueTime 560 IRIG-B decoder
5 */
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
11 #if defined(REFCLOCK) && defined(CLOCK_TT560)
13 #include "ntpd.h"
14 #include "ntp_io.h"
15 #include "ntp_refclock.h"
16 #include "ntp_unixtime.h"
17 #include "sys/tt560_api.h"
18 #include "ntp_stdlib.h"
20 #include <stdio.h>
21 #include <ctype.h>
24 * This driver supports the TrueTime 560 IRIG-B decoder for the PCI bus.
25 */
28 * TT560 interface definitions
30 #define DEVICE "/dev/tt560%d" /* device name and unit */
31 #define PRECISION (-20) /* precision assumed (1 us) */
32 #define REFID "IRIG" /* reference ID */
33 #define DESCRIPTION "TrueTime 560 IRIG-B PCI Decoder"
36 * Unit control structure
38 struct tt560unit {
39 tt_mem_space_t *tt_mem; /* mapped address of PCI board */
40 time_freeze_reg_t tt560rawt; /* data returned from PCI board */
43 typedef union byteswap_u
45 unsigned int long_word;
46 unsigned char byte[4];
47 } byteswap_t;
50 * Function prototypes
52 static int tt560_start P((int, struct peer *));
53 static void tt560_shutdown P((int, struct peer *));
54 static void tt560_poll P((int unit, struct peer *));
57 * Transfer vector
59 struct refclock refclock_tt560 = {
60 tt560_start, /* clock_start */
61 tt560_shutdown, /* clock_shutdown */
62 tt560_poll, /* clock_poll */
63 noentry, /* clock_control (not used) */
64 noentry, /* clock_init (not used) */
65 noentry, /* clock_buginfo (not used) */
66 NOFLAGS /* clock_flags (not used) */
71 * tt560_start - open the TT560 device and initialize data for processing
73 static int
74 tt560_start(
75 int unit,
76 struct peer *peer
79 register struct tt560unit *up;
80 struct refclockproc *pp;
81 char device[20];
82 int fd;
83 caddr_t membase;
86 * Open TT560 device
88 (void)sprintf(device, DEVICE, unit);
89 fd = open(device, O_RDWR);
90 if (fd == -1) {
91 msyslog(LOG_ERR, "tt560_start: open of %s: %m", device);
92 return (0);
96 * Map the device registers into user space.
98 membase = mmap ((caddr_t) 0, TTIME_MEMORY_SIZE,
99 PROT_READ | PROT_WRITE,
100 MAP_SHARED, fd, (off_t)0);
102 if (membase == (caddr_t) -1) {
103 msyslog(LOG_ERR, "tt560_start: mapping of %s: %m", device);
104 (void) close(fd);
105 return (0);
109 * Allocate and initialize unit structure
111 if (!(up = (struct tt560unit *) emalloc(sizeof(struct tt560unit)))) {
112 (void) close(fd);
113 return (0);
115 memset((char *)up, 0, sizeof(struct tt560unit));
116 up->tt_mem = (tt_mem_space_t *)membase;
117 pp = peer->procptr;
118 pp->io.clock_recv = noentry;
119 pp->io.srcclock = (caddr_t)peer;
120 pp->io.datalen = 0;
121 pp->io.fd = fd;
122 pp->unitptr = (caddr_t)up;
125 * Initialize miscellaneous peer variables
127 peer->precision = PRECISION;
128 peer->burst = NSTAGE;
129 pp->clockdesc = DESCRIPTION;
130 memcpy((char *)&pp->refid, REFID, 4);
131 return (1);
136 * tt560_shutdown - shut down the clock
138 static void
139 tt560_shutdown(
140 int unit,
141 struct peer *peer
144 register struct tt560unit *up;
145 struct refclockproc *pp;
147 pp = peer->procptr;
148 up = (struct tt560unit *)pp->unitptr;
149 io_closeclock(&pp->io);
150 free(up);
155 * tt560_poll - called by the transmit procedure
157 static void
158 tt560_poll(
159 int unit,
160 struct peer *peer
163 register struct tt560unit *up;
164 struct refclockproc *pp;
165 time_freeze_reg_t *tp;
166 tt_mem_space_t *mp;
168 int i;
169 unsigned int *p_time_t, *tt_mem_t;
172 * This is the main routine. It snatches the time from the TT560
173 * board and tacks on a local timestamp.
175 pp = peer->procptr;
176 up = (struct tt560unit *)pp->unitptr;
177 mp = up->tt_mem;
178 tp = &up->tt560rawt;
180 p_time_t = (unsigned int *)tp;
181 tt_mem_t = (unsigned int *)&mp->time_freeze_reg;
183 *tt_mem_t = 0; /* update the time freeze register */
184 /* and copy time stamp to memory */
185 for (i=0; i < TIME_FREEZE_REG_LEN; i++) {
186 *p_time_t = byte_swap(*tt_mem_t);
187 p_time_t++;
188 tt_mem_t++;
191 get_systime(&pp->lastrec);
192 pp->polls++;
195 * We get down to business, check the timecode format and decode
196 * its contents. If the timecode has invalid length or is not in
197 * proper format, we declare bad format and exit. Note: we
198 * can't use the sec/usec conversion produced by the driver,
199 * since the year may be suspect. All format error checking is
200 * done by the sprintf() and sscanf() routines.
202 sprintf(pp->a_lastcode,
203 "%1x%1x%1x %1x%1x:%1x%1x:%1x%1x.%1x%1x%1x%1x%1x%1x %1x",
204 tp->hun_day, tp->tens_day, tp->unit_day,
205 tp->tens_hour, tp->unit_hour,
206 tp->tens_min, tp->unit_min,
207 tp->tens_sec, tp->unit_sec,
208 tp->hun_ms, tp->tens_ms, tp->unit_ms,
209 tp->hun_us, tp->tens_us, tp->unit_us,
210 tp->status);
211 pp->lencode = strlen(pp->a_lastcode);
212 #ifdef DEBUG
213 if (debug)
214 printf("tt560: time %s timecode %d %s\n",
215 ulfptoa(&pp->lastrec, 6), pp->lencode,
216 pp->a_lastcode);
217 #endif
218 if (sscanf(pp->a_lastcode, "%3d %2d:%2d:%2d.%6ld",
219 &pp->day, &pp->hour, &pp->minute, &pp->second, &pp->usec)
220 != 5) {
221 refclock_report(peer, CEVNT_BADTIME);
222 return;
224 if ((tp->status & 0x6) != 0x6)
225 pp->leap = LEAP_NOTINSYNC;
226 else
227 pp->leap = LEAP_NOWARNING;
228 if (!refclock_process(pp)) {
229 refclock_report(peer, CEVNT_BADTIME);
230 return;
232 if (peer->burst > 0)
233 return;
234 if (pp->coderecv == pp->codeproc) {
235 refclock_report(peer, CEVNT_TIMEOUT);
236 return;
238 record_clock_stats(&peer->srcadr, pp->a_lastcode);
239 refclock_receive(peer);
240 peer->burst = NSTAGE;
243 /******************************************************************
245 * byte_swap
247 * Inputs: 32 bit integer
249 * Output: byte swapped 32 bit integer.
251 * This routine is used to compensate for the byte alignment
252 * differences between big-endian and little-endian integers.
254 ******************************************************************/
255 static unsigned int
256 byte_swap(unsigned int input_num)
258 byteswap_t byte_swap;
259 unsigned char temp;
261 byte_swap.long_word = input_num;
263 temp = byte_swap.byte[3];
264 byte_swap.byte[3] = byte_swap.byte[0];
265 byte_swap.byte[0] = temp;
267 temp = byte_swap.byte[2];
268 byte_swap.byte[2] = byte_swap.byte[1];
269 byte_swap.byte[1] = temp;
271 return (byte_swap.long_word);
274 #else
275 int refclock_tt560_bs;
276 #endif /* REFCLOCK */