+
+
+
+
+
+
+/*** openntpd-4.6 uses only adjtime, not adjtimex ***/
+
+/*** ntp-4.2.6/ntpd/ntp_loopfilter.c - adjtimex usage ***/
+
+#if 0
+static double
+direct_freq(double fp_offset)
+{
+
+#ifdef KERNEL_PLL
+ /*
+ * If the kernel is enabled, we need the residual offset to
+ * calculate the frequency correction.
+ */
+ if (pll_control && kern_enable) {
+ memset(&ntv, 0, sizeof(ntv));
+ ntp_adjtime(&ntv);
+#ifdef STA_NANO
+ clock_offset = ntv.offset / 1e9;
+#else /* STA_NANO */
+ clock_offset = ntv.offset / 1e6;
+#endif /* STA_NANO */
+ drift_comp = FREQTOD(ntv.freq);
+ }
+#endif /* KERNEL_PLL */
+ set_freq((fp_offset - clock_offset) / (current_time - clock_epoch) + drift_comp);
+ wander_resid = 0;
+ return drift_comp;
+}
+
+static void
+set_freq(double freq) /* frequency update */
+{
+ char tbuf[80];
+
+ drift_comp = freq;
+
+#ifdef KERNEL_PLL
+ /*
+ * If the kernel is enabled, update the kernel frequency.
+ */
+ if (pll_control && kern_enable) {
+ memset(&ntv, 0, sizeof(ntv));
+ ntv.modes = MOD_FREQUENCY;
+ ntv.freq = DTOFREQ(drift_comp);
+ ntp_adjtime(&ntv);
+ snprintf(tbuf, sizeof(tbuf), "kernel %.3f PPM", drift_comp * 1e6);
+ report_event(EVNT_FSET, NULL, tbuf);
+ } else {
+ snprintf(tbuf, sizeof(tbuf), "ntpd %.3f PPM", drift_comp * 1e6);
+ report_event(EVNT_FSET, NULL, tbuf);
+ }
+#else /* KERNEL_PLL */
+ snprintf(tbuf, sizeof(tbuf), "ntpd %.3f PPM", drift_comp * 1e6);
+ report_event(EVNT_FSET, NULL, tbuf);
+#endif /* KERNEL_PLL */
+}
+
+...
+...
+...
+
+#ifdef KERNEL_PLL
+ /*
+ * This code segment works when clock adjustments are made using
+ * precision time kernel support and the ntp_adjtime() system
+ * call. This support is available in Solaris 2.6 and later,
+ * Digital Unix 4.0 and later, FreeBSD, Linux and specially
+ * modified kernels for HP-UX 9 and Ultrix 4. In the case of the
+ * DECstation 5000/240 and Alpha AXP, additional kernel
+ * modifications provide a true microsecond clock and nanosecond
+ * clock, respectively.
+ *
+ * Important note: The kernel discipline is used only if the
+ * step threshold is less than 0.5 s, as anything higher can
+ * lead to overflow problems. This might occur if some misguided
+ * lad set the step threshold to something ridiculous.
+ */
+ if (pll_control && kern_enable) {
+
+#define MOD_BITS (MOD_OFFSET | MOD_MAXERROR | MOD_ESTERROR | MOD_STATUS | MOD_TIMECONST)
+
+ /*
+ * We initialize the structure for the ntp_adjtime()
+ * system call. We have to convert everything to
+ * microseconds or nanoseconds first. Do not update the
+ * system variables if the ext_enable flag is set. In
+ * this case, the external clock driver will update the
+ * variables, which will be read later by the local
+ * clock driver. Afterwards, remember the time and
+ * frequency offsets for jitter and stability values and
+ * to update the frequency file.
+ */
+ memset(&ntv, 0, sizeof(ntv));
+ if (ext_enable) {
+ ntv.modes = MOD_STATUS;
+ } else {
+#ifdef STA_NANO
+ ntv.modes = MOD_BITS | MOD_NANO;
+#else /* STA_NANO */
+ ntv.modes = MOD_BITS;
+#endif /* STA_NANO */
+ if (clock_offset < 0)
+ dtemp = -.5;
+ else
+ dtemp = .5;
+#ifdef STA_NANO
+ ntv.offset = (int32)(clock_offset * 1e9 + dtemp);
+ ntv.constant = sys_poll;
+#else /* STA_NANO */
+ ntv.offset = (int32)(clock_offset * 1e6 + dtemp);
+ ntv.constant = sys_poll - 4;
+#endif /* STA_NANO */
+ ntv.esterror = (u_int32)(clock_jitter * 1e6);
+ ntv.maxerror = (u_int32)((sys_rootdelay / 2 + sys_rootdisp) * 1e6);
+ ntv.status = STA_PLL;
+
+ /*
+ * Enable/disable the PPS if requested.
+ */
+ if (pps_enable) {
+ if (!(pll_status & STA_PPSTIME))
+ report_event(EVNT_KERN,
+ NULL, "PPS enabled");
+ ntv.status |= STA_PPSTIME | STA_PPSFREQ;
+ } else {
+ if (pll_status & STA_PPSTIME)
+ report_event(EVNT_KERN,
+ NULL, "PPS disabled");
+ ntv.status &= ~(STA_PPSTIME |
+ STA_PPSFREQ);
+ }
+ if (sys_leap == LEAP_ADDSECOND)
+ ntv.status |= STA_INS;
+ else if (sys_leap == LEAP_DELSECOND)
+ ntv.status |= STA_DEL;
+ }
+
+ /*
+ * Pass the stuff to the kernel. If it squeals, turn off
+ * the pps. In any case, fetch the kernel offset,
+ * frequency and jitter.
+ */
+ if (ntp_adjtime(&ntv) == TIME_ERROR) {
+ if (!(ntv.status & STA_PPSSIGNAL))
+ report_event(EVNT_KERN, NULL,
+ "PPS no signal");
+ }
+ pll_status = ntv.status;
+#ifdef STA_NANO
+ clock_offset = ntv.offset / 1e9;
+#else /* STA_NANO */
+ clock_offset = ntv.offset / 1e6;
+#endif /* STA_NANO */
+ clock_frequency = FREQTOD(ntv.freq);
+
+ /*
+ * If the kernel PPS is lit, monitor its performance.
+ */
+ if (ntv.status & STA_PPSTIME) {
+#ifdef STA_NANO
+ clock_jitter = ntv.jitter / 1e9;
+#else /* STA_NANO */
+ clock_jitter = ntv.jitter / 1e6;
+#endif /* STA_NANO */
+ }
+
+#if defined(STA_NANO) && NTP_API == 4
+ /*
+ * If the TAI changes, update the kernel TAI.
+ */
+ if (loop_tai != sys_tai) {
+ loop_tai = sys_tai;
+ ntv.modes = MOD_TAI;
+ ntv.constant = sys_tai;
+ ntp_adjtime(&ntv);
+ }
+#endif /* STA_NANO */
+ }
+#endif /* KERNEL_PLL */
+#endif