netdev.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,
	Stephane Grosjean <s.grosjean@peak-system.com>,
	Lukas Magel <lukas.magel@posteo.net>,
	Marc Kleine-Budde <mkl@pengutronix.de>
Subject: [PATCH net-next 27/47] can: peak_usb: add ethtool interface to user-configurable CAN channel identifier
Date: Mon,  6 Feb 2023 14:16:00 +0100	[thread overview]
Message-ID: <20230206131620.2758724-28-mkl@pengutronix.de> (raw)
In-Reply-To: <20230206131620.2758724-1-mkl@pengutronix.de>

From: Stephane Grosjean <s.grosjean@peak-system.com>

This patch introduces 3 new functions implementing support for ethtool
access to the CAN channel ID of all USB CAN network interfaces managed by
the driver. With this patch, it is possible to read/write the CAN
channel ID from/to the EEPROM via the ethtool interface.

The CAN channel ID is a user-configurable device identifier that can be
set individually for each CAN interface of a PEAK USB device. Depending on
the device, the identifier has a length of 8 or 32 bit. The identifier
is stored in the non-volatile memory of the device.

The identifier of a CAN interface can be read/written as an 8 or 32 bit
byte string in native (little-endian) byte order, where the length depends
on the device type.

Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>
Signed-off-by: Lukas Magel <lukas.magel@posteo.net>
Link: https://lore.kernel.org/all/20230116200932.157769-6-lukas.magel@posteo.net
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/usb/peak_usb/pcan_usb.c      |  9 +++
 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 80 ++++++++++++++++++++
 drivers/net/can/usb/peak_usb/pcan_usb_core.h |  6 ++
 drivers/net/can/usb/peak_usb/pcan_usb_fd.c   |  3 +
 drivers/net/can/usb/peak_usb/pcan_usb_pro.c  |  3 +
 5 files changed, 101 insertions(+)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c
index 44e894a1f2c2..bead4f4ba472 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb.c
@@ -982,9 +982,18 @@ static int pcan_usb_set_phys_id(struct net_device *netdev,
 	return err;
 }
 
