Skip to contentSkip to conversation

can: gs_usb: new features: GS_CAN_FEATURE_GET_STATE, GS_CAN_FEATURE_BERR_REPORTING

PATCH Superseded Marc Kleine-Budde 1 participant 9 messages Oct 19, 2022 CAN network drivers

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Marc Kleine-Budde

author maintainer
#0 lore raw

v2 cover can: gs_usb: new features: GS_CAN_FEATURE_GET_STATE, GS_CAN_FEATURE_BERR_REPORTING

Hello,

this series targets the gs_us driver. First there are some cleanup patches by me, followed by now 4 patches by Jeroen Hofstee's. These patches are based on the venus-5.19.4-gsusb branch of his Linux repository and add 2 new features (GS_CAN_FEATURE_GET_STATE, GS_CAN_FEATURE_BERR_REPORTING) to the driver.

Changes since v1: https://lore.kernel.org/all/[email protected]
- added Jeroen's S-o-b to patches 4...7
- split former "[PATCH 4/6] can: gs_usb: add ability to enable / disable berr rerporting" into 2 patches, fix subject, update patch description

Changes since venus-5.19.4-gsusb: https://github.com/jhofstee/linux/tree/venus-5.19.4-gsusb
- fixed 2nd parameter of usb_control_msg_recv()
- rebased to current net-next/main
- renumbered the FEATURE, MODE and BREQ bits, as termination support
landed mainline first
- split GS_CAN_FEATURE_GET_STATE patch into documentation and
implementation part
- added Not-Signed-off-by: tag, as these patches are not Signed-off yet

regards,
Marc

author maintainer
#1 lore raw

PATCH v2 1/7 can: gs_usb: gs_can_open(): allow loopback and listen only at the same time

There's no reason why loopback and listen only should not be allowed at the same time. Replace the "else if" by "if" to reflect this in the code.

---
 drivers/net/can/usb/gs_usb.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/can/usb/gs_usb.c +2 -1
index 384847b4a4c9..aa6c84b7db06 100644
@@ -911,7 +911,8 @@ static int gs_can_open(struct net_device *netdev)
 	/* flags */
 	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
 		flags |= GS_CAN_MODE_LOOP_BACK;
-	else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
+
+	if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
 		flags |= GS_CAN_MODE_LISTEN_ONLY;
 
 	/* Controller is not allowed to retry TX
Signature · 1 line
2.35.1
author maintainer
#2 lore raw

PATCH v2 2/7 can: gs_usb: gs_can_open(): sort checks for ctrlmode

In reply to Marc Kleine-Budde

Sort the checks for dev->can.ctrlmode by values of CAN_CTRLMODE_*, so that it's clear where to add new checks.

While there, remove the comment that the Atmel UC3C hardware doesn't support One Shot Mode. The One Shot mode is only available and to be activated by the user, if the device specifies the feature bit GS_CAN_FEATURE_ONE_SHOT.

---
 drivers/net/can/usb/gs_usb.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/net/can/usb/gs_usb.c +5 -7
index aa6c84b7db06..5dad0ebb3d3e 100644
@@ -843,8 +843,6 @@ static int gs_can_open(struct net_device *netdev)
 
 	ctrlmode = dev->can.ctrlmode;
 	if (ctrlmode & CAN_CTRLMODE_FD) {
-		flags |= GS_CAN_MODE_FD;
-
 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
 			dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
 		else
@@ -915,14 +913,14 @@ static int gs_can_open(struct net_device *netdev)
 	if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
 		flags |= GS_CAN_MODE_LISTEN_ONLY;
 
-	/* Controller is not allowed to retry TX
-	 * this mode is unavailable on atmels uc3c hardware
-	 */
+	if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
+		flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
+
 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
 		flags |= GS_CAN_MODE_ONE_SHOT;
 
-	if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
-		flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
+	if (ctrlmode & CAN_CTRLMODE_FD)
+		flags |= GS_CAN_MODE_FD;
 
 	/* if hardware supports timestamps, enable it */
 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
