linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christoph Fritz <christoph.fritz@hexdev.de>
To: Oliver Hartkopp <socketcan@hartkopp.net>,
	Pavel Pisa <pisa@cmp.felk.cvut.cz>,
	Richard Weinberger <richard@nod.at>,
	Andreas Lauser <andreas.lauser@mbition.io>,
	Wolfgang Grandegger <wg@grandegger.com>,
	Marc Kleine-Budde <mkl@pengutronix.de>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Jonathan Corbet <corbet@lwn.net>
Cc: linux-can@vger.kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] [RFC] can: Add LIN proto skeleton
Date: Sun, 27 Nov 2022 20:02:44 +0100	[thread overview]
Message-ID: <20221127190244.888414-3-christoph.fritz@hexdev.de> (raw)
In-Reply-To: <20221127190244.888414-1-christoph.fritz@hexdev.de>

From: Richard Weinberger <richard@nod.at>

Signed-off-by: Richard Weinberger <richard@nod.at>
---
 include/uapi/linux/can.h     |   7 +-
 include/uapi/linux/can/lin.h |  15 +++
 net/can/Kconfig              |   5 +
 net/can/Makefile             |   3 +
 net/can/lin.c                | 207 +++++++++++++++++++++++++++++++++++
 5 files changed, 236 insertions(+), 1 deletion(-)
 create mode 100644 include/uapi/linux/can/lin.h
 create mode 100644 net/can/lin.c

diff --git a/include/uapi/linux/can.h b/include/uapi/linux/can.h
index 8596f9b23c68..73526805dc5f 100644
--- a/include/uapi/linux/can.h
+++ b/include/uapi/linux/can.h
@@ -178,7 +178,8 @@ struct canfd_frame {
 #define CAN_MCNET	5 /* Bosch MCNet */
 #define CAN_ISOTP	6 /* ISO 15765-2 Transport Protocol */
 #define CAN_J1939	7 /* SAE J1939 */
-#define CAN_NPROTO	8
+#define CAN_LIN		8 /* LIN Bus */
+#define CAN_NPROTO	9
 
 #define SOL_CAN_BASE 100
 
@@ -212,6 +213,10 @@ struct sockaddr_can {
 			__u8 addr;
 		} j1939;
 
+		struct {
+			__u8 addr; //Dummy for now
+		} lin;
+
 		/* reserved for future CAN protocols address information */
 	} can_addr;
 };
diff --git a/include/uapi/linux/can/lin.h b/include/uapi/linux/can/lin.h
new file mode 100644
index 000000000000..7e9f44992b7d
--- /dev/null
+++ b/include/uapi/linux/can/lin.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: (GPL-2.0-only WITH Linux-syscall-note) */
+/*
+ * linux/can/lin.h
+ * TODO
+ */
+
+#ifndef _UAPI_CAN_LIN_H
+#define _UAPI_CAN_LIN_H
+
+#include <linux/types.h>
+#include <linux/can.h>
+
+#define SOL_CAN_LIN (SOL_CAN_BASE + CAN_LIN)
+
+#endif /* !_UAPI_CAN_LIN_H */
diff --git a/net/can/Kconfig b/net/can/Kconfig
index cb56be8e3862..d05e3fa813e2 100644
--- a/net/can/Kconfig
+++ b/net/can/Kconfig
@@ -70,4 +70,9 @@ config CAN_ISOTP
 	  If you want to perform automotive vehicle diagnostic services (UDS),
 	  say 'y'.
 
+config CAN_LIN
+	tristate "Support for LIN bus"
+	help
+	  TODO
+
 endif
diff --git a/net/can/Makefile b/net/can/Makefile
index 58f2c31c1ef3..5db51a56a78a 100644
--- a/net/can/Makefile
+++ b/net/can/Makefile
@@ -20,3 +20,6 @@ obj-$(CONFIG_CAN_J1939)	+= j1939/
 
 obj-$(CONFIG_CAN_ISOTP)	+= can-isotp.o
 can-isotp-y		:= isotp.o
