From: Stephane Grosjean <s.grosjean@peak-system.com>
To: linux-can Mailing List <linux-can@vger.kernel.org>
Cc: Stephane Grosjean <s.grosjean@peak-system.com>
Subject: [PATCH 6/6] can: peak_usb: add ethtool interface to user defined flashed device number
Date: Fri, 28 Jan 2022 16:01:57 +0100 [thread overview]
Message-ID: <20220128150157.1222850-7-s.grosjean@peak-system.com> (raw)
In-Reply-To: <20220128150157.1222850-1-s.grosjean@peak-system.com>
This patch introduces 3 new functions implementing support for eeprom
access of USB - CAN network interfaces managed by the driver, through the
ethtool interface. All of them (except the PCAN-USB interface) interpret
the 4 data bytes as a 32-bit value to be read/write in the non-volatile
memory of the device. The PCAN-USB only manages a single byte value.
Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>
---
drivers/net/can/usb/peak_usb/pcan_usb.c | 9 +++
drivers/net/can/usb/peak_usb/pcan_usb_core.c | 69 ++++++++++++++++++++
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, 90 insertions(+)
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c
index b29daaab2e6e..60c9329701a5 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb.c
@@ -981,8 +981,17 @@ static int pcan_usb_set_phys_id(struct net_device *netdev,
return err;
}
+/* This device only handles 8-bit user device 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_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 aa8bcdcfa2fb..4e858d592e59 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -782,6 +782,75 @@ static const struct net_device_ops peak_usb_netdev_ops = {
.ndo_change_mtu = can_change_mtu,
};
+/* CAN-USB devices generally handle 32-bit user device id.
+ * 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_user_devid() operation. It is used
+ * here to fill the data buffer with the user defined device number.
+ */
+int peak_usb_get_eeprom(struct net_device *netdev,
+ struct ethtool_eeprom *eeprom, u8 *data)
+{
+ struct peak_usb_device *dev = netdev_priv(netdev);
+ u32 devid;
+ int err;
+
+ if (!eeprom->len)
+ return -EINVAL;
+
+ err = dev->adapter->dev_get_user_devid(dev, &devid);
+ if (!err) {
+ memcpy(data, (u8 *)&devid + eeprom->offset, eeprom->len);
+
+ /* update cached value */
+ dev->device_number = devid;
+ }
+
+ return err;
+}
+
+/* Every CAN-USB device exports the dev_get_user_devid()/dev_set_user_devid()
+ * operations. They are used here to set the new user defined device number.
+ */
+int peak_usb_set_eeprom(struct net_device *netdev,
+ struct ethtool_eeprom *eeprom, u8 *data)
+{
+ struct peak_usb_device *dev = netdev_priv(netdev);
+ u32 devid;
+ int err;
+
+ if (!eeprom->len)
+ return -EINVAL;
+
+ /* first, read the current user defined device value number */
+ err = dev->adapter->dev_get_user_devid(dev, &devid);
+ if (err) {
+ netdev_err(netdev, "Failed to init device id (err %d)\n", err);
+ return err;
+ }
+
+ /* do update the value with user given bytes */
+ memcpy((u8 *)&devid + eeprom->offset, data, eeprom->len);
+
+ /* flash the new value now */
+ err = dev->adapter->dev_set_user_devid(dev, devid);
+ if (err) {
+ netdev_err(netdev, "Failed to write new device id (err %d)\n",
+ err);
+ return err;
+ }
+
+ /* update cached value with the new one */
+ dev->device_number = devid;
+
+ return 0;
+}
+
/*
* create one device which is attached to CAN controller #ctrl_idx of the
* usb adapter.
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 7fdc779986f0..4f4394733208 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.h
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.h
@@ -147,4 +147,10 @@ int peak_usb_netif_rx_64(struct sk_buff *skb, u32 ts_low, u32 ts_high);
void peak_usb_async_complete(struct urb *urb);
void peak_usb_restart_complete(struct peak_usb_device *dev);
+/* common 32-bit devid 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 7440d5b145b5..cec09c7ffce2 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
@@ -1072,6 +1072,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_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 e98b08746e04..35177a3d1eba 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
@@ -1036,6 +1036,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_eeprom_len = peak_usb_get_eeprom_len,
+ .get_eeprom = peak_usb_get_eeprom,
+ .set_eeprom = peak_usb_set_eeprom,
};
/*
--
2.25.1
next prev parent reply other threads:[~2022-01-28 15:02 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-28 15:01 [PATCH 0/6] can: peak_usb: add ethtool interface to flashed value Stephane Grosjean
2022-01-28 15:01 ` [PATCH 1/6] can: peak_usb: rename a callback to a more explicit name Stephane Grosjean
2022-01-28 15:01 ` [PATCH 2/6] can: peak_usb: add callback to read user value of CANFD devices Stephane Grosjean
2022-01-29 13:48 ` Marc Kleine-Budde
2022-01-28 15:01 ` [PATCH 3/6] can: peak_usb: correction of a wrong field name Stephane Grosjean
2022-01-28 15:01 ` [PATCH 4/6] can: peak_usb: allow flashing of user defined value Stephane Grosjean
2022-01-29 13:53 ` Marc Kleine-Budde
2022-01-28 15:01 ` [PATCH 5/6] can: peak_usb: replace unregister_netdev() with unregister_candev() Stephane Grosjean
2022-01-28 15:01 ` Stephane Grosjean [this message]
2022-01-29 13:58 ` [PATCH 6/6] can: peak_usb: add ethtool interface to user defined flashed device number Marc Kleine-Budde
2022-01-31 14:25 Stéphane Grosjean
2022-01-31 14:31 ` Marc Kleine-Budde
2022-02-11 10:57 ` Stéphane Grosjean
2022-02-15 8:34 ` Stéphane Grosjean
2022-02-15 15:10 ` Marc Kleine-Budde
2022-02-16 9:51 ` Marc Kleine-Budde
2022-02-21 9:29 Stéphane Grosjean
2022-02-21 22:44 ` Vincent Mailhol
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=20220128150157.1222850-7-s.grosjean@peak-system.com \
--to=s.grosjean@peak-system.com \
--cc=linux-can@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).