Skip to contentSkip to conversation

can: slcan: Replace sizeof struct can_frame with CAN_MTU

RFC PATCH In review Marek Vasut 3 participants 11 messages Jun 9, 2016 – Jun 17, 2016

Subsystems (3)
lore
Share & tools
lore
#1 lore raw

RFC PATCH 2/5 can: Factor out alloc_socketdev()

In reply to Marek Vasut

Factor out alloc_socketdev() from alloc_candev() to make it possible to register different socket devices -- like kline -- using the same codepath.

Cc Oliver Hartkopp <[email protected]>
Cc Marc Kleine-Budde <[email protected]>
---
 drivers/net/can/dev.c   | 11 +++++++++--
 include/linux/can/dev.h |  1 +
 2 files changed, 10 insertions(+), 2 deletions(-)
drivers/net/can/dev.c +9 -2
index 141c2a4..f8ef6b0 100644
@@ -641,7 +641,8 @@ EXPORT_SYMBOL_GPL(alloc_can_err_skb);
 /*
  * Allocate and setup space for the CAN network device
  */
-struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
+struct net_device *alloc_socketdev(const char *name, int sizeof_priv,
+				   unsigned int echo_skb_max)
 {
 	struct net_device *dev;
 	struct can_priv *priv;
@@ -653,7 +654,7 @@ struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
 	else
 		size = sizeof_priv;
 
-	dev = alloc_netdev(size, "can%d", NET_NAME_UNKNOWN, can_setup);
+	dev = alloc_netdev(size, name, NET_NAME_UNKNOWN, can_setup);
 	if (!dev)
 		return NULL;
 
@@ -671,6 +672,12 @@ struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
 
 	return dev;
 }
+EXPORT_SYMBOL_GPL(alloc_socketdev);
+
+struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
+{
+	return alloc_socketdev("can%d", sizeof_priv, echo_skb_max);
+}
 EXPORT_SYMBOL_GPL(alloc_candev);
 
 /*
include/linux/can/dev.h +1 -0
index 735f9f8..9b0520d 100644
@@ -114,6 +114,7 @@ u8 can_dlc2len(u8 can_dlc);
 /* map the sanitized data length to an appropriate data length code */
 u8 can_len2dlc(u8 len);
 
+struct net_device *alloc_socketdev(const char *name, int sizeof_priv, unsigned int echo_skb_max);
 struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max);
 void free_candev(struct net_device *dev);
 
Signature · 1 line
2.7.0
#3 lore raw

RFC PATCH 4/5 can: kline: Add KLine rtnl configuration options

In reply to Marek Vasut

Extend the can rtnl with kline timing parameters W0...W5, P1...P4 .

Cc Oliver Hartkopp <[email protected]>
Cc Marc Kleine-Budde <[email protected]>
---
 drivers/net/can/dev.c            | 16 +++++++++++++++-
 include/linux/can/dev.h          |  2 ++
 include/uapi/linux/can/netlink.h | 18 ++++++++++++++++++
 3 files changed, 35 insertions(+), 1 deletion(-)
drivers/net/can/dev.c +15 -1
index 0db16f6..e867b7a 100644
@@ -787,6 +787,7 @@ static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
 				= { .len = sizeof(struct can_bittiming) },
 	[IFLA_CAN_DATA_BITTIMING_CONST]
 				= { .len = sizeof(struct can_bittiming_const) },
+	[IFLA_KLINE_TIMING]	= { .len = sizeof(struct kline_timing) },
 };
 
 static int can_changelink(struct net_device *dev,
@@ -878,6 +879,14 @@ static int can_changelink(struct net_device *dev,
 		}
 	}
 
+	if (data[IFLA_KLINE_TIMING]) {
+		/* Do not allow changing bittiming while running */
+		if (dev->flags & IFF_UP)
+			return -EBUSY;
+		memcpy(&priv->kline_timing, nla_data(data[IFLA_KLINE_TIMING]),
+		       sizeof(struct kline_timing));
+	}
+
 	return 0;
 }
 
@@ -900,6 +909,7 @@ 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));
+	size += nla_total_size(sizeof(struct kline_timing));	/* IFLA_KLINE_TIMING */
 
 	return size;
 }
@@ -938,7 +948,11 @@ 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)) ||
+
+	    nla_put(skb, IFLA_KLINE_TIMING,
+		    sizeof(priv->kline_timing), &priv->kline_timing)
+	    )
 		return -EMSGSIZE;
 
 	return 0;
