2012-01-25 02:12:57 +08:00
|
|
|
/**
|
|
|
|
* @file phc2sys.c
|
|
|
|
* @brief Utility program to synchronize two clocks via a PPS.
|
|
|
|
* @note Copyright (C) 2012 Richard Cochran <richardcochran@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2013-01-30 01:06:18 +08:00
|
|
|
#include <poll.h>
|
2012-01-25 02:12:57 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2012-12-18 21:00:49 +08:00
|
|
|
#include <inttypes.h>
|
2012-01-25 02:12:57 +08:00
|
|
|
|
|
|
|
#include <linux/pps.h>
|
|
|
|
#include <linux/ptp_clock.h>
|
|
|
|
|
2013-03-08 00:27:29 +08:00
|
|
|
#include "clockadj.h"
|
2013-01-30 01:06:18 +08:00
|
|
|
#include "ds.h"
|
|
|
|
#include "fsm.h"
|
2012-01-25 02:12:57 +08:00
|
|
|
#include "missing.h"
|
2013-04-08 21:44:05 +08:00
|
|
|
#include "phc.h"
|
2013-01-15 00:09:12 +08:00
|
|
|
#include "pi.h"
|
2013-01-30 01:06:18 +08:00
|
|
|
#include "pmc_common.h"
|
|
|
|
#include "print.h"
|
2013-01-15 00:09:12 +08:00
|
|
|
#include "servo.h"
|
2012-09-28 11:42:37 +08:00
|
|
|
#include "sk.h"
|
2013-02-08 02:56:53 +08:00
|
|
|
#include "stats.h"
|
2012-11-26 20:25:35 +08:00
|
|
|
#include "sysoff.h"
|
2013-01-30 01:06:18 +08:00
|
|
|
#include "tlv.h"
|
2013-03-08 00:27:30 +08:00
|
|
|
#include "util.h"
|
2012-12-10 17:28:28 +08:00
|
|
|
#include "version.h"
|
2012-01-25 02:12:57 +08:00
|
|
|
|
|
|
|
#define KP 0.7
|
|
|
|
#define KI 0.3
|
|
|
|
#define NS_PER_SEC 1000000000LL
|
|
|
|
|
2013-01-18 01:31:39 +08:00
|
|
|
#define PHC_PPS_OFFSET_LIMIT 10000000
|
2013-03-08 00:27:30 +08:00
|
|
|
#define PMC_UPDATE_INTERVAL (60 * NS_PER_SEC)
|
2013-01-18 01:31:39 +08:00
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
struct clock;
|
2013-03-08 00:27:30 +08:00
|
|
|
static int update_sync_offset(struct clock *clock, int64_t offset, uint64_t ts);
|
2013-03-08 00:27:28 +08:00
|
|
|
|
2012-01-25 02:12:57 +08:00
|
|
|
static clockid_t clock_open(char *device)
|
|
|
|
{
|
2013-04-08 21:44:07 +08:00
|
|
|
int clkid;
|
2012-11-01 22:15:35 +08:00
|
|
|
|
|
|
|
if (device[0] != '/') {
|
|
|
|
if (!strcasecmp(device, "CLOCK_REALTIME"))
|
|
|
|
return CLOCK_REALTIME;
|
|
|
|
|
|
|
|
fprintf(stderr, "unknown clock %s\n", device);
|
|
|
|
return CLOCK_INVALID;
|
|
|
|
}
|
|
|
|
|
2013-04-08 21:44:07 +08:00
|
|
|
clkid = phc_open(device);
|
|
|
|
if (clkid == CLOCK_INVALID)
|
2012-01-25 02:12:57 +08:00
|
|
|
fprintf(stderr, "cannot open %s: %m\n", device);
|
2013-04-08 21:44:07 +08:00
|
|
|
return clkid;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
2012-09-26 02:03:04 +08:00
|
|
|
static int read_phc(clockid_t clkid, clockid_t sysclk, int readings,
|
2013-02-08 02:56:52 +08:00
|
|
|
int64_t *offset, uint64_t *ts, int64_t *delay)
|
2012-01-25 02:12:57 +08:00
|
|
|
{
|
2012-08-29 21:28:42 +08:00
|
|
|
struct timespec tdst1, tdst2, tsrc;
|
|
|
|
int i;
|
|
|
|
int64_t interval, best_interval = INT64_MAX;
|
2012-01-25 02:12:57 +08:00
|
|
|
|
2012-08-29 21:28:42 +08:00
|
|
|
/* Pick the quickest clkid reading. */
|
2012-08-31 01:44:38 +08:00
|
|
|
for (i = 0; i < readings; i++) {
|
2012-08-29 21:28:42 +08:00
|
|
|
if (clock_gettime(sysclk, &tdst1) ||
|
|
|
|
clock_gettime(clkid, &tsrc) ||
|
|
|
|
clock_gettime(sysclk, &tdst2)) {
|
2013-02-06 00:36:08 +08:00
|
|
|
pr_err("failed to read clock: %m");
|
2012-08-29 21:28:42 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
interval = (tdst2.tv_sec - tdst1.tv_sec) * NS_PER_SEC +
|
|
|
|
tdst2.tv_nsec - tdst1.tv_nsec;
|
|
|
|
|
|
|
|
if (best_interval > interval) {
|
|
|
|
best_interval = interval;
|
|
|
|
*offset = (tdst1.tv_sec - tsrc.tv_sec) * NS_PER_SEC +
|
2012-09-26 02:03:04 +08:00
|
|
|
tdst1.tv_nsec - tsrc.tv_nsec + interval / 2;
|
2012-08-29 21:28:42 +08:00
|
|
|
*ts = tdst2.tv_sec * NS_PER_SEC + tdst2.tv_nsec;
|
|
|
|
}
|
2012-08-30 20:45:50 +08:00
|
|
|
}
|
2013-02-08 02:56:52 +08:00
|
|
|
*delay = best_interval;
|
2012-08-29 21:29:43 +08:00
|
|
|
|
|
|
|
return 1;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
2013-01-15 00:09:12 +08:00
|
|
|
struct clock {
|
|
|
|
clockid_t clkid;
|
|
|
|
struct servo *servo;
|
2013-03-08 00:27:30 +08:00
|
|
|
enum servo_state servo_state;
|
2013-01-15 00:09:12 +08:00
|
|
|
const char *source_label;
|
2013-02-08 02:56:53 +08:00
|
|
|
struct stats *offset_stats;
|
|
|
|
struct stats *freq_stats;
|
|
|
|
struct stats *delay_stats;
|
2013-02-12 20:23:05 +08:00
|
|
|
unsigned int stats_max_count;
|
2013-03-08 00:27:28 +08:00
|
|
|
int sync_offset;
|
|
|
|
int sync_offset_direction;
|
2013-03-08 00:27:30 +08:00
|
|
|
int leap;
|
|
|
|
int leap_set;
|
2013-03-08 00:27:33 +08:00
|
|
|
int kernel_leap;
|
2013-03-08 00:27:28 +08:00
|
|
|
struct pmc *pmc;
|
|
|
|
int pmc_ds_idx;
|
|
|
|
int pmc_ds_requested;
|
2013-03-08 00:27:30 +08:00
|
|
|
uint64_t pmc_last_update;
|
2012-01-25 02:12:57 +08:00
|
|
|
};
|
|
|
|
|
2013-02-08 02:56:53 +08:00
|
|
|
static void update_clock_stats(struct clock *clock,
|
|
|
|
int64_t offset, double freq, int64_t delay)
|
|
|
|
{
|
|
|
|
struct stats_result offset_stats, freq_stats, delay_stats;
|
|
|
|
|
|
|
|
stats_add_value(clock->offset_stats, offset);
|
|
|
|
stats_add_value(clock->freq_stats, freq);
|
|
|
|
if (delay >= 0)
|
|
|
|
stats_add_value(clock->delay_stats, delay);
|
|
|
|
|
|
|
|
if (stats_get_num_values(clock->offset_stats) < clock->stats_max_count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
stats_get_result(clock->offset_stats, &offset_stats);
|
|
|
|
stats_get_result(clock->freq_stats, &freq_stats);
|
|
|
|
|
|
|
|
if (!stats_get_result(clock->delay_stats, &delay_stats)) {
|
|
|
|
pr_info("rms %4.0f max %4.0f "
|
|
|
|
"freq %+6.0f +/- %3.0f "
|
|
|
|
"delay %5.0f +/- %3.0f",
|
|
|
|
offset_stats.rms, offset_stats.max_abs,
|
|
|
|
freq_stats.mean, freq_stats.stddev,
|
|
|
|
delay_stats.mean, delay_stats.stddev);
|
|
|
|
} else {
|
|
|
|
pr_info("rms %4.0f max %4.0f "
|
|
|
|
"freq %+6.0f +/- %3.0f",
|
|
|
|
offset_stats.rms, offset_stats.max_abs,
|
|
|
|
freq_stats.mean, freq_stats.stddev);
|
|
|
|
}
|
|
|
|
|
|
|
|
stats_reset(clock->offset_stats);
|
|
|
|
stats_reset(clock->freq_stats);
|
|
|
|
stats_reset(clock->delay_stats);
|
|
|
|
}
|
|
|
|
|
2013-02-08 02:56:52 +08:00
|
|
|
static void update_clock(struct clock *clock,
|
|
|
|
int64_t offset, uint64_t ts, int64_t delay)
|
2012-09-26 14:42:19 +08:00
|
|
|
{
|
2013-01-15 00:09:12 +08:00
|
|
|
enum servo_state state;
|
|
|
|
double ppb;
|
2012-09-26 14:42:19 +08:00
|
|
|
|
2013-03-08 00:27:30 +08:00
|
|
|
if (update_sync_offset(clock, offset, ts))
|
2013-03-08 00:27:28 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (clock->sync_offset_direction)
|
|
|
|
offset += clock->sync_offset * NS_PER_SEC *
|
|
|
|
clock->sync_offset_direction;
|
|
|
|
|
2013-01-15 00:09:12 +08:00
|
|
|
ppb = servo_sample(clock->servo, offset, ts, &state);
|
2013-03-08 00:27:30 +08:00
|
|
|
clock->servo_state = state;
|
2013-01-15 00:09:12 +08:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case SERVO_UNLOCKED:
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
2013-01-15 00:09:12 +08:00
|
|
|
case SERVO_JUMP:
|
2013-03-08 00:27:29 +08:00
|
|
|
clockadj_step(clock->clkid, -offset);
|
2013-01-15 00:09:12 +08:00
|
|
|
/* Fall through. */
|
|
|
|
case SERVO_LOCKED:
|
2013-03-08 00:27:29 +08:00
|
|
|
clockadj_set_freq(clock->clkid, -ppb);
|
2013-04-08 21:44:10 +08:00
|
|
|
if (clock->clkid == CLOCK_REALTIME)
|
|
|
|
sysclk_set_sync();
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-01-15 00:09:12 +08:00
|
|
|
|
2013-02-08 02:56:53 +08:00
|
|
|
if (clock->offset_stats) {
|
|
|
|
update_clock_stats(clock, offset, ppb, delay);
|
2013-02-08 02:56:52 +08:00
|
|
|
} else {
|
2013-02-08 02:56:53 +08:00
|
|
|
if (delay >= 0) {
|
|
|
|
pr_info("%s offset %9" PRId64 " s%d freq %+7.0f "
|
|
|
|
"delay %6" PRId64,
|
|
|
|
clock->source_label, offset, state, ppb, delay);
|
|
|
|
} else {
|
|
|
|
pr_info("%s offset %9" PRId64 " s%d freq %+7.0f",
|
|
|
|
clock->source_label, offset, state, ppb);
|
|
|
|
}
|
2013-02-08 02:56:52 +08:00
|
|
|
}
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
2012-08-29 21:29:43 +08:00
|
|
|
static int read_pps(int fd, int64_t *offset, uint64_t *ts)
|
2012-01-25 02:12:57 +08:00
|
|
|
{
|
|
|
|
struct pps_fdata pfd;
|
2012-08-29 21:29:43 +08:00
|
|
|
|
2012-01-25 02:12:57 +08:00
|
|
|
pfd.timeout.sec = 10;
|
|
|
|
pfd.timeout.nsec = 0;
|
|
|
|
pfd.timeout.flags = ~PPS_TIME_INVALID;
|
|
|
|
if (ioctl(fd, PPS_FETCH, &pfd)) {
|
2013-02-06 00:36:08 +08:00
|
|
|
pr_err("failed to fetch PPS: %m");
|
2012-01-25 02:12:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-08-29 21:29:43 +08:00
|
|
|
|
|
|
|
*ts = pfd.info.assert_tu.sec * NS_PER_SEC;
|
|
|
|
*ts += pfd.info.assert_tu.nsec;
|
|
|
|
|
|
|
|
*offset = *ts % NS_PER_SEC;
|
|
|
|
if (*offset > NS_PER_SEC / 2)
|
|
|
|
*offset -= NS_PER_SEC;
|
|
|
|
|
|
|
|
return 1;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
2013-02-06 00:36:07 +08:00
|
|
|
static int do_pps_loop(struct clock *clock, int fd,
|
2013-03-08 00:27:28 +08:00
|
|
|
clockid_t src, int n_readings)
|
2012-09-26 02:00:32 +08:00
|
|
|
{
|
2013-02-08 02:56:52 +08:00
|
|
|
int64_t pps_offset, phc_offset, phc_delay;
|
2013-01-18 01:31:39 +08:00
|
|
|
uint64_t pps_ts, phc_ts;
|
2012-09-26 02:00:32 +08:00
|
|
|
|
2013-01-15 00:09:12 +08:00
|
|
|
clock->source_label = "pps";
|
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
/* The sync offset can't be applied with PPS alone. */
|
|
|
|
if (src == CLOCK_INVALID)
|
|
|
|
clock->sync_offset_direction = 0;
|
|
|
|
|
2012-09-26 02:00:32 +08:00
|
|
|
while (1) {
|
|
|
|
if (!read_pps(fd, &pps_offset, &pps_ts)) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-18 01:31:39 +08:00
|
|
|
|
|
|
|
/* If a PHC is available, use it to get the whole number
|
|
|
|
of seconds in the offset and PPS for the rest. */
|
|
|
|
if (src != CLOCK_INVALID) {
|
|
|
|
if (!read_phc(src, clock->clkid, n_readings,
|
2013-02-08 02:56:52 +08:00
|
|
|
&phc_offset, &phc_ts, &phc_delay))
|
2013-01-18 01:31:39 +08:00
|
|
|
return -1;
|
2013-02-06 00:36:12 +08:00
|
|
|
|
2013-01-18 01:31:39 +08:00
|
|
|
/* Convert the time stamp to the PHC time. */
|
|
|
|
phc_ts -= phc_offset;
|
|
|
|
|
|
|
|
/* Check if it is close to the start of the second. */
|
|
|
|
if (phc_ts % NS_PER_SEC > PHC_PPS_OFFSET_LIMIT) {
|
2013-02-06 00:36:08 +08:00
|
|
|
pr_warning("PPS is not in sync with PHC"
|
|
|
|
" (0.%09lld)", phc_ts % NS_PER_SEC);
|
2013-01-18 01:31:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
phc_ts = phc_ts / NS_PER_SEC * NS_PER_SEC;
|
|
|
|
pps_offset = pps_ts - phc_ts;
|
|
|
|
}
|
|
|
|
|
2013-02-08 02:56:52 +08:00
|
|
|
update_clock(clock, pps_offset, pps_ts, -1);
|
2012-09-26 02:00:32 +08:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:09:12 +08:00
|
|
|
static int do_sysoff_loop(struct clock *clock, clockid_t src,
|
2013-03-08 00:27:28 +08:00
|
|
|
int rate, int n_readings)
|
2012-11-26 20:25:35 +08:00
|
|
|
{
|
|
|
|
uint64_t ts;
|
2013-02-08 02:56:52 +08:00
|
|
|
int64_t offset, delay;
|
2012-11-26 20:25:35 +08:00
|
|
|
int err = 0, fd = CLOCKID_TO_FD(src);
|
2013-01-15 00:09:12 +08:00
|
|
|
|
|
|
|
clock->source_label = "sys";
|
|
|
|
|
2012-11-26 20:25:35 +08:00
|
|
|
while (1) {
|
|
|
|
usleep(1000000 / rate);
|
2013-02-08 02:56:52 +08:00
|
|
|
if (sysoff_measure(fd, n_readings, &offset, &ts, &delay)) {
|
2012-11-26 20:25:35 +08:00
|
|
|
err = -1;
|
|
|
|
break;
|
|
|
|
}
|
2013-02-08 02:56:52 +08:00
|
|
|
update_clock(clock, offset, ts, delay);
|
2012-11-26 20:25:35 +08:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:09:13 +08:00
|
|
|
static int do_phc_loop(struct clock *clock, clockid_t src,
|
2013-03-08 00:27:28 +08:00
|
|
|
int rate, int n_readings)
|
2013-01-15 00:09:13 +08:00
|
|
|
{
|
|
|
|
uint64_t ts;
|
2013-02-08 02:56:52 +08:00
|
|
|
int64_t offset, delay;
|
2013-01-15 00:09:13 +08:00
|
|
|
|
|
|
|
clock->source_label = "phc";
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
usleep(1000000 / rate);
|
2013-02-08 02:56:52 +08:00
|
|
|
if (!read_phc(src, clock->clkid, n_readings,
|
|
|
|
&offset, &ts, &delay)) {
|
2013-01-15 00:09:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-08 02:56:52 +08:00
|
|
|
update_clock(clock, offset, ts, delay);
|
2013-01-15 00:09:13 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-30 01:06:18 +08:00
|
|
|
static int is_msg_mgt(struct ptp_message *msg)
|
|
|
|
{
|
|
|
|
struct TLV *tlv;
|
|
|
|
|
|
|
|
if (msg_type(msg) != MANAGEMENT)
|
|
|
|
return 0;
|
|
|
|
if (management_action(msg) != RESPONSE)
|
|
|
|
return 0;
|
|
|
|
if (msg->tlv_count != 1)
|
|
|
|
return 0;
|
|
|
|
tlv = (struct TLV *) msg->management.suffix;
|
|
|
|
if (tlv->type != TLV_MANAGEMENT)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_mgt_id(struct ptp_message *msg)
|
|
|
|
{
|
2013-04-04 23:28:30 +08:00
|
|
|
struct management_tlv *mgt = (struct management_tlv *) msg->management.suffix;
|
|
|
|
return mgt->id;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *get_mgt_data(struct ptp_message *msg)
|
|
|
|
{
|
2013-04-04 23:28:30 +08:00
|
|
|
struct management_tlv *mgt = (struct management_tlv *) msg->management.suffix;
|
|
|
|
return mgt->data;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
|
2013-04-08 21:44:08 +08:00
|
|
|
static int init_pmc(struct clock *clock, int domain_number)
|
2013-03-08 00:27:28 +08:00
|
|
|
{
|
2013-04-08 21:44:08 +08:00
|
|
|
clock->pmc = pmc_create(TRANS_UDS, "/var/run/phc2sys", 0,
|
|
|
|
domain_number, 0);
|
2013-03-08 00:27:28 +08:00
|
|
|
if (!clock->pmc) {
|
|
|
|
pr_err("failed to create pmc");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_pmc(struct clock *clock, int timeout,
|
|
|
|
int wait_sync, int get_utc_offset)
|
2013-01-30 01:06:18 +08:00
|
|
|
{
|
|
|
|
struct ptp_message *msg;
|
|
|
|
void *data;
|
|
|
|
#define N_FD 1
|
|
|
|
struct pollfd pollfd[N_FD];
|
2013-03-08 00:27:28 +08:00
|
|
|
int cnt, ds_done;
|
2013-01-30 01:06:18 +08:00
|
|
|
#define N_ID 2
|
|
|
|
int ds_ids[N_ID] = {
|
|
|
|
PORT_DATA_SET,
|
|
|
|
TIME_PROPERTIES_DATA_SET
|
|
|
|
};
|
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
while (clock->pmc_ds_idx < N_ID) {
|
|
|
|
/* Check if the data set is really needed. */
|
|
|
|
if ((ds_ids[clock->pmc_ds_idx] == PORT_DATA_SET &&
|
|
|
|
!wait_sync) ||
|
|
|
|
(ds_ids[clock->pmc_ds_idx] == TIME_PROPERTIES_DATA_SET &&
|
|
|
|
!get_utc_offset)) {
|
|
|
|
clock->pmc_ds_idx++;
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-30 01:06:18 +08:00
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
pollfd[0].fd = pmc_get_transport_fd(clock->pmc);
|
2013-01-30 01:06:18 +08:00
|
|
|
pollfd[0].events = POLLIN|POLLPRI;
|
2013-03-08 00:27:28 +08:00
|
|
|
if (!clock->pmc_ds_requested)
|
2013-01-30 01:06:18 +08:00
|
|
|
pollfd[0].events |= POLLOUT;
|
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
cnt = poll(pollfd, N_FD, timeout);
|
2013-01-30 01:06:18 +08:00
|
|
|
if (cnt < 0) {
|
2013-02-06 00:36:08 +08:00
|
|
|
pr_err("poll failed");
|
2013-01-30 01:06:18 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!cnt) {
|
2013-03-08 00:27:28 +08:00
|
|
|
/* Request the data set again in the next run. */
|
|
|
|
clock->pmc_ds_requested = 0;
|
|
|
|
return 0;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
/* Send a new request if there are no pending messages. */
|
|
|
|
if ((pollfd[0].revents & POLLOUT) &&
|
|
|
|
!(pollfd[0].revents & (POLLIN|POLLPRI))) {
|
|
|
|
pmc_send_get_action(clock->pmc,
|
|
|
|
ds_ids[clock->pmc_ds_idx]);
|
|
|
|
clock->pmc_ds_requested = 1;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(pollfd[0].revents & (POLLIN|POLLPRI)))
|
|
|
|
continue;
|
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
msg = pmc_recv(clock->pmc);
|
2013-01-30 01:06:18 +08:00
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
continue;
|
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
if (!is_msg_mgt(msg) ||
|
|
|
|
get_mgt_id(msg) != ds_ids[clock->pmc_ds_idx]) {
|
2013-01-30 01:06:18 +08:00
|
|
|
msg_put(msg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = get_mgt_data(msg);
|
|
|
|
ds_done = 0;
|
|
|
|
|
|
|
|
switch (get_mgt_id(msg)) {
|
|
|
|
case PORT_DATA_SET:
|
|
|
|
switch (((struct portDS *)data)->portState) {
|
|
|
|
case PS_MASTER:
|
|
|
|
case PS_SLAVE:
|
|
|
|
ds_done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case TIME_PROPERTIES_DATA_SET:
|
2013-03-08 00:27:28 +08:00
|
|
|
clock->sync_offset = ((struct timePropertiesDS *)data)->
|
|
|
|
currentUtcOffset;
|
2013-03-08 00:27:30 +08:00
|
|
|
if (((struct timePropertiesDS *)data)->flags & LEAP_61)
|
|
|
|
clock->leap = 1;
|
|
|
|
else if (((struct timePropertiesDS *)data)->flags & LEAP_59)
|
|
|
|
clock->leap = -1;
|
|
|
|
else
|
|
|
|
clock->leap = 0;
|
2013-01-30 01:06:18 +08:00
|
|
|
ds_done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ds_done) {
|
|
|
|
/* Proceed with the next data set. */
|
2013-03-08 00:27:28 +08:00
|
|
|
clock->pmc_ds_idx++;
|
|
|
|
clock->pmc_ds_requested = 0;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
msg_put(msg);
|
|
|
|
}
|
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
clock->pmc_ds_idx = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_pmc(struct clock *clock)
|
|
|
|
{
|
|
|
|
pmc_destroy(clock->pmc);
|
|
|
|
clock->pmc = NULL;
|
|
|
|
}
|
2013-01-30 01:06:18 +08:00
|
|
|
|
2013-03-08 00:27:30 +08:00
|
|
|
static int update_sync_offset(struct clock *clock, int64_t offset, uint64_t ts)
|
2013-03-08 00:27:28 +08:00
|
|
|
{
|
2013-03-08 00:27:30 +08:00
|
|
|
int clock_leap;
|
|
|
|
|
|
|
|
if (clock->pmc &&
|
|
|
|
!(ts > clock->pmc_last_update &&
|
|
|
|
ts - clock->pmc_last_update < PMC_UPDATE_INTERVAL)) {
|
|
|
|
if (run_pmc(clock, 0, 0, 1) > 0)
|
|
|
|
clock->pmc_last_update = ts;
|
2013-03-08 00:27:28 +08:00
|
|
|
}
|
2013-03-08 00:27:30 +08:00
|
|
|
|
|
|
|
/* Handle leap seconds. */
|
|
|
|
|
|
|
|
if (!clock->leap && !clock->leap_set)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* If the system clock is the master clock, get a time stamp from
|
|
|
|
it, as it is the clock which will include the leap second. */
|
|
|
|
if (clock->clkid != CLOCK_REALTIME) {
|
|
|
|
struct timespec tp;
|
|
|
|
if (clock_gettime(CLOCK_REALTIME, &tp)) {
|
|
|
|
pr_err("failed to read clock: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ts = tp.tv_sec * NS_PER_SEC + tp.tv_nsec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the clock will be stepped, the time stamp has to be the
|
|
|
|
target time. Ignore possible 1 second error in UTC offset. */
|
|
|
|
if (clock->clkid == CLOCK_REALTIME &&
|
|
|
|
clock->servo_state == SERVO_UNLOCKED) {
|
|
|
|
ts -= offset + clock->sync_offset * NS_PER_SEC *
|
|
|
|
clock->sync_offset_direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Suspend clock updates in the last second before midnight. */
|
|
|
|
if (is_utc_ambiguous(ts)) {
|
|
|
|
pr_info("clock update suspended due to leap second");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
clock_leap = leap_second_status(ts, clock->leap_set,
|
|
|
|
&clock->leap, &clock->sync_offset);
|
|
|
|
|
|
|
|
if (clock->leap_set != clock_leap) {
|
|
|
|
/* Only the system clock can leap. */
|
2013-03-08 00:27:33 +08:00
|
|
|
if (clock->clkid == CLOCK_REALTIME && clock->kernel_leap)
|
2013-04-08 21:44:09 +08:00
|
|
|
sysclk_set_leap(clock_leap);
|
2013-03-08 00:27:30 +08:00
|
|
|
clock->leap_set = clock_leap;
|
|
|
|
}
|
|
|
|
|
2013-01-30 01:06:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-25 02:12:57 +08:00
|
|
|
static void usage(char *progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"\n"
|
|
|
|
"usage: %s [options]\n\n"
|
2012-11-01 22:15:36 +08:00
|
|
|
" -c [dev|name] slave clock (CLOCK_REALTIME)\n"
|
|
|
|
" -d [dev] master PPS device\n"
|
|
|
|
" -s [dev|name] master clock\n"
|
|
|
|
" -i [iface] master clock by network interface\n"
|
|
|
|
" -P [kp] proportional constant (0.7)\n"
|
|
|
|
" -I [ki] integration constant (0.3)\n"
|
2013-02-06 00:36:04 +08:00
|
|
|
" -S [step] step threshold (disabled)\n"
|
2012-11-01 22:15:36 +08:00
|
|
|
" -R [rate] slave clock update rate in HZ (1)\n"
|
|
|
|
" -N [num] number of master clock readings per update (5)\n"
|
2012-11-06 00:13:30 +08:00
|
|
|
" -O [offset] slave-master time offset (0)\n"
|
2013-02-08 02:56:53 +08:00
|
|
|
" -u [num] number of clock updates in summary stats (0)\n"
|
2013-01-30 01:06:18 +08:00
|
|
|
" -w wait for ptp4l\n"
|
2013-04-08 21:44:08 +08:00
|
|
|
" -n [num] domain number (0)\n"
|
2013-03-08 00:27:33 +08:00
|
|
|
" -x apply leap seconds by servo instead of kernel\n"
|
2013-03-28 09:46:00 +08:00
|
|
|
" -l [num] set the logging level to 'num' (6)\n"
|
|
|
|
" -m print messages to stdout\n"
|
|
|
|
" -q do not print messages to the syslog\n"
|
2012-12-10 17:28:28 +08:00
|
|
|
" -v prints the software version and exits\n"
|
2013-03-28 09:46:00 +08:00
|
|
|
" -h prints this message and exits\n"
|
2012-01-25 02:12:57 +08:00
|
|
|
"\n",
|
|
|
|
progname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-02-06 00:36:07 +08:00
|
|
|
char *progname, *ethdev = NULL;
|
2013-01-15 00:09:12 +08:00
|
|
|
clockid_t src = CLOCK_INVALID;
|
2013-04-08 21:44:08 +08:00
|
|
|
int c, domain_number = 0, phc_readings = 5, phc_rate = 1, pps_fd = -1;
|
2013-04-08 21:44:05 +08:00
|
|
|
int max_ppb, r, wait_sync = 0, forced_sync_offset = 0;
|
2013-02-06 00:36:08 +08:00
|
|
|
int print_level = LOG_INFO, use_syslog = 1, verbose = 0;
|
2013-01-15 00:09:14 +08:00
|
|
|
double ppb;
|
2013-03-08 00:27:30 +08:00
|
|
|
struct clock dst_clock = {
|
|
|
|
.clkid = CLOCK_REALTIME,
|
|
|
|
.servo_state = SERVO_UNLOCKED,
|
2013-03-08 00:27:33 +08:00
|
|
|
.kernel_leap = 1,
|
2013-03-08 00:27:30 +08:00
|
|
|
};
|
2013-01-15 00:09:12 +08:00
|
|
|
|
|
|
|
configured_pi_kp = KP;
|
|
|
|
configured_pi_ki = KI;
|
2012-01-25 02:12:57 +08:00
|
|
|
|
|
|
|
/* Process the command line arguments. */
|
|
|
|
progname = strrchr(argv[0], '/');
|
|
|
|
progname = progname ? 1+progname : argv[0];
|
2013-02-08 02:56:53 +08:00
|
|
|
while (EOF != (c = getopt(argc, argv,
|
2013-04-08 21:44:08 +08:00
|
|
|
"c:d:hs:P:I:S:R:N:O:i:u:wn:xl:mqv"))) {
|
2012-01-25 02:12:57 +08:00
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
2013-01-15 00:09:12 +08:00
|
|
|
dst_clock.clkid = clock_open(optarg);
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
|
|
|
case 'd':
|
2013-02-06 00:36:07 +08:00
|
|
|
pps_fd = open(optarg, O_RDONLY);
|
|
|
|
if (pps_fd < 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"cannot open '%s': %m\n", optarg);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
src = clock_open(optarg);
|
|
|
|
break;
|
|
|
|
case 'P':
|
2013-01-15 00:09:12 +08:00
|
|
|
configured_pi_kp = atof(optarg);
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
|
|
|
case 'I':
|
2013-01-15 00:09:12 +08:00
|
|
|
configured_pi_ki = atof(optarg);
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
2013-02-06 00:36:04 +08:00
|
|
|
case 'S':
|
|
|
|
configured_pi_offset = atof(optarg);
|
|
|
|
break;
|
2012-08-31 01:44:38 +08:00
|
|
|
case 'R':
|
|
|
|
phc_rate = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
phc_readings = atoi(optarg);
|
|
|
|
break;
|
2012-11-06 00:13:30 +08:00
|
|
|
case 'O':
|
2013-03-08 00:27:28 +08:00
|
|
|
dst_clock.sync_offset = atoi(optarg);
|
|
|
|
dst_clock.sync_offset_direction = -1;
|
2013-01-30 01:06:18 +08:00
|
|
|
forced_sync_offset = 1;
|
2012-11-06 00:13:30 +08:00
|
|
|
break;
|
2012-09-28 11:42:37 +08:00
|
|
|
case 'i':
|
|
|
|
ethdev = optarg;
|
|
|
|
break;
|
2013-02-08 02:56:53 +08:00
|
|
|
case 'u':
|
|
|
|
dst_clock.stats_max_count = atoi(optarg);
|
|
|
|
break;
|
2013-01-30 01:06:18 +08:00
|
|
|
case 'w':
|
|
|
|
wait_sync = 1;
|
|
|
|
break;
|
2013-04-08 21:44:08 +08:00
|
|
|
case 'n':
|
|
|
|
domain_number = atoi(optarg);
|
|
|
|
break;
|
2013-03-08 00:27:33 +08:00
|
|
|
case 'x':
|
|
|
|
dst_clock.kernel_leap = 0;
|
|
|
|
break;
|
2013-02-06 00:36:08 +08:00
|
|
|
case 'l':
|
|
|
|
print_level = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
use_syslog = 0;
|
|
|
|
break;
|
2012-12-10 17:28:28 +08:00
|
|
|
case 'v':
|
|
|
|
version_show(stdout);
|
|
|
|
return 0;
|
2012-01-25 02:12:57 +08:00
|
|
|
case 'h':
|
|
|
|
usage(progname);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
usage(progname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-28 11:42:37 +08:00
|
|
|
if (src == CLOCK_INVALID && ethdev) {
|
2012-11-14 05:35:04 +08:00
|
|
|
struct sk_ts_info ts_info;
|
2012-09-28 11:42:37 +08:00
|
|
|
char phc_device[16];
|
2012-11-14 05:35:04 +08:00
|
|
|
if (sk_get_ts_info(ethdev, &ts_info) || !ts_info.valid) {
|
2012-09-28 11:42:37 +08:00
|
|
|
fprintf(stderr, "can't autodiscover PHC device\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-18 21:00:50 +08:00
|
|
|
if (ts_info.phc_index < 0) {
|
|
|
|
fprintf(stderr, "interface %s doesn't have a PHC\n", ethdev);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-14 05:35:04 +08:00
|
|
|
sprintf(phc_device, "/dev/ptp%d", ts_info.phc_index);
|
2012-09-28 11:42:37 +08:00
|
|
|
src = clock_open(phc_device);
|
|
|
|
}
|
2013-02-06 00:36:07 +08:00
|
|
|
if (!(pps_fd >= 0 || src != CLOCK_INVALID) ||
|
2013-01-18 01:31:40 +08:00
|
|
|
dst_clock.clkid == CLOCK_INVALID ||
|
2013-02-06 00:36:07 +08:00
|
|
|
(pps_fd >= 0 && dst_clock.clkid != CLOCK_REALTIME)) {
|
2012-01-25 02:12:57 +08:00
|
|
|
usage(progname);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-08-29 21:29:43 +08:00
|
|
|
|
2013-02-08 02:56:53 +08:00
|
|
|
if (dst_clock.stats_max_count > 0) {
|
|
|
|
dst_clock.offset_stats = stats_create();
|
|
|
|
dst_clock.freq_stats = stats_create();
|
|
|
|
dst_clock.delay_stats = stats_create();
|
|
|
|
if (!dst_clock.offset_stats ||
|
|
|
|
!dst_clock.freq_stats ||
|
|
|
|
!dst_clock.delay_stats) {
|
|
|
|
fprintf(stderr, "failed to create stats");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 01:06:18 +08:00
|
|
|
print_set_progname(progname);
|
2013-02-06 00:36:08 +08:00
|
|
|
print_set_verbose(verbose);
|
|
|
|
print_set_syslog(use_syslog);
|
|
|
|
print_set_level(print_level);
|
2013-01-30 01:06:18 +08:00
|
|
|
|
|
|
|
if (wait_sync) {
|
2013-04-08 21:44:08 +08:00
|
|
|
if (init_pmc(&dst_clock, domain_number))
|
2013-03-08 00:27:28 +08:00
|
|
|
return -1;
|
2013-01-30 01:06:18 +08:00
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
while (1) {
|
|
|
|
r = run_pmc(&dst_clock, 1000,
|
|
|
|
wait_sync, !forced_sync_offset);
|
|
|
|
if (r < 0)
|
|
|
|
return -1;
|
|
|
|
else if (r > 0)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
pr_notice("Waiting for ptp4l...");
|
|
|
|
}
|
2013-01-30 01:06:18 +08:00
|
|
|
|
|
|
|
if (!forced_sync_offset) {
|
|
|
|
if (src != CLOCK_REALTIME &&
|
|
|
|
dst_clock.clkid == CLOCK_REALTIME)
|
2013-03-08 00:27:28 +08:00
|
|
|
dst_clock.sync_offset_direction = 1;
|
2013-01-30 01:06:18 +08:00
|
|
|
else if (src == CLOCK_REALTIME &&
|
|
|
|
dst_clock.clkid != CLOCK_REALTIME)
|
2013-03-08 00:27:28 +08:00
|
|
|
dst_clock.sync_offset_direction = -1;
|
|
|
|
else
|
|
|
|
dst_clock.sync_offset_direction = 0;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
2013-03-08 00:27:28 +08:00
|
|
|
|
|
|
|
if (forced_sync_offset || !dst_clock.sync_offset_direction)
|
|
|
|
close_pmc(&dst_clock);
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
|
2013-03-08 00:27:29 +08:00
|
|
|
ppb = clockadj_get_freq(dst_clock.clkid);
|
2013-01-15 00:09:14 +08:00
|
|
|
/* The reading may silently fail and return 0, reset the frequency to
|
|
|
|
make sure ppb is the actual frequency of the clock. */
|
2013-03-08 00:27:29 +08:00
|
|
|
clockadj_set_freq(dst_clock.clkid, ppb);
|
2013-04-08 21:44:05 +08:00
|
|
|
if (dst_clock.clkid == CLOCK_REALTIME) {
|
2013-04-08 21:44:09 +08:00
|
|
|
sysclk_set_leap(0);
|
2013-04-08 21:44:05 +08:00
|
|
|
max_ppb = sysclk_max_freq();
|
|
|
|
} else {
|
|
|
|
max_ppb = phc_max_adj(dst_clock.clkid);
|
|
|
|
if (!max_ppb) {
|
|
|
|
pr_err("clock is not adjustable");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2013-01-15 00:09:12 +08:00
|
|
|
|
2013-01-15 00:09:14 +08:00
|
|
|
dst_clock.servo = servo_create(CLOCK_SERVO_PI, -ppb, max_ppb, 0);
|
2013-01-15 00:09:12 +08:00
|
|
|
|
2013-02-06 00:36:07 +08:00
|
|
|
if (pps_fd >= 0)
|
2013-03-08 00:27:28 +08:00
|
|
|
return do_pps_loop(&dst_clock, pps_fd, src, phc_readings);
|
2012-08-29 21:29:43 +08:00
|
|
|
|
2013-04-08 21:44:06 +08:00
|
|
|
if (dst_clock.clkid == CLOCK_REALTIME && src != CLOCK_REALTIME &&
|
2012-11-26 20:25:35 +08:00
|
|
|
SYSOFF_SUPPORTED == sysoff_probe(CLOCKID_TO_FD(src), phc_readings))
|
2013-03-08 00:27:28 +08:00
|
|
|
return do_sysoff_loop(&dst_clock, src, phc_rate, phc_readings);
|
2013-01-15 00:09:12 +08:00
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
return do_phc_loop(&dst_clock, src, phc_rate, phc_readings);
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|