Skip to contentSkip to conversation

can: add CAN interface termination API

RFC PATCH Superseded Oliver Hartkopp 2 participants 3 messages Jan 7, 2017 – Jan 8, 2017 CAN network drivers

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 can: add CAN interface termination API

This patch adds a netlink interface to configure the CAN bus termination of CAN interfaces.

Inside the driver an array of supported termination values is defined:

const u16 drvname_termination = { 60, 120, CAN_TERMINATION_DISABLED };

struct drvname_priv *priv;
priv = netdev_priv(dev);

priv->termination_const = &drvname_termination; priv->termination_const_size = sizeof(drvname_termination); priv->termination = CAN_TERMINATION_DISABLED;

And the funtion to set the value has to be defined:

priv->do_set_termination = drvname_set_termination;

---
 drivers/net/can/dev.c            | 34 +++++++++++++++++++++++++++++++++-
 include/linux/can/dev.h          |  4 ++++
 include/uapi/linux/can/netlink.h |  5 +++++
 3 files changed, 42 insertions(+), 1 deletion(-)
drivers/net/can/dev.c +33 -1
index 8d6208c0b400..a779172cffcb 100644
@@ -958,6 +958,27 @@ static int can_changelink(struct net_device *dev,
 		}
 	}
 
+	if (data[IFLA_CAN_TERMINATION]) {
+		u16 t = nla_get_u16(data[IFLA_CAN_TERMINATION]);
+		int num_t = priv->termination_const_size / sizeof(u16);
+		int i;
+
+		/* check whether given value is supported by the interface */
+		for (i = 0; i < num_t && t != priv->termination_const[i]; i++);
+		if (i >= num_t)
+			return -EINVAL;
+
+		priv->termination = t;
+
+		if (priv->do_set_termination) {
+			/* Finally, set the termination */
+			err = priv->do_set_termination(dev);
+			if (err)
+				return err;
+		} else
+			return -EOPNOTSUPP;
+	}
+
 	return 0;
 }
 
@@ -980,6 +1001,11 @@ static size_t can_get_size(const struct net_device *dev)
 		size += nla_total_size(sizeof(struct can_bittiming));
 	if (priv->data_bittiming_const)				/* IFLA_CAN_DATA_BITTIMING_CONST */
 		size += nla_total_size(sizeof(struct can_bittiming_const));
+	if (priv->termination_const &&
+	    priv->termination_const_size) {			/* IFLA_CAN_TERMINATION_CONST */
+		size += nla_total_size(priv->termination_const_size);
+		size += nla_total_size(sizeof(u16));		/* IFLA_CAN_TERMINATION */
+	}
 
 	return size;
 }
@@ -1018,7 +1044,13 @@ static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
 	    (priv->data_bittiming_const &&
 	     nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
 		     sizeof(*priv->data_bittiming_const),
-		     priv->data_bittiming_const)))
+		     priv->data_bittiming_const)) ||
+
+	    (priv->termination_const && priv->termination_const_size &&
+	     nla_put(skb, IFLA_CAN_TERMINATION_CONST,
+		     priv->termination_const_size,
+		     priv->termination_const) &&
+	     nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination)))
 		return -EMSGSIZE;
 
 	return 0;
include/linux/can/dev.h +4 -0
index 5f5270941ba0..8539fbdc5f5e 100644
@@ -38,6 +38,9 @@ struct can_priv {
 	struct can_bittiming bittiming, data_bittiming;
 	const struct can_bittiming_const *bittiming_const,
 		*data_bittiming_const;
+	const u16 *termination_const;
+	const int termination_const_size;
+	u16 termination;
 	struct can_clock clock;
 
 	enum can_state state;
@@ -53,6 +56,7 @@ struct can_priv {
 	int (*do_set_bittiming)(struct net_device *dev);
 	int (*do_set_data_bittiming)(struct net_device *dev);
 	int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
+	int (*do_set_termination)(struct net_device *dev);
 	int (*do_get_state)(const struct net_device *dev,
 			    enum can_state *state);
 	int (*do_get_berr_counter)(const struct net_device *dev,
include/uapi/linux/can/netlink.h +5 -0
index 94ffe0c83ce7..c52347a20477 100644
@@ -127,9 +127,14 @@ enum {
 	IFLA_CAN_BERR_COUNTER,
 	IFLA_CAN_DATA_BITTIMING,
 	IFLA_CAN_DATA_BITTIMING_CONST,
+	IFLA_CAN_TERMINATION,
+	IFLA_CAN_TERMINATION_CONST,
 	__IFLA_CAN_MAX
 };
 
 #define IFLA_CAN_MAX	(__IFLA_CAN_MAX - 1)
 
+/* termination range: 1..65534 Ohms */
+#define CAN_TERMINATION_DISABLED 0xFFFFU
+
 #endif /* !_UAPI_CAN_NETLINK_H */
Signature · 1 line
2.11.0
#1 lore raw

Hi Oliver,

Looks good to me. Two minor comments below.

drivers/net/can/dev.c static int can_changelink(struct net_device *dev, · line 971

Quoted 11 lines
drivers/net/can/dev.c +11 -0
+       if (data[IFLA_CAN_TERMINATION]) {
+               u16 t = nla_get_u16(data[IFLA_CAN_TERMINATION]);
+               int num_t = priv->termination_const_size / sizeof(u16);
+               int i;
+
+               /* check whether given value is supported by the interface */
+               for (i = 0; i < num_t && t != priv->termination_const[i]; i++);
+               if (i >= num_t)
+                       return -EINVAL;
+
+               priv->termination = t;

I would update the value after successful call to do_set_termination.

include/uapi/linux/can/netlink.h enum { · line 139

Quoted 3 lines
include/uapi/linux/can/netlink.h +3 -0
+/* termination range: 1..65534 Ohms */
+#define CAN_TERMINATION_DISABLED 0xFFFFU
+

0 seems more natural to me. Why you are using 0xFFFF?

Regards,
Remik

#2 lore raw

Hi Remik,

Quoted 5 lines · Remigiusz
Quoted 5 lines
drivers/net/can/dev.c +5 -0
+       if (data[IFLA_CAN_TERMINATION]) {
+               u16 t = nla_get_u16(data[IFLA_CAN_TERMINATION]);
+               int num_t = priv->termination_const_size / sizeof(u16);
+               int i;
+
Quoted 8 lines

drivers/net/can/dev.c static int can_changelink(struct net_device *dev, · line 971

Quoted 6 lines
drivers/net/can/dev.c +6 -0
+               /* check whether given value is supported by the interface */
+               for (i = 0; i < num_t && t != priv->termination_const[i]; i++);
+               if (i >= num_t)
+                       return -EINVAL;
+
+               priv->termination = t;

I would update the value after successful call to do_set_termination.

Sounds good.

Then I would change the do_set_termination() prototype:

	int (*do_set_termination)(struct net_device *dev);
to
	int (*do_set_termination)(struct net_device *dev, u16 term);
Quoted 5 lines

include/uapi/linux/can/netlink.h enum { · line 139

Quoted 3 lines
include/uapi/linux/can/netlink.h +3 -0
+/* termination range: 1..65534 Ohms */
+#define CAN_TERMINATION_DISABLED 0xFFFFU
+

0 seems more natural to me. Why you are using 0xFFFF?

I was initially thinking about a zero terminated array of termination values. But now we have a termination_const_size which allows to walk through the array without a trailing zero.

I'll change it to

+#define CAN_TERMINATION_DISABLED 0

Thanks,
Oliver