Add a new function to phc_ctl to display the devices pin configuration
data. First, obtain the device capabilities to determine the number of
pins. Then, for each pin, print the name, function, and channel
information.

Signed-off-by: Jacob Keller <jacob.e.kel...@intel.com>
---
Changes since v2:
* made output a bit less verbose
* changed pin_cfg to get_pins_cfg

 phc_ctl.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/phc_ctl.c b/phc_ctl.c
index 92e597c40a23..6522f8d212b4 100644
--- a/phc_ctl.c
+++ b/phc_ctl.c
@@ -114,6 +114,7 @@ static void usage(const char *progname)
                "  freq [ppb]      adjust PHC frequency (default returns 
current offset)\n"
                "  cmp             compare PHC offset to CLOCK_REALTIME\n"
                "  caps            display device capabilities (default if no 
command given)\n"
+               "  get_pins_cfg    display device pins configuration\n"
                "  wait <seconds>  pause between commands\n"
                "\n",
                progname);
@@ -320,6 +321,77 @@ static int do_caps(clockid_t clkid, int cmdc, char *cmdv[])
        return 0;
 }
 
+static const char *pin_func_string(unsigned int func)
+{
+       switch (func) {
+       case PTP_PF_NONE:
+               return "no function";
+       case PTP_PF_EXTTS:
+               return "external timestamping";
+       case PTP_PF_PEROUT:
+               return "periodic output";
+       case PTP_PF_PHYSYNC:
+               return "phy sync";
+       default:
+               return "unknown function";
+       }
+}
+
+static int do_get_pins_cfg(clockid_t clkid, int cmdc, char *cmdv[])
+{
+       struct ptp_clock_caps caps;
+       struct ptp_pin_desc pin_desc;
+       unsigned int index;
+
+       if (clkid == CLOCK_REALTIME) {
+               pr_warning("CLOCK_REALTIME is not a PHC device.");
+               return 0;
+       }
+
+       /* Get device capabilities first */
+       if (ioctl(CLOCKID_TO_FD(clkid), PTP_CLOCK_GETCAPS, &caps)) {
+               pr_err("get capabilities failed: %s",
+                       strerror(errno));
+               return -1;
+       }
+
+       if (caps.n_pins == 0) {
+               pr_notice("device has no configurable pins");
+               return (0);
+       }
+
+       pr_notice("device has %d configurable input/output pins",
+               caps.n_pins);
+
+       /* For each pin, read its configuration */
+       for (index = 0; index < caps.n_pins; index++) {
+               memset(&pin_desc, 0, sizeof(pin_desc));
+               pin_desc.index = index;
+
+               if (ioctl(CLOCKID_TO_FD(clkid), PTP_PIN_GETFUNC, &pin_desc)) {
+                       pr_warning("get pin configuration for pin %d failed: 
%s",
+                               index,
+                               strerror(errno));
+                       /* keep going */
+                       continue;
+               }
+
+               if (pin_desc.func == PTP_PF_NONE) {
+                       pr_notice("pin %d [%s] not configured",
+                               pin_desc.index,
+                               pin_desc.name);
+               } else {
+                       pr_notice("pin %d [%s] %s on channel %d",
+                               pin_desc.index,
+                               pin_desc.name,
+                               pin_func_string(pin_desc.func),
+                               pin_desc.chan);
+               }
+       }
+
+       return 0;
+}
+
 static int do_cmp(clockid_t clkid, int cmdc, char *cmdv[])
 {
        int64_t sys_offset, delay;
@@ -403,6 +475,7 @@ static const struct cmd_t all_commands[] = {
        { "freq", &do_freq },
        { "cmp", &do_cmp },
        { "caps", &do_caps },
+       { "get_pins_cfg", &do_get_pins_cfg },
        { "wait", &do_wait },
        { 0, 0 }
 };
-- 
2.38.0.83.gd420dda05763



_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to