+/* This device only handles 8-bit CAN channel id. */
+static int pcan_usb_get_eeprom_len(struct net_device *netdev)
+{
+	return sizeof(u8);
+}
+
 static const struct ethtool_ops pcan_usb_ethtool_ops = {
 	.set_phys_id = pcan_usb_set_phys_id,
 	.get_ts_info = pcan_get_ts_info,
+	.get_eeprom_len	= pcan_usb_get_eeprom_len,
+	.get_eeprom = peak_usb_get_eeprom,
+	.set_eeprom = peak_usb_set_eeprom,
 };
 
 /*
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
index 3a73eac314ff..3bfd27742ae4 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -808,6 +808,86 @@ static const struct net_device_ops peak_usb_netdev_ops = {
 	.ndo_change_mtu = can_change_mtu,
 };
 
+/* CAN-USB devices generally handle 32-bit CAN channel IDs.
+ * In case one doesn't, then it have to overload this function.
+ */
+int peak_usb_get_eeprom_len(struct net_device *netdev)
+{
+	return sizeof(u32);
+}
+
+/* Every CAN-USB device exports the dev_get_can_channel_id() operation. It is used
+ * here to fill the data buffer with the user defined CAN channel ID.
+ */
+int peak_usb_get_eeprom(struct net_device *netdev,
+			struct ethtool_eeprom *eeprom, u8 *data)
+{
+	struct peak_usb_device *dev = netdev_priv(netdev);
+	u32 ch_id;
+	__le32 ch_id_le;
+	int err;
+
+	err = dev->adapter->dev_get_can_channel_id(dev, &ch_id);
+	if (err)
+		return err;
+
+	/* ethtool operates on individual bytes. The byte order of the CAN
+	 * channel id in memory depends on the kernel architecture. We
+	 * convert the CAN channel id back to the native byte order of the PEAK
+	 * device itself to ensure that the order is consistent for all
+	 * host architectures.
+	 */
+	ch_id_le = cpu_to_le32(ch_id);
+	memcpy(data, (u8 *)&ch_id_le + eeprom->offset, eeprom->len);
+
+	/* update cached value */
+	dev->can_channel_id = ch_id;
+	return err;
+}
+
+/* Every CAN-USB device exports the dev_get_can_channel_id()/dev_set_can_channel_id()
+ * operations. They are used here to set the new user defined CAN channel ID.
+ */
+int peak_usb_set_eeprom(struct net_device *netdev,
+			struct ethtool_eeprom *eeprom, u8 *data)
+{
+	struct peak_usb_device *dev = netdev_priv(netdev);
+	u32 ch_id;
+	__le32 ch_id_le;
+	int err;
+
+	/* first, read the current user defined CAN channel ID */
+	err = dev->adapter->dev_get_can_channel_id(dev, &ch_id);
+	if (err) {
+		netdev_err(netdev, "Failed to init CAN channel id (err %d)\n", err);
+		return err;
+	}
+
+	/* do update the value with user given bytes.
+	 * ethtool operates on individual bytes. The byte order of the CAN
+	 * channel ID in memory depends on the kernel architecture. We
+	 * convert the CAN channel ID back to the native byte order of the PEAK
+	 * device itself to ensure that the order is consistent for all
+	 * host architectures.
+	 */
+	ch_id_le = cpu_to_le32(ch_id);
+	memcpy((u8 *)&ch_id_le + eeprom->offset, data, eeprom->len);
+	ch_id = le32_to_cpu(ch_id_le);
+
+	/* flash the new value now */
+	err = dev->adapter->dev_set_can_channel_id(dev, ch_id);
+	if (err) {
+		netdev_err(netdev, "Failed to write new CAN channel id (err %d)\n",
+			   err);
+		return err;
+	}
+
+	/* update cached value with the new one */
+	dev->can_channel_id = ch_id;
+
+	return 0;
+}
+
 int pcan_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
 {
 	info->so_timestamping =
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.h b/drivers/net/can/usb/peak_usb/pcan_usb_core.h
index 1e461aef0f2a..980e315186cf 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.h
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.h
@@ -149,4 +149,10 @@ void peak_usb_async_complete(struct urb *urb);
 void peak_usb_restart_complete(struct peak_usb_device *dev);
 int pcan_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info);
 