include/linux/can/dev.h +2 -0
index c67082d..f95f24d 100644
@@ -65,6 +65,8 @@ struct can_priv {
 	struct led_trigger *rxtx_led_trig;
 	char rxtx_led_trig_name[CAN_LED_NAME_SZ];
 #endif
+
+	struct kline_timing kline_timing;
 };
 
 /*
include/uapi/linux/can/netlink.h +18 -0
index 94ffe0c..da5f4b3 100644
@@ -113,6 +113,23 @@ struct can_device_stats {
 };
 
 /*
+ * KLine timing parameters
+ */
+struct kline_timing {
+	__u32 w0;
+	__u32 w1;
+	__u32 w2;
+	__u32 w3;
+	__u32 w4;
+	__u32 w5;
+	__u32 p1;
+	__u32 p2_94;
+	__u32 p2_08;
+	__u32 p3;
+	__u32 p4;
+};
+
+/*
  * CAN netlink interface
  */
 enum {
@@ -127,6 +144,7 @@ enum {
 	IFLA_CAN_BERR_COUNTER,
 	IFLA_CAN_DATA_BITTIMING,
 	IFLA_CAN_DATA_BITTIMING_CONST,
+	IFLA_KLINE_TIMING,
 	__IFLA_CAN_MAX
 };
 
Signature · 1 line
2.7.0
#0 lore raw

RFC PATCH 1/5 can: slcan: Replace sizeof struct can_frame with CAN_MTU

Use CAN_MTU macro instead of sizeof(struct can_frame) just like the other drivers do.

Cc Oliver Hartkopp <[email protected]>
Cc Marc Kleine-Budde <[email protected]>
---
 drivers/net/can/slcan.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/can/slcan.c +2 -2
index 9a3f15c..eb71737 100644
@@ -354,7 +354,7 @@ static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
 {
 	struct slcan *sl = netdev_priv(dev);
 
-	if (skb->len != sizeof(struct can_frame))
+	if (skb->len != CAN_MTU)
 		goto out;
 
 	spin_lock(&sl->lock);
@@ -442,7 +442,7 @@ static void slc_setup(struct net_device *dev)
 	dev->addr_len		= 0;
 	dev->tx_queue_len	= 10;
 
-	dev->mtu		= sizeof(struct can_frame);
+	dev->mtu		= CAN_MTU;
 	dev->type		= ARPHRD_CAN;
 
 	/* New-style flags. */
Signature · 1 line
2.7.0
#8 lore raw

RFC PATCH 5/5 can: kline: Add KLine ldisc

Add initial TTY line discipline connected into the socketcan stack which facilitates the communication with KLine device.

Cc Oliver Hartkopp <[email protected]>
Cc Marc Kleine-Budde <[email protected]>

---

NOTE: There is a lot of stuff in this patch which is not fleshed out yet.

 drivers/net/can/Kconfig    |  19 ++
 drivers/net/can/Makefile   |   1 +
 drivers/net/can/sl_kline.c | 683 +++++++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/can.h   |  33 +++
 include/uapi/linux/tty.h   |   1 +
 net/can/af_can.c           |   6 +-
 net/can/raw.c              |   2 +-
 7 files changed, 743 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/can/sl_kline.c
drivers/net/can/Kconfig +19 -0
index 20be638..e1a8728 100644
@@ -29,6 +29,25 @@ config CAN_SLCAN
 	  can be changed by the 'maxdev=xx' module option. This driver can
 	  also be built as a module. If so, the module will be called slcan.
 
+config CAN_SL_KLINE
+	tristate "Serial K-Line protocol using SocketCAN (sl_kline)"
+	depends on TTY
+	---help---
+	  SocketCAN driver for the ISO 9141-2 K-Line protocol using TTY
+	  line discipline. This driver allows userland to communicate
+	  using standard SocketCAN interface to a device using K-Line
+	  protocol. Such K-Line device is connected to a regular serial
+	  port via level-shifter or similar logic. The TTY line discipline
+	  type is N_SL_KLINE.
+
+	  Userspace tools to attach the SL_KLINE ldisc (sl_kline_attach,
+	  sl_kline_d) can be found in the can-utils at the SocketCAN git).
+
+	  The sl_kline driver supports up to 10 KLine netdevices by default
+	  which can be changed by the 'maxdev=xx' module option. This driver
+	  can also be built as a module. If so, the module will be called
+	  sl_kline.
+
 config CAN_DEV
 	tristate "Platform CAN drivers with Netlink support"
 	default y
drivers/net/can/Makefile +1 -0
index e3db0c8..60648e9 100644
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_CAN_VCAN)		+= vcan.o
 obj-$(CONFIG_CAN_SLCAN)		+= slcan.o
