Skip to contentSkip to conversation

can: add CAN interface termination API

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

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 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..f98825f6ea2e 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;
+
+		if (priv->do_set_termination) {
+			/* Finally, set the termination */
+			err = priv->do_set_termination(dev, t);
+			if (err)
+				return err;
+
+			priv->termination = t;
+		} 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..7aa0eb1a75e3 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, u16 term);
 	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..7414771926fb 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)
 
+/* u16 termination range: 1..65535 Ohms */
+#define CAN_TERMINATION_DISABLED 0
+
 #endif /* !_UAPI_CAN_NETLINK_H */
Signature · 1 line
2.11.0
#1 lore raw
Quoted 6 lines · Oliver Hartkopp

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 };

drvname_termination[]

Quoted 6 lines

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

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

ARRAY_SIZE()

Quoted 18 lines

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 +0 -0
index 8d6208c0b400..f98825f6ea2e 100644

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

Quoted 6 lines
drivers/net/can/dev.c +3 -0
@@ -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);

ARRAY_SIZE...or see below:

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

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

Nitpick: unusual for() loop, better make more explicit what's going on:

for (i = 0; i < ARRAY_SIZE(priv->termination_const_size), i++) {
	if (t == priv->termination_const[i])
		break;
}

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

Quoted 4 lines
drivers/net/can/dev.c +4 -0
+		if (i >= num_t)
+			return -EINVAL;
+
+		if (priv->do_set_termination) {

What about moving the check for priv->do_set_termination in front of the loop?

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

Quoted 8 lines
drivers/net/can/dev.c +8 -0
+			/* Finally, set the termination */
+			err = priv->do_set_termination(dev, t);
+			if (err)
+				return err;
+
+			priv->termination = t;
+		} else
+			return -EOPNOTSUPP;

{ } on both legs of the else

Quoted 64 lines
drivers/net/can/dev.c +14 -1
+	}
+
 	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..7aa0eb1a75e3 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, u16 term);
 	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 +1 -0
index 94ffe0c83ce7..7414771926fb 100644
@@ -127,9 +127,14 @@ enum {
 	IFLA_CAN_BERR_COUNTER,
 	IFLA_CAN_DATA_BITTIMING,
 	IFLA_CAN_DATA_BITTIMING_CONST,
+	IFLA_CAN_TERMINATION,

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

Quoted 10 lines
include/uapi/linux/can/netlink.h +4 -0
+	IFLA_CAN_TERMINATION_CONST,
 	__IFLA_CAN_MAX
 };
 
 #define IFLA_CAN_MAX	(__IFLA_CAN_MAX - 1)
 
+/* u16 termination range: 1..65535 Ohms */
+#define CAN_TERMINATION_DISABLED 0
+
 #endif /* !_UAPI_CAN_NETLINK_H */

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
#2 lore raw

Hello Marc,

Quoted 10 lines · Marc Kleine-Budde
Quoted 6 lines · Oliver Hartkopp

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 };

drvname_termination[]

Ok.

Quoted 8 lines
Quoted 6 lines

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

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

ARRAY_SIZE()

When we use ARRAY_SIZE(drvname_termination) here ...

Quoted 21 lines
Quoted 21 lines

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 +0 -0
index 8d6208c0b400..f98825f6ea2e 100644
@@ -958,6 +958,27 @@ static int can_changelink(struct net_device *dev,
 		}
 	}
 
Quoted 3 lines

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

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

... this needs to turn into

	int num_t = priv->termination_const_size;

but ...

Quoted 11 lines

ARRAY_SIZE...or see below:

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

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

Nitpick: unusual for() loop, better make more explicit what's going on:

for (i = 0; i < ARRAY_SIZE(priv->termination_const_size), i++) {

... this does never work as it references an integer ...

Quoted 28 lines
	if (t == priv->termination_const[i])
		break;
}

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

Quoted 4 lines
drivers/net/can/dev.c +4 -0
+		if (i >= num_t)
+			return -EINVAL;
+
+		if (priv->do_set_termination) {

What about moving the check for priv->do_set_termination in front of the loop?

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

Quoted 8 lines
drivers/net/can/dev.c +8 -0
+			/* Finally, set the termination */
+			err = priv->do_set_termination(dev, t);
+			if (err)
+				return err;
+
+			priv->termination = t;
+		} else
+			return -EOPNOTSUPP;

{ } on both legs of the else

Quoted 5 lines
drivers/net/can/dev.c +2 -0
+	}
+
 	return 0;
 }
 
Quoted 7 lines

drivers/net/can/dev.c static size_t can_get_size(const struct net_device *dev) · line 1006

Quoted 7 lines
drivers/net/can/dev.c +3 -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);

... this will break ...

Quoted 15 lines
Quoted 6 lines
drivers/net/can/dev.c +2 -0
+		size += nla_total_size(sizeof(u16));		/* IFLA_CAN_TERMINATION */
+	}
 
 	return size;
 }

drivers/net/can/dev.c static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) · line 1051

Quoted 9 lines
drivers/net/can/dev.c +5 -1
@@ -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,

... and this too.

Quoted 4 lines

drivers/net/can/dev.c static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) · line 1055

Quoted 4 lines
drivers/net/can/dev.c +2 -0
+		     priv->termination_const) &&
+	     nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination)))
 		return -EMSGSIZE;
 

I just wonder, which is the most readable representation here?

Regards,
Oliver

#3 lore raw
Quoted 20 lines · Oliver Hartkopp

Hello Marc,

Quoted 10 lines · Marc Kleine-Budde
Quoted 6 lines · Oliver Hartkopp

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 };

drvname_termination[]

Ok.

Quoted 4 lines
Quoted 4 lines

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

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

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

ARRAY_SIZE()

When we use ARRAY_SIZE(drvname_termination) here ...

Remigiusz's patch using _len = ARRAY_SIZE() here and _len * (sizeof()) in the netlink interface looks nice to me.

Quoted 41 lines
Quoted 21 lines
Quoted 21 lines

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 +0 -0
index 8d6208c0b400..f98825f6ea2e 100644
@@ -958,6 +958,27 @@ static int can_changelink(struct net_device *dev,
 		}
 	}
 
Quoted 3 lines

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

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

... this needs to turn into

	int num_t = priv->termination_const_size;

but ...

Quoted 10 lines

ARRAY_SIZE...or see below:

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

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

Nitpick: unusual for() loop, better make more explicit what's going on:

Quoted 3 lines
Quoted 1 line

for (i = 0; i < ARRAY_SIZE(priv->termination_const_size), i++) {

... this does never work as it references an integer ...

Yes, doh!

[...]

Quoted 28 lines
Quoted 7 lines

drivers/net/can/dev.c static size_t can_get_size(const struct net_device *dev) · line 1006

Quoted 7 lines
drivers/net/can/dev.c +3 -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);

... this will break ...

Quoted 15 lines
Quoted 6 lines
drivers/net/can/dev.c +2 -0
+		size += nla_total_size(sizeof(u16));		/* IFLA_CAN_TERMINATION */
+	}
 
 	return size;
 }

drivers/net/can/dev.c static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) · line 1051

Quoted 9 lines
drivers/net/can/dev.c +5 -1
@@ -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,

... and this too.

Quoted 6 lines
Quoted 4 lines

drivers/net/can/dev.c static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) · line 1055

Quoted 4 lines
drivers/net/can/dev.c +2 -0
+		     priv->termination_const) &&
+	     nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination)))
 		return -EMSGSIZE;
 

I just wonder, which is the most readable representation here?

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |