From patchwork Thu May 2 07:55:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Fritz X-Patchwork-Id: 794250 Received: from fritzc.com (mail.fritzc.com [213.160.72.247]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ECC7733CFC; Thu, 2 May 2024 07:56:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.160.72.247 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636611; cv=none; b=FrEtSHwcTDhtSsIDC2XqauRI+/aTO8tpAj1wwLcye6j7XDf8O3AZNCJWiUJPF20VI+lnej2gQiAMTdpet4Yr8ObAICy/EvIpIIS/RFLCHxGdFMhm3VDhAWJlv7cs1SYmSiVxFHcOlKYoemhCcJwJ3oVpbAgwmLOvFWNYFIzbiq8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636611; c=relaxed/simple; bh=0C3hVHADKleL+xH8Etg55tcmjzDkaqrNc4T+f2Ny/Oo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aqp9fInEy5qfZ0isCrLbi5pe2bIlzvyEnLT7WLXlc28wx79ycD+YHXS7AvaJwid9UV01u1Ndqp4PZBoO9CcJbM/QPmYcb+2JS5/rEP2hgnMECp+JGYQTFYNgMZJRezRmb9NTVRIriIt/tGShO2wHVAAnia30OcPIP4/APJQfZ1Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de; spf=pass smtp.mailfrom=hexdev.de; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b=wnDz3TzV; arc=none smtp.client-ip=213.160.72.247 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hexdev.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b="wnDz3TzV" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=fritzc.com; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=LopWCCJvbmY6iL0+SJB5tqGLqyWd9kUah2zsix2RxWk=; b=wnDz3TzVkFPO8IRVvwmGRwykdk fnjcSVsUjd07mBClHoO3j25YgPewlsOCx7ZILIO7EuhwenYHmJDDUxlBy/fwhYQFN8xoyb9K8oYUg WdaYzZ/FCLRGbDEdL+ZiN9QCJnU7eGHqrg4dQ4LSjt+VXQ5igs/S+LEH8liUMg/DouEI=; Received: from 127.0.0.1 by fritzc.com with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim latest) (envelope-from ) id 1s2RIt-001Yg7-2j; Thu, 02 May 2024 09:56:32 +0200 From: Christoph Fritz To: Oliver Hartkopp , Marc Kleine-Budde , Vincent Mailhol , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jiri Kosina , Benjamin Tissoires , Greg Kroah-Hartman , Jiri Slaby , Sebastian Reichel , Linus Walleij Cc: Andreas Lauser , Jonathan Corbet , Pavel Pisa , linux-can@vger.kernel.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-input@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH v2 01/12] can: Add LIN bus as CAN abstraction Date: Thu, 2 May 2024 09:55:23 +0200 Message-Id: <20240502075534.882628-2-christoph.fritz@hexdev.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240502075534.882628-1-christoph.fritz@hexdev.de> References: <20240502075534.882628-1-christoph.fritz@hexdev.de> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This patch adds a LIN (local interconnect network) bus abstraction on top of CAN. It is a glue driver adapting CAN on one side while offering LIN abstraction on the other side. So that upcoming LIN device drivers can make use of it. Tested-by: Andreas Lauser Signed-off-by: Christoph Fritz --- drivers/net/can/Kconfig | 10 + drivers/net/can/Makefile | 1 + drivers/net/can/lin.c | 495 +++++++++++++++++++++++++++++++ include/net/lin.h | 91 ++++++ include/uapi/linux/can/netlink.h | 1 + 5 files changed, 598 insertions(+) create mode 100644 drivers/net/can/lin.c create mode 100644 include/net/lin.h diff --git a/drivers/net/can/Kconfig b/drivers/net/can/Kconfig index 2e31db55d9278..0934bbf8d03b2 100644 --- a/drivers/net/can/Kconfig +++ b/drivers/net/can/Kconfig @@ -171,6 +171,16 @@ config CAN_KVASER_PCIEFD Kvaser M.2 PCIe 4xCAN Kvaser PCIe 8xCAN +config CAN_LIN + tristate "LIN mode support" + help + This is a glue driver for LIN-BUS support. + + The local interconnect (LIN) bus is a simple bus with a feature + subset of CAN. It is often combined with CAN for simple controls. + + Actual device drivers need to be enabled too. + config CAN_SLCAN tristate "Serial / USB serial CAN Adaptors (slcan)" depends on TTY diff --git a/drivers/net/can/Makefile b/drivers/net/can/Makefile index 4669cd51e7bf5..0093ee9219ca8 100644 --- a/drivers/net/can/Makefile +++ b/drivers/net/can/Makefile @@ -25,6 +25,7 @@ obj-$(CONFIG_CAN_GRCAN) += grcan.o obj-$(CONFIG_CAN_IFI_CANFD) += ifi_canfd/ obj-$(CONFIG_CAN_JANZ_ICAN3) += janz-ican3.o obj-$(CONFIG_CAN_KVASER_PCIEFD) += kvaser_pciefd.o +obj-$(CONFIG_CAN_LIN) += lin.o obj-$(CONFIG_CAN_MSCAN) += mscan/ obj-$(CONFIG_CAN_M_CAN) += m_can/ obj-$(CONFIG_CAN_PEAK_PCIEFD) += peak_canfd/ diff --git a/drivers/net/can/lin.c b/drivers/net/can/lin.c new file mode 100644 index 0000000000000..95906003666fb --- /dev/null +++ b/drivers/net/can/lin.c @@ -0,0 +1,495 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* Copyright (C) 2024 hexDEV GmbH - https://hexdev.de */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const u8 lin_id_parity_tbl[] = { + 0x80, 0xC0, 0x40, 0x00, 0xC0, 0x80, 0x00, 0x40, + 0x00, 0x40, 0xC0, 0x80, 0x40, 0x00, 0x80, 0xC0, + 0x40, 0x00, 0x80, 0xC0, 0x00, 0x40, 0xC0, 0x80, + 0xC0, 0x80, 0x00, 0x40, 0x80, 0xC0, 0x40, 0x00, + 0x00, 0x40, 0xC0, 0x80, 0x40, 0x00, 0x80, 0xC0, + 0x80, 0xC0, 0x40, 0x00, 0xC0, 0x80, 0x00, 0x40, + 0xC0, 0x80, 0x00, 0x40, 0x80, 0xC0, 0x40, 0x00, + 0x40, 0x00, 0x80, 0xC0, 0x00, 0x40, 0xC0, 0x80, +}; + +u8 lin_get_id_parity(u8 id) +{ + return lin_id_parity_tbl[id]; +} +EXPORT_SYMBOL(lin_get_id_parity); + +static ssize_t lin_identifier_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct lin_attr *lin_attr = container_of(attr, struct lin_attr, attr); + struct lin_device *ldev = lin_attr->ldev; + ssize_t count = 0; + struct lin_responder_answer answ; + int k, ret; + long id; + + if (!ldev->ldev_ops->get_responder_answer) + return -EOPNOTSUPP; + + ret = kstrtol(attr->attr.name, 16, &id); + if (ret) + return ret; + if (id < 0 || id >= LIN_NUM_IDS) + return -EINVAL; + + count += scnprintf(buf + count, PAGE_SIZE - count, + "%-6s %-11s %-9s %-9s %-2s %-24s %-6s\n", + "state", "cksum-mode", "is_event", "event_id", + "n", "data", "cksum"); + + ret = ldev->ldev_ops->get_responder_answer(ldev, id, &answ); + if (ret) + return ret; + + count += scnprintf(buf + count, PAGE_SIZE - count, + "%-6s %-11s %-9s %-9u %-2u ", + answ.is_active ? "active" : "off", + answ.lf.checksum_mode ? "enhanced" : "classic", + answ.is_event_frame ? "yes" : "no", + answ.event_associated_id, + answ.lf.len); + + for (k = 0; k < answ.lf.len; k++) + count += scnprintf(buf + count, PAGE_SIZE - count, + "%02x ", answ.lf.data[k]); + for (; k < 8; k++) + count += scnprintf(buf + count, PAGE_SIZE - count, + " "); + if (answ.lf.len) + count += scnprintf(buf + count, PAGE_SIZE - count, + " %02x", answ.lf.checksum); + + count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); + + return count; +} + +static const char *parse_and_advance(const char *buf, long *result, + unsigned int base) +{ + char num_str[5] = {0}; + int num_len = 0; + + while (*buf && isspace(*buf)) + buf++; + while (*buf && isalnum(*buf) && num_len < sizeof(num_str) - 1) + num_str[num_len++] = *buf++; + if (kstrtol(num_str, base, result)) + return NULL; + + return buf; +} + +static ssize_t lin_identifier_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + struct lin_attr *lin_attr = container_of(attr, struct lin_attr, attr); + struct lin_device *ldev = lin_attr->ldev; + struct lin_responder_answer answ = { 0 }; + const char *ptr = buf; + int ret; + long v; + + if (!ldev->ldev_ops->update_responder_answer) + return -EOPNOTSUPP; + + ret = kstrtol(attr->attr.name, 16, &v); + if (ret) + return ret; + if (v < 0 || v >= LIN_NUM_IDS) + return -EINVAL; + answ.lf.lin_id = v; + + ptr = parse_and_advance(ptr, &v, 2); + if (!ptr) + return -EINVAL; + answ.is_active = v != 0; + + ptr = parse_and_advance(ptr, &v, 2); + if (!ptr) + return -EINVAL; + answ.lf.checksum_mode = v != 0; + + ptr = parse_and_advance(ptr, &v, 2); + if (!ptr) + return -EINVAL; + answ.is_event_frame = v != 0; + + ptr = parse_and_advance(ptr, &v, 16); + if (!ptr || v > LIN_ID_MASK) + return -EINVAL; + answ.event_associated_id = v; + + ptr = parse_and_advance(ptr, &v, 16); + if (!ptr || v > LIN_MAX_DLEN) + return -EINVAL; + answ.lf.len = v; + + for (int i = 0; i < answ.lf.len; i++) { + ptr = parse_and_advance(ptr, &v, 16); + if (!ptr) + return -EINVAL; + answ.lf.data[i] = v; + } + + ret = ldev->ldev_ops->update_responder_answer(ldev, &answ); + if (ret) + return ret; + + return count; +} + +static int lin_create_sysfs_id_files(struct net_device *ndev) +{ + struct lin_device *ldev = netdev_priv(ndev); + struct kobj_attribute *attr; + int ret; + + for (int id = 0; id < LIN_NUM_IDS; id++) { + ldev->sysfs_entries[id].ldev = ldev; + attr = &ldev->sysfs_entries[id].attr; + attr->attr.name = kasprintf(GFP_KERNEL, "%02x", id); + if (!attr->attr.name) + return -ENOMEM; + attr->attr.mode = 0644; + attr->show = lin_identifier_show; + attr->store = lin_identifier_store; + + sysfs_attr_init(&attr->attr); + ret = sysfs_create_file(ldev->lin_ids_kobj, &attr->attr); + if (ret) { + kfree(attr->attr.name); + return -ENOMEM; + } + } + + return 0; +} + +static void lin_remove_sysfs_id_files(struct net_device *ndev) +{ + struct lin_device *ldev = netdev_priv(ndev); + struct kobj_attribute *attr; + + for (int id = 0; id < LIN_NUM_IDS; id++) { + attr = &ldev->sysfs_entries[id].attr; + sysfs_remove_file(ldev->lin_ids_kobj, &attr->attr); + kfree(attr->attr.name); + } +} + +static void lin_tx_work_handler(struct work_struct *ws) +{ + struct lin_device *ldev = container_of(ws, struct lin_device, + tx_work); + struct net_device *ndev = ldev->ndev; + struct canfd_frame *cfd; + struct lin_frame lf; + int ret; + + ldev->tx_busy = true; + + cfd = (struct canfd_frame *)ldev->tx_skb->data; + lf.checksum_mode = (cfd->can_id & LIN_ENHANCED_CKSUM_FLAG) ? + LINBUS_ENHANCED : LINBUS_CLASSIC; + lf.lin_id = cfd->can_id & LIN_ID_MASK; + lf.len = min(cfd->len, LIN_MAX_DLEN); + memcpy(lf.data, cfd->data, lf.len); + + ret = ldev->ldev_ops->ldo_tx(ldev, &lf); + if (ret) { + DEV_STATS_INC(ndev, tx_dropped); + netdev_err_once(ndev, "transmission failure %d\n", ret); + goto lin_tx_out; + } + + DEV_STATS_INC(ndev, tx_packets); + DEV_STATS_ADD(ndev, tx_bytes, lf.len); + +lin_tx_out: + ldev->tx_busy = false; + netif_wake_queue(ndev); +} + +static netdev_tx_t lin_start_xmit(struct sk_buff *skb, + struct net_device *ndev) +{ + struct lin_device *ldev = netdev_priv(ndev); + + if (ldev->tx_busy) + return NETDEV_TX_BUSY; + + netif_stop_queue(ndev); + ldev->tx_skb = skb; + queue_work(ldev->wq, &ldev->tx_work); + + return NETDEV_TX_OK; +} + +static int lin_open(struct net_device *ndev) +{ + struct lin_device *ldev = netdev_priv(ndev); + int ret; + + ldev->tx_busy = false; + + ret = open_candev(ndev); + if (ret) + return ret; + + netif_wake_queue(ndev); + + ldev->can.state = CAN_STATE_ERROR_ACTIVE; + ndev->mtu = CANFD_MTU; + + return ldev->ldev_ops->ldo_open(ldev); +} + +static int lin_stop(struct net_device *ndev) +{ + struct lin_device *ldev = netdev_priv(ndev); + + close_candev(ndev); + + flush_work(&ldev->tx_work); + + ldev->can.state = CAN_STATE_STOPPED; + + return ldev->ldev_ops->ldo_stop(ldev); +} + +static const struct net_device_ops lin_netdev_ops = { + .ndo_open = lin_open, + .ndo_stop = lin_stop, + .ndo_start_xmit = lin_start_xmit, + .ndo_change_mtu = can_change_mtu, +}; + +u8 lin_get_checksum(u8 pid, u8 n_of_bytes, const u8 *bytes, + enum lin_checksum_mode cm) +{ + unsigned int csum = 0; + int i; + + if (cm == LINBUS_ENHANCED) + csum += pid; + + for (i = 0; i < n_of_bytes; i++) { + csum += bytes[i]; + if (csum > 255) + csum -= 255; + } + + return (~csum & 0xff); +} +EXPORT_SYMBOL_GPL(lin_get_checksum); + +static int lin_bump_rx_err(struct lin_device *ldev, const struct lin_frame *lf) +{ + struct net_device *ndev = ldev->ndev; + struct can_frame cf = {0 }; + + if (lf->lin_id > LIN_ID_MASK) { + netdev_dbg(ndev, "id exceeds LIN max id\n"); + cf.can_id = CAN_ERR_FLAG | CAN_ERR_PROT; + cf.data[3] = CAN_ERR_PROT_LOC_ID12_05; + } + + if (lf->len > LIN_MAX_DLEN) { + netdev_dbg(ndev, "frame exceeds number of bytes\n"); + cf.can_id = CAN_ERR_FLAG | CAN_ERR_PROT; + cf.data[3] = CAN_ERR_PROT_LOC_DLC; + } + + if (lf->len) { + u8 checksum = lin_get_checksum(LIN_FORM_PID(lf->lin_id), + lf->len, lf->data, + lf->checksum_mode); + + if (checksum != lf->checksum) { + netdev_dbg(ndev, "expected cksm: 0x%02x got: 0x%02x\n", + checksum, lf->checksum); + cf.can_id = CAN_ERR_FLAG | CAN_ERR_PROT; + cf.data[2] = CAN_ERR_PROT_FORM; + } + } + + if (cf.can_id & CAN_ERR_FLAG) { + struct can_frame *err_cf; + struct sk_buff *skb = alloc_can_err_skb(ndev, &err_cf); + + if (unlikely(!skb)) + return -ENOMEM; + + err_cf->can_id |= cf.can_id; + memcpy(err_cf->data, cf.data, CAN_MAX_DLEN); + + netif_rx(skb); + + return -EREMOTEIO; + } + + return 0; +} + +int lin_rx(struct lin_device *ldev, const struct lin_frame *lf) +{ + struct net_device *ndev = ldev->ndev; + struct can_frame *cf; + struct sk_buff *skb; + int ret; + + if (ldev->can.state == CAN_STATE_STOPPED) + return 0; + + netdev_dbg(ndev, "id:%02x, len:%u, data:%*ph, checksum:%02x (%s)\n", + lf->lin_id, lf->len, lf->len, lf->data, lf->checksum, + lf->checksum_mode ? "enhanced" : "classic"); + + ret = lin_bump_rx_err(ldev, lf); + if (ret) { + DEV_STATS_INC(ndev, rx_dropped); + return ret; + } + + skb = alloc_can_skb(ndev, &cf); + if (unlikely(!skb)) { + DEV_STATS_INC(ndev, rx_dropped); + return -ENOMEM; + } + + cf->can_id = lf->lin_id; + cf->len = min(lf->len, LIN_MAX_DLEN); + memcpy(cf->data, lf->data, cf->len); + + DEV_STATS_INC(ndev, rx_packets); + DEV_STATS_ADD(ndev, rx_bytes, cf->len); + + netif_receive_skb(skb); + + return 0; +} +EXPORT_SYMBOL_GPL(lin_rx); + +static int lin_set_bittiming(struct net_device *ndev) +{ + struct lin_device *ldev = netdev_priv(ndev); + unsigned int bitrate = ldev->can.bittiming.bitrate; + + return ldev->ldev_ops->update_bitrate(ldev, bitrate); +} + +static const u32 lin_bitrate[] = { 1200, 2400, 4800, 9600, 19200 }; + +struct lin_device *register_lin(struct device *dev, + const struct lin_device_ops *ldops) +{ + struct net_device *ndev; + struct lin_device *ldev; + int ret; + + if (!ldops || !ldops->ldo_tx || !ldops->update_bitrate || + !ldops->ldo_open || !ldops->ldo_stop) { + netdev_err(ndev, "missing mandatory lin_device_ops\n"); + return ERR_PTR(-EINVAL); + } + + ndev = alloc_candev(sizeof(struct lin_device), 1); + if (!ndev) + return ERR_PTR(-ENOMEM); + + ldev = netdev_priv(ndev); + + ldev->ldev_ops = ldops; + ndev->netdev_ops = &lin_netdev_ops; + ndev->flags |= IFF_ECHO; + ndev->mtu = CANFD_MTU; + ldev->can.bittiming.bitrate = LIN_DEFAULT_BAUDRATE; + ldev->can.ctrlmode = CAN_CTRLMODE_LIN; + ldev->can.ctrlmode_supported = 0; + ldev->can.bitrate_const = lin_bitrate; + ldev->can.bitrate_const_cnt = ARRAY_SIZE(lin_bitrate); + ldev->can.do_set_bittiming = lin_set_bittiming; + ldev->ndev = ndev; + ldev->dev = dev; + + SET_NETDEV_DEV(ndev, dev); + + ret = lin_set_bittiming(ndev); + if (ret) { + netdev_err(ndev, "set bittiming failed\n"); + goto exit_candev; + } + + ret = register_candev(ndev); + if (ret) + goto exit_candev; + + ldev->lin_ids_kobj = kobject_create_and_add("lin_ids", &ndev->dev.kobj); + if (!ldev->lin_ids_kobj) { + netdev_err(ndev, "Failed to create sysfs directory\n"); + ret = -ENOMEM; + goto exit_unreg; + } + + ret = lin_create_sysfs_id_files(ndev); + if (ret) { + netdev_err(ndev, "Failed to create sysfs entry: %d\n", ret); + goto exit_kobj_put; + } + + /* Using workqueue as tx over USB/SPI/... may sleep */ + ldev->wq = alloc_workqueue(dev_name(dev), WQ_FREEZABLE | WQ_MEM_RECLAIM, + 0); + if (!ldev->wq) + goto exit_rm_files; + + INIT_WORK(&ldev->tx_work, lin_tx_work_handler); + + netdev_info(ndev, "LIN initialized.\n"); + + return ldev; + +exit_rm_files: + lin_remove_sysfs_id_files(ndev); +exit_kobj_put: + kobject_put(ldev->lin_ids_kobj); +exit_unreg: + unregister_candev(ndev); +exit_candev: + free_candev(ndev); + return ERR_PTR(ret); +} +EXPORT_SYMBOL_GPL(register_lin); + +void unregister_lin(struct lin_device *ldev) +{ + struct net_device *ndev = ldev->ndev; + + lin_remove_sysfs_id_files(ndev); + kobject_put(ldev->lin_ids_kobj); + unregister_candev(ndev); + destroy_workqueue(ldev->wq); + free_candev(ndev); +} +EXPORT_SYMBOL_GPL(unregister_lin); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Christoph Fritz "); +MODULE_DESCRIPTION("LIN bus to CAN glue driver"); diff --git a/include/net/lin.h b/include/net/lin.h new file mode 100644 index 0000000000000..944e775c40e79 --- /dev/null +++ b/include/net/lin.h @@ -0,0 +1,91 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* Copyright (C) 2024 hexDEV GmbH - https://hexdev.de */ + +#ifndef _NET_LIN_H_ +#define _NET_LIN_H_ + +#include +#include +#include + +#define LIN_NUM_IDS 64 +#define LIN_HEADER_SIZE 3 +#define LIN_MAX_DLEN 8 + +#define LIN_MAX_BAUDRATE 20000 +#define LIN_MIN_BAUDRATE 1000 +#define LIN_DEFAULT_BAUDRATE 9600 +#define LIN_SYNC_BYTE 0x55 + +#define LIN_ID_MASK GENMASK(5, 0) +/* special ID descriptions for LIN */ +#define LIN_ENHANCED_CKSUM_FLAG 0x00000100U + +extern u8 lin_get_id_parity(u8 id); + +#define LIN_GET_ID(PID) FIELD_GET(LIN_ID_MASK, PID) +#define LIN_FORM_PID(ID) (LIN_GET_ID(ID) | \ + lin_get_id_parity(LIN_GET_ID(ID))) +#define LIN_GET_PARITY(PID) ((PID) & ~LIN_ID_MASK) +#define LIN_CHECK_PID(PID) (LIN_GET_PARITY(PID) == \ + LIN_GET_PARITY(LIN_FORM_PID(PID))) + +struct lin_attr { + struct kobj_attribute attr; + struct lin_device *ldev; +}; + +struct lin_device { + struct can_priv can; /* must be the first member */ + struct net_device *ndev; + struct device *dev; + const struct lin_device_ops *ldev_ops; + struct workqueue_struct *wq; + struct work_struct tx_work; + bool tx_busy; + struct sk_buff *tx_skb; + struct kobject *lin_ids_kobj; + struct lin_attr sysfs_entries[LIN_NUM_IDS]; +}; + +enum lin_checksum_mode { + LINBUS_CLASSIC = 0, + LINBUS_ENHANCED, +}; + +struct lin_frame { + u8 lin_id; + u8 len; + u8 data[LIN_MAX_DLEN]; + u8 checksum; + enum lin_checksum_mode checksum_mode; +}; + +struct lin_responder_answer { + bool is_active; + bool is_event_frame; + u8 event_associated_id; + struct lin_frame lf; +}; + +struct lin_device_ops { + int (*ldo_open)(struct lin_device *ldev); + int (*ldo_stop)(struct lin_device *ldev); + int (*ldo_tx)(struct lin_device *ldev, const struct lin_frame *frame); + int (*update_bitrate)(struct lin_device *ldev, u16 bitrate); + int (*update_responder_answer)(struct lin_device *ldev, + const struct lin_responder_answer *answ); + int (*get_responder_answer)(struct lin_device *ldev, u8 id, + struct lin_responder_answer *answ); +}; + +int lin_rx(struct lin_device *ldev, const struct lin_frame *lf); + +u8 lin_get_checksum(u8 pid, u8 n_of_bytes, const u8 *bytes, + enum lin_checksum_mode cm); + +struct lin_device *register_lin(struct device *dev, + const struct lin_device_ops *ldops); +void unregister_lin(struct lin_device *ldev); + +#endif /* _NET_LIN_H_ */ diff --git a/include/uapi/linux/can/netlink.h b/include/uapi/linux/can/netlink.h index 02ec32d694742..51b0e2a7624e4 100644 --- a/include/uapi/linux/can/netlink.h +++ b/include/uapi/linux/can/netlink.h @@ -103,6 +103,7 @@ struct can_ctrlmode { #define CAN_CTRLMODE_CC_LEN8_DLC 0x100 /* Classic CAN DLC option */ #define CAN_CTRLMODE_TDC_AUTO 0x200 /* CAN transiver automatically calculates TDCV */ #define CAN_CTRLMODE_TDC_MANUAL 0x400 /* TDCV is manually set up by user */ +#define CAN_CTRLMODE_LIN 0x800 /* LIN bus mode */ /* * CAN device statistics From patchwork Thu May 2 07:55:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Fritz X-Patchwork-Id: 794253 Received: from fritzc.com (mail.fritzc.com [213.160.72.247]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C670200A0; Thu, 2 May 2024 07:56:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.160.72.247 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636607; cv=none; b=okU7JE7uYZBV4VDjMeiGIiqezvh0GIn9uGWX+2IdRmxaXX3F+Ph5GmCB4wG8SadSv3cSJjDiMic6vz6WT5UDkrPNwQt6JXey4GLxfG2uMQXt3GCFDOrYiFegy/36Z593c+J4VOpoY9mT0UYOQF5BXCypbbMm7M8fW3eUy5czdrQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636607; c=relaxed/simple; bh=ZrFdSLcIMeL0Gw5YEaKUCGUEp/+XyImajt9Wq1/xfjE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Hv6kpB3aeLoAS4MIcMCJqW9TTkDwVOUelSYPK10lW9FREcsaft5+2OEemxgPSwth7/ehoI3I2J3yFDN+2C2YWU1oR7xAm9j/pdgXRQNMFOUNo4Axm7a4iW6GQgLyhTJ5Ia+FTGTatZ529uEpV2oO/9htXhMFzVW4VTtjuH2FQJo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de; spf=pass smtp.mailfrom=hexdev.de; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b=ig7BB6RC; arc=none smtp.client-ip=213.160.72.247 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hexdev.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b="ig7BB6RC" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=fritzc.com; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=ekCjuw1duvOWVggYCKKNCs0aIF99UZrHoS1CtHiTWfg=; b=ig7BB6RCwLMH866/CZi96QIftI dcIdraX20HU3RrpL/n7DR1l1cNJypVYf0eToJqsGZbWr6+gsmO2S0rR1qf74SYOH7tQRGrqC/ttFT svgoB9oC12687OxMmxGSPvG+7ASwW3Wxv+s2RAQNW/o6yr0YsbVKoyLGTqTnQXXGm6bU=; Received: from 127.0.0.1 by fritzc.com with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim latest) (envelope-from ) id 1s2RIx-001Yg7-0R; Thu, 02 May 2024 09:56:35 +0200 From: Christoph Fritz To: Oliver Hartkopp , Marc Kleine-Budde , Vincent Mailhol , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jiri Kosina , Benjamin Tissoires , Greg Kroah-Hartman , Jiri Slaby , Sebastian Reichel , Linus Walleij Cc: Andreas Lauser , Jonathan Corbet , Pavel Pisa , linux-can@vger.kernel.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-input@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH v2 03/12] tty: serdev: Add flag buffer aware receive_buf_fp() Date: Thu, 2 May 2024 09:55:25 +0200 Message-Id: <20240502075534.882628-4-christoph.fritz@hexdev.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240502075534.882628-1-christoph.fritz@hexdev.de> References: <20240502075534.882628-1-christoph.fritz@hexdev.de> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This patch introduces an additional receive buffer callback variation besides the already existing receive_buf(). This new callback function also passes the flag buffer (TTY_NORMAL, TTY_BREAK, and friends). If defined, this function gets prioritized and called instead of the standard receive_buf(). An alternative approach could have been to enhance the receive_buf() function and update all drivers that use it. Signed-off-by: Christoph Fritz --- drivers/tty/serdev/serdev-ttyport.c | 2 +- include/linux/serdev.h | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/drivers/tty/serdev/serdev-ttyport.c b/drivers/tty/serdev/serdev-ttyport.c index 3d7ae7fa50186..bb47691afdb21 100644 --- a/drivers/tty/serdev/serdev-ttyport.c +++ b/drivers/tty/serdev/serdev-ttyport.c @@ -32,7 +32,7 @@ static size_t ttyport_receive_buf(struct tty_port *port, const u8 *cp, if (!test_bit(SERPORT_ACTIVE, &serport->flags)) return 0; - ret = serdev_controller_receive_buf(ctrl, cp, count); + ret = serdev_controller_receive_buf(ctrl, cp, fp, count); dev_WARN_ONCE(&ctrl->dev, ret > count, "receive_buf returns %zu (count = %zu)\n", diff --git a/include/linux/serdev.h b/include/linux/serdev.h index ff78efc1f60df..c6ef5a8988e07 100644 --- a/include/linux/serdev.h +++ b/include/linux/serdev.h @@ -23,11 +23,17 @@ struct serdev_device; * struct serdev_device_ops - Callback operations for a serdev device * @receive_buf: Function called with data received from device; * returns number of bytes accepted; may sleep. + * @receive_buf_fp: Function called with data and flag buffer received + * from device; If defined, this function gets called + * instead of @receive_buf; + * returns number of bytes accepted; may sleep. * @write_wakeup: Function called when ready to transmit more data; must * not sleep. */ struct serdev_device_ops { size_t (*receive_buf)(struct serdev_device *, const u8 *, size_t); + ssize_t (*receive_buf_fp)(struct serdev_device *, const u8 *, + const u8 *, size_t); void (*write_wakeup)(struct serdev_device *); }; @@ -186,15 +192,20 @@ static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl } static inline size_t serdev_controller_receive_buf(struct serdev_controller *ctrl, - const u8 *data, + const u8 *data, const u8 *fp, size_t count) { struct serdev_device *serdev = ctrl->serdev; - if (!serdev || !serdev->ops->receive_buf) + if (!serdev || !serdev->ops) return 0; - return serdev->ops->receive_buf(serdev, data, count); + if (serdev->ops->receive_buf_fp) + return serdev->ops->receive_buf_fp(serdev, data, fp, count); + else if (serdev->ops->receive_buf) + return serdev->ops->receive_buf(serdev, data, count); + + return 0; } #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS) From patchwork Thu May 2 07:55:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Fritz X-Patchwork-Id: 794252 Received: from fritzc.com (mail.fritzc.com [213.160.72.247]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C6A22032D; Thu, 2 May 2024 07:56:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.160.72.247 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636609; cv=none; b=N50dJ5EEZVfJwtF7IXGPxHiogoNI+WXtll3oNkpMCpCkojtWAmRAtvR1UoiYeq2Xy5E5N1d9sSilQzaT+iAKSYPYNaTVED8i5932LaTo/QXYqiOHK9eBIikVLiLUjQvrz1KebvoUtUr6A97FGXcx5yFsHOxhsJ8eOd6c4JeIcn8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636609; c=relaxed/simple; bh=Tzx1/eWSRHcqk3KBn3lZQgePhYDOso/sqz5cAwscRmg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WDwlh+45zfycAWrKSZkI6fclTlyCWSe6TPnYNasSHe9CSg8TZ0IQceQ3WblligTQJ29uLQhGZhSW2CYfOFb3NVYI7axQwi8oLOUA351UmHpxkyGwNgjRwOktjw0nVH7fUbl7k1sQWlibBEa6xe5sanrpPBy9G0PvJL7inw7Q3SE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de; spf=pass smtp.mailfrom=hexdev.de; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b=jwtCNtc9; arc=none smtp.client-ip=213.160.72.247 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hexdev.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b="jwtCNtc9" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=fritzc.com; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=Snf7mJCYWthVKyp64Q4r3SmYiNCoko04PmFq7ktAJeo=; b=jwtCNtc97I012wkju8IW/6eHTb h7mQJwxgL338t4QIUCBz2yZp4/kHG4RyfTVSvkYvTgDJNZnpXDYLNzf5tlGuxk+E4LzIVY5/PkvH3 Id/Juk+jnsBvfuWM+reBR2AsATdvEpchNNFZbDbdkzdl3rRy46wH0fdV0AeS5BtJoZjg=; Received: from 127.0.0.1 by fritzc.com with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim latest) (envelope-from ) id 1s2RIy-001Yg7-0s; Thu, 02 May 2024 09:56:36 +0200 From: Christoph Fritz To: Oliver Hartkopp , Marc Kleine-Budde , Vincent Mailhol , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jiri Kosina , Benjamin Tissoires , Greg Kroah-Hartman , Jiri Slaby , Sebastian Reichel , Linus Walleij Cc: Andreas Lauser , Jonathan Corbet , Pavel Pisa , linux-can@vger.kernel.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-input@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH v2 04/12] tty: serdev: Add method to enable break flags Date: Thu, 2 May 2024 09:55:26 +0200 Message-Id: <20240502075534.882628-5-christoph.fritz@hexdev.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240502075534.882628-1-christoph.fritz@hexdev.de> References: <20240502075534.882628-1-christoph.fritz@hexdev.de> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The recently introduced callback function receive_buf_fp() brings flags buffer support. To allow signaling of TTY_BREAK flags, this patch introduces serdev_device_set_break_detection() and an implementation for ttyport. This enables serdev devices to configure their underlying tty port to signal or ignore break conditions. Signed-off-by: Christoph Fritz --- drivers/tty/serdev/core.c | 11 +++++++++++ drivers/tty/serdev/serdev-ttyport.c | 17 +++++++++++++++++ include/linux/serdev.h | 2 ++ 3 files changed, 30 insertions(+) diff --git a/drivers/tty/serdev/core.c b/drivers/tty/serdev/core.c index 613cb356b918d..23a1e76cb553b 100644 --- a/drivers/tty/serdev/core.c +++ b/drivers/tty/serdev/core.c @@ -339,6 +339,17 @@ unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned i } EXPORT_SYMBOL_GPL(serdev_device_set_baudrate); +void serdev_device_set_break_detection(struct serdev_device *serdev, bool enable) +{ + struct serdev_controller *ctrl = serdev->ctrl; + + if (!ctrl || !ctrl->ops->set_break_detection) + return; + + ctrl->ops->set_break_detection(ctrl, enable); +} +EXPORT_SYMBOL_GPL(serdev_device_set_break_detection); + void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable) { struct serdev_controller *ctrl = serdev->ctrl; diff --git a/drivers/tty/serdev/serdev-ttyport.c b/drivers/tty/serdev/serdev-ttyport.c index bb47691afdb21..e928bf4175c6f 100644 --- a/drivers/tty/serdev/serdev-ttyport.c +++ b/drivers/tty/serdev/serdev-ttyport.c @@ -192,6 +192,22 @@ static void ttyport_set_flow_control(struct serdev_controller *ctrl, bool enable tty_set_termios(tty, &ktermios); } +static void ttyport_set_break_detection(struct serdev_controller *ctrl, bool enable) +{ + struct serport *serport = serdev_controller_get_drvdata(ctrl); + struct tty_struct *tty = serport->tty; + struct ktermios ktermios = tty->termios; + + ktermios.c_iflag &= ~(IGNBRK | BRKINT); + + if (enable) + ktermios.c_iflag |= BRKINT; + else + ktermios.c_iflag |= IGNBRK; + + tty_set_termios(tty, &ktermios); +} + static int ttyport_set_parity(struct serdev_controller *ctrl, enum serdev_parity parity) { @@ -263,6 +279,7 @@ static const struct serdev_controller_ops ctrl_ops = { .open = ttyport_open, .close = ttyport_close, .set_flow_control = ttyport_set_flow_control, + .set_break_detection = ttyport_set_break_detection, .set_parity = ttyport_set_parity, .set_baudrate = ttyport_set_baudrate, .wait_until_sent = ttyport_wait_until_sent, diff --git a/include/linux/serdev.h b/include/linux/serdev.h index c6ef5a8988e07..d08aa7e16f9f8 100644 --- a/include/linux/serdev.h +++ b/include/linux/serdev.h @@ -94,6 +94,7 @@ struct serdev_controller_ops { int (*open)(struct serdev_controller *); void (*close)(struct serdev_controller *); void (*set_flow_control)(struct serdev_controller *, bool); + void (*set_break_detection)(struct serdev_controller *, bool); int (*set_parity)(struct serdev_controller *, enum serdev_parity); unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int); void (*wait_until_sent)(struct serdev_controller *, long); @@ -215,6 +216,7 @@ void serdev_device_close(struct serdev_device *); int devm_serdev_device_open(struct device *, struct serdev_device *); unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int); void serdev_device_set_flow_control(struct serdev_device *, bool); +void serdev_device_set_break_detection(struct serdev_device *, bool); int serdev_device_write_buf(struct serdev_device *, const u8 *, size_t); void serdev_device_wait_until_sent(struct serdev_device *, long); int serdev_device_get_tiocm(struct serdev_device *); From patchwork Thu May 2 07:55:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Fritz X-Patchwork-Id: 794251 Received: from fritzc.com (mail.fritzc.com [213.160.72.247]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF37D23775; Thu, 2 May 2024 07:56:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.160.72.247 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636609; cv=none; b=D5UFQQ6d5PjdED2hCQIBUNl+vY7CHZ04O9ekbchhuLdQdRUAMuPPvRQsjNhceF7jqH6J2LEMRE2Y731aXowY4ee2Cxeao40z33T1AJKNSOgKeGWU5Eb1uyY077Cv4UFADPgqfawh6BRxmoD9GPOtpnd9/RPLF883VJud7FfkFJo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636609; c=relaxed/simple; bh=KjK+ygnrZsuhjU+N9ZY0h772Kw/iSxchdX9Fp9lMvyU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Spu8M7IjLv4G8ta9nLkgt6GsW1ruGd6Oi/kx5BY0Nfy4iErTB76d7scSFCKvMaOiAtKmRJDYIHrof1xx4N/G8pzQFHvOyfnRQBBSM37wQnf4Yey8ge0RFWMo2MMdbtyr866oafMeA4aOJfLHNFRZm5E0VMzoG9NtAkL4JeH5vy4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de; spf=pass smtp.mailfrom=hexdev.de; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b=xoBTEJjv; arc=none smtp.client-ip=213.160.72.247 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hexdev.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b="xoBTEJjv" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=fritzc.com; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=ETncEe944ZpMNYFBQ9viLt3yvbXCLqJ9t/iaLRcdEi4=; b=xoBTEJjvm0H0EV+kfYfdn/xdhF U/7CzF+rECZaVpwsYWBldA8DuZ9QxeC5+Wa6hfGySNRvbLZq2jNJExW2Tlkjhv/55dv0B0UYHYOep dXBrpkixvuHcC0QYdsebn6dGOT/Obz3COtizSbpKybABj2Brd/hVPFTCbFADuz94wQ/k=; Received: from 127.0.0.1 by fritzc.com with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim latest) (envelope-from ) id 1s2RJ0-001Yg7-2g; Thu, 02 May 2024 09:56:39 +0200 From: Christoph Fritz To: Oliver Hartkopp , Marc Kleine-Budde , Vincent Mailhol , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jiri Kosina , Benjamin Tissoires , Greg Kroah-Hartman , Jiri Slaby , Sebastian Reichel , Linus Walleij Cc: Andreas Lauser , Jonathan Corbet , Pavel Pisa , linux-can@vger.kernel.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-input@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH v2 06/12] dt-bindings: net/can: Add serial (serdev) LIN adapter Date: Thu, 2 May 2024 09:55:28 +0200 Message-Id: <20240502075534.882628-7-christoph.fritz@hexdev.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240502075534.882628-1-christoph.fritz@hexdev.de> References: <20240502075534.882628-1-christoph.fritz@hexdev.de> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This patch adds dt-bindings for serial LIN bus adapters. These adapters are basically just LIN transceivers that get hard-wired with serial devices. Signed-off-by: Christoph Fritz --- .../bindings/net/can/hexdev,lin-serdev.yaml | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml diff --git a/Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml b/Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml new file mode 100644 index 0000000000000..cbf9f63a2a49f --- /dev/null +++ b/Documentation/devicetree/bindings/net/can/hexdev,lin-serdev.yaml @@ -0,0 +1,32 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/net/can/hexdev,lin-serdev.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Serial LIN Adapter + +description: + LIN transceiver, mostly hard-wired to a serial device, used for communication + on a LIN bus. + For more details on an adapter, visit . + +maintainers: + - Christoph Fritz + +properties: + compatible: + const: hexdev,lin-serdev + +required: + - compatible + +unevaluatedProperties: false + +examples: + - | + serial { + linbus { + compatible = "linux,lin-serdev"; + }; + }; From patchwork Thu May 2 07:55:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Fritz X-Patchwork-Id: 794249 Received: from fritzc.com (mail.fritzc.com [213.160.72.247]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0826A42071; Thu, 2 May 2024 07:56:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.160.72.247 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636611; cv=none; b=IqFVmc8Y1dDPHDBdqpYHBBf08oNHTTDEtNNnO1UrVr+dKpuswdtUzKnHYjm2z+hd0bFIQ72ticwfL8PQ53Q3/pdeb14mD0A5FyaMoIIM/CE9+08AZxyfu1k/IYzQpyiqLwG7DE23Oya2RCWBhAwWKTGw1OetwfCzHNMU60HBV0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636611; c=relaxed/simple; bh=AaIXybvh0Qdg2fe21y2T3UM1+G6/Z3jU1KBSAcy/czo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tW2kzkI3x3xiM2XJ0ZE48U8g3yV3WLIUnAXtWCf8QXkWRedDiNy3WNZMw1PEcG9EXaKtgeqXXL5OS3ozS7l2BMLYvKRHWwoaIlwT+W9T7Lr10e7xDGzm76Em7rku8tqZCRTJAh0+nbNPOd1d7UaBPJanr2fb5soVCMCO7sVWHys= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de; spf=pass smtp.mailfrom=hexdev.de; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b=wo9Tg0nd; arc=none smtp.client-ip=213.160.72.247 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hexdev.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b="wo9Tg0nd" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=fritzc.com; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=M8Jz2oA9KeaIgZDfV7MrQ+GmqetbV9IhG4DXAmmuPXQ=; b=wo9Tg0ndWiLo8PrD6NBggezdTH nf+T+iUc5dwDPSJ3XEFeaTeJlhME+mIS4FFopCqPrzR4UTAjwuvMoamVZ1VK7ElhAN+aM3ZMB+BBa 9vX1nGhrdwLbbpoy3PChsk8FQ62Yzf+YlFtARqk+uBOgTIKGXgPVP72SEIcZC+WpnmE8=; Received: from 127.0.0.1 by fritzc.com with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim latest) (envelope-from ) id 1s2RJ3-001Yg7-1Y; Thu, 02 May 2024 09:56:41 +0200 From: Christoph Fritz To: Oliver Hartkopp , Marc Kleine-Budde , Vincent Mailhol , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jiri Kosina , Benjamin Tissoires , Greg Kroah-Hartman , Jiri Slaby , Sebastian Reichel , Linus Walleij Cc: Andreas Lauser , Jonathan Corbet , Pavel Pisa , linux-can@vger.kernel.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-input@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH v2 08/12] can: lin: Add special frame id for rx offload config Date: Thu, 2 May 2024 09:55:30 +0200 Message-Id: <20240502075534.882628-9-christoph.fritz@hexdev.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240502075534.882628-1-christoph.fritz@hexdev.de> References: <20240502075534.882628-1-christoph.fritz@hexdev.de> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 A LIN bus supports up to 64 identifiers in one byte. This commit adds a special frame ID, beyond the actual LIN identifiers, for signaling RX offload configuration requests. This ID will be utilized in future LIN enhancements to the CAN broadcast manager. Signed-off-by: Christoph Fritz --- include/net/lin.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/net/lin.h b/include/net/lin.h index 944e775c40e79..e7c7c820a6e18 100644 --- a/include/net/lin.h +++ b/include/net/lin.h @@ -19,6 +19,7 @@ #define LIN_ID_MASK GENMASK(5, 0) /* special ID descriptions for LIN */ +#define LIN_RXOFFLOAD_DATA_FLAG 0x00000200U #define LIN_ENHANCED_CKSUM_FLAG 0x00000100U extern u8 lin_get_id_parity(u8 id); From patchwork Thu May 2 07:55:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Fritz X-Patchwork-Id: 794248 Received: from fritzc.com (mail.fritzc.com [213.160.72.247]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4968224205; Thu, 2 May 2024 07:56:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.160.72.247 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636612; cv=none; b=aDziSAhc/6WKUbs+kV+y5mXjVjIPyVL+mJWYCHC/V6l166QTxBlzWaUhFmEdaWvXDj39X+wIgmpvRoBxAkW4nL8xFOaJpcWkljxqAJAUJO7d8lKxDerxFUTZi3XhKvgvclhvhRhbeNuTotuAnHotDpfhu3cBnrDA8pieeDqPcWs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636612; c=relaxed/simple; bh=GSulo82rKnPvXyP3RQsoy6rgLW6KWucKAnbWqMbp1f0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uJ/Fp2gCSb688oG7GmxHwrnP4C2zk7VNXPPPjkV3oJumf78AUAf3uQLr3DAHDrHlQJfy/dCiX9qly50h6Od672m45jJgApS5DUSB+vY7KwQjq5znECm/PQHc4heY5BLzlscyg5XeVs0Hi9ryo2DvUoVmAKAmmidfw8FNT6TvnGU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de; spf=pass smtp.mailfrom=hexdev.de; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b=aji8x1hI; arc=none smtp.client-ip=213.160.72.247 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hexdev.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b="aji8x1hI" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=fritzc.com; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=v2goQZydtQiYmy2+LR80/ACY6cQ1Zev0nahvSPqVvHU=; b=aji8x1hI8HOI2IoWVdBiQhMo/Z vUus8nS7OJlbv5qhBYJFerWfMToP/RJwQ4EOl1qlzQMk3R9wZpjC4Imm3crpOMuobA6rWcVZcMtmR rVMhFwUvLkJhG3eQSrN0g/Juu9BfzUqd6JkgD/tPW6znbJSs+USg2bsq7BV3FoEJsnME=; Received: from 127.0.0.1 by fritzc.com with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim latest) (envelope-from ) id 1s2RJ5-001Yg7-2I; Thu, 02 May 2024 09:56:44 +0200 From: Christoph Fritz To: Oliver Hartkopp , Marc Kleine-Budde , Vincent Mailhol , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jiri Kosina , Benjamin Tissoires , Greg Kroah-Hartman , Jiri Slaby , Sebastian Reichel , Linus Walleij Cc: Andreas Lauser , Jonathan Corbet , Pavel Pisa , linux-can@vger.kernel.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-input@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH v2 10/12] can: lin: Handle rx offload config frames Date: Thu, 2 May 2024 09:55:32 +0200 Message-Id: <20240502075534.882628-11-christoph.fritz@hexdev.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240502075534.882628-1-christoph.fritz@hexdev.de> References: <20240502075534.882628-1-christoph.fritz@hexdev.de> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The CAN Broadcast Manager now has the capability to dispatch CANFD frames marked with the id LINBUS_RXOFFLOAD_ID. This patch introduces functionality to interpret these specific frames, enabling the configuration of RX offloading within the LIN driver. Signed-off-by: Christoph Fritz --- drivers/net/can/lin.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/drivers/net/can/lin.c b/drivers/net/can/lin.c index 95906003666fb..ee2ebea2c865f 100644 --- a/drivers/net/can/lin.c +++ b/drivers/net/can/lin.c @@ -194,6 +194,27 @@ static void lin_remove_sysfs_id_files(struct net_device *ndev) } } +static int lin_setup_rxoffload(struct lin_device *ldev, + struct canfd_frame *cfd) +{ + struct lin_responder_answer answ; + + if (!(cfd->flags & CANFD_FDF)) + return -EMSGSIZE; + + BUILD_BUG_ON(sizeof(struct lin_responder_answer) > sizeof(cfd->data)); + memcpy(&answ, cfd->data, sizeof(struct lin_responder_answer)); + + answ.lf.checksum_mode = (cfd->can_id & LIN_ENHANCED_CKSUM_FLAG) ? + LINBUS_ENHANCED : LINBUS_CLASSIC; + + if (answ.lf.lin_id > LIN_ID_MASK || + answ.event_associated_id > LIN_ID_MASK) + return -EINVAL; + + return ldev->ldev_ops->update_responder_answer(ldev, &answ); +} + static void lin_tx_work_handler(struct work_struct *ws) { struct lin_device *ldev = container_of(ws, struct lin_device, @@ -206,6 +227,14 @@ static void lin_tx_work_handler(struct work_struct *ws) ldev->tx_busy = true; cfd = (struct canfd_frame *)ldev->tx_skb->data; + + if (cfd->can_id & LIN_RXOFFLOAD_DATA_FLAG) { + ret = lin_setup_rxoffload(ldev, cfd); + if (ret < 0) + netdev_err(ndev, "setting up rx failed %d\n", ret); + goto lin_tx_out; + } + lf.checksum_mode = (cfd->can_id & LIN_ENHANCED_CKSUM_FLAG) ? LINBUS_ENHANCED : LINBUS_CLASSIC; lf.lin_id = cfd->can_id & LIN_ID_MASK; From patchwork Thu May 2 07:55:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Fritz X-Patchwork-Id: 794247 Received: from fritzc.com (mail.fritzc.com [213.160.72.247]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8679954277; Thu, 2 May 2024 07:56:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.160.72.247 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636617; cv=none; b=OLASDwYXjUBXTFNexPreDPHsZACop6iao5acfSZEOEgsTCkAmrzKdIeX4I9f9558pyOo8ZTwfu5y0ag4NClSrBjaonOpNutKtt/2i+GjIVmCtVqGxIojyiH7mCGYGUQnj0RtkDft2j2VVc7aCLobk2XFNHb1HP3kAM2Ie2a8RRA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636617; c=relaxed/simple; bh=EqNe2Hv2CPPHgjp7xFwYFu75robyX6ah/DfzzEc5leM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OXHILNx3GT3NRQGYPyJ0yR30f6z+3qZZ7wvfOoNck90cnfy7LBxHtZ+mlO+dDysdEHdQmK19H+qlNWMzE0+OUH5RrTmTfyVwRsAg01U/HlE5rvYpl10f2/uwSpbHSH1cGdviHzbqoxZf7IINV0USF92sA+hGsyPh8MvA8VWVWbw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de; spf=pass smtp.mailfrom=hexdev.de; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b=CrK5D5t+; arc=none smtp.client-ip=213.160.72.247 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=hexdev.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hexdev.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=fritzc.com header.i=@fritzc.com header.b="CrK5D5t+" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=fritzc.com; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=YlFhQ3rwhJDgy3vyDj0hKBqSv4ZLIP4QMDO+nEa0Vc4=; b=CrK5D5t+ghYDT+W0nvalQIx3G9 GJcEqo9dZsZjBfE6TgnwTmhORJuYWzX8Z+6iEDxgLHArX8BkGvCWpSnu0MumXoW+14DuqWQ3P/roi YHS7xNJSWGPcWAtVgAcwoeEaZ8Q0tLkGXjZBFsrteLpg6Ajcva6pRQXCeAvZtEcxITfY=; Received: from 127.0.0.1 by fritzc.com with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim latest) (envelope-from ) id 1s2RJ8-001Yg7-04; Thu, 02 May 2024 09:56:46 +0200 From: Christoph Fritz To: Oliver Hartkopp , Marc Kleine-Budde , Vincent Mailhol , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jiri Kosina , Benjamin Tissoires , Greg Kroah-Hartman , Jiri Slaby , Sebastian Reichel , Linus Walleij Cc: Andreas Lauser , Jonathan Corbet , Pavel Pisa , linux-can@vger.kernel.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-input@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH v2 12/12] HID: hexLIN: Implement ability to update lin mode Date: Thu, 2 May 2024 09:55:34 +0200 Message-Id: <20240502075534.882628-13-christoph.fritz@hexdev.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240502075534.882628-1-christoph.fritz@hexdev.de> References: <20240502075534.882628-1-christoph.fritz@hexdev.de> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This patch enhances the hexLIN driver by implementing the newly introduced update_lin_mode() callback. So that either commander or responder mode can be configured on this hardware. Signed-off-by: Christoph Fritz --- drivers/hid/hid-hexdev-hexlin.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/hid/hid-hexdev-hexlin.c b/drivers/hid/hid-hexdev-hexlin.c index 34019043f329e..64c1afcba0550 100644 --- a/drivers/hid/hid-hexdev-hexlin.c +++ b/drivers/hid/hid-hexdev-hexlin.c @@ -180,6 +180,8 @@ HEXLIN_GET_CMD(get_baudrate, HEXLIN_GET_BAUDRATE) } HEXLIN_VAL_CMD(send_break, HEXLIN_SEND_BREAK, hexlin_val8_req, u8) +HEXLIN_VAL_CMD(set_mode_controller, HEXLIN_SET_MODE_CONTROLLER, hexlin_val8_req, + bool) static int hexlin_queue_frames_insert(struct hexlin_priv_data *priv, const u8 *raw_data, int sz) @@ -349,6 +351,14 @@ static int hexlin_ldo_tx(struct lin_device *ldev, return ret; } +static int hexlin_update_lin_mode(struct lin_device *ldev, enum lin_mode lm) +{ + struct hid_device *hdev = to_hid_device(ldev->dev); + struct hexlin_priv_data *priv = hid_get_drvdata(hdev); + + return hexlin_set_mode_controller(priv, lm == LINBUS_COMMANDER); +} + static int hexlin_update_bitrate(struct lin_device *ldev, u16 bitrate) { struct hid_device *hdev = to_hid_device(ldev->dev); @@ -420,6 +430,7 @@ static const struct lin_device_ops hexlin_ldo = { .ldo_open = hexlin_open, .ldo_stop = hexlin_stop, .ldo_tx = hexlin_ldo_tx, + .update_lin_mode = hexlin_update_lin_mode, .update_bitrate = hexlin_update_bitrate, .get_responder_answer = hexlin_get_responder_answer, .update_responder_answer = hexlin_update_resp_answer,