+/* common 32-bit CAN channel ID ethtool management */
+int peak_usb_get_eeprom_len(struct net_device *netdev);
+int peak_usb_get_eeprom(struct net_device *netdev,
+			struct ethtool_eeprom *eeprom, u8 *data);
+int peak_usb_set_eeprom(struct net_device *netdev,
+			struct ethtool_eeprom *eeprom, u8 *data);
 #endif
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
index 1ea4cfdfd640..fd925ae96331 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
@@ -1124,6 +1124,9 @@ static int pcan_usb_fd_set_phys_id(struct net_device *netdev,
 static const struct ethtool_ops pcan_usb_fd_ethtool_ops = {
 	.set_phys_id = pcan_usb_fd_set_phys_id,
 	.get_ts_info = pcan_get_ts_info,
+	.get_eeprom_len	= peak_usb_get_eeprom_len,
+	.get_eeprom = peak_usb_get_eeprom,
+	.set_eeprom = peak_usb_set_eeprom,
 };
 
 /* describes the PCAN-USB FD adapter */
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
index 061f04c20f96..0c805d9672bf 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
@@ -1037,6 +1037,9 @@ static int pcan_usb_pro_set_phys_id(struct net_device *netdev,
 static const struct ethtool_ops pcan_usb_pro_ethtool_ops = {
 	.set_phys_id = pcan_usb_pro_set_phys_id,
 	.get_ts_info = pcan_get_ts_info,
+	.get_eeprom_len	= peak_usb_get_eeprom_len,
+	.get_eeprom = peak_usb_get_eeprom,
+	.set_eeprom = peak_usb_set_eeprom,
 };
 
 /*
-- 
2.39.1



  parent reply	other threads:[~2023-02-06 13:17 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-06 13:15 [PATCH net-next 0/47] pull-request: can-next 2023-02-06 Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 01/47] can: gw: give feedback on missing CGW_FLAGS_CAN_IIF_TX_OK flag Marc Kleine-Budde
2023-02-07 15:30   ` patchwork-bot+netdevbpf
2023-02-06 13:15 ` [PATCH net-next 02/47] can: isotp: check CAN address family in isotp_bind() Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 03/47] can: mcp251xfd: regmap: optimizing transfer size for CRC transfers size 1 Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 04/47] dt-bindings: can: renesas,rcar-canfd: R-Car V3U is R-Car Gen4 Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 05/47] dt-bindings: can: renesas,rcar-canfd: Document R-Car V4H support Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 06/47] dt-bindings: can: renesas,rcar-canfd: Add transceiver support Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 07/47] can: rcar_canfd: Fix R-Car V3U CAN mode selection Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 08/47] can: rcar_canfd: Fix R-Car V3U GAFLCFG field accesses Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 09/47] can: rcar_canfd: Abstract out DCFG address differences Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 10/47] can: rcar_canfd: Add support for R-Car Gen4 Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 11/47] can: rcar_canfd: Fix R-Car Gen4 DCFG.DSJW field width Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 12/47] can: rcar_canfd: Fix R-Car Gen4 CFCC.CFTML " Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 13/47] can: rcar_canfd: Sort included header files Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 14/47] can: rcar_canfd: Add helper variable dev Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 15/47] can: ems_pci: Fix code style, copyright and email address Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 16/47] can: ems_pci: Add Asix AX99100 definitions Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 17/47] can: ems_pci: Initialize BAR registers Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 18/47] can: ems_pci: Add read/write register and post irq functions Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 19/47] can: ems_pci: Initialize CAN controller base addresses Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 20/47] can: ems_pci: Add IRQ enable Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 21/47] can: ems_pci: Deassert hardware reset Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 22/47] can: ems_pci: Add myself as module author Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 23/47] can: peak_usb: rename device_id to CAN channel ID Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 24/47] can: peak_usb: add callback to read CAN channel ID of PEAK CAN-FD devices Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 25/47] can: peak_usb: allow flashing of the CAN channel ID Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 26/47] can: peak_usb: replace unregister_netdev() with unregister_candev() Marc Kleine-Budde
2023-02-06 13:16 ` Marc Kleine-Budde [this message]
2023-02-06 13:16 ` [PATCH net-next 28/47] can: peak_usb: export PCAN CAN channel ID as sysfs device attribute Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 29/47] can: peak_usb: align CAN channel ID format in log with sysfs attribute Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 30/47] can: peak_usb: Reorder include directives alphabetically Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 31/47] can: bittiming(): replace open coded variants of can_bit_time() Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 32/47] can: bittiming: can_fixup_bittiming(): use CAN_SYNC_SEG instead of 1 Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 33/47] can: bittiming: can_fixup_bittiming(): set effective tq Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 34/47] can: bittiming: can_get_bittiming(): use direct return and remove unneeded else Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 35/47] can: dev: register_candev(): ensure that bittiming const are valid Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 36/47] can: dev: register_candev(): bail out if both fixed bit rates and bit timing constants are provided Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 37/47] can: netlink: can_validate(): validate sample point for CAN and CAN-FD Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 38/47] can: netlink: can_changelink(): convert from netdev_err() to NL_SET_ERR_MSG_FMT() Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 39/47] can: bittiming: can_changelink() pass extack down callstack Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 40/47] can: bittiming: factor out can_sjw_set_default() and can_sjw_check() Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 41/47] can: bittiming: can_fixup_bittiming(): report error via netlink and harmonize error value Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 42/47] can: bittiming: can_sjw_check(): " Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 43/47] can: bittiming: can_sjw_check(): check that SJW is not longer than either Phase Buffer Segment Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 44/47] can: bittiming: can_sjw_set_default(): use Phase Seg2 / 2 as default for SJW Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 45/47] can: bittiming: can_calc_bittiming(): clean up SJW handling Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 46/47] can: bittiming: can_calc_bittiming(): convert from netdev_err() to NL_SET_ERR_MSG_FMT() Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 47/47] can: bittiming: can_validate_bitrate(): report error via netlink 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=20230206131620.2758724-28-mkl@pengutronix.de \
    --to=mkl@pengutronix.de \
    --cc=davem@davemloft.net \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=lukas.magel@posteo.net \
    --cc=netdev@vger.kernel.org \
    --cc=s.grosjean@peak-system.com \
    /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).