1*86d7f5d3SJohn Marino /*********************************************************************** 2*86d7f5d3SJohn Marino * * 3*86d7f5d3SJohn Marino * Copyright (c) David L. Mills 1993-2001 * 4*86d7f5d3SJohn Marino * * 5*86d7f5d3SJohn Marino * Permission to use, copy, modify, and distribute this software and * 6*86d7f5d3SJohn Marino * its documentation for any purpose and without fee is hereby * 7*86d7f5d3SJohn Marino * granted, provided that the above copyright notice appears in all * 8*86d7f5d3SJohn Marino * copies and that both the copyright notice and this permission * 9*86d7f5d3SJohn Marino * notice appear in supporting documentation, and that the name * 10*86d7f5d3SJohn Marino * University of Delaware not be used in advertising or publicity * 11*86d7f5d3SJohn Marino * pertaining to distribution of the software without specific, * 12*86d7f5d3SJohn Marino * written prior permission. The University of Delaware makes no * 13*86d7f5d3SJohn Marino * representations about the suitability this software for any * 14*86d7f5d3SJohn Marino * purpose. It is provided "as is" without express or implied * 15*86d7f5d3SJohn Marino * warranty. * 16*86d7f5d3SJohn Marino * * 17*86d7f5d3SJohn Marino **********************************************************************/ 18*86d7f5d3SJohn Marino 19*86d7f5d3SJohn Marino /* 20*86d7f5d3SJohn Marino * Modification history timex.h 21*86d7f5d3SJohn Marino * 22*86d7f5d3SJohn Marino * 16 Aug 00 David L. Mills 23*86d7f5d3SJohn Marino * API Version 4. Added MOD_TAI and tai member of ntptimeval 24*86d7f5d3SJohn Marino * structure. 25*86d7f5d3SJohn Marino * 26*86d7f5d3SJohn Marino * 17 Nov 98 David L. Mills 27*86d7f5d3SJohn Marino * Revised for nanosecond kernel and user interface. 28*86d7f5d3SJohn Marino * 29*86d7f5d3SJohn Marino * 26 Sep 94 David L. Mills 30*86d7f5d3SJohn Marino * Added defines for hybrid phase/frequency-lock loop. 31*86d7f5d3SJohn Marino * 32*86d7f5d3SJohn Marino * 19 Mar 94 David L. Mills 33*86d7f5d3SJohn Marino * Moved defines from kernel routines to header file and added new 34*86d7f5d3SJohn Marino * defines for PPS phase-lock loop. 35*86d7f5d3SJohn Marino * 36*86d7f5d3SJohn Marino * 20 Feb 94 David L. Mills 37*86d7f5d3SJohn Marino * Revised status codes and structures for external clock and PPS 38*86d7f5d3SJohn Marino * signal discipline. 39*86d7f5d3SJohn Marino * 40*86d7f5d3SJohn Marino * 28 Nov 93 David L. Mills 41*86d7f5d3SJohn Marino * Adjusted parameters to improve stability and increase poll 42*86d7f5d3SJohn Marino * interval. 43*86d7f5d3SJohn Marino * 44*86d7f5d3SJohn Marino * 17 Sep 93 David L. Mills 45*86d7f5d3SJohn Marino * Created file 46*86d7f5d3SJohn Marino * 47*86d7f5d3SJohn Marino * $FreeBSD: src/sys/sys/timex.h,v 1.12.2.1 2001/04/22 11:19:39 jhay Exp $ 48*86d7f5d3SJohn Marino * $DragonFly: src/sys/sys/timex.h,v 1.7 2006/10/23 21:50:33 dillon Exp $ 49*86d7f5d3SJohn Marino */ 50*86d7f5d3SJohn Marino /* 51*86d7f5d3SJohn Marino * This header file defines the Network Time Protocol (NTP) interfaces 52*86d7f5d3SJohn Marino * for user and daemon application programs. These are implemented using 53*86d7f5d3SJohn Marino * defined syscalls and data structures and require specific kernel 54*86d7f5d3SJohn Marino * support. 55*86d7f5d3SJohn Marino * 56*86d7f5d3SJohn Marino * The original precision time kernels developed from 1993 have an 57*86d7f5d3SJohn Marino * ultimate resolution of one microsecond; however, the most recent 58*86d7f5d3SJohn Marino * kernels have an ultimate resolution of one nanosecond. In these 59*86d7f5d3SJohn Marino * kernels, a ntp_adjtime() syscalls can be used to determine which 60*86d7f5d3SJohn Marino * resolution is in use and to select either one at any time. The 61*86d7f5d3SJohn Marino * resolution selected affects the scaling of certain fields in the 62*86d7f5d3SJohn Marino * ntp_gettime() and ntp_adjtime() syscalls, as described below. 63*86d7f5d3SJohn Marino * 64*86d7f5d3SJohn Marino * NAME 65*86d7f5d3SJohn Marino * ntp_gettime - NTP user application interface 66*86d7f5d3SJohn Marino * 67*86d7f5d3SJohn Marino * SYNOPSIS 68*86d7f5d3SJohn Marino * #include <sys/timex.h> 69*86d7f5d3SJohn Marino * 70*86d7f5d3SJohn Marino * int ntp_gettime(struct ntptimeval *ntv); 71*86d7f5d3SJohn Marino * 72*86d7f5d3SJohn Marino * DESCRIPTION 73*86d7f5d3SJohn Marino * The time returned by ntp_gettime() is in a timespec structure, 74*86d7f5d3SJohn Marino * but may be in either microsecond (seconds and microseconds) or 75*86d7f5d3SJohn Marino * nanosecond (seconds and nanoseconds) format. The particular 76*86d7f5d3SJohn Marino * format in use is determined by the STA_NANO bit of the status 77*86d7f5d3SJohn Marino * word returned by the ntp_adjtime() syscall. 78*86d7f5d3SJohn Marino * 79*86d7f5d3SJohn Marino * NAME 80*86d7f5d3SJohn Marino * ntp_adjtime - NTP daemon application interface 81*86d7f5d3SJohn Marino * 82*86d7f5d3SJohn Marino * SYNOPSIS 83*86d7f5d3SJohn Marino * #include <sys/timex.h> 84*86d7f5d3SJohn Marino * #include <sys/syscall.h> 85*86d7f5d3SJohn Marino * 86*86d7f5d3SJohn Marino * int syscall(SYS_ntp_adjtime, tptr); 87*86d7f5d3SJohn Marino * int SYS_ntp_adjtime; 88*86d7f5d3SJohn Marino * struct timex *tptr; 89*86d7f5d3SJohn Marino * 90*86d7f5d3SJohn Marino * DESCRIPTION 91*86d7f5d3SJohn Marino * Certain fields of the timex structure are interpreted in either 92*86d7f5d3SJohn Marino * microseconds or nanoseconds according to the state of the 93*86d7f5d3SJohn Marino * STA_NANO bit in the status word. See the description below for 94*86d7f5d3SJohn Marino * further information. 95*86d7f5d3SJohn Marino */ 96*86d7f5d3SJohn Marino #ifndef _SYS_TIMEX_H_ 97*86d7f5d3SJohn Marino #define _SYS_TIMEX_H_ 98*86d7f5d3SJohn Marino 99*86d7f5d3SJohn Marino #define NTP_API 4 /* NTP API version */ 100*86d7f5d3SJohn Marino 101*86d7f5d3SJohn Marino #ifndef _SYS_SYSCALL_H_ 102*86d7f5d3SJohn Marino #include <sys/syscall.h> 103*86d7f5d3SJohn Marino #endif 104*86d7f5d3SJohn Marino #ifndef _SYS_TIME_H_ 105*86d7f5d3SJohn Marino #include <sys/time.h> 106*86d7f5d3SJohn Marino #endif 107*86d7f5d3SJohn Marino 108*86d7f5d3SJohn Marino /* 109*86d7f5d3SJohn Marino * The following defines establish the performance envelope of the 110*86d7f5d3SJohn Marino * kernel discipline loop. Phase or frequency errors greater than 111*86d7f5d3SJohn Marino * NAXPHASE or MAXFREQ are clamped to these maxima. For update intervals 112*86d7f5d3SJohn Marino * less than MINSEC, the loop always operates in PLL mode; while, for 113*86d7f5d3SJohn Marino * update intervals greater than MAXSEC, the loop always operates in FLL 114*86d7f5d3SJohn Marino * mode. Between these two limits the operating mode is selected by the 115*86d7f5d3SJohn Marino * STA_FLL bit in the status word. 116*86d7f5d3SJohn Marino */ 117*86d7f5d3SJohn Marino #define MAXPHASE 500000000L /* max phase error (ns) */ 118*86d7f5d3SJohn Marino #define MAXFREQ 500000L /* max freq error (ns/s) */ 119*86d7f5d3SJohn Marino #define MINSEC 256 /* min FLL update interval (s) */ 120*86d7f5d3SJohn Marino #define MAXSEC 2048 /* max PLL update interval (s) */ 121*86d7f5d3SJohn Marino #define NANOSECOND 1000000000L /* nanoseconds in one second */ 122*86d7f5d3SJohn Marino #define SCALE_PPM (65536 / 1000) /* crude ns/s to scaled PPM */ 123*86d7f5d3SJohn Marino #define MAXTC 10 /* max time constant */ 124*86d7f5d3SJohn Marino 125*86d7f5d3SJohn Marino /* 126*86d7f5d3SJohn Marino * The following defines and structures define the user interface for 127*86d7f5d3SJohn Marino * the ntp_gettime() and ntp_adjtime() syscalls. 128*86d7f5d3SJohn Marino * 129*86d7f5d3SJohn Marino * Control mode codes (timex.modes) 130*86d7f5d3SJohn Marino */ 131*86d7f5d3SJohn Marino #define MOD_OFFSET 0x0001 /* set time offset */ 132*86d7f5d3SJohn Marino #define MOD_FREQUENCY 0x0002 /* set frequency offset */ 133*86d7f5d3SJohn Marino #define MOD_MAXERROR 0x0004 /* set maximum time error */ 134*86d7f5d3SJohn Marino #define MOD_ESTERROR 0x0008 /* set estimated time error */ 135*86d7f5d3SJohn Marino #define MOD_STATUS 0x0010 /* set clock status bits */ 136*86d7f5d3SJohn Marino #define MOD_TIMECONST 0x0020 /* set PLL time constant */ 137*86d7f5d3SJohn Marino #define MOD_PPSMAX 0x0040 /* set PPS maximum averaging time */ 138*86d7f5d3SJohn Marino #define MOD_TAI 0x0080 /* set TAI offset */ 139*86d7f5d3SJohn Marino #define MOD_MICRO 0x1000 /* select microsecond resolution */ 140*86d7f5d3SJohn Marino #define MOD_NANO 0x2000 /* select nanosecond resolution */ 141*86d7f5d3SJohn Marino #define MOD_CLKB 0x4000 /* select clock B */ 142*86d7f5d3SJohn Marino #define MOD_CLKA 0x8000 /* select clock A */ 143*86d7f5d3SJohn Marino 144*86d7f5d3SJohn Marino /* 145*86d7f5d3SJohn Marino * Status codes (timex.status) 146*86d7f5d3SJohn Marino */ 147*86d7f5d3SJohn Marino #define STA_PLL 0x0001 /* enable PLL updates (rw) */ 148*86d7f5d3SJohn Marino #define STA_PPSFREQ 0x0002 /* enable PPS freq discipline (rw) */ 149*86d7f5d3SJohn Marino #define STA_PPSTIME 0x0004 /* enable PPS time discipline (rw) */ 150*86d7f5d3SJohn Marino #define STA_FLL 0x0008 /* enable FLL mode (rw) */ 151*86d7f5d3SJohn Marino #define STA_INS 0x0010 /* insert leap (rw) */ 152*86d7f5d3SJohn Marino #define STA_DEL 0x0020 /* delete leap (rw) */ 153*86d7f5d3SJohn Marino #define STA_UNSYNC 0x0040 /* clock unsynchronized (rw) */ 154*86d7f5d3SJohn Marino #define STA_FREQHOLD 0x0080 /* hold frequency (rw) */ 155*86d7f5d3SJohn Marino #define STA_PPSSIGNAL 0x0100 /* PPS signal present (ro) */ 156*86d7f5d3SJohn Marino #define STA_PPSJITTER 0x0200 /* PPS signal jitter exceeded (ro) */ 157*86d7f5d3SJohn Marino #define STA_PPSWANDER 0x0400 /* PPS signal wander exceeded (ro) */ 158*86d7f5d3SJohn Marino #define STA_PPSERROR 0x0800 /* PPS signal calibration error (ro) */ 159*86d7f5d3SJohn Marino #define STA_CLOCKERR 0x1000 /* clock hardware fault (ro) */ 160*86d7f5d3SJohn Marino #define STA_NANO 0x2000 /* resolution (0 = us, 1 = ns) (ro) */ 161*86d7f5d3SJohn Marino #define STA_MODE 0x4000 /* mode (0 = PLL, 1 = FLL) (ro) */ 162*86d7f5d3SJohn Marino #define STA_CLK 0x8000 /* clock source (0 = A, 1 = B) (ro) */ 163*86d7f5d3SJohn Marino 164*86d7f5d3SJohn Marino #define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | \ 165*86d7f5d3SJohn Marino STA_PPSERROR | STA_CLOCKERR | STA_NANO | STA_MODE | STA_CLK) 166*86d7f5d3SJohn Marino 167*86d7f5d3SJohn Marino /* 168*86d7f5d3SJohn Marino * Clock states (time_state) 169*86d7f5d3SJohn Marino */ 170*86d7f5d3SJohn Marino #define TIME_OK 0 /* no leap second warning */ 171*86d7f5d3SJohn Marino #define TIME_INS 1 /* insert leap second warning */ 172*86d7f5d3SJohn Marino #define TIME_DEL 2 /* delete leap second warning */ 173*86d7f5d3SJohn Marino #define TIME_OOP 3 /* leap second in progress */ 174*86d7f5d3SJohn Marino #define TIME_WAIT 4 /* leap second has occured */ 175*86d7f5d3SJohn Marino #define TIME_ERROR 5 /* error (see status word) */ 176*86d7f5d3SJohn Marino 177*86d7f5d3SJohn Marino /* 178*86d7f5d3SJohn Marino * NTP user interface (ntp_gettime()) - used to read kernel clock values 179*86d7f5d3SJohn Marino * 180*86d7f5d3SJohn Marino * Note: The time member is in microseconds if STA_NANO is zero and 181*86d7f5d3SJohn Marino * nanoseconds if not. 182*86d7f5d3SJohn Marino */ 183*86d7f5d3SJohn Marino struct ntptimeval { 184*86d7f5d3SJohn Marino struct timespec time; /* current time (ns) (ro) */ 185*86d7f5d3SJohn Marino long maxerror; /* maximum error (us) (ro) */ 186*86d7f5d3SJohn Marino long esterror; /* estimated error (us) (ro) */ 187*86d7f5d3SJohn Marino long tai; /* TAI offset */ 188*86d7f5d3SJohn Marino int time_state; /* time status */ 189*86d7f5d3SJohn Marino }; 190*86d7f5d3SJohn Marino 191*86d7f5d3SJohn Marino /* 192*86d7f5d3SJohn Marino * NTP daemon interface (ntp_adjtime()) - used to discipline CPU clock 193*86d7f5d3SJohn Marino * oscillator and determine status. 194*86d7f5d3SJohn Marino * 195*86d7f5d3SJohn Marino * Note: The offset, precision and jitter members are in microseconds if 196*86d7f5d3SJohn Marino * STA_NANO is zero and nanoseconds if not. 197*86d7f5d3SJohn Marino */ 198*86d7f5d3SJohn Marino struct timex { 199*86d7f5d3SJohn Marino unsigned int modes; /* clock mode bits (wo) */ 200*86d7f5d3SJohn Marino long offset; /* time offset (ns/us) (rw) */ 201*86d7f5d3SJohn Marino long freq; /* frequency offset (scaled PPM) (rw) */ 202*86d7f5d3SJohn Marino long maxerror; /* maximum error (us) (rw) */ 203*86d7f5d3SJohn Marino long esterror; /* estimated error (us) (rw) */ 204*86d7f5d3SJohn Marino int status; /* clock status bits (rw) */ 205*86d7f5d3SJohn Marino long constant; /* poll interval (log2 s) (rw) */ 206*86d7f5d3SJohn Marino long precision; /* clock precision (ns/us) (ro) */ 207*86d7f5d3SJohn Marino long tolerance; /* clock frequency tolerance (scaled 208*86d7f5d3SJohn Marino * PPM) (ro) */ 209*86d7f5d3SJohn Marino /* 210*86d7f5d3SJohn Marino * The following read-only structure members are implemented 211*86d7f5d3SJohn Marino * only if the PPS signal discipline is configured in the 212*86d7f5d3SJohn Marino * kernel. They are included in all configurations to insure 213*86d7f5d3SJohn Marino * portability. 214*86d7f5d3SJohn Marino */ 215*86d7f5d3SJohn Marino long ppsfreq; /* PPS frequency (scaled PPM) (ro) */ 216*86d7f5d3SJohn Marino long jitter; /* PPS jitter (ns/us) (ro) */ 217*86d7f5d3SJohn Marino int shift; /* interval duration (s) (shift) (ro) */ 218*86d7f5d3SJohn Marino long stabil; /* PPS stability (scaled PPM) (ro) */ 219*86d7f5d3SJohn Marino long jitcnt; /* jitter limit exceeded (ro) */ 220*86d7f5d3SJohn Marino long calcnt; /* calibration intervals (ro) */ 221*86d7f5d3SJohn Marino long errcnt; /* calibration errors (ro) */ 222*86d7f5d3SJohn Marino long stbcnt; /* stability limit exceeded (ro) */ 223*86d7f5d3SJohn Marino }; 224*86d7f5d3SJohn Marino 225*86d7f5d3SJohn Marino #ifdef __DragonFly__ 226*86d7f5d3SJohn Marino 227*86d7f5d3SJohn Marino #ifdef _KERNEL 228*86d7f5d3SJohn Marino 229*86d7f5d3SJohn Marino int ntp_update_second (time_t, int64_t *); 230*86d7f5d3SJohn Marino 231*86d7f5d3SJohn Marino #else 232*86d7f5d3SJohn Marino 233*86d7f5d3SJohn Marino #include <sys/cdefs.h> 234*86d7f5d3SJohn Marino 235*86d7f5d3SJohn Marino __BEGIN_DECLS 236*86d7f5d3SJohn Marino int ntp_adjtime (struct timex *); 237*86d7f5d3SJohn Marino int ntp_gettime (struct ntptimeval *); 238*86d7f5d3SJohn Marino __END_DECLS 239*86d7f5d3SJohn Marino #endif /* _KERNEL */ 240*86d7f5d3SJohn Marino 241*86d7f5d3SJohn Marino #endif /* __DragonFly__ */ 242*86d7f5d3SJohn Marino 243*86d7f5d3SJohn Marino #endif /* !_SYS_TIMEX_H_ */ 244