linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Marc Kleine-Budde <mkl@pengutronix.de>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org,
	kernel@pengutronix.de,
	Vincent Mailhol <mailhol.vincent@wanadoo.fr>,
	Andrew Lunn <andrew@lunn.ch>,
	Marc Kleine-Budde <mkl@pengutronix.de>
Subject: [PATCH net-next 19/39] can: etas_es58x: export product information through devlink_ops::info_get()
Date: Mon, 12 Dec 2022 12:30:25 +0100	[thread overview]
Message-ID: <20221212113045.222493-20-mkl@pengutronix.de> (raw)
In-Reply-To: <20221212113045.222493-1-mkl@pengutronix.de>

From: Vincent Mailhol <mailhol.vincent@wanadoo.fr>

ES58x devices report below product information through a custom usb
string:

  * the firmware version
  * the bootloader version
  * the hardware revision

Parse this string, store the results in struct es58x_dev, export:

  * the firmware version through devlink's "fw" name
  * the bootloader version through devlink's "fw.bootloader" name
  * the hardware revisionthrough devlink's "board.rev" name

Those devlink entries are not critical to use the device, if parsing
fails, print an informative log message and continue to probe the
device.

In addition to that, use usb_device::serial to report the device
serial number.

Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Link: https://lore.kernel.org/all/20221130174658.29282-6-mailhol.vincent@wanadoo.fr
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/usb/etas_es58x/es58x_core.c   |   1 +
 drivers/net/can/usb/etas_es58x/es58x_core.h   |  41 ++++
 .../net/can/usb/etas_es58x/es58x_devlink.c    | 222 ++++++++++++++++++
 3 files changed, 264 insertions(+)

