]> git.ipfire.org Git - thirdparty/chrony.git/blob - refclock_phc.c
ntp: fix log message for replaced source
[thirdparty/chrony.git] / refclock_phc.c
1 /*
2 chronyd/chronyc - Programs for keeping computer clocks accurate.
3
4 **********************************************************************
5 * Copyright (C) Miroslav Lichvar 2013, 2017
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 **********************************************************************
21
22 =======================================================================
23
24 PTP hardware clock (PHC) refclock driver.
25
26 */
27
28 #include "config.h"
29
30 #include "refclock.h"
31
32 #ifdef FEAT_PHC
33
34 #include "sysincl.h"
35
36 #include "refclock.h"
37 #include "hwclock.h"
38 #include "local.h"
39 #include "logging.h"
40 #include "memory.h"
41 #include "util.h"
42 #include "sched.h"
43 #include "sys_linux.h"
44
45 struct phc_instance {
46 int fd;
47 int mode;
48 int nocrossts;
49 int extpps;
50 int pin;
51 int channel;
52 HCL_Instance clock;
53 };
54
55 static void read_ext_pulse(int sockfd, int event, void *anything);
56
57 static int phc_initialise(RCL_Instance instance)
58 {
59 const char *options[] = {"nocrossts", "extpps", "pin", "channel", "clear", NULL};
60 struct phc_instance *phc;
61 int phc_fd, rising_edge;
62 char *path, *s;
63
64 RCL_CheckDriverOptions(instance, options);
65
66 path = RCL_GetDriverParameter(instance);
67
68 phc_fd = SYS_Linux_OpenPHC(path, 0);
69 if (phc_fd < 0) {
70 LOG_FATAL("Could not open PHC");
71 return 0;
72 }
73
74 phc = MallocNew(struct phc_instance);
75 phc->fd = phc_fd;
76 phc->mode = 0;
77 phc->nocrossts = RCL_GetDriverOption(instance, "nocrossts") ? 1 : 0;
78 phc->extpps = RCL_GetDriverOption(instance, "extpps") ? 1 : 0;
79
80 if (phc->extpps) {
81 s = RCL_GetDriverOption(instance, "pin");
82 phc->pin = s ? atoi(s) : 0;
83 s = RCL_GetDriverOption(instance, "channel");
84 phc->channel = s ? atoi(s) : 0;
85 rising_edge = RCL_GetDriverOption(instance, "clear") ? 0 : 1;
86 phc->clock = HCL_CreateInstance(0, 16, UTI_Log2ToDouble(RCL_GetDriverPoll(instance)));
87
88 if (!SYS_Linux_SetPHCExtTimestamping(phc->fd, phc->pin, phc->channel,
89 rising_edge, !rising_edge, 1))
90 LOG_FATAL("Could not enable external PHC timestamping");
91
92 SCH_AddFileHandler(phc->fd, SCH_FILE_INPUT, read_ext_pulse, instance);
93 } else {
94 phc->pin = phc->channel = 0;
95 phc->clock = NULL;
96 }
97
98 RCL_SetDriverData(instance, phc);
99 return 1;
100 }
101
102 static void phc_finalise(RCL_Instance instance)
103 {
104 struct phc_instance *phc;
105
106 phc = (struct phc_instance *)RCL_GetDriverData(instance);
107
108 if (phc->extpps) {
109 SCH_RemoveFileHandler(phc->fd);
110 SYS_Linux_SetPHCExtTimestamping(phc->fd, phc->pin, phc->channel, 0, 0, 0);
111 HCL_DestroyInstance(phc->clock);
112 }
113
114 close(phc->fd);
115 Free(phc);
116 }
117
118 static void read_ext_pulse(int fd, int event, void *anything)
119 {
120 RCL_Instance instance;
121 struct phc_instance *phc;
122 struct timespec phc_ts, local_ts;
123 double local_err;
124 int channel;
125
126 instance = anything;
127 phc = RCL_GetDriverData(instance);
128
129 if (!SYS_Linux_ReadPHCExtTimestamp(phc->fd, &phc_ts, &channel))
130 return;
131
132 if (channel != phc->channel) {
133 DEBUG_LOG("Unexpected extts channel %d\n", channel);
134 return;
135 }
136
137 if (!HCL_CookTime(phc->clock, &phc_ts, &local_ts, &local_err))
138 return;
139
140 RCL_AddCookedPulse(instance, &local_ts, 1.0e-9 * local_ts.tv_nsec, local_err,
141 UTI_DiffTimespecsToDouble(&phc_ts, &local_ts));
142 }
143
144 static int phc_poll(RCL_Instance instance)
145 {
146 struct phc_instance *phc;
147 struct timespec phc_ts, sys_ts, local_ts;
148 double offset, phc_err, local_err;
149
150 phc = (struct phc_instance *)RCL_GetDriverData(instance);
151
152 if (!SYS_Linux_GetPHCSample(phc->fd, phc->nocrossts, RCL_GetPrecision(instance),
153 &phc->mode, &phc_ts, &sys_ts, &phc_err))
154 return 0;
155
156 if (phc->extpps) {
157 LCL_CookTime(&sys_ts, &local_ts, &local_err);
158 HCL_AccumulateSample(phc->clock, &phc_ts, &local_ts, phc_err + local_err);
159 return 0;
160 }
161
162 offset = UTI_DiffTimespecsToDouble(&phc_ts, &sys_ts);
163
164 DEBUG_LOG("PHC offset: %+.9f err: %.9f", offset, phc_err);
165
166 return RCL_AddSample(instance, &sys_ts, offset, LEAP_Normal);
167 }
168
169 RefclockDriver RCL_PHC_driver = {
170 phc_initialise,
171 phc_finalise,
172 phc_poll
173 };
174
175 #else
176
177 RefclockDriver RCL_PHC_driver = { NULL, NULL, NULL };
178
179 #endif