From patchwork Sat Nov 18 10:44:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Xiaoxu X-Patchwork-Id: 745609 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 B21D5C54E76 for ; Sat, 18 Nov 2023 10:45:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229743AbjKRKpg (ORCPT ); Sat, 18 Nov 2023 05:45:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229449AbjKRKpf (ORCPT ); Sat, 18 Nov 2023 05:45:35 -0500 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF2D695; Sat, 18 Nov 2023 02:45:30 -0800 (PST) Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4SXVm52g3lz4f3jqj; Sat, 18 Nov 2023 18:45:25 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id F35C11A01A4; Sat, 18 Nov 2023 18:45:27 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.107]) by APP1 (Coremail) with SMTP id cCh0CgBXWhDClVhlAjMGBQ--.58652S5; Sat, 18 Nov 2023 18:45:27 +0800 (CST) From: Zhang Xiaoxu To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com, linux-kernel@vger.kernel.org, broonie@kernel.org, rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com, linux-spi@vger.kernel.org Subject: [PATCH v4 -next 1/4] spi: mockup: Add SPI controller testing driver Date: Sat, 18 Nov 2023 18:44:39 +0800 Message-Id: <20231118104442.861759-2-zhangxiaoxu@huawecloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231118104442.861759-1-zhangxiaoxu@huawecloud.com> References: <20231118104442.861759-1-zhangxiaoxu@huawecloud.com> MIME-Version: 1.0 X-CM-TRANSID: cCh0CgBXWhDClVhlAjMGBQ--.58652S5 X-Coremail-Antispam: 1UD129KBjvJXoWxtFWfJw4rGr4rWr18ZrW7CFg_yoWDGry3pF W5CFy3trW8JFs8Wr4fGay8uF95Crn29ryUt3s3Z34FywnxtrWkZwn7GFy5tFs5JFWDGr12 vFWFyF1UCrWYyr7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUB2b4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUGw A2048vs2IY020Ec7CjxVAFwI0_JFI_Gr1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l42xK82IY64kExVAvwVAq07 x20xyl4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8G jcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2I x0cI8IcVAFwI0_Gr0_Xr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr1lIxAIcVCF 04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r4j6F4UMIIF0xvEx4A2jsIEc7 CjxVAFwI0_Gr1j6F4UJbIYCTnIWIevJa73UjIFyTuYvjxUzb18DUUUU Sender: zhangxiaoxu@huaweicloud.com 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. Its controlled by configfs and can by use as below: Create the controller by executing the following command: $ mkdir /config/spi-mockup/spi0 Configure it by: $ echo 40000 > /config/spi-mockup/spi0/min_speed $ echo 25000000 > /config/spi-mockup/spi0/max_speed $ echo 0 > /config/spi-mockup/spi0/flags $ echo 8 > /config/spi-mockup/spi0/num_cs Enable: $ echo 1 > /config/spi-mockup/spi0/live Disable: $ echo 0 > /config/spi-mockup/spi0/live Remove: $ rmdir /config/spi-mockup/spi0 Signed-off-by: Wei Yongjun Signed-off-by: Zhang Xiaoxu --- drivers/spi/Kconfig | 11 ++ drivers/spi/Makefile | 1 + drivers/spi/spi-mockup.c | 305 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 317 insertions(+) create mode 100644 drivers/spi/spi-mockup.c diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 35dbfacecf1c..6524735a4936 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -1230,6 +1230,17 @@ 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" + 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 4ff8d725ba5e..ab5a1a12b5ac 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..acb502c14676 --- /dev/null +++ b/drivers/spi/spi-mockup.c @@ -0,0 +1,305 @@ +/* + * 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 U16_MAX + +struct spi_mockup_host { + struct config_group group; + + struct mutex lock; + + unsigned int bus_nr; + u32 min_speed; + u32 max_speed; + u16 flags; + u16 num_cs; + + struct platform_device *pdev; + struct spi_controller *ctrl; +}; + +static struct spi_mockup_host *to_spi_mockup_host(struct config_item *item) +{ + struct config_group *group = to_config_group(item); + + return container_of(group, struct spi_mockup_host, group); +} + +static ssize_t __host_online(struct spi_mockup_host *host) +{ + int ret; + struct platform_device_info pdevinfo = {0}; + + if (host->pdev) + return -EEXIST; + + pdevinfo.name = "spi-mockup"; + pdevinfo.id = host->bus_nr; + + /* Use the pointer of host as the data, then probe + * can init the host->ctrl */ + pdevinfo.data = &host; + pdevinfo.size_data = sizeof(&host); + + host->pdev = platform_device_register_full(&pdevinfo); + if (IS_ERR(host->pdev)) { + ret = PTR_ERR(host->pdev); + host->pdev = NULL; + return ret; + } + + return 0; +} + +static ssize_t __host_offline(struct spi_mockup_host *host) +{ + if (!host->pdev) + return -ENODEV; + + platform_device_unregister(host->pdev); + host->pdev = NULL; + host->ctrl = NULL; + + return 0; +} + +static ssize_t +spi_mockup_live_store(struct config_item *item, const char *buf, size_t len) +{ + struct spi_mockup_host *host = to_spi_mockup_host(item); + int ret; + bool res; + + ret = kstrtobool(buf, &res); + if (ret) + return ret; + + mutex_lock(&host->lock); + if (res) + ret = __host_online(host); + else + ret = __host_offline(host); + mutex_unlock(&host->lock); + + return ret ? ret : len; +} + +static ssize_t +spi_mockup_live_show(struct config_item *item, char *buf) +{ + struct spi_mockup_host *host = to_spi_mockup_host(item); + + return sprintf(buf, "%s", (host->pdev) ? "true" : "false"); +} +CONFIGFS_ATTR(spi_mockup_, live); + + +#define SPI_MOCKUP_ATTR(type, name) \ +static ssize_t spi_mockup_ ## name ## _store(struct config_item *item, \ + const char *buf, size_t len) \ +{ \ + int ret; \ + type val; \ + struct spi_mockup_host *host = to_spi_mockup_host(item); \ + \ + mutex_lock(&host->lock); \ + if (host->pdev) { \ + ret = -EBUSY; \ + goto out; \ + } \ + \ + ret = kstrto ## type(buf, 0, &val); \ + if (ret) \ + goto out; \ + \ + host->name = val; \ +out: \ + mutex_unlock(&host->lock); \ + return ret ? ret : len; \ +} \ +static ssize_t spi_mockup_ ## name ## _show(struct config_item *item, \ + char *buf) \ +{ \ + struct spi_mockup_host *host = to_spi_mockup_host(item); \ + return sprintf(buf, "%u", host->name); \ +} \ +CONFIGFS_ATTR(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_host_attrs[] = { + &spi_mockup_attr_live, + &spi_mockup_attr_min_speed, + &spi_mockup_attr_max_speed, + &spi_mockup_attr_flags, + &spi_mockup_attr_num_cs, + NULL, +}; + +static void spi_mockup_host_release(struct config_item *item) +{ + struct spi_mockup_host *host = to_spi_mockup_host(item); + + __host_offline(host); + kfree(host); +} + +static struct configfs_item_operations spi_mockup_host_item_ops = { + .release = spi_mockup_host_release, +}; + +static const struct config_item_type spi_mockup_host_config_group_type = { + .ct_owner = THIS_MODULE, + .ct_attrs = spi_mockup_host_attrs, + .ct_item_ops = &spi_mockup_host_item_ops, +}; + +static struct config_group * +spi_mockup_host_make_group(struct config_group *group, const char *name) +{ + int ret, nchar; + unsigned int nr; + struct spi_mockup_host *host; + + ret = sscanf(name, "spi%u%n", &nr, &nchar); + if (ret != 1 || nchar != strlen(name)) + return ERR_PTR(-EINVAL); + + host = kzalloc(sizeof(*host), GFP_KERNEL); + if (!host) + return ERR_PTR(-ENOMEM); + + host->bus_nr = nr; + host->num_cs = MOCKUP_CHIPSELECT_MAX; + mutex_init(&host->lock); + + config_group_init_type_name(&host->group, name, + &spi_mockup_host_config_group_type); + + return &host->group; +} + +static struct configfs_group_operations spi_mockup_host_group_ops = { + .make_group = spi_mockup_host_make_group, +}; + +static const struct config_item_type spi_mockup_host_type = { + .ct_owner = THIS_MODULE, + .ct_group_ops = &spi_mockup_host_group_ops, +}; + +static struct configfs_subsystem spi_mockup_config_subsys = { + .su_group = { + .cg_item = { + .ci_namebuf = "spi-mockup", + .ci_type = &spi_mockup_host_type, + } + } +}; + +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 spi_controller *ctrl; + struct spi_mockup_host **host; + + host = dev_get_platdata(&pdev->dev); + if (!host || !(*host)) + return -EINVAL; + + ctrl = spi_alloc_host(&pdev->dev, 0); + if (!ctrl) { + pr_err("failed to alloc spi controller\n"); + return -ENOMEM; + } + + platform_set_drvdata(pdev, ctrl); + + ctrl->bus_num = pdev->id; + ctrl->mode_bits = SPI_MODE_USER_MASK; + ctrl->dev.of_node = pdev->dev.of_node; + ctrl->transfer_one_message = spi_mockup_transfer; + ctrl->min_speed_hz = (*host)->min_speed; + ctrl->max_speed_hz = (*host)->max_speed; + ctrl->num_chipselect = (*host)->num_cs; + ctrl->flags = (*host)->flags; + + ret = devm_spi_register_controller(&pdev->dev, ctrl); + if (ret) { + spi_controller_put(ctrl); + return ret; + } + + (*host)->ctrl = ctrl; + + return 0; +} + +static struct platform_driver spi_mockup_driver = { + .probe = spi_mockup_probe, + .driver = { + .name = "spi-mockup", + }, +}; + +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"); +MODULE_LICENSE("GPL"); From patchwork Sat Nov 18 10:44:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Xiaoxu X-Patchwork-Id: 745141 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 A0D46C5AE5E for ; Sat, 18 Nov 2023 10:45:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229449AbjKRKph (ORCPT ); Sat, 18 Nov 2023 05:45:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229702AbjKRKpf (ORCPT ); Sat, 18 Nov 2023 05:45:35 -0500 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E079710DF; Sat, 18 Nov 2023 02:45:30 -0800 (PST) Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4SXVm41TWpz4f3lfP; Sat, 18 Nov 2023 18:45:24 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 540E81A01A1; Sat, 18 Nov 2023 18:45:28 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.107]) by APP1 (Coremail) with SMTP id cCh0CgBXWhDClVhlAjMGBQ--.58652S6; Sat, 18 Nov 2023 18:45:28 +0800 (CST) From: Zhang Xiaoxu To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com, linux-kernel@vger.kernel.org, broonie@kernel.org, rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com, linux-spi@vger.kernel.org Subject: [PATCH v4 -next 2/4] spi: mockup: Add register spi device support Date: Sat, 18 Nov 2023 18:44:40 +0800 Message-Id: <20231118104442.861759-3-zhangxiaoxu@huawecloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231118104442.861759-1-zhangxiaoxu@huawecloud.com> References: <20231118104442.861759-1-zhangxiaoxu@huawecloud.com> MIME-Version: 1.0 X-CM-TRANSID: cCh0CgBXWhDClVhlAjMGBQ--.58652S6 X-Coremail-Antispam: 1UD129KBjvJXoW3Jr4DAFW7Gw1DWw47GFyDWrg_yoW3JFWDpF yrAa4YvrWrGrZ8Wr45JFW8W34Fkr18KryUA3s3C3WFkwnxJry8Xas7GF1ayF1rAFWkGF17 ZFyrAF18Cr4jyrUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUB2b4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUXw A2048vs2IY020Ec7CjxVAFwI0_Gr0_Xr1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l42xK82IY64kExVAvwVAq07 x20xyl4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8G jcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2I x0cI8IcVAFwI0_Gr0_Xr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr1lIxAIcVCF 04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r4j6F4UMIIF0xvEx4A2jsIEc7 CjxVAFwI0_Gr1j6F4UJbIYCTnIWIevJa73UjIFyTuYvjxU2TmhDUUUU Sender: zhangxiaoxu@huaweicloud.com X-CM-SenderInfo: x2kd0wp0ld053x6k3tpzhluzxrxghudrp/ Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org From: Zhang Xiaoxu This enable register the spi device to mockup host, it depends configfs. After enable the mockup host, register the device by executing the following command: $ mkdir /config/spi-mockup/spi0/max31722 $ echo -n max31722 > /config/spi-mockup/spi0/max31722/device_id $ echo 1 > /config/spi-mockup/spi0/max31722/live Unregister by: $ echo 0 > /config/spi-mockup/spi0/max31722/live $ rmdir /config/spi-mockup/spi0/max31722 Signed-off-by: Zhang Xiaoxu --- drivers/spi/spi-mockup.c | 208 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) diff --git a/drivers/spi/spi-mockup.c b/drivers/spi/spi-mockup.c index acb502c14676..f053579cc37f 100644 --- a/drivers/spi/spi-mockup.c +++ b/drivers/spi/spi-mockup.c @@ -18,6 +18,7 @@ struct spi_mockup_host { struct config_group group; + struct config_group targets_group; struct mutex lock; @@ -29,6 +30,7 @@ struct spi_mockup_host { struct platform_device *pdev; struct spi_controller *ctrl; + unsigned long bitmap[BITS_TO_LONGS(MOCKUP_CHIPSELECT_MAX)]; }; static struct spi_mockup_host *to_spi_mockup_host(struct config_item *item) @@ -38,6 +40,205 @@ static struct spi_mockup_host *to_spi_mockup_host(struct config_item *item) return container_of(group, struct spi_mockup_host, group); } +static struct spi_mockup_host * +to_spi_mockup_host_from_targets(struct config_group *targets_group) +{ + return container_of(targets_group, + struct spi_mockup_host, targets_group); +} + +struct spi_mockup_target { + struct config_group group; + unsigned short chip; + char device_id[SPI_NAME_SIZE]; + struct spi_device *spi; + struct spi_mockup_host *host; +}; + +static struct spi_mockup_target *to_spi_mockup_target(struct config_item *item) +{ + struct config_group *group = to_config_group(item); + + return container_of(group, struct spi_mockup_target, group); +} + +static ssize_t +spi_mockup_target_device_id_show(struct config_item *item, char *buf) +{ + struct spi_mockup_target *target = to_spi_mockup_target(item); + + return sprintf(buf, "%s\n", target->device_id); +} + +static ssize_t +spi_mockup_target_device_id_store(struct config_item *item, + const char *buf, size_t len) +{ + struct spi_mockup_target *target = to_spi_mockup_target(item); + + if (len > SPI_NAME_SIZE) + return -EINVAL; + + memcpy(target->device_id, buf, len); + + return len; +} +CONFIGFS_ATTR(spi_mockup_target_, device_id); + +static int __target_online(struct spi_mockup_target *target) +{ + struct spi_board_info info = {0}; + struct device *dev; + + if (target->spi) + return -EBUSY; + + if (!target->host->pdev) + return -ENODEV; + + target->chip = find_first_zero_bit(target->host->bitmap, + MOCKUP_CHIPSELECT_MAX); + if (target->chip < 0) + return target->chip; + + if (target->chip > target->host->num_cs) + return -EBUSY; + + info.chip_select = target->chip; + strncpy(info.modalias, target->device_id, + strlen(target->device_id)); + + target->spi = spi_new_device(target->host->ctrl, &info); + if (!target->spi) + return -ENOMEM; + + bitmap_set(target->host->bitmap, target->chip, 1); + + dev = &target->host->ctrl->dev; + dev_info(dev, "Instantiated device %s at 0x%02x\n", + info.modalias, info.chip_select); + + return 0; +} + +static int __target_offline(struct spi_mockup_target *target) +{ + struct device *dev; + + if (!target->spi) + return -ENODEV; + + dev = &target->host->ctrl->dev; + dev_info(dev, "Deleting device %s at 0x%02hx\n", + dev_name(&target->spi->dev), target->chip); + + spi_unregister_device(target->spi); + target->spi = NULL; + + bitmap_clear(target->host->bitmap, target->chip, 1); + + + return 0; +} + +static ssize_t +spi_mockup_target_live_store(struct config_item *item, + const char *buf, size_t len) +{ + struct spi_mockup_target *target = to_spi_mockup_target(item); + int ret; + bool res; + + ret = kstrtobool(buf, &res); + if (ret) + return -EINVAL; + + if (!strlen(target->device_id)) + return -EINVAL; + + mutex_lock(&target->host->lock); + if (res) + ret = __target_online(target); + else + ret = __target_offline(target); + mutex_unlock(&target->host->lock); + + return ret ? ret : len; +} + +static ssize_t +spi_mockup_target_live_show(struct config_item *item, char *buf) +{ + struct spi_mockup_target *target = to_spi_mockup_target(item); + + return sprintf(buf, "%s\n", (target->spi) ? "true" : "false"); +} +CONFIGFS_ATTR(spi_mockup_target_, live); + + +static struct configfs_attribute *spi_mockup_target_attrs[] = { + &spi_mockup_target_attr_device_id, + &spi_mockup_target_attr_live, + NULL, +}; + +static void spi_mockup_target_release(struct config_item *item) +{ + struct spi_mockup_target *target = to_spi_mockup_target(item); + + __target_offline(target); + kfree(target); +} + +static struct configfs_item_operations spi_mockup_target_item_ops = { + .release = spi_mockup_target_release, +}; + +static const struct config_item_type spi_mockup_target_item_type = { + .ct_owner = THIS_MODULE, + .ct_attrs = spi_mockup_target_attrs, + .ct_item_ops = &spi_mockup_target_item_ops, +}; + +static struct config_group * +spi_mockup_target_make_group(struct config_group *group, const char *name) +{ + struct spi_mockup_target *target; + struct spi_mockup_host *host = to_spi_mockup_host_from_targets(group); + + target = kzalloc(sizeof(*target), GFP_KERNEL); + if (!target) + return ERR_PTR(-ENOMEM); + + target->host = host; + + config_group_init_type_name(&target->group, name, + &spi_mockup_target_item_type); + + return &target->group; +} + +static struct configfs_group_operations spi_mockup_targets_group_ops = { + .make_group = spi_mockup_target_make_group, +}; + +static void spi_mockup_targets_group_release(struct config_item *item) +{ + struct spi_mockup_target *target = to_spi_mockup_target(item); + + kfree(target); +} + +static struct configfs_item_operations spi_mockup_targets_group_item_ops = { + .release = spi_mockup_targets_group_release, +}; + +static const struct config_item_type spi_mockup_target_type = { + .ct_owner = THIS_MODULE, + .ct_group_ops = &spi_mockup_targets_group_ops, + .ct_item_ops = &spi_mockup_targets_group_item_ops, +}; + static ssize_t __host_online(struct spi_mockup_host *host) { int ret; @@ -69,6 +270,9 @@ static ssize_t __host_offline(struct spi_mockup_host *host) if (!host->pdev) return -ENODEV; + if (!bitmap_empty(host->bitmap, host->num_cs)) + return -EBUSY; + platform_device_unregister(host->pdev); host->pdev = NULL; host->ctrl = NULL; @@ -192,6 +396,10 @@ spi_mockup_host_make_group(struct config_group *group, const char *name) config_group_init_type_name(&host->group, name, &spi_mockup_host_config_group_type); + config_group_init_type_name(&host->targets_group, "targets", + &spi_mockup_target_type); + configfs_add_default_group(&host->targets_group, &host->group); + return &host->group; } From patchwork Sat Nov 18 10:44:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Xiaoxu X-Patchwork-Id: 745142 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 F06D2C5AE5B for ; Sat, 18 Nov 2023 10:45:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230015AbjKRKpg (ORCPT ); Sat, 18 Nov 2023 05:45:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229664AbjKRKpf (ORCPT ); Sat, 18 Nov 2023 05:45:35 -0500 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57F6310EC; Sat, 18 Nov 2023 02:45:31 -0800 (PST) Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4SXVm60Zxjz4f3jYj; Sat, 18 Nov 2023 18:45:26 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id A90ED1A0176; Sat, 18 Nov 2023 18:45:28 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.107]) by APP1 (Coremail) with SMTP id cCh0CgBXWhDClVhlAjMGBQ--.58652S7; Sat, 18 Nov 2023 18:45:28 +0800 (CST) From: Zhang Xiaoxu To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com, linux-kernel@vger.kernel.org, broonie@kernel.org, rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com, linux-spi@vger.kernel.org Subject: [PATCH v4 -next 3/4] spi: mockup: Add writeable tracepoint for spi transfer Date: Sat, 18 Nov 2023 18:44:41 +0800 Message-Id: <20231118104442.861759-4-zhangxiaoxu@huawecloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231118104442.861759-1-zhangxiaoxu@huawecloud.com> References: <20231118104442.861759-1-zhangxiaoxu@huawecloud.com> MIME-Version: 1.0 X-CM-TRANSID: cCh0CgBXWhDClVhlAjMGBQ--.58652S7 X-Coremail-Antispam: 1UD129KBjvJXoWxWryrZF4rZF4rZF4UKr1UAwb_yoW7Jr4fpF yUCF45KrW8Jw429F4fuFW8Ary3Aa1kurW7K3sI9wn8Ar18try8Xw4qgry5tFy8JrsFgFyU uFZ2kryjkr45Zw7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUB2b4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUWw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l42xK82IY64kExVAvwVAq07 x20xyl4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8G jcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2I x0cI8IcVAFwI0_Gr0_Xr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr1lIxAIcVCF 04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r4j6F4UMIIF0xvEx4A2jsIEc7 CjxVAFwI0_Gr1j6F4UJbIYCTnIWIevJa73UjIFyTuYvjxUrhFxUUUUU Sender: zhangxiaoxu@huaweicloud.com 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 | 53 +++++++++++++++++++++++++++++-- include/linux/spi/spi-mockup.h | 17 ++++++++++ include/trace/events/spi_mockup.h | 31 ++++++++++++++++++ 4 files changed, 100 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 6524735a4936..aee2dfcc9e54 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -1232,6 +1232,7 @@ config SPI_TLE62X0 config SPI_MOCKUP tristate "SPI controller Testing Driver" + 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 f053579cc37f..a86bd6c7bd2e 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 U16_MAX struct spi_mockup_host { @@ -421,13 +424,59 @@ static struct configfs_subsystem spi_mockup_config_subsys = { } }; +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 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 18 10:44:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Xiaoxu X-Patchwork-Id: 745608 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 9BDC7C5AE5C for ; Sat, 18 Nov 2023 10:45:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232457AbjKRKph (ORCPT ); Sat, 18 Nov 2023 05:45:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229716AbjKRKpf (ORCPT ); Sat, 18 Nov 2023 05:45:35 -0500 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F10610DA; Sat, 18 Nov 2023 02:45:31 -0800 (PST) Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4SXVm73l0yz4f3kFr; Sat, 18 Nov 2023 18:45:27 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 0C4761A016E; Sat, 18 Nov 2023 18:45:29 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.107]) by APP1 (Coremail) with SMTP id cCh0CgBXWhDClVhlAjMGBQ--.58652S8; Sat, 18 Nov 2023 18:45:28 +0800 (CST) From: Zhang Xiaoxu To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com, linux-kernel@vger.kernel.org, broonie@kernel.org, rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com, linux-spi@vger.kernel.org Subject: [PATCH v4 -next 4/4] spi: mockup: Add documentation Date: Sat, 18 Nov 2023 18:44:42 +0800 Message-Id: <20231118104442.861759-5-zhangxiaoxu@huawecloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231118104442.861759-1-zhangxiaoxu@huawecloud.com> References: <20231118104442.861759-1-zhangxiaoxu@huawecloud.com> MIME-Version: 1.0 X-CM-TRANSID: cCh0CgBXWhDClVhlAjMGBQ--.58652S8 X-Coremail-Antispam: 1UD129KBjvJXoW3Wry5Jr4xWFW7Jry5WFWkCrg_yoW7trWDpF WUJFy3K3y8XF9xWF1Sq3s5G343Aas7ZFZrGFZ5tF10yr1q9r95AF1xtryYqan8WF4DCFy0 vFW5AFW8Kry7Z3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBSb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l42xK82IY64kExVAvwV Aq07x20xyl4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_Gr0_Xr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr1lIxAI cVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r4j6F4UMIIF0xvEx4A2js IEc7CjxVAFwI0_Gr1j6F4UJbIYCTnIWIevJa73UjIFyTuYvjxUruWlDUUUU Sender: zhangxiaoxu@huaweicloud.com 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 | 198 +++++++++++++++++++++++++++++++ 2 files changed, 199 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..a5b69ce83ad9 --- /dev/null +++ b/Documentation/spi/spi-mockup.rst @@ -0,0 +1,198 @@ +.. 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. create the spi mockup controller; + 2. load EBPF program as device's backend; + 3. create target chip device to the spi mockup controller. + +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 controller. + +:: + + $ mkdir /sys/kernel/config/spi-mockup/spi0 + + # configure the spi mockup controller 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 controller + $ echo 1 > /sys/kernel/config/spi-mockup/spi0/live + +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 + +Create the mchp23k256 device, this is accomplished by executing the +following command: + +:: + + $ mkdir /sys/kernel/config/spi-mockup/spi0/targets/mchp23k256 + $ echo -n mchp23k256 > /sys/kernel/config/spi-mockup/spi0/targets/mchp23k256/device_id + $ echo 1 > /sys/kernel/config/spi-mockup/spi0/targets/mchp23k256/live + + +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 + 00000000 61 61 61 61 0a 00 00 00 00 00 00 00 00 00 00 00 |aaaa............| + 00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| + * + 00008000 + + $ bpftool map update name regs_mchp23k256 key 0 0 0 0 value 0 + + $ hexdump /dev/mtd0 + 00000000 00 61 61 61 0a 00 00 00 00 00 00 00 00 00 00 00 |.aaa............| + 00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| + * + 00008000 + + +Remove the mchp23k256 device by executing the following command: + +:: + + $ echo 0 > /sys/kernel/config/spi-mockup/spi0/targets/mchp23k256/live + $ rmdir /sys/kernel/config/spi-mockup/spi0/targets/mchp23k256 + +Remove the spi mockup controller by executing the following command: + +:: + + $ echo 0 > /sys/kernel/config/spi-mockup/spi0/live + $ rmdir /sys/kernel/config/spi-mockup/spi0