+
+obj-$(CONFIG_CAN_LIN)	+= can-lin.o
+can-lin-y		:= lin.o
diff --git a/net/can/lin.c b/net/can/lin.c
new file mode 100644
index 000000000000..f8c8217efc8c
--- /dev/null
+++ b/net/can/lin.c
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+
+//TODO copyright
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/hrtimer.h>
+#include <linux/wait.h>
+#include <linux/uio.h>
+#include <linux/net.h>
+#include <linux/netdevice.h>
+#include <linux/socket.h>
+#include <linux/if_arp.h>
+#include <linux/skbuff.h>
+#include <linux/can.h>
+#include <linux/can/core.h>
+#include <linux/can/skb.h>
+#include <linux/can/lin.h>
+#include <linux/slab.h>
+#include <net/sock.h>
+#include <net/net_namespace.h>
+
+MODULE_DESCRIPTION("PF_CAN LIN protocol");
+MODULE_LICENSE("GPLv2");
+MODULE_ALIAS("can-proto-8");
+
+struct lin_sock {
+	struct sock sk;
+};
+
+static inline struct lin_sock *lin_sk(const struct sock *sk)
+{
+	return (struct lin_sock *)sk;
+}
+
+static int lin_release(struct socket *sock)
+{
+	struct sock *sk = sock->sk;
+	struct lin_sock *so;
+	struct net *net;
+
+	if (!sk)
+		return 0;
+
+	so = lin_sk(sk);
+	net = sock_net(sk);
+
+	// Nothing do to so far
+
+	sock_orphan(sk);
+	sock->sk = NULL;
+
+	release_sock(sk);
+	sock_put(sk);
+
+	return 0;
+}
+
+static int lin_bind(struct socket *sock, struct sockaddr *uaddr, int len)
+{
+	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
+	struct sock *sk = sock->sk;
+	struct net *net = sock_net(sk);
+	struct net_device *dev;
+	int err = 0;
+
+	//TODO
+	dev = dev_get_by_index(net, addr->can_ifindex);
+	if (!dev) {
+		err = -ENODEV;
+		goto out;
+	}
+	if (dev->type != ARPHRD_CAN) {
+		dev_put(dev);
+		err = -ENODEV;
+		goto out;
+	}
+
+out:
+	return err;
+}
+
+static int lin_setsockopt_locked(struct socket *sock, int level, int optname,
+			    sockptr_t optval, unsigned int optlen)
+{
+	struct sock *sk = sock->sk;
+	struct lin_sock *so = lin_sk(sk);
+	int ret = 0;
+
+	(void)so;
+
+	switch (optname) {
+	// Nothing to do so far
+	default:
+		ret = -ENOPROTOOPT;
+	}
+
+	return ret;
+}
+
+static int lin_setsockopt(struct socket *sock, int level, int optname,
+			    sockptr_t optval, unsigned int optlen)
+
+{
+	struct sock *sk = sock->sk;
+	int ret;
+
+	if (level != SOL_CAN_LIN)
+		return -EINVAL;
+
+	lock_sock(sk);
+	ret = lin_setsockopt_locked(sock, level, optname, optval, optlen);
+	release_sock(sk);
+	return ret;
+}
+
+static int lin_getsockopt(struct socket *sock, int level, int optname,
+			    char __user *optval, int __user *optlen)
+{
+	struct sock *sk = sock->sk;
+	struct lin_sock *so = lin_sk(sk);
+	int len;
+	void *val;
+
+	(void)so;
+
+	if (level != SOL_CAN_LIN)
+		return -EINVAL;
+	if (get_user(len, optlen))
+		return -EFAULT;
+	if (len < 0)
+		return -EINVAL;
+
+	switch (optname) {
+	//Nothing to do so far.
+	default:
+		return -ENOPROTOOPT;
+	}
+
+	if (put_user(len, optlen))
+		return -EFAULT;
+	if (copy_to_user(optval, val, len))
+		return -EFAULT;
+	return 0;
+}
+
+static int lin_init(struct sock *sk)
+{
+	struct lin_sock *so = lin_sk(sk);
+
+	(void)so;
+	// Nothing to do so far
+
+	return 0;
+}
+
+static const struct proto_ops lin_ops = {
+	.family = PF_CAN,
+	.release = lin_release,
+	.bind = lin_bind,
+	.connect = sock_no_connect,
+	.socketpair = sock_no_socketpair,
+	.accept = sock_no_accept,
+	.getname = sock_no_getname,
+	.poll = datagram_poll,
+	.ioctl = sock_no_ioctl,
+	.gettstamp = sock_gettstamp,
+	.listen = sock_no_listen,
+	.shutdown = sock_no_shutdown,
+	.setsockopt = lin_setsockopt,
+	.getsockopt = lin_getsockopt,
+	.sendmsg = sock_no_sendmsg,
+	.recvmsg = sock_no_recvmsg,
+	.mmap = sock_no_mmap,
+	.sendpage = sock_no_sendpage,
+};
+
+static struct proto lin_proto __read_mostly = {
+	.name = "CAN_LIN",
+	.owner = THIS_MODULE,
+	.obj_size = sizeof(struct lin_sock),
+	.init = lin_init,
+};
+
+static const struct can_proto lin_can_proto = {
+	.type = SOCK_DGRAM,
+	.protocol = CAN_LIN,
+	.ops = &lin_ops,
+	.prot = &lin_proto,
+};
+
+static __init int lin_module_init(void)
+{
+	pr_info("can: lin protocol\n");
+
+	return can_proto_register(&lin_can_proto);
+}
+
+static __exit void lin_module_exit(void)
+{
+	can_proto_unregister(&lin_can_proto);
+}
+
+module_init(lin_module_init);
+module_exit(lin_module_exit);
-- 
2.30.2


  parent reply	other threads:[~2022-11-27 19:18 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-27 19:02 [RFC][PATCH 0/2] LIN support for Linux Christoph Fritz
2022-11-27 19:02 ` [PATCH 1/2] [RFC] can: Introduce LIN bus as CANFD abstraction Christoph Fritz
2022-11-27 19:02 ` Christoph Fritz [this message]
2022-11-28  8:21 ` [RFC][PATCH 0/2] LIN support for Linux Oliver Hartkopp
2022-11-28 10:16   ` Christoph Fritz
2022-11-28 14:49     ` Pavel Pisa
2022-11-28 17:02       ` Ryan Edwards
2022-11-28 17:52         ` Pavel Pisa
2022-11-28 18:47           ` Ryan Edwards
2022-11-28 21:48             ` Christoph Fritz
2022-11-28 22:47               ` Andrew Lunn
2022-11-30 21:02     ` Oliver Hartkopp

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=20221127190244.888414-3-christoph.fritz@hexdev.de \
    --to=christoph.fritz@hexdev.de \
    --cc=andreas.lauser@mbition.io \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pisa@cmp.felk.cvut.cz \
    --cc=richard@nod.at \
    --cc=socketcan@hartkopp.net \
    --cc=wg@grandegger.com \
    /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).