diff --git a/drivers/net/can/usb/etas_es58x/es58x_core.c b/drivers/net/can/usb/etas_es58x/es58x_core.c
index de884de9fe57..4d6d5a4ac06e 100644
--- a/drivers/net/can/usb/etas_es58x/es58x_core.c
+++ b/drivers/net/can/usb/etas_es58x/es58x_core.c
@@ -2271,6 +2271,7 @@ static int es58x_probe(struct usb_interface *intf,
 	if (ret)
 		return ret;
 
+	es58x_parse_product_info(es58x_dev);
 	devlink_register(priv_to_devlink(es58x_dev));
 
 	for (ch_idx = 0; ch_idx < es58x_dev->num_can_ch; ch_idx++) {
diff --git a/drivers/net/can/usb/etas_es58x/es58x_core.h b/drivers/net/can/usb/etas_es58x/es58x_core.h
index a76789119229..c1ba1a4e8857 100644
--- a/drivers/net/can/usb/etas_es58x/es58x_core.h
+++ b/drivers/net/can/usb/etas_es58x/es58x_core.h
@@ -359,6 +359,39 @@ struct es58x_operators {
 	int (*get_timestamp)(struct es58x_device *es58x_dev);
 };
 
+/**
+ * struct es58x_sw_version - Version number of the firmware or the
+ *	bootloader.
+ * @major: Version major number, represented on two digits.
+ * @minor: Version minor number, represented on two digits.
+ * @revision: Version revision number, represented on two digits.
+ *
+ * The firmware and the bootloader share the same format: "xx.xx.xx"
+ * where 'x' is a digit. Both can be retrieved from the product
+ * information string.
+ */
+struct es58x_sw_version {
+	u8 major;
+	u8 minor;
+	u8 revision;
+};
+
+/**
+ * struct es58x_hw_revision - Hardware revision number.
+ * @letter: Revision letter.
+ * @major: Version major number, represented on three digits.
+ * @minor: Version minor number, represented on three digits.
+ *
+ * The hardware revision uses its own format: "axxx/xxx" where 'a' is
+ * a letter and 'x' a digit. It can be retrieved from the product
+ * information string.
+ */
+struct es58x_hw_revision {
+	char letter;
+	u16 major;
+	u16 minor;
+};
+
 /**
  * struct es58x_device - All information specific to an ES58X device.
  * @dev: Device information.
@@ -376,6 +409,9 @@ struct es58x_operators {
  *	queue wake/stop logic should prevent this URB from getting
  *	empty. Please refer to es58x_get_tx_urb() for more details.
  * @tx_urbs_idle_cnt: number of urbs in @tx_urbs_idle.
+ * @firmware_version: The firmware version number.
+ * @bootloader_version: The bootloader version number.
+ * @hardware_revision: The hardware revision number.
  * @ktime_req_ns: kernel timestamp when es58x_set_realtime_diff_ns()
  *	was called.
  * @realtime_diff_ns: difference in nanoseconds between the clocks of
@@ -411,6 +447,10 @@ struct es58x_device {
 	struct usb_anchor tx_urbs_idle;
 	atomic_t tx_urbs_idle_cnt;
 
+	struct es58x_sw_version firmware_version;
+	struct es58x_sw_version bootloader_version;
+	struct es58x_hw_revision hardware_revision;
+
 	u64 ktime_req_ns;
 	s64 realtime_diff_ns;
 
@@ -696,6 +736,7 @@ int es58x_send_msg(struct es58x_device *es58x_dev, u8 cmd_type, u8 cmd_id,
 		   const void *msg, u16 cmd_len, int channel_idx);
 
 /* es58x_devlink.c. */
+void es58x_parse_product_info(struct es58x_device *es58x_dev);
 extern const struct devlink_ops es58x_dl_ops;
 
 /* es581_4.c. */
diff --git a/drivers/net/can/usb/etas_es58x/es58x_devlink.c b/drivers/net/can/usb/etas_es58x/es58x_devlink.c
index af6ca7ada23f..9fba29e2f57c 100644
--- a/drivers/net/can/usb/etas_es58x/es58x_devlink.c
+++ b/drivers/net/can/usb/etas_es58x/es58x_devlink.c
@@ -7,7 +7,229 @@
  * Copyright (c) 2022 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
  */
 
+#include <linux/ctype.h>
+#include <linux/device.h>
+#include <linux/usb.h>
 #include <net/devlink.h>
 
+#include "es58x_core.h"
+
+/* USB descriptor index containing the product information string. */
+#define ES58X_PROD_INFO_IDX 6
+
+/**
+ * es58x_parse_sw_version() - Extract boot loader or firmware version.
+ * @es58x_dev: ES58X device.
+ * @prod_info: USB custom string returned by the device.
+ * @prefix: Select which information should be parsed. Set it to "FW"
+ *	to parse the firmware version or to "BL" to parse the
+ *	bootloader version.
+ *
+ * The @prod_info string contains the firmware and the bootloader
+ * version number all prefixed by a magic string and concatenated with
+ * other numbers. Depending on the device, the firmware (bootloader)
+ * format is either "FW_Vxx.xx.xx" ("BL_Vxx.xx.xx") or "FW:xx.xx.xx"
+ * ("BL:xx.xx.xx") where 'x' represents a digit. @prod_info must
+ * contains the common part of those prefixes: "FW" or "BL".
+ *
+ * Parse @prod_info and store the version number in
+ * &es58x_dev.firmware_version or &es58x_dev.bootloader_version
+ * according to @prefix value.
+ *
+ * Return: zero on success, -EINVAL if @prefix contains an invalid
+ *	value and -EBADMSG if @prod_info could not be parsed.
+ */
+static int es58x_parse_sw_version(struct es58x_device *es58x_dev,
+				  const char *prod_info, const char *prefix)
+{
+	struct es58x_sw_version *version;
+	int major, minor, revision;
+
+	if (!strcmp(prefix, "FW"))
+		version = &es58x_dev->firmware_version;
+	else if (!strcmp(prefix, "BL"))
+		version = &es58x_dev->bootloader_version;
+	else
+		return -EINVAL;
+
+	/* Go to prefix */
+	prod_info = strstr(prod_info, prefix);
+	if (!prod_info)
+		return -EBADMSG;
+	/* Go to beginning of the version number */
+	while (!isdigit(*prod_info)) {
+		prod_info++;
+		if (!*prod_info)
+			return -EBADMSG;
+	}
+
+	if (sscanf(prod_info, "%2u.%2u.%2u", &major, &minor, &revision) != 3)
+		return -EBADMSG;
+
+	version->major = major;
+	version->minor = minor;
+	version->revision = revision;
+
+	return 0;
+}
+
+/**
+ * es58x_parse_hw_rev() - Extract hardware revision number.
+ * @es58x_dev: ES58X device.
+ * @prod_info: USB custom string returned by the device.
+ *
+ * @prod_info contains the hardware revision prefixed by a magic
+ * string and conquenated together with other numbers. Depending on
+ * the device, the hardware revision format is either
+ * "HW_VER:axxx/xxx" or "HR:axxx/xxx" where 'a' represents a letter
+ * and 'x' a digit.
+ *
+ * Parse @prod_info and store the hardware revision number in
+ * &es58x_dev.hardware_revision.
+ *
+ * Return: zero on success, -EBADMSG if @prod_info could not be
+ *	parsed.
+ */
+static int es58x_parse_hw_rev(struct es58x_device *es58x_dev,
+			      const char *prod_info)
+{
+	char letter;
+	int major, minor;
+
+	/* The only occurrence of 'H' is in the hardware revision prefix. */
+	prod_info = strchr(prod_info, 'H');
+	if (!prod_info)
+		return -EBADMSG;
+	/* Go to beginning of the hardware revision */
+	prod_info = strchr(prod_info, ':');
+	if (!prod_info)
+		return -EBADMSG;
+	prod_info++;
+
+	if (sscanf(prod_info, "%c%3u/%3u", &letter, &major, &minor) != 3)
+		return -EBADMSG;
+
+	es58x_dev->hardware_revision.letter = letter;
+	es58x_dev->hardware_revision.major = major;
+	es58x_dev->hardware_revision.minor = minor;
+
+	return 0;
+}
+
+/**
+ * es58x_parse_product_info() - Parse the ES58x product information
+ *	string.
+ * @es58x_dev: ES58X device.
+ *
+ * Retrieve the product information string and parse it to extract the
+ * firmware version, the bootloader version and the hardware
+ * revision.
+ *
+ * If the function fails, simply emit a log message and continue
+ * because product information is not critical for the driver to
+ * operate.
+ */
+void es58x_parse_product_info(struct es58x_device *es58x_dev)
+{
+	char *prod_info;
+
+	prod_info = usb_cache_string(es58x_dev->udev, ES58X_PROD_INFO_IDX);
+	if (!prod_info) {
+		dev_warn(es58x_dev->dev,
+			 "could not retrieve the product info string\n");
+		return;
+	}
+
+	if (es58x_parse_sw_version(es58x_dev, prod_info, "FW") ||
+	    es58x_parse_sw_version(es58x_dev, prod_info, "BL") ||
+	    es58x_parse_hw_rev(es58x_dev, prod_info))
+		dev_info(es58x_dev->dev,
+			 "could not parse product info: '%s'\n", prod_info);
+
+	kfree(prod_info);
+}
+
+/**
+ * es58x_sw_version_is_set() - Check if the version is a valid number.
+ * @sw_ver: Version number of either the firmware or the bootloader.
+ *
+ * If &es58x_sw_version.major, &es58x_sw_version.minor and
+ * &es58x_sw_version.revision are all zero, the product string could
+ * not be parsed and the version number is invalid.
+ */
+static inline bool es58x_sw_version_is_set(struct es58x_sw_version *sw_ver)
+{
+	return sw_ver->major || sw_ver->minor || sw_ver->revision;
+}
+
+/**
+ * es58x_hw_revision_is_set() - Check if the revision is a valid number.
+ * @hw_rev: Revision number of the hardware.
+ *
+ * If &es58x_hw_revision.letter is the null character, the product
+ * string could not be parsed and the hardware revision number is
+ * invalid.
+ */
+static inline bool es58x_hw_revision_is_set(struct es58x_hw_revision *hw_rev)
+{
+	return hw_rev->letter != '\0';
+}
+
+/**
+ * es58x_devlink_info_get() - Report the product information.
+ * @devlink: Devlink.
+ * @req: skb wrapper where to put requested information.
+ * @extack: Unused.
+ *
+ * Report the firmware version, the bootloader version, the hardware
+ * revision and the serial number through netlink.
+ *
+ * Return: zero on success, errno when any error occurs.
+ */
+static int es58x_devlink_info_get(struct devlink *devlink,
+				  struct devlink_info_req *req,
+				  struct netlink_ext_ack *extack)
+{
+	struct es58x_device *es58x_dev = devlink_priv(devlink);
+	struct es58x_sw_version *fw_ver = &es58x_dev->firmware_version;
+	struct es58x_sw_version *bl_ver = &es58x_dev->bootloader_version;
+	struct es58x_hw_revision *hw_rev = &es58x_dev->hardware_revision;
+	char buf[max(sizeof("xx.xx.xx"), sizeof("axxx/xxx"))];
+	int ret = 0;
+
+	if (es58x_sw_version_is_set(fw_ver)) {
+		snprintf(buf, sizeof(buf), "%02u.%02u.%02u",
+			 fw_ver->major, fw_ver->minor, fw_ver->revision);
+		ret = devlink_info_version_running_put(req,
+						       DEVLINK_INFO_VERSION_GENERIC_FW,
+						       buf);
+		if (ret)
+			return ret;
+	}
+
+	if (es58x_sw_version_is_set(bl_ver)) {
+		snprintf(buf, sizeof(buf), "%02u.%02u.%02u",
+			 bl_ver->major, bl_ver->minor, bl_ver->revision);
+		ret = devlink_info_version_running_put(req,
+						       DEVLINK_INFO_VERSION_GENERIC_FW_BOOTLOADER,
+						       buf);
+		if (ret)
+			return ret;
+	}
+
+	if (es58x_hw_revision_is_set(hw_rev)) {
+		snprintf(buf, sizeof(buf), "%c%03u/%03u",
+			 hw_rev->letter, hw_rev->major, hw_rev->minor);
+		ret = devlink_info_version_fixed_put(req,
+						     DEVLINK_INFO_VERSION_GENERIC_BOARD_REV,
+						     buf);
+		if (ret)
+			return ret;
+	}
+
+	return devlink_info_serial_number_put(req, es58x_dev->udev->serial);
+}
+
 const struct devlink_ops es58x_dl_ops = {
+	.info_get = es58x_devlink_info_get,
 };
-- 
2.35.1



  parent reply	other threads:[~2022-12-12 11:32 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-12 11:30 [PATCH net-next 0/39] pull-request: can-next 2022-12-12 Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 01/39] can: kvaser_usb: kvaser_usb_set_bittiming(): fix redundant initialization warning for err Marc Kleine-Budde
2022-12-12 12:20   ` patchwork-bot+netdevbpf
2022-12-12 11:30 ` [PATCH net-next 02/39] can: kvaser_usb: kvaser_usb_set_{,data}bittiming(): remove empty lines in variable declaration Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 03/39] can: rcar_canfd: rcar_canfd_probe: Add struct rcar_canfd_hw_info to driver data Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 04/39] can: m_can: sort header inclusion alphabetically Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 05/39] can: rcar_canfd: Add max_channels to struct rcar_canfd_hw_info Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 06/39] can: rcar_canfd: Add shared_global_irqs " Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 07/39] can: rcar_canfd: Add postdiv " Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 08/39] can: rcar_canfd: Add multi_channel_irqs " Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 09/39] can: ctucanfd: Drop obsolete dependency on COMPILE_TEST Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 10/39] can: etas_es58x: sort the includes by alphabetic order Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 11/39] can: flexcan: add auto stop mode for IMX93 to support wakeup Marc Kleine-Budde
2022-12-13  2:22   ` Bough Chen
2022-12-13  8:11     ` Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 12/39] can: etas_es58x: add devlink support Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 13/39] dt-bindings: can: fsl,flexcan: add imx93 compatible Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 14/39] can: etas_es58x: add devlink port support Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 15/39] dt-bindings: can: renesas,rcar-canfd: Document RZ/Five SoC Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 16/39] USB: core: export usb_cache_string() Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 17/39] can: c_can: use devm_platform_get_and_ioremap_resource() Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 18/39] net: devlink: add DEVLINK_INFO_VERSION_GENERIC_FW_BOOTLOADER Marc Kleine-Budde
2022-12-12 11:30 ` Marc Kleine-Budde [this message]
2022-12-12 11:30 ` [PATCH net-next 20/39] can: etas_es58x: remove es58x_get_product_info() Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 21/39] Documentation: devlink: add devlink documentation for the etas_es58x driver Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 22/39] can: ucan: use strscpy() to instead of strncpy() Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 23/39] net: af_can: remove useless parameter 'err' in 'can_rx_register()' Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 24/39] can: ucan: remove unused ucan_priv::intf Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 25/39] can: gs_usb: remove gs_can::iface Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 26/39] can: m_can: Call the RAM init directly from m_can_chip_config Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 27/39] can: raw: add support for SO_MARK Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 28/39] dt-bindings: can: renesas,rcar-canfd: Fix number of channels for R-Car V3U Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 29/39] can: m_can: Eliminate double read of TXFQS in tx_handler Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 30/39] can: m_can: Avoid reading irqstatus twice Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 31/39] can: m_can: Read register PSR only on error Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 32/39] can: m_can: Count TXE FIFO getidx in the driver Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 33/39] can: m_can: Count read getindex " Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 34/39] can: m_can: Batch acknowledge transmit events Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 35/39] can: m_can: Batch acknowledge rx fifo Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 36/39] can: tcan4x5x: Remove invalid write in clear_interrupts Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 37/39] can: tcan4x5x: Fix use of register error status mask Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 38/39] can: tcan4x5x: Fix register range of first two blocks Marc Kleine-Budde
2022-12-12 11:30 ` [PATCH net-next 39/39] can: tcan4x5x: Specify separate read/write ranges Marc Kleine-Budde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221212113045.222493-20-mkl@pengutronix.de \
    --to=mkl@pengutronix.de \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol.vincent@wanadoo.fr \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).