On Sat, Oct 09, 2021 at 05:11:53PM +0300, Vladimir Oltean wrote: > diff --git a/ts2phc.c b/ts2phc.c > index 67df5a532559..ca7684b314a4 100644 > --- a/ts2phc.c > +++ b/ts2phc.c > @@ -7,9 +7,14 @@ > * @note SPDX-License-Identifier: GPL-2.0+ > */ > #include <stdlib.h> > +#include <net/if.h> > +#include <sys/types.h> > +#include <unistd.h>
Alphabetical order please. > +#include "clockadj.h" > #include "config.h" > #include "interface.h" > +#include "phc.h" > #include "print.h" > #include "ts2phc.h" > #include "version.h" > @@ -27,6 +32,80 @@ static void ts2phc_cleanup(struct ts2phc_private *priv) > config_destroy(priv->cfg); > } > > +static struct servo *servo_add(struct ts2phc_private *priv, struct clock > *clock) > +{ This really wants a prefix in the name, like ts2phc_add_servo(); > + enum servo_type type = config_get_int(priv->cfg, NULL, "clock_servo"); > + struct servo *servo; > + int fadj, max_adj; > + > + fadj = (int) clockadj_get_freq(clock->clkid); > + /* Due to a bug in older kernels, the reading may silently fail > + * and return 0. Set the frequency back to make sure fadj is > + * the actual frequency of the clock. > + */ > + if (!clock->no_adj) { > + clockadj_set_freq(clock->clkid, fadj); > + } > + > + max_adj = phc_max_adj(clock->clkid); > + > + servo = servo_create(priv->cfg, type, -fadj, max_adj, 0); > + if (!servo) > + return NULL; > + > + servo_sync_interval(servo, SERVO_SYNC_INTERVAL); > + > + return servo; > +} > + > +struct clock *clock_add(struct ts2phc_private *priv, const char *device) > +{ Prefix please. > + clockid_t clkid = CLOCK_INVALID; > + int phc_index = -1; > + struct clock *c; > + int err; > + > + clkid = posix_clock_open(device, &phc_index); > + if (clkid == CLOCK_INVALID) > + return NULL; > + > + LIST_FOREACH(c, &priv->clocks, list) { > + if (c->phc_index == phc_index) { > + /* Already have the clock, don't add it again */ > + posix_clock_close(clkid); > + return c; > + } > + } > + > + c = calloc(1, sizeof(*c)); > + if (!c) { > + pr_err("failed to allocate memory for a clock"); > + return NULL; > + } > + c->clkid = clkid; > + c->phc_index = phc_index; > + c->servo_state = SERVO_UNLOCKED; > + c->servo = servo_add(priv, c); > + c->no_adj = config_get_int(priv->cfg, NULL, "free_running"); > + err = asprintf(&c->name, "/dev/ptp%d", phc_index); > + if (err < 0) { > + free(c); > + posix_clock_close(clkid); > + return NULL; > + } > + > + LIST_INSERT_HEAD(&priv->clocks, c, list); > + return c; > +} > + > +void clock_destroy(struct clock *c) > +{ Ditto. > + servo_destroy(c->servo); > + posix_clock_close(c->clkid); > + free(c->name); > + free(c); > +} > + > static void usage(char *progname) > { > fprintf(stderr, > diff --git a/ts2phc.h b/ts2phc.h > index e47ea4753617..43725e9edfdc 100644 > --- a/ts2phc.h > +++ b/ts2phc.h > @@ -7,16 +7,40 @@ > #ifndef HAVE_TS2PHC_H > #define HAVE_TS2PHC_H > > +#include <sys/queue.h> > +#include <time.h> > +#include "servo.h" > + > struct ts2phc_slave_array; > > +#define SERVO_SYNC_INTERVAL 1.0 > + > +struct clock { How about ts2phc_clock ? > + LIST_ENTRY(clock) list; > + LIST_ENTRY(clock) dst_list; > + clockid_t clkid; > + int phc_index; > + int state; > + int new_state; > + struct servo *servo; > + enum servo_state servo_state; > + char *name; > + bool no_adj; > +}; > + > struct ts2phc_private { > struct ts2phc_master *master; > STAILQ_HEAD(slave_ifaces_head, ts2phc_slave) slaves; > unsigned int n_slaves; > struct ts2phc_slave_array *polling_array; > struct config *cfg; > + struct clock *source; > + LIST_HEAD(clock_head, clock) clocks; > }; > > +struct clock *clock_add(struct ts2phc_private *priv, const char *device); > +void clock_destroy(struct clock *clock); > + > #include "ts2phc_master.h" > #include "ts2phc_slave.h" > Thanks, Richard _______________________________________________ Linuxptp-devel mailing list Linuxptp-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linuxptp-devel