From patchwork Sat Nov 4 06:46:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Xiaoxu X-Patchwork-Id: 741717 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 B1A38C0018A for ; Sat, 4 Nov 2023 06:47:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231703AbjKDGra (ORCPT ); Sat, 4 Nov 2023 02:47:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231264AbjKDGr2 (ORCPT ); Sat, 4 Nov 2023 02:47:28 -0400 Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5ECD6D51; Fri, 3 Nov 2023 23:47:24 -0700 (PDT) Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4SMp7j5y6Hz4f3jMf; Sat, 4 Nov 2023 14:47:13 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 234261A016E; Sat, 4 Nov 2023 14:47:20 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.107]) by APP4 (Coremail) with SMTP id gCh0CgDnfd3z6EVlglQtEw--.50758S5; Sat, 04 Nov 2023 14:47:19 +0800 (CST) From: Zhang Xiaoxu To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com, broonie@kernel.org, rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com, linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 -next 1/5] spi: mockup: Add SPI controller testing driver Date: Sat, 4 Nov 2023 14:46:46 +0800 Message-Id: <20231104064650.972687-2-zhangxiaoxu@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231104064650.972687-1-zhangxiaoxu@huaweicloud.com> References: <20231104064650.972687-1-zhangxiaoxu@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgDnfd3z6EVlglQtEw--.50758S5 X-Coremail-Antispam: 1UD129KBjvJXoW3Gw15Cr4xAFyxur43KFyrtFb_yoW3CrWUpF yrJay7JrW8GF13uF1S9rW8uFy5Ww1kCayxt3yfJa4Ik3yfXFyDXa4ktryYvay5JFyDXFyU ZFWa9a48CFWUZF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvGb4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUGw A2048vs2IY020Ec7CjxVAFwI0_JFI_Gr1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr 0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY 17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcV C0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY 6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa 73UjIFyTuYvjxUzUUUUUUUU X-CM-SenderInfo: x2kd0wp0ld053x6k3tpzhluzxrxghudrp/ Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org From: Zhang Xiaoxu 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 | 211 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 224 insertions(+) create mode 100644 drivers/spi/spi-mockup.c diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 2c21d5b96fdc..9169081cfecb 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -1218,6 +1218,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 6af54842b9fa..f28074e61df9 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..683a0fc43f0d --- /dev/null +++ b/drivers/spi/spi-mockup.c @@ -0,0 +1,211 @@ +// 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) +{ + int ret; + struct mockup_spi *mock; + struct spi_controller *ctrl; + + 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 Sat Nov 4 06:46: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: 741036 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 95263C4332F for ; Sat, 4 Nov 2023 06:47:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231199AbjKDGr2 (ORCPT ); Sat, 4 Nov 2023 02:47:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229509AbjKDGr1 (ORCPT ); Sat, 4 Nov 2023 02:47:27 -0400 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D6D51B9; Fri, 3 Nov 2023 23:47:23 -0700 (PDT) Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4SMp7m5zRbz4f3lVR; Sat, 4 Nov 2023 14:47:16 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 764561A016F; Sat, 4 Nov 2023 14:47:20 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.107]) by APP4 (Coremail) with SMTP id gCh0CgDnfd3z6EVlglQtEw--.50758S6; Sat, 04 Nov 2023 14:47:20 +0800 (CST) From: Zhang Xiaoxu To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com, broonie@kernel.org, rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com, linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 -next 2/5] spi: mockup: Add writeable tracepoint for spi transfer Date: Sat, 4 Nov 2023 14:46:47 +0800 Message-Id: <20231104064650.972687-3-zhangxiaoxu@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231104064650.972687-1-zhangxiaoxu@huaweicloud.com> References: <20231104064650.972687-1-zhangxiaoxu@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgDnfd3z6EVlglQtEw--.50758S6 X-Coremail-Antispam: 1UD129KBjvJXoWxWryrZF4rZF4rZF4UKr1UAwb_yoW7Jw1UpF yUCF45KrW8Jw429F4furW8Ary3Aa1kurW7K3sI9wnIyr1UtF1kXa1qgry5tFy8JrsrKFyU uFZ2kryjkr45Zw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvGb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUXw A2048vs2IY020Ec7CjxVAFwI0_Gr0_Xr1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr 0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY 17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcV C0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY 6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa 73UjIFyTuYvjxU2-B_UUUUU X-CM-SenderInfo: x2kd0wp0ld053x6k3tpzhluzxrxghudrp/ Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org From: Zhang Xiaoxu 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 9169081cfecb..871e3824b8eb 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -1221,6 +1221,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 683a0fc43f0d..fcaaa61bdb38 100644 --- a/drivers/spi/spi-mockup.c +++ b/drivers/spi/spi-mockup.c @@ -13,6 +13,9 @@ #include #include +#define CREATE_TRACE_POINTS +#include + #define MOCKUP_CHIPSELECT_MAX 8 struct mockup_spi { @@ -149,13 +152,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 Sat Nov 4 06:46: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: 741719 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 0CDA4C41535 for ; Sat, 4 Nov 2023 06:47:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231658AbjKDGr3 (ORCPT ); Sat, 4 Nov 2023 02:47:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229661AbjKDGr1 (ORCPT ); Sat, 4 Nov 2023 02:47:27 -0400 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C13E7D4C; Fri, 3 Nov 2023 23:47:23 -0700 (PDT) Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4SMp7p4MR3z4f3jqq; Sat, 4 Nov 2023 14:47:18 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id CA72A1A0172; Sat, 4 Nov 2023 14:47:20 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.107]) by APP4 (Coremail) with SMTP id gCh0CgDnfd3z6EVlglQtEw--.50758S7; Sat, 04 Nov 2023 14:47:20 +0800 (CST) From: Zhang Xiaoxu To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com, broonie@kernel.org, rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com, linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 -next 3/5] spi: mockup: Add support register the device through configfs Date: Sat, 4 Nov 2023 14:46:48 +0800 Message-Id: <20231104064650.972687-4-zhangxiaoxu@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231104064650.972687-1-zhangxiaoxu@huaweicloud.com> References: <20231104064650.972687-1-zhangxiaoxu@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgDnfd3z6EVlglQtEw--.50758S7 X-Coremail-Antispam: 1UD129KBjvJXoW3AF47Jw4rGF13Jw1xurW3KFg_yoW7Zw4DpF Z8AFy5trW8GFs8WF4fC34kuF95Gr1xKr4Uta43Aw1FkwnrAry8Zas7WFyYyr48JFWDGF47 ZFWfZF10krWjvrUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvGb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUWw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr 0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY 17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcV C0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY 6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa 73UjIFyTuYvjxUrfOzDUUUU X-CM-SenderInfo: x2kd0wp0ld053x6k3tpzhluzxrxghudrp/ Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org From: Zhang Xiaoxu This enable/disable the spi mockup device through the configfs: mkdir /sys/kernel/config/spi-mockup/spi0 echo 1 > /sys/kernel/config/spi-mockup/spi0/enable echo 1 > /sys/kernel/config/spi-mockup/spi0/disable Signed-off-by: Zhang Xiaoxu --- drivers/spi/spi-mockup.c | 163 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 161 insertions(+), 2 deletions(-) diff --git a/drivers/spi/spi-mockup.c b/drivers/spi/spi-mockup.c index fcaaa61bdb38..b449a2b7cdd4 100644 --- a/drivers/spi/spi-mockup.c +++ b/drivers/spi/spi-mockup.c @@ -12,6 +12,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include @@ -224,7 +225,7 @@ static int spi_mockup_probe(struct platform_device *pdev) ctrl->dev.groups = spi_mockup_groups; ctrl->num_chipselect = MOCKUP_CHIPSELECT_MAX; ctrl->mode_bits = SPI_MODE_USER_MASK; - ctrl->bus_num = 0; + ctrl->bus_num = pdev->id; ctrl->transfer_one_message = spi_mockup_transfer; mock = spi_controller_get_devdata(ctrl); @@ -252,7 +253,165 @@ static struct platform_driver spi_mockup_driver = { .of_match_table = spi_mockup_match, }, }; -module_platform_driver(spi_mockup_driver); + +struct spi_mockup_device { + struct config_group group; + unsigned int bus_nr; + struct mutex lock; + struct platform_device *pdev; +}; + +static struct spi_mockup_device *to_spi_mockup_dev(struct config_item *item) +{ + struct config_group *group = to_config_group(item); + + return container_of(group, struct spi_mockup_device, group); +} + +static ssize_t +spi_mockup_enable_store(struct config_item *item, const char *page, size_t len) +{ + int ret = len; + struct platform_device_info pdevinfo = {0}; + struct spi_mockup_device *dev = to_spi_mockup_dev(item); + + mutex_lock(&dev->lock); + if (dev->pdev) { + ret = -EEXIST; + goto out; + } + + pdevinfo.name = "spi-mockup"; + pdevinfo.id = dev->bus_nr; + dev->pdev = platform_device_register_full(&pdevinfo); + if (IS_ERR(dev->pdev)) { + ret = PTR_ERR(dev->pdev); + dev->pdev = NULL; + goto out; + } +out: + mutex_unlock(&dev->lock); + return ret; +} +CONFIGFS_ATTR_WO(spi_mockup_, enable); + +static ssize_t +spi_mockup_disable_store(struct config_item *item, const char *page, size_t len) +{ + int ret = len; + struct spi_mockup_device *dev = to_spi_mockup_dev(item); + + mutex_lock(&dev->lock); + if (!dev->pdev) { + ret = -ENODEV; + goto out; + } + + platform_device_unregister(dev->pdev); + dev->pdev = NULL; +out: + mutex_unlock(&dev->lock); + return ret; +} +CONFIGFS_ATTR_WO(spi_mockup_, disable); + +static struct configfs_attribute *spi_mockup_configfs_attrs[] = { + &spi_mockup_attr_enable, + &spi_mockup_attr_disable, + NULL, +}; + +static const struct config_item_type spi_mockup_device_config_group_type = { + .ct_owner = THIS_MODULE, + .ct_attrs = spi_mockup_configfs_attrs, +}; + +static struct config_group * +spi_mockup_config_make_device_group(struct config_group *group, + const char *name) +{ + int ret, nchar; + unsigned int nr; + struct spi_mockup_device *dev; + + ret = sscanf(name, "spi%u%n", &nr, &nchar); + if (ret != 1 || nchar != strlen(name)) + return ERR_PTR(-EINVAL); + + dev = kzalloc(sizeof(*dev), GFP_KERNEL); + if (!dev) + return ERR_PTR(-ENOMEM); + + dev->bus_nr = nr; + mutex_init(&dev->lock); + + config_group_init_type_name(&dev->group, name, + &spi_mockup_device_config_group_type); + + return &dev->group; +} + +static void spi_mockup_config_group_release(struct config_item *item) +{ + struct spi_mockup_device *dev = to_spi_mockup_dev(item); + + kfree(dev); +} + +static struct configfs_item_operations spi_mockup_config_item_ops = { + .release = spi_mockup_config_group_release, +}; + +static struct configfs_group_operations spi_mockup_config_group_ops = { + .make_group = spi_mockup_config_make_device_group, +}; + +static const struct config_item_type spi_mockup_config_type = { + .ct_owner = THIS_MODULE, + .ct_group_ops = &spi_mockup_config_group_ops, + .ct_item_ops = &spi_mockup_config_item_ops, +}; + +static struct configfs_subsystem spi_mockup_config_subsys = { + .su_group = { + .cg_item = { + .ci_namebuf = "spi-mockup", + .ci_type = &spi_mockup_config_type, + } + } +}; + +static int __init spi_mockup_init(void) +{ + int ret; + + ret = platform_driver_register(&spi_mockup_driver); + if (ret) { + pr_err("spi mockup driver registering failed with %d\n", ret); + return ret; + } + + config_group_init(&spi_mockup_config_subsys.su_group); + mutex_init(&spi_mockup_config_subsys.su_mutex); + ret = configfs_register_subsystem(&spi_mockup_config_subsys); + if (ret) { + pr_err("spi mockup configfs registering failed with %d\n", ret); + mutex_destroy(&spi_mockup_config_subsys.su_mutex); + platform_driver_unregister(&spi_mockup_driver); + return ret; + } + + return ret; +} +module_init(spi_mockup_init); + +static void __exit spi_mockup_exit(void) +{ + configfs_unregister_subsystem(&spi_mockup_config_subsys); + mutex_destroy(&spi_mockup_config_subsys.su_mutex); + return platform_driver_unregister(&spi_mockup_driver); +} +module_exit(spi_mockup_exit); MODULE_AUTHOR("Wei Yongjun "); MODULE_DESCRIPTION("SPI controller Testing Driver"); From patchwork Sat Nov 4 06:46: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: 741718 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 3E026C0018A for ; Sat, 4 Nov 2023 06:47:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229509AbjKDGr3 (ORCPT ); Sat, 4 Nov 2023 02:47:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230048AbjKDGr1 (ORCPT ); Sat, 4 Nov 2023 02:47:27 -0400 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FD1ED4E; Fri, 3 Nov 2023 23:47:24 -0700 (PDT) Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4SMp7n3p7Pz4f3lDl; Sat, 4 Nov 2023 14:47:17 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 29FEF1A016E; Sat, 4 Nov 2023 14:47:21 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.107]) by APP4 (Coremail) with SMTP id gCh0CgDnfd3z6EVlglQtEw--.50758S8; Sat, 04 Nov 2023 14:47:21 +0800 (CST) From: Zhang Xiaoxu To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com, broonie@kernel.org, rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com, linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 -next 4/5] spi: mockup: Add speed and flags attribute support Date: Sat, 4 Nov 2023 14:46:49 +0800 Message-Id: <20231104064650.972687-5-zhangxiaoxu@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231104064650.972687-1-zhangxiaoxu@huaweicloud.com> References: <20231104064650.972687-1-zhangxiaoxu@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgDnfd3z6EVlglQtEw--.50758S8 X-Coremail-Antispam: 1UD129KBjvJXoWxCF18Kw1xCw4DJF4fZr1UJrb_yoWrJF43pF WUAFy5t3y8JFn8WF43J3yku345Arn5KrWUGa4akr1Fk3s8KrykX3Z7JF15tan5JFWDCF4j vFWIvF40kF4Ivr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvEb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7 v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF 1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Gr0_Xr1lIx AIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI 42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWI evJa73UjIFyTuYvjxUrCJmUUUUU X-CM-SenderInfo: x2kd0wp0ld053x6k3tpzhluzxrxghudrp/ Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org From: Zhang Xiaoxu This enable configure the spi speed and flags through configfs: echo 40000 > /sys/kernel/config/spi-mockup/spi0/min_speed echo 25000000 > /sys/kernel/config/spi-mockup/spi0/max_speed echo 0 > /sys/kernel/config/spi-mockup/spi0/flags echo 8 > /sys/kernel/config/spi-mockup/spi0/num_cs Then enable the device can use the special config: echo 1 > /sys/kernel/config/spi-mockup/spi0/enable Signed-off-by: Zhang Xiaoxu --- drivers/spi/spi-mockup.c | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/drivers/spi/spi-mockup.c b/drivers/spi/spi-mockup.c index b449a2b7cdd4..06848504d144 100644 --- a/drivers/spi/spi-mockup.c +++ b/drivers/spi/spi-mockup.c @@ -207,11 +207,19 @@ static int spi_mockup_transfer(struct spi_controller *ctrl, return ret; } +struct spi_mockup_priv_data { + u32 min_speed; + u32 max_speed; + u16 flags; + u16 num_cs; +}; + static int spi_mockup_probe(struct platform_device *pdev) { int ret; struct mockup_spi *mock; struct spi_controller *ctrl; + struct spi_mockup_priv_data *data; ctrl = spi_alloc_host(&pdev->dev, sizeof(struct mockup_spi)); if (!ctrl) { @@ -228,6 +236,14 @@ static int spi_mockup_probe(struct platform_device *pdev) ctrl->bus_num = pdev->id; ctrl->transfer_one_message = spi_mockup_transfer; + data = dev_get_platdata(&pdev->dev); + if (data) { + ctrl->min_speed_hz = data->min_speed; + ctrl->max_speed_hz = data->max_speed; + ctrl->flags = data->flags; + ctrl->num_chipselect = data->num_cs; + } + mock = spi_controller_get_devdata(ctrl); mutex_init(&mock->lock); @@ -259,6 +275,7 @@ struct spi_mockup_device { unsigned int bus_nr; struct mutex lock; struct platform_device *pdev; + struct spi_mockup_priv_data data; }; static struct spi_mockup_device *to_spi_mockup_dev(struct config_item *item) @@ -283,6 +300,9 @@ spi_mockup_enable_store(struct config_item *item, const char *page, size_t len) pdevinfo.name = "spi-mockup"; pdevinfo.id = dev->bus_nr; + pdevinfo.data = &dev->data; + pdevinfo.size_data = sizeof(dev->data); + dev->pdev = platform_device_register_full(&pdevinfo); if (IS_ERR(dev->pdev)) { ret = PTR_ERR(dev->pdev); @@ -315,9 +335,43 @@ spi_mockup_disable_store(struct config_item *item, const char *page, size_t len) } CONFIGFS_ATTR_WO(spi_mockup_, disable); +#define SPI_MOCKUP_ATTR(type, name) \ +static ssize_t spi_mockup_ ## name ## _store(struct config_item *item, \ + const char *page, size_t len) \ +{ \ + int ret; \ + type val; \ + struct spi_mockup_device *dev = to_spi_mockup_dev(item); \ + \ + mutex_lock(&dev->lock); \ + if (dev->pdev) { \ + ret = -EBUSY; \ + goto out; \ + } \ + \ + ret = kstrto ## type(page, 0, &val); \ + if (ret) \ + goto out; \ + \ + dev->data.name = val; \ +out: \ + mutex_unlock(&dev->lock); \ + return ret ? ret : len; \ +} \ +CONFIGFS_ATTR_WO(spi_mockup_, name) \ + +SPI_MOCKUP_ATTR(u32, min_speed) +SPI_MOCKUP_ATTR(u32, max_speed) +SPI_MOCKUP_ATTR(u16, flags) +SPI_MOCKUP_ATTR(u16, num_cs) + static struct configfs_attribute *spi_mockup_configfs_attrs[] = { &spi_mockup_attr_enable, &spi_mockup_attr_disable, + &spi_mockup_attr_min_speed, + &spi_mockup_attr_max_speed, + &spi_mockup_attr_flags, + &spi_mockup_attr_num_cs, NULL, }; @@ -342,6 +396,7 @@ spi_mockup_config_make_device_group(struct config_group *group, if (!dev) return ERR_PTR(-ENOMEM); + dev->data.num_cs = MOCKUP_CHIPSELECT_MAX; dev->bus_nr = nr; mutex_init(&dev->lock); From patchwork Sat Nov 4 06:46:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Xiaoxu X-Patchwork-Id: 741035 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 F0C6DC4167D for ; Sat, 4 Nov 2023 06:47:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231682AbjKDGra (ORCPT ); Sat, 4 Nov 2023 02:47:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231270AbjKDGr2 (ORCPT ); Sat, 4 Nov 2023 02:47:28 -0400 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82309D52; Fri, 3 Nov 2023 23:47:24 -0700 (PDT) Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4SMp7q2HGdz4f3kJs; Sat, 4 Nov 2023 14:47:19 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 820631A0171; Sat, 4 Nov 2023 14:47:21 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.107]) by APP4 (Coremail) with SMTP id gCh0CgDnfd3z6EVlglQtEw--.50758S9; Sat, 04 Nov 2023 14:47:21 +0800 (CST) From: Zhang Xiaoxu To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com, broonie@kernel.org, rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com, linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 -next 5/5] spi: mockup: Add documentation Date: Sat, 4 Nov 2023 14:46:50 +0800 Message-Id: <20231104064650.972687-6-zhangxiaoxu@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231104064650.972687-1-zhangxiaoxu@huaweicloud.com> References: <20231104064650.972687-1-zhangxiaoxu@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgDnfd3z6EVlglQtEw--.50758S9 X-Coremail-Antispam: 1UD129KBjvJXoW3JryxuF18tF43Ar1kArWrZrb_yoW7Aw4fpF WUtry3K3y8Xr9rWF1fXa4kJFy3A3s7Zay7GFWvgF1Yyr1qvr95JF1xKryYq3Z8WF4kAFy0 vFW5AFW8Kr17ZaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9jb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7 v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF 1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Gr0_Xr1lIx AIcVC0I7IYx2IY6xkF7I0E14v26F4j6r4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4l IxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvf C2KfnxnUUI43ZEXa7IU1FfO7UUUUU== X-CM-SenderInfo: x2kd0wp0ld053x6k3tpzhluzxrxghudrp/ Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org From: Zhang Xiaoxu Add documentation for the SPI mockup controller driver. This include the tutorial for how to mockup a spi device. Signed-off-by: Wei Yongjun Signed-off-by: Zhang Xiaoxu --- Documentation/spi/index.rst | 1 + Documentation/spi/spi-mockup.rst | 196 +++++++++++++++++++++++++++++++ 2 files changed, 197 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..dd9feb81535e --- /dev/null +++ b/Documentation/spi/spi-mockup.rst @@ -0,0 +1,196 @@ +.. 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. + +Register the spi mockup device. + +:: + + $ mkdir /sys/kernel/config/spi-mockup/spi0 + + # configure the spi mockup attribute + $ echo 40000 > /sys/kernel/config/spi-mockup/spi0/min_speed + $ echo 25000000 > /sys/kernel/config/spi-mockup/spi0/max_speed + $ echo 0 > /sys/kernel/config/spi-mockup/spi0/flags + $ echo 8 > /sys/kernel/config/spi-mockup/spi0/num_cs + + # enable the spi mockup device + $ echo 1 > /sys/kernel/config/spi-mockup/spi0/enable + +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 + +Remove the spi mockup device by executing the following command: + +:: + + $ echo 0 > /sys/kernel/config/spi-mockup/spi0/disable + $ rmdir /sys/kernel/config/spi-mockup/spi0