+obj-$(CONFIG_CAN_SL_KLINE)	+= sl_kline.o
 
 obj-$(CONFIG_CAN_DEV)		+= can-dev.o
 can-dev-y			:= dev.o
drivers/net/can/sl_kline.c new file +683 -0

683 lines not on this page · raw message

include/uapi/linux/can.h +33 -0
index 9692cda..732cdc0 100644
@@ -89,6 +89,10 @@ typedef __u32 can_err_mask_t;
 #define CANFD_MAX_DLC 15
 #define CANFD_MAX_DLEN 64
 
+/* KLine payload length and DLC FIXME */
+#define KLINE_MAX_DLC	15
+#define KLINE_MAX_DLEN	255
+
 /**
  * struct can_frame - basic CAN frame structure
  * @can_id:  CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
@@ -146,8 +150,37 @@ struct canfd_frame {
 	__u8    data[CANFD_MAX_DLEN] __attribute__((aligned(8)));
 };
 
+/*
+ * FIXME
+ * defined bits for kline_frame.flags
+ *
+ */
+#define KLINE_ISO9141_START 0x1  /* ISO slow start sequence */
+#define KLINE_KWP_SLOW_START 0x2 /* KWP slow start sequence */
+#define KLINE_KWP_FAST_START 0x4 /* KWP fast start sequence */
+
+/**
+ * FIXME
+ * struct canfd_frame - CAN flexible data rate frame structure
+ * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
+ * @len:    frame payload length in byte (0 .. CANFD_MAX_DLEN)
+ * @flags:  additional flags for CAN FD
+ * @__res0: reserved / padding
+ * @__res1: reserved / padding
+ * @data:   CAN FD frame payload (up to CANFD_MAX_DLEN byte)
+ */
+struct kline_frame {
+	canid_t can_id;  /* 32 bit KLine address + EFF/RTR/ERR flags (unused) */
+	__u8    len;     /* frame payload length in byte */
+	__u8    flags;   /* additional flags for KLine */
+	__u8    __res0;  /* reserved / padding */
+	__u8    __res1;  /* reserved / padding */
+	__u8    data[KLINE_MAX_DLEN] __attribute__((aligned(8)));
+};
+
 #define CAN_MTU		(sizeof(struct can_frame))
 #define CANFD_MTU	(sizeof(struct canfd_frame))
+#define KLINE_MTU	(sizeof(struct kline_frame))
 
 /* particular protocols of the protocol family PF_CAN */
 #define CAN_RAW		1 /* RAW sockets */
include/uapi/linux/tty.h +1 -0
index 01c4410..80b8f1e 100644
@@ -35,5 +35,6 @@
 #define N_TRACESINK	23	/* Trace data routing for MIPI P1149.7 */
 #define N_TRACEROUTER	24	/* Trace data routing for MIPI P1149.7 */
 #define N_NCI		25	/* NFC NCI UART */
+#define N_SL_KLINE	26	/* ISO 9141-2 K-Line via SocketCAN */
 
 #endif /* _UAPI_LINUX_TTY_H */
net/can/af_can.c +5 -1
index 166d436..1a8b836 100644
@@ -238,6 +238,10 @@ int can_send(struct sk_buff *skb, int loop)
 		skb->protocol = htons(ETH_P_CANFD);
 		if (unlikely(cfd->len > CANFD_MAX_DLEN))
 			goto inval_skb;
+	} else if (skb->len == KLINE_MTU) {
+		skb->protocol = htons(ETH_P_CAN);
+		if (unlikely(cfd->len > KLINE_MAX_DLEN))
+			goto inval_skb;
 	} else
 		goto inval_skb;
 
