phc2sys: generalize run_pmc

Make run_pmc usable for any kind of management message. Create wrappers for
waiting for ptp4l and for getting UTC offset.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: split update_sync_offset

Split the generic (global) part of update_sync_offset and the part that
affects individual clocks. This is in preparation for phc2sys handling
synchronization of more clocks.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: split clock and node

Split members that apply to all synchronized clocks and members that apply
to an individual clock. Keep all clocks in a list, with a pointer to the
source clock. This will allow to support multiple clocks synchronization.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: store information about clocks being UTC or TAI

For now, only CLOCK_REALTIME can be UTC. This may stay this way forever but
now we have a clean separation between codepaths where CLOCK_REALTIME is
required and codepaths any UTC clock should take.

The main motiviation behind this change is removal of sync_offset_direction.
It has to be computed on the fly based on the source and destination when we
have multiple clocks supported and automatic following of ptp4l state
changes implemented.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: rearrange declarations

This just moves code around to have related functions together and forward
declaration at the beginning of the file. No code changes.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: open devices in clock_add

Do not call clock_open to open a clock device but let clock_add do that and
return the newly created struct. Also, store the device (interface) name in
struct clock.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: track ports

Add tracking of which ports have been added and to which clock they belong.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
pmc_common: easy way to set port and broadcast target

Implement pmc_target_port to set a port number, leaving clock identity
unchanged, and pmc_target_all to set clock identity and port number to all
1's.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: event subscription

Add support for subscribing to events (run_pmc_subscribe) and receiving and
handling of received events (run_pmc_events).

Add initial support for port status changes.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: propagate received errors

Recognize errors returned in MANAGEMENT_ERROR_STATUS TLV and return
a distinct value from run_pmc in case such error is received.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: autoconfiguration

Add automatic configuration option (-a).

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: autoconfigure realtime clock on demand only

By default, do not synchronize CLOCK_REALTIME. To do it, -r option is
needed. That will only consider CLOCK_REALTIME as the destination. To
consider it also as a possible time source, use -rr.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: check clockIdentity

Make sure that we handle only one PTP clock (node). This is for an extra
safety.

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: man page update for -a and -r options

Signed-off-by: Jiri Benc <jbenc@redhat.com>
phc2sys: reset sync offset if non PTP timescale

When grandmaster does not use PTP timescale but the previous one did, we
need to reset sync offset and leap flag.

Signed-off-by: Jiri Benc <jbenc@redhat.com>