From patchwork Wed Jul 26 15:08:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Xiaoxu X-Patchwork-Id: 708253 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D94EC001DE for ; Wed, 26 Jul 2023 07:14:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231841AbjGZHOj (ORCPT ); Wed, 26 Jul 2023 03:14:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230123AbjGZHOM (ORCPT ); Wed, 26 Jul 2023 03:14:12 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C0A959DD; Wed, 26 Jul 2023 00:10:31 -0700 (PDT) Received: from dggpeml100024.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4R9lNB2LkbzLp4H; Wed, 26 Jul 2023 15:07:54 +0800 (CST) Received: from china (10.175.101.107) by dggpeml100024.china.huawei.com (7.185.36.115) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Wed, 26 Jul 2023 15:10:28 +0800 From: Zhang Xiaoxu To: , , , CC: , , , Subject: [PATCH v2 -next 1/3] spi: mockup: Add SPI controller testing driver Date: Wed, 26 Jul 2023 15:08:47 +0000 Message-ID: <20230726150849.28407-2-zhangxiaoxu5@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230726150849.28407-1-zhangxiaoxu5@huawei.com> References: <20230726150849.28407-1-zhangxiaoxu5@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.101.107] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml100024.china.huawei.com (7.185.36.115) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org From: Wei Yongjun This enables SPI controller Testing driver, which provides a way to test SPI subsystem. This is accomplished by executing the following command: $ echo adcxx1s 0 > /sys/class/spi_master/spi0/new_device The name of the target driver and its chip select were used to instantiate the device. $ ls /sys/bus/spi/devices/spi0.0/hwmon/hwmon0/ -l total 0 lrwxrwxrwx 1 root root 0 Aug 10 08:58 device -> ../../../spi0.0 drwxr-xr-x 2 root root 0 Aug 10 08:58 power lrwxrwxrwx 1 root root 0 Aug 10 08:58 subsystem -> ../../../../../../../../class/hwmon -rw-r--r-- 1 root root 4096 Aug 10 08:58 uevent Remove target device by executing the following command: $ echo 0 > /sys/class/spi_master/spi0/delete_device Signed-off-by: Wei Yongjun Signed-off-by: Zhang Xiaoxu --- drivers/spi/Kconfig | 12 +++ drivers/spi/Makefile | 1 + drivers/spi/spi-mockup.c | 212 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 225 insertions(+) create mode 100644 drivers/spi/spi-mockup.c diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 8962b2557615..1768e57f0088 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -1185,6 +1185,18 @@ config SPI_TLE62X0 sysfs interface, with each line presented as a kind of GPIO exposing both switch control and diagnostic feedback. +config SPI_MOCKUP + tristate "SPI controller Testing Driver" + depends on OF + help + This enables SPI controller testing driver, which provides a way to + test SPI subsystem. + + If you do build this module, be sure to read the notes and warnings + in . + + If you don't know what to do here, definitely say N. + # # Add new SPI protocol masters in alphabetical order above this line # diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index 080c2c1b3ec1..8ce94e7295b7 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile @@ -12,6 +12,7 @@ obj-$(CONFIG_SPI_MEM) += spi-mem.o obj-$(CONFIG_SPI_MUX) += spi-mux.o obj-$(CONFIG_SPI_SPIDEV) += spidev.o obj-$(CONFIG_SPI_LOOPBACK_TEST) += spi-loopback-test.o +obj-$(CONFIG_SPI_MOCKUP) += spi-mockup.o # SPI master controller drivers (bus) obj-$(CONFIG_SPI_ALTERA) += spi-altera-platform.o diff --git a/drivers/spi/spi-mockup.c b/drivers/spi/spi-mockup.c new file mode 100644 index 000000000000..4debfd1fb9fd --- /dev/null +++ b/drivers/spi/spi-mockup.c @@ -0,0 +1,212 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * SPI controller Testing Driver + * + * Copyright(c) 2022 Huawei Technologies Co., Ltd. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#define MOCKUP_CHIPSELECT_MAX 8 + +struct mockup_spi { + struct mutex lock; + struct spi_device *devs[MOCKUP_CHIPSELECT_MAX]; +}; + +static struct spi_controller *to_spi_controller(struct device *dev) +{ + return container_of(dev, struct spi_controller, dev); +} + +static ssize_t +new_device_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct spi_controller *ctrl = to_spi_controller(dev); + struct spi_board_info info; + struct mockup_spi *mock; + struct spi_device *spi; + char *blank, end; + int status; + + memset(&info, 0, sizeof(struct spi_board_info)); + + blank = strchr(buf, ' '); + if (!blank) { + dev_err(dev, "%s: Extra parameters\n", "new_device"); + return -EINVAL; + } + + if (blank - buf > SPI_NAME_SIZE - 1) { + dev_err(dev, "%s: Invalid device name\n", "new_device"); + return -EINVAL; + } + + memcpy(info.modalias, buf, blank - buf); + + status = sscanf(++blank, "%hi%c", &info.chip_select, &end); + if (status < 1) { + dev_err(dev, "%s: Can't parse SPI chipselect\n", "new_device"); + return -EINVAL; + } + + if (status > 1 && end != '\n') { + dev_err(dev, "%s: Extra parameters\n", "new_device"); + return -EINVAL; + } + + if (info.chip_select >= ctrl->num_chipselect) { + dev_err(dev, "%s: Invalid chip_select\n", "new_device"); + return -EINVAL; + } + + mock = spi_controller_get_devdata(ctrl); + mutex_lock(&mock->lock); + + if (mock->devs[info.chip_select]) { + dev_err(dev, "%s: Chipselect %d already in use\n", + "new_device", info.chip_select); + mutex_unlock(&mock->lock); + return -EINVAL; + } + + spi = spi_new_device(ctrl, &info); + if (!spi) { + mutex_unlock(&mock->lock); + return -ENOMEM; + } + mock->devs[info.chip_select] = spi; + + mutex_unlock(&mock->lock); + + dev_info(dev, "%s: Instantiated device %s at 0x%02x\n", "new_device", + info.modalias, info.chip_select); + + return count; +} +static DEVICE_ATTR_WO(new_device); + +static ssize_t +delete_device_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct spi_controller *ctrl = to_spi_controller(dev); + struct mockup_spi *mock; + struct spi_device *spi; + unsigned short chip; + char end; + int res; + + /* Parse parameters, reject extra parameters */ + res = sscanf(buf, "%hi%c", &chip, &end); + if (res < 1) { + dev_err(dev, "%s: Can't parse SPI address\n", "delete_device"); + return -EINVAL; + } + if (res > 1 && end != '\n') { + dev_err(dev, "%s: Extra parameters\n", "delete_device"); + return -EINVAL; + } + + if (chip >= ctrl->num_chipselect) { + dev_err(dev, "%s: Invalid chip_select\n", "delete_device"); + return -EINVAL; + } + + mock = spi_controller_get_devdata(ctrl); + mutex_lock(&mock->lock); + + spi = mock->devs[chip]; + if (!spi) { + mutex_unlock(&mock->lock); + dev_err(dev, "%s: Invalid chip_select\n", "delete_device"); + return -ENOENT; + } + + dev_info(dev, "%s: Deleting device %s at 0x%02hx\n", "delete_device", + dev_name(&spi->dev), chip); + + spi_unregister_device(spi); + mock->devs[chip] = NULL; + + mutex_unlock(&mock->lock); + + return count; +} +static DEVICE_ATTR_WO(delete_device); + +static struct attribute *spi_mockup_attrs[] = { + &dev_attr_new_device.attr, + &dev_attr_delete_device.attr, + NULL +}; +ATTRIBUTE_GROUPS(spi_mockup); + +static int spi_mockup_transfer(struct spi_controller *ctrl, + struct spi_message *msg) +{ + msg->status = 0; + spi_finalize_current_message(ctrl); + + return 0; +} + +static int spi_mockup_probe(struct platform_device *pdev) +{ + struct spi_controller *ctrl; + struct mockup_spi *mock; + int ret; + + ctrl = spi_alloc_host(&pdev->dev, sizeof(struct mockup_spi)); + if (!ctrl) { + pr_err("failed to alloc spi controller\n"); + return -ENOMEM; + } + + platform_set_drvdata(pdev, ctrl); + + ctrl->dev.of_node = pdev->dev.of_node; + ctrl->dev.groups = spi_mockup_groups; + ctrl->num_chipselect = MOCKUP_CHIPSELECT_MAX; + ctrl->mode_bits = SPI_MODE_USER_MASK; + ctrl->bus_num = 0; + ctrl->transfer_one_message = spi_mockup_transfer; + + mock = spi_controller_get_devdata(ctrl); + mutex_init(&mock->lock); + + ret = devm_spi_register_controller(&pdev->dev, ctrl); + if (ret) { + spi_controller_put(ctrl); + return ret; + } + + return 0; +} + +static const struct of_device_id spi_mockup_match[] = { + { .compatible = "spi-mockup", }, + { } +}; +MODULE_DEVICE_TABLE(of, spi_mockup_match); + +static struct platform_driver spi_mockup_driver = { + .probe = spi_mockup_probe, + .driver = { + .name = "spi-mockup", + .of_match_table = spi_mockup_match, + }, +}; +module_platform_driver(spi_mockup_driver); + +MODULE_AUTHOR("Wei Yongjun "); +MODULE_DESCRIPTION("SPI controller Testing Driver"); +MODULE_LICENSE("GPL"); From patchwork Wed Jul 26 15:08:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Xiaoxu X-Patchwork-Id: 706946 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72797C41513 for ; Wed, 26 Jul 2023 07:14:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232460AbjGZHOl (ORCPT ); Wed, 26 Jul 2023 03:14:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232132AbjGZHON (ORCPT ); Wed, 26 Jul 2023 03:14:13 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9CED2D42; Wed, 26 Jul 2023 00:10:32 -0700 (PDT) Received: from dggpeml100024.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4R9lMG6mlqzCrW0; Wed, 26 Jul 2023 15:07:06 +0800 (CST) Received: from china (10.175.101.107) by dggpeml100024.china.huawei.com (7.185.36.115) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Wed, 26 Jul 2023 15:10:29 +0800 From: Zhang Xiaoxu To: , , , CC: , , , Subject: [PATCH v2 -next 2/3] spi: mockup: Add writeable tracepoint for spi transfer Date: Wed, 26 Jul 2023 15:08:48 +0000 Message-ID: <20230726150849.28407-3-zhangxiaoxu5@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230726150849.28407-1-zhangxiaoxu5@huawei.com> References: <20230726150849.28407-1-zhangxiaoxu5@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.101.107] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml100024.china.huawei.com (7.185.36.115) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org From: Wei Yongjun Add writeable tracepoint for transfer_one_message(), then bpf program can be used to control read and write data from spi host, as mockup chip's expectation. For example: #include "vmlinux.h" #include #include SEC("raw_tp.w/spi_transfer_writeable") int BPF_PROG(spi_transfer_writeable_test, struct spi_msg_ctx *msg, u8 chip, unsigned int len) { if (msg->tx_nbits) msg->data[0] = 0x20; return 0; } char LICENSE[] SEC("license") = "GPL"; This will be useful for writing spi device mockup backend. Signed-off-by: Wei Yongjun Signed-off-by: Zhang Xiaoxu --- drivers/spi/Kconfig | 1 + drivers/spi/spi-mockup.c | 52 +++++++++++++++++++++++++++++-- include/linux/spi/spi-mockup.h | 17 ++++++++++ include/trace/events/spi_mockup.h | 31 ++++++++++++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 include/linux/spi/spi-mockup.h create mode 100644 include/trace/events/spi_mockup.h diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 1768e57f0088..608e224e52ef 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -1188,6 +1188,7 @@ config SPI_TLE62X0 config SPI_MOCKUP tristate "SPI controller Testing Driver" depends on OF + select BPF_EVENTS help This enables SPI controller testing driver, which provides a way to test SPI subsystem. diff --git a/drivers/spi/spi-mockup.c b/drivers/spi/spi-mockup.c index 4debfd1fb9fd..3835012e2ee8 100644 --- a/drivers/spi/spi-mockup.c +++ b/drivers/spi/spi-mockup.c @@ -14,6 +14,9 @@ #include #include +#define CREATE_TRACE_POINTS +#include + #define MOCKUP_CHIPSELECT_MAX 8 struct mockup_spi { @@ -150,13 +153,58 @@ static struct attribute *spi_mockup_attrs[] = { }; ATTRIBUTE_GROUPS(spi_mockup); +static int spi_mockup_transfer_writeable(struct spi_message *msg) +{ + struct spi_msg_ctx *ctx; + struct spi_transfer *t; + int ret = 0; + + ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC); + if (!ctx) + return -ENOMEM; + + list_for_each_entry(t, &msg->transfers, transfer_list) { + if (t->len > SPI_BUFSIZ_MAX) + return -E2BIG; + + memset(ctx, 0, sizeof(*ctx)); + ctx->cs_off = t->cs_off; + ctx->cs_change = t->cs_change; + ctx->tx_nbits = t->tx_nbits; + ctx->rx_nbits = t->rx_nbits; + + if (t->tx_nbits) + memcpy(ctx->data, t->tx_buf, t->len); + + trace_spi_transfer_writeable(ctx, msg->spi->chip_select, t->len); + + if (ctx->ret) { + ret = ctx->ret; + break; + } + + if (t->rx_nbits) + memcpy(t->rx_buf, ctx->data, t->len); + msg->actual_length += t->len; + } + + kfree(ctx); + + return ret; +} + static int spi_mockup_transfer(struct spi_controller *ctrl, struct spi_message *msg) { - msg->status = 0; + int ret = 0; + + if (trace_spi_transfer_writeable_enabled()) + ret = spi_mockup_transfer_writeable(msg); + + msg->status = ret; spi_finalize_current_message(ctrl); - return 0; + return ret; } static int spi_mockup_probe(struct platform_device *pdev) diff --git a/include/linux/spi/spi-mockup.h b/include/linux/spi/spi-mockup.h new file mode 100644 index 000000000000..224894b416fb --- /dev/null +++ b/include/linux/spi/spi-mockup.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __LINUX_SPI_MOCKUP_H +#define __LINUX_SPI_MOCKUP_H + +#define SPI_BUFSIZ_MAX 0x1000 + +struct spi_msg_ctx { + int ret; + unsigned cs_off:1; + unsigned cs_change:1; + unsigned tx_nbits:3; + unsigned rx_nbits:3; + __u8 data[SPI_BUFSIZ_MAX]; +}; + +#endif diff --git a/include/trace/events/spi_mockup.h b/include/trace/events/spi_mockup.h new file mode 100644 index 000000000000..46debf26a5e3 --- /dev/null +++ b/include/trace/events/spi_mockup.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * SPI mockup controller transfer writeable tracepoint + * + * Copyright(c) 2022 Huawei Technologies Co., Ltd. + */ + +#undef TRACE_SYSTEM +#define TRACE_SYSTEM spi_mockup + +#if !defined(_TRACE_SPI_MOCKUP_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_SPI_MOCKUP_H + +#include +#include + +#ifndef DECLARE_TRACE_WRITABLE +#define DECLARE_TRACE_WRITABLE(call, proto, args, size) \ + DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) +#endif + +DECLARE_TRACE_WRITABLE(spi_transfer_writeable, + TP_PROTO(struct spi_msg_ctx *msg, u8 chip_select, unsigned int len), + TP_ARGS(msg, chip_select, len), + sizeof(struct spi_msg_ctx) +); + +#endif /* _TRACE_SPI_MOCKUP_H */ + +/* This part must be outside protection */ +#include From patchwork Wed Jul 26 15:08:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Xiaoxu X-Patchwork-Id: 708252 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21399C001E0 for ; Wed, 26 Jul 2023 07:14:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232167AbjGZHOm (ORCPT ); Wed, 26 Jul 2023 03:14:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232141AbjGZHON (ORCPT ); Wed, 26 Jul 2023 03:14:13 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 521542705; Wed, 26 Jul 2023 00:10:32 -0700 (PDT) Received: from dggpeml100024.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4R9lPN2n6pzVk3X; Wed, 26 Jul 2023 15:08:56 +0800 (CST) Received: from china (10.175.101.107) by dggpeml100024.china.huawei.com (7.185.36.115) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Wed, 26 Jul 2023 15:10:29 +0800 From: Zhang Xiaoxu To: , , , CC: , , , Subject: [PATCH v2 -next 3/3] spi: mockup: Add documentation Date: Wed, 26 Jul 2023 15:08:49 +0000 Message-ID: <20230726150849.28407-4-zhangxiaoxu5@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230726150849.28407-1-zhangxiaoxu5@huawei.com> References: <20230726150849.28407-1-zhangxiaoxu5@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.101.107] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml100024.china.huawei.com (7.185.36.115) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org From: Wei Yongjun Add documentation for the SPI mockup controller driver. This include the tutorial for how to mockup a api device. Signed-off-by: Wei Yongjun Signed-off-by: Zhang Xiaoxu --- Documentation/spi/index.rst | 1 + Documentation/spi/spi-mockup.rst | 174 +++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 Documentation/spi/spi-mockup.rst diff --git a/Documentation/spi/index.rst b/Documentation/spi/index.rst index 06c34ea11bcf..a8f4f5cd0f09 100644 --- a/Documentation/spi/index.rst +++ b/Documentation/spi/index.rst @@ -13,6 +13,7 @@ Serial Peripheral Interface (SPI) pxa2xx spi-lm70llp spi-sc18is602 + spi-mockup .. only:: subproject and html diff --git a/Documentation/spi/spi-mockup.rst b/Documentation/spi/spi-mockup.rst new file mode 100644 index 000000000000..5e720de19991 --- /dev/null +++ b/Documentation/spi/spi-mockup.rst @@ -0,0 +1,174 @@ +.. SPDX-License-Identifier: GPL-2.0 + +========== +spi-mockup +========== + +Description +=========== + +This module is a very simple fake SPI controller driver. It implements +a BPF based interface to mockup SPI device. + +No hardware is needed nor associated with this module. It will respond +spi message by BPF program attached to spi_transfer_writeable tracepoint +by reading from or writing BPF maps. + +The typical use-case is like this: + 1. load EBPF program as device's backend + 2. create target chip device + +Example +======= + +This example show how to mock a MTD device by using spi-mockup driver. + +Compile your copy of the kernel source. Make sure to configure the spi-mockup +and the target chip driver as a module. + +Write a BPF program as device's backup. + +:: + + #define MCHP23K256_CMD_WRITE_STATUS 0x01 + #define MCHP23K256_CMD_WRITE 0x02 + #define MCHP23K256_CMD_READ 0x03 + + #define CHIP_REGS_SIZE 0x20000 + + #define MAX_CMD_SIZE 4 + + struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, CHIP_REGS_SIZE); + __type(key, __u32); + __type(value, __u8); + } regs_mchp23k256 SEC(".maps"); + + static unsigned int chip_reg = 0; + + static int spi_transfer_read(struct spi_msg_ctx *msg, unsigned int len) + { + int i, key; + u8 *reg; + + for (i = 0; i < len && i < sizeof(msg->data); i++) { + key = i + chip_reg; + + reg = bpf_map_lookup_elem(®s_mchp23k256, &key); + if (!reg) { + bpf_printk("key %d not exists", key); + return -EINVAL; + } + + msg->data[i] = *reg; + } + + return 0; + } + + static int spi_transfer_write(struct spi_msg_ctx *msg, unsigned int len) + { + u8 opcode = msg->data[0], value; + int i, key; + + switch (opcode) { + case MCHP23K256_CMD_READ: + case MCHP23K256_CMD_WRITE: + if (len < 2) + return -EINVAL; + + chip_reg = 0; + for (i = 0; i < MAX_CMD_SIZE && i < len - 1; i++) + chip_reg = (chip_reg << 8) + msg->data[1 + i]; + + return 0; + case MCHP23K256_CMD_WRITE_STATUS: + // ignore write status + return 0; + default: + break; + } + + for (i = 0; i < len && i < sizeof(msg->data); i++) { + value = msg->data[i]; + key = chip_reg + i; + + if (bpf_map_update_elem(®s_mchp23k256, &key, &value, + BPF_EXIST)) { + bpf_printk("key %d not exists", key); + return -EINVAL; + } + } + + return 0; + } + + SEC("raw_tp.w/spi_transfer_writeable") + int BPF_PROG(mtd_mchp23k256, struct spi_msg_ctx *msg, u8 chip, unsigned int len) + { + int ret = 0; + + if (msg->tx_nbits) + ret = spi_transfer_write(msg, len); + else if (msg->rx_nbits) + ret = spi_transfer_read(msg, len); + + return ret; + } + + char LICENSE[] SEC("license") = "GPL"; + +Use bpftool to load the BPF program. + +:: + + bpftool prog load mtd-mchp23k256.o /sys/fs/bpf/mtd_mchp23k256 autoattach + + +This is accomplished by executing the following command: + +:: + + $ echo mchp23k256 0 > /sys/class/spi_master/spi0/new_device + + +The name of the target driver and its chip select were used to instantiate +the device. + +Now, the mchp23k256 MTD device named /dev/mtd0 has been created successfully. + +:: + + $ ls /sys/bus/spi/devices/spi0.0/mtd/ + mtd0 mtd0ro + + $ cat /sys/class/mtd/mtd0/name + spi0.0 + + $ hexdump /dev/mtd0 + 0000000 0000 0000 0000 0000 0000 0000 0000 0000 + * + 0008000 + + $echo aaaa > /dev/mtd0 + + $ hexdump /dev/mtd0 + 0000000 6161 6161 000a 0000 0000 0000 0000 0000 + 0000010 0000 0000 0000 0000 0000 0000 0000 0000 + * + 0008000 + + $ bpftool map update name regs_mchp23k256 key 0 0 0 0 value 0 + + $ hexdump /dev/mtd0 + 0000000 6100 6161 000a 0000 0000 0000 0000 0000 + 0000010 0000 0000 0000 0000 0000 0000 0000 0000 + * + 0008000 + +Remove the mockup device by executing the following command: + +:: + + $echo 0 > /sys/class/spi_master/spi0/delete_device