@@ -715,7 +719,7 @@ static int can_rcv(struct sk_buff *skb, struct net_device *dev,
 		goto drop;
 
 	if (WARN_ONCE(dev->type != ARPHRD_CAN ||
-		      skb->len != CAN_MTU ||
+		      (skb->len != CAN_MTU && skb->len != KLINE_MTU) ||
 		      cfd->len > CAN_MAX_DLEN,
 		      "PF_CAN: dropped non conform CAN skbuf: "
 		      "dev type %d, len %d, datalen %d\n",
net/can/raw.c +1 -1
index 2e67b14..8a95404 100644
@@ -734,7 +734,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		if (unlikely(size != CANFD_MTU && size != CAN_MTU))
 			return -EINVAL;
 	} else {
-		if (unlikely(size != CAN_MTU))
+		if (unlikely((size != CAN_MTU) && (size != KLINE_MTU)))
 			return -EINVAL;
 	}
 
Signature · 1 line
2.7.0
#2 lore raw

RFC PATCH 3/5 can: Add register_candevice()

In reply to Marek Vasut

Add register_candevice() call, which unlike register_candev(), registers the candev without rtnl lock held. This is very similar to register_netdev() and register_netdevice().

Cc Oliver Hartkopp <[email protected]>
Cc Marc Kleine-Budde <[email protected]>
---
 drivers/net/can/dev.c   | 7 +++++++
 include/linux/can/dev.h | 1 +
 2 files changed, 8 insertions(+)
drivers/net/can/dev.c +7 -0
index f8ef6b0..0db16f6 100644
@@ -984,6 +984,13 @@ static struct rtnl_link_ops can_link_ops __read_mostly = {
 /*
  * Register the CAN network device
  */
+int register_candevice(struct net_device *dev)
+{
+	dev->rtnl_link_ops = &can_link_ops;
+	return register_netdevice(dev);
+}
+EXPORT_SYMBOL_GPL(register_candevice);
+
 int register_candev(struct net_device *dev)
 {
 	dev->rtnl_link_ops = &can_link_ops;
include/linux/can/dev.h +1 -0
index 9b0520d..c67082d 100644
@@ -125,6 +125,7 @@ int open_candev(struct net_device *dev);
 void close_candev(struct net_device *dev);
 int can_change_mtu(struct net_device *dev, int new_mtu);
 
+int register_candevice(struct net_device *dev);
 int register_candev(struct net_device *dev);
 void unregister_candev(struct net_device *dev);
 
Signature · 1 line
2.7.0
#4 lore raw

Re: [PATCH 4/5][RFC] can: kline: Add KLine rtnl configuration options

In reply to Marek Vasut

Hello Marek,

these look like the timing parameters for KWP2000 protocol.

include/uapi/linux/can/netlink.h struct can_device_stats { · line 130

Quoted 13 lines
include/uapi/linux/can/netlink.h +13 -0
+struct kline_timing {
+	__u32 w0;
+	__u32 w1;
+	__u32 w2;
+	__u32 w3;
+	__u32 w4;
+	__u32 w5;
+	__u32 p1;
+	__u32 p2_94;
+	__u32 p2_08;
+	__u32 p3;
+	__u32 p4;
+};

The wX parameters are the wake up timings and the pX are the response timings in between tester and ecu.
Imho the protocol needs to be separated from the device driver since there are other protocols like kwp500 and kwp1281 that use k-line.

Regards,
Patrick

#5 lore raw

Re: [PATCH 4/5][RFC] can: kline: Add KLine rtnl configuration options

Quoted 1 line · Menschel.P

Hello Marek,

Hi,

Quoted 10 lines

these look like the timing parameters for KWP2000 protocol.

Quoted 9 lines
include/uapi/linux/can/netlink.h +9 -0
+struct kline_timing {
+    __u32 w0;
+    __u32 w1;
+    __u32 w2;
+    __u32 w3;
+    __u32 w4;
+    __u32 w5;
+    __u32 p1;
+    __u32 p2_94;
Quoted 8 lines

include/uapi/linux/can/netlink.h struct can_device_stats { · line 130

Quoted 4 lines
include/uapi/linux/can/netlink.h +4 -0
+    __u32 p2_08;
+    __u32 p3;
+    __u32 p4;
+};

The wX parameters are the wake up timings and the pX are the response timings in between tester and ecu.
Imho the protocol needs to be separated from the device driver since there are other protocols like kwp500 and kwp1281 that use k-line.

Do you have some idea how to design the framework ?

Signature · 2 lines
Best regards,
Marek Vasut
#6 lore raw

Re: [PATCH 4/5][RFC] can: kline: Add KLine rtnl configuration options

Am 11.06.2016 um 21:40 schrieb Marek Vasut:

Quoted 24 lines · Menschel.P
Quoted 1 line

Hello Marek,

Hi,

Quoted 10 lines

these look like the timing parameters for KWP2000 protocol.

Quoted 9 lines
include/uapi/linux/can/netlink.h +9 -0
+struct kline_timing {
+    __u32 w0;
+    __u32 w1;
+    __u32 w2;
+    __u32 w3;
+    __u32 w4;
+    __u32 w5;
+    __u32 p1;
+    __u32 p2_94;
Quoted 8 lines

include/uapi/linux/can/netlink.h struct can_device_stats { · line 130

Quoted 4 lines
include/uapi/linux/can/netlink.h +4 -0
+    __u32 p2_08;
+    __u32 p3;
+    __u32 p4;
+};

The wX parameters are the wake up timings and the pX are the response timings in between tester and ecu.
Imho the protocol needs to be separated from the device driver since there are other protocols like kwp500 and kwp1281 that use k-line.

Quoted 2 lines

Do you have some idea how to design the framework ?

Hi,

As I wrote before, I favor the serial port since K-Line is pure serial communication.
Let the serial driver do the wakeup pattern in kernel space by using pinctl to pull up/ down the line for the necessary time.
If no pinctl available use regular RTS/CTS pins with external transistors. There is already an extension for RS485 support, e.g. RX/TX switching of the transceiver PHY.

The protocol handler would hand the wakeup parameters to the serial driver when Open() is called.

Regards,
Patrick

#7 lore raw

Re: [PATCH 4/5][RFC] can: kline: Add KLine rtnl configuration options

Quoted 28 lines · Patrick Menschel

Am 11.06.2016 um 21:40 schrieb Marek Vasut:

Quoted 24 lines · Menschel.P
Quoted 1 line

Hello Marek,

Hi,

Quoted 10 lines

these look like the timing parameters for KWP2000 protocol.

Quoted 9 lines
include/uapi/linux/can/netlink.h +9 -0
+struct kline_timing {
+    __u32 w0;
+    __u32 w1;
+    __u32 w2;
+    __u32 w3;
+    __u32 w4;
+    __u32 w5;
+    __u32 p1;
+    __u32 p2_94;
Quoted 8 lines

include/uapi/linux/can/netlink.h struct can_device_stats { · line 130

Quoted 4 lines
include/uapi/linux/can/netlink.h +4 -0
+    __u32 p2_08;
+    __u32 p3;
+    __u32 p4;
+};

The wX parameters are the wake up timings and the pX are the response timings in between tester and ecu.
Imho the protocol needs to be separated from the device driver since there are other protocols like kwp500 and kwp1281 that use k-line.

Quoted 2 lines

Do you have some idea how to design the framework ?

Quoted 6 lines

Hi,

As I wrote before, I favor the serial port since K-Line is pure serial communication.
Let the serial driver do the wakeup pattern in kernel space by using pinctl to pull up/ down the line for the necessary time.

Not the serial driver, this can be all moved into a line discipline to avoid adding cruft into the serial driver itself. But how do you propose to configure the necessary timings, by IOCTLs ? That feels like bloating the interface. Sysfs might also work, though I am not sure about that.

Quoted 3 lines

If no pinctl available use regular RTS/CTS pins with external transistors. There is already an extension for RS485 support, e.g. RX/TX switching of the transceiver PHY.

The RS485 bit can only ever work if the UART supports it. I have seen some parts using GPIO for the switching and that cannot work reliably on higher-speed links.

Quoted 2 lines

The protocol handler would hand the wakeup parameters to the serial driver when Open() is called.

How ?

Quoted 3 lines

Regards,
Patrick

Signature · 2 lines
Best regards,
Marek Vasut
#9 lore raw
In reply to Marek Vasut
Quoted 6 lines · Marek Vasut

Use CAN_MTU macro instead of sizeof(struct can_frame) just like the other drivers do.

Cc Oliver Hartkopp <[email protected]>
Cc Marc Kleine-Budde <[email protected]>

Added this one to can-next.

Thnaks,
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   |
#10 lore raw
Quoted 9 lines · Marc Kleine-Budde
Quoted 6 lines · Marek Vasut

Use CAN_MTU macro instead of sizeof(struct can_frame) just like the other drivers do.

Cc Oliver Hartkopp <[email protected]>
Cc Marc Kleine-Budde <[email protected]>

Added this one to can-next.

Thnaks, at least this one was a bit helpful.

Quoted 3 lines

Thnaks,
Marc

Signature · 2 lines
Best regards,
Marek Vasut