Signature · 1 line
2.35.1
author maintainer
#3 lore raw

PATCH v2 3/7 can: gs_usb: gs_can_open(): merge setting of timestamp flags and init

In reply to Marc Kleine-Budde

Merge the bodies of 2 consecutive "if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)" statements.

---
 drivers/net/can/usb/gs_usb.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/can/usb/gs_usb.c +3 -3
index 5dad0ebb3d3e..f9e2b394c71e 100644
@@ -923,12 +923,12 @@ static int gs_can_open(struct net_device *netdev)
 		flags |= GS_CAN_MODE_FD;
 
 	/* if hardware supports timestamps, enable it */
-	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
+	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
 		flags |= GS_CAN_MODE_HW_TIMESTAMP;
 
-	/* start polling timestamp */
-	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
+		/* start polling timestamp */
 		gs_usb_timestamp_init(dev);
+	}
 
 	/* finally start device */
 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
Signature · 1 line
2.35.1
author maintainer
#4 lore raw

PATCH v2 4/7 can: gs_usb: document GS_CAN_FEATURE_BERR_REPORTING

In reply to Marc Kleine-Budde

From: Jeroen Hofstee <[email protected]>

Document the new feature ("GS_CAN_FEATURE_BERR_REPORTING") that indicates that the bus error reporting in the CAN controller can switched on and off with the GS_CAN_MODE_BERR_REPORTING mode bit in the GS_USB_BREQ_MODE control message.

---
 drivers/net/can/usb/gs_usb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/can/usb/gs_usb.c +3 -1
