On Mon, 26 Jun 2023 at 21:04, Jacob Keller wrote:
> 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
> ---
> missing.h | 5 +
> phc.c | 8 +++
> phc.h | 13 +++
> phc_ctl.c | 66 +++
> 4 files changed, 92 insertions(+)
>
> diff --git a/missing.h b/missing.h
> index 9b37dc258c0f..4a71307169ad 100644
> --- a/missing.h
> +++ b/missing.h
> @@ -240,10 +240,15 @@ struct ptp_pin_desc {
> unsigned int rsv[5];
> };
>
> +#define PTP_PIN_GETFUNC_IOWR(PTP_CLK_MAGIC, 6, struct ptp_pin_desc)
> #define PTP_PIN_SETFUNC_IOW(PTP_CLK_MAGIC, 7, struct ptp_pin_desc)
>
> #endif /*!PTP_PIN_SETFUNC*/
>
> +#ifndef PTP_PIN_GETFUNC2
> +#define PTP_PIN_GETFUNC2 _IOWR(PTP_CLK_MAGIC, 15, struct ptp_pin_desc)
> +#endif
> +
> #ifndef PTP_PIN_SETFUNC2
> #define PTP_PIN_SETFUNC2 _IOW(PTP_CLK_MAGIC, 16, struct ptp_pin_desc)
> #endif
> diff --git a/phc.c b/phc.c
> index 4491a91d55f0..11ca23a46cec 100644
> --- a/phc.c
> +++ b/phc.c
> @@ -108,6 +108,14 @@ int phc_number_pins(clockid_t clkid)
> return caps.n_pins;
> }
>
> +int phc_pin_getfunc(clockid_t clkid, struct ptp_pin_desc *desc)
> +{
> + int err = ioctl(CLOCKID_TO_FD(clkid), PTP_PIN_GETFUNC, desc);
> + if (err)
> + fprintf(stderr, "failed to set pin configuration, error:
> %m\n");
> + return err;
> +}
> +
> int phc_pin_setfunc(clockid_t clkid, struct ptp_pin_desc *desc)
> {
> int err = ioctl(CLOCKID_TO_FD(clkid), PTP_PIN_SETFUNC, desc);
> diff --git a/phc.h b/phc.h
> index 830e002a8690..064d29bf36e0 100644
> --- a/phc.h
> +++ b/phc.h
> @@ -66,6 +66,19 @@ int phc_max_adj(clockid_t clkid);
> */
> int phc_number_pins(clockid_t clkid);
>
> +/**
> + * Reads configuration of a pin of a PTP hardware clock device.
> + *
> + * @param clkid A clock ID obtained using phc_open().
> + *
> + * @param desc Pointer to a pin descriptor with the 'index' field set.
> On
> + * success, the rest of the structure is updated with the
> + * current pin configuration.
> + *
> + * @return Zero on success, non-zero otherwise.
> + */
> +int phc_pin_getfunc(clockid_t clkid, struct ptp_pin_desc *desc);
> +
> /**
> * Configures a pin of a PTP hardware clock device.
> *
> diff --git a/phc_ctl.c b/phc_ctl.c
> index d61d27d8f632..bb5337c14de7 100644
> --- a/phc_ctl.c
> +++ b/phc_ctl.c
> @@ -113,6 +113,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"
> " capsdisplay device capabilities (default if
> no command given)\n"
> + " get_pins_cfgdisplay device pins configuration\n"
> " wait pause between commands\n"
> "\n",
> progname);
> @@ -319,6 +320,70 @@ static int do_caps(clockid_t clkid, int cmdc, char
> *cmdv[])
> return 0;
> }
>
> +static const char *pin_func_string(unsigned int func)
> +{
> + switch (func) {
> + /* PTP_PF_NONE is already checked by the caller */
> + 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_pin_desc pin_desc;
> + unsigned int index;
> + int n_pins;
> +
> + if (clkid >= 0) {
> + pr_warning("The provided clock is not a PHC device.");
> + return 0;
> + }
> +
> + n_pins = phc_number_pins(clkid);
> + if (n_pins == 0) {
> + pr_notice("device has no configurable pins");
> + return (0);
>
For the record, it is not that important.
Richard can fix it before applying.
I just like consistency.
Above you return "0"
And here "(0)"
Erez
> + }
> +
> + pr_notice("device has %d configurable input/output pins",
> + n_pins);
> +
> + /* For each pin, read its configuration */
> + for (index = 0; index < n_pins; index++) {
> + memset(_desc, 0, sizeof(pin_desc));
> + pin_desc.index = index;
> +
> + if (phc_pin_getfunc(clkid, _desc)) {
> + pr_warning("get pin configuration for pin %d
> failed: %s",
> + index,
> + strerror(errno));
> + /* keep going */
> + continue;
> + }
> +