index f9e2b394c71e..68e474a762c5 100644
@@ -134,6 +134,7 @@ struct gs_device_config {
 /* GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) */
 /* GS_CAN_FEATURE_BT_CONST_EXT BIT(10) */
 /* GS_CAN_FEATURE_TERMINATION BIT(11) */
+#define GS_CAN_MODE_BERR_REPORTING BIT(12)
 
 struct gs_device_mode {
 	__le32 mode;
@@ -174,7 +175,8 @@ struct gs_device_termination_state {
 #define GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9)
 #define GS_CAN_FEATURE_BT_CONST_EXT BIT(10)
 #define GS_CAN_FEATURE_TERMINATION BIT(11)
-#define GS_CAN_FEATURE_MASK GENMASK(11, 0)
+#define GS_CAN_FEATURE_BERR_REPORTING BIT(12)
+#define GS_CAN_FEATURE_MASK GENMASK(12, 0)
 
 /* internal quirks - keep in GS_CAN_FEATURE space for now */
 
Signature · 1 line
2.35.1
author maintainer
#5 lore raw

PATCH v2 5/7 can: gs_usb: add ability to enable / disable berr reporting

In reply to Marc Kleine-Budde

From: Jeroen Hofstee <[email protected]>

The open source firmware candleLight report bus errors unconditionally. This adds support to enable / disable bus error reporting with the standard netlink property.

---
 drivers/net/can/usb/gs_usb.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/net/can/usb/gs_usb.c +6 -0
index 68e474a762c5..e2e23467caf2 100644
@@ -921,6 +921,9 @@ static int gs_can_open(struct net_device *netdev)
 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
 		flags |= GS_CAN_MODE_ONE_SHOT;
 
+	if (ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
+		flags |= GS_CAN_MODE_BERR_REPORTING;
+
 	if (ctrlmode & CAN_CTRLMODE_FD)
 		flags |= GS_CAN_MODE_FD;
 
@@ -1226,6 +1229,9 @@ static struct gs_can *gs_make_candev(unsigned int channel,
 		}
 	}
 
+	if (feature & GS_CAN_FEATURE_BERR_REPORTING)
+		dev->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
+
 	/* The CANtact Pro from LinkLayer Labs is based on the
 	 * LPC54616 µC, which is affected by the NXP LPC USB transfer
 	 * erratum. However, the current firmware (version 2) doesn't
Signature · 1 line
2.35.1
author maintainer
#6 lore raw

PATCH v2 6/7 can: gs_usb: document GS_CAN_FEATURE_GET_STATE

In reply to Marc Kleine-Budde

From: Jeroen Hofstee <[email protected]>

Document the new feature ("GS_CAN_FEATURE_GET_STATE") that indicates that the state of the CAN controller can be queried with the new GS_USB_BREQ_GET_STATE control message.

---
 drivers/net/can/usb/gs_usb.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/net/can/usb/gs_usb.c +4 -1
index e2e23467caf2..3f00e8d79b75 100644
@@ -66,6 +66,7 @@ enum gs_usb_breq {
 	GS_USB_BREQ_BT_CONST_EXT,
 	GS_USB_BREQ_SET_TERMINATION,
 	GS_USB_BREQ_GET_TERMINATION,
+	GS_USB_BREQ_GET_STATE,
 };
 
 enum gs_can_mode {
@@ -135,6 +136,7 @@ struct gs_device_config {
 /* GS_CAN_FEATURE_BT_CONST_EXT BIT(10) */
 /* GS_CAN_FEATURE_TERMINATION BIT(11) */
 #define GS_CAN_MODE_BERR_REPORTING BIT(12)
+/* GS_CAN_FEATURE_GET_STATE BIT(13) */
 
 struct gs_device_mode {
 	__le32 mode;
@@ -176,7 +178,8 @@ struct gs_device_termination_state {
 #define GS_CAN_FEATURE_BT_CONST_EXT BIT(10)
 #define GS_CAN_FEATURE_TERMINATION BIT(11)
 #define GS_CAN_FEATURE_BERR_REPORTING BIT(12)
-#define GS_CAN_FEATURE_MASK GENMASK(12, 0)
+#define GS_CAN_FEATURE_GET_STATE BIT(13)
+#define GS_CAN_FEATURE_MASK GENMASK(13, 0)
 
 /* internal quirks - keep in GS_CAN_FEATURE space for now */
 
Signature · 1 line
2.35.1
author maintainer
#7 lore raw

PATCH v2 7/7 can: gs_usb: add support for reading error counters

In reply to Marc Kleine-Budde

From: Jeroen Hofstee <[email protected]>

Add support for reading the device state and CAN error counters, using the GS_USB_BREQ_GET_STATE control message, if supported by the device, indicated by the GS_CAN_FEATURE_GET_STATE feature flag.

---
 drivers/net/can/usb/gs_usb.c | 39 ++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
drivers/net/can/usb/gs_usb.c +39 -0
index 3f00e8d79b75..b279cf54cca1 100644
@@ -961,6 +961,42 @@ static int gs_can_open(struct net_device *netdev)
 	return 0;
 }
 
+static int gs_usb_get_state(const struct net_device *netdev,
+			    struct can_berr_counter *bec,
+			    enum can_state *state)
+{
+	struct gs_can *dev = netdev_priv(netdev);
+	struct gs_device_state ds;
+	int rc;
+
+	rc = usb_control_msg_recv(interface_to_usbdev(dev->iface), 0,
+				  GS_USB_BREQ_GET_STATE,
+				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
+				  dev->channel, 0,
+				  &ds, sizeof(ds),
+				  USB_CTRL_GET_TIMEOUT,
+				  GFP_KERNEL);
+	if (rc)
+		return rc;
+
+	if (ds.state >= CAN_STATE_MAX)
+		return -EOPNOTSUPP;
+
+	*state = ds.state;
+	bec->txerr = ds.txerr;
+	bec->rxerr = ds.rxerr;
+
+	return 0;
+}
+
+static int gs_usb_can_get_berr_counter(const struct net_device *netdev,
+				       struct can_berr_counter *bec)
+{
+	enum can_state state;
+
+	return gs_usb_get_state(netdev, bec, &state);
+}
+
 static int gs_can_close(struct net_device *netdev)
 {
 	int rc;
@@ -1235,6 +1271,9 @@ static struct gs_can *gs_make_candev(unsigned int channel,
 	if (feature & GS_CAN_FEATURE_BERR_REPORTING)
 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
 
+	if (feature & GS_CAN_FEATURE_GET_STATE)
+		dev->can.do_get_berr_counter = gs_usb_can_get_berr_counter;
+
 	/* The CANtact Pro from LinkLayer Labs is based on the
 	 * LPC54616 µC, which is affected by the NXP LPC USB transfer
 	 * erratum. However, the current firmware (version 2) doesn't
Signature · 1 line
2.35.1
author maintainer
#8 lore raw

Re: [PATCH v2 7/7] can: gs_usb: add support for reading error counters

Quoted 34 lines · Marc Kleine-Budde

From: Jeroen Hofstee <[email protected]>

Add support for reading the device state and CAN error counters, using the GS_USB_BREQ_GET_STATE control message, if supported by the device, indicated by the GS_CAN_FEATURE_GET_STATE feature flag.

---
 drivers/net/can/usb/gs_usb.c | 39 ++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
drivers/net/can/usb/gs_usb.c +14 -0
index 3f00e8d79b75..b279cf54cca1 100644
@@ -961,6 +961,42 @@ static int gs_can_open(struct net_device *netdev)
 	return 0;
 }
 
+static int gs_usb_get_state(const struct net_device *netdev,
+			    struct can_berr_counter *bec,
+			    enum can_state *state)
+{
+	struct gs_can *dev = netdev_priv(netdev);
+	struct gs_device_state ds;
+	int rc;
+
+	rc = usb_control_msg_recv(interface_to_usbdev(dev->iface), 0,
+				  GS_USB_BREQ_GET_STATE,
+				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
+				  dev->channel, 0,
+				  &ds, sizeof(ds),
+				  USB_CTRL_GET_TIMEOUT,

drivers/net/can/usb/gs_usb.c static int gs_can_open(struct net_device *netdev) · line 987

Quoted 10 lines
drivers/net/can/usb/gs_usb.c +10 -0
+				  GFP_KERNEL);
+	if (rc)
+		return rc;
+
+	if (ds.state >= CAN_STATE_MAX)
+		return -EOPNOTSUPP;
+
+	*state = ds.state;
+	bec->txerr = ds.txerr;
+	bec->rxerr = ds.rxerr;

This is not endianness safe:

| drivers/net/can/usb/gs_usb.c:982:15: warning: restricted __le32 degrades to integer
| drivers/net/can/usb/gs_usb.c:985:16: warning: incorrect type in assignment (different base types)
| drivers/net/can/usb/gs_usb.c:985:16:    expected unsigned int enum can_state
| drivers/net/can/usb/gs_usb.c:985:16:    got restricted __le32 [addressable] [usertype] state
| drivers/net/can/usb/gs_usb.c:986:20: warning: incorrect type in assignment (different base types)
| drivers/net/can/usb/gs_usb.c:986:20:    expected unsigned short [usertype] txerr
| drivers/net/can/usb/gs_usb.c:986:20:    got restricted __le32 [addressable] [usertype] txerr
| drivers/net/can/usb/gs_usb.c:987:20: warning: incorrect type in assignment (different base types)
| drivers/net/can/usb/gs_usb.c:987:20:    expected unsigned short [usertype] rxerr
| drivers/net/can/usb/gs_usb.c:987:20:    got restricted __le32 [addressable] [usertype] rxerr

Fixes with:

drivers/net/can/usb/gs_usb.c +4 -4
index b279cf54cca1..ccb1a29835a2 100644
@@ -979,12 +979,12 @@ static int gs_usb_get_state(const struct net_device *netdev,
        if (rc)
                return rc;
 
-       if (ds.state >= CAN_STATE_MAX)
+       if (le32_to_cpu(ds.state) >= CAN_STATE_MAX)
                return -EOPNOTSUPP;
 
-       *state = ds.state;
-       bec->txerr = ds.txerr;
-       bec->rxerr = ds.rxerr;
+       *state = le32_to_cpu(ds.state);
+       bec->txerr = le32_to_cpu(ds.txerr);
+       bec->rxerr = le32_to_cpu(ds.rxerr);
 
        return 0;
 }

regards,
Marc

--

Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |