From: Zhang Xiaoxu <zhangxiaoxu5@huawei.com>
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 <zhangxiaoxu@huawei.com>
---
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;
}
--
2.34.1
Hi Zhang,
kernel test robot noticed the following build warnings:
[auto build test WARNING on next-20231117]
url: https://github.com/intel-lab-lkp/linux/commits/Zhang-Xiaoxu/spi-mockup-Add-SPI-controller-testing-driver/20231118-184747
base: next-20231117
patch link: https://lore.kernel.org/r/20231118104442.861759-3-zhangxiaoxu%40huawecloud.com
patch subject: [PATCH v4 -next 2/4] spi: mockup: Add register spi device support
config: openrisc-allmodconfig (https://download.01.org/0day-ci/archive/20231120/202311200018.xB4hEnZx-lkp@intel.com/config)
compiler: or1k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231120/202311200018.xB4hEnZx-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202311200018.xB4hEnZx-lkp@intel.com/
All warnings (new ones prefixed by >>):
In function '__target_online',
inlined from 'spi_mockup_target_live_store' at drivers/spi/spi-mockup.c:161:9:
>> drivers/spi/spi-mockup.c:108:9: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-truncation]
108 | strncpy(info.modalias, target->device_id,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109 | strlen(target->device_id));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/spi/spi-mockup.c:108:9: note: length computed here
108 | strncpy(info.modalias, target->device_id,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109 | strlen(target->device_id));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
vim +/strncpy +108 drivers/spi/spi-mockup.c
87
88 static int __target_online(struct spi_mockup_target *target)
89 {
90 struct spi_board_info info = {0};
91 struct device *dev;
92
93 if (target->spi)
94 return -EBUSY;
95
96 if (!target->host->pdev)
97 return -ENODEV;
98
99 target->chip = find_first_zero_bit(target->host->bitmap,
100 MOCKUP_CHIPSELECT_MAX);
101 if (target->chip < 0)
102 return target->chip;
103
104 if (target->chip > target->host->num_cs)
105 return -EBUSY;
106
107 info.chip_select = target->chip;
> 108 strncpy(info.modalias, target->device_id,
109 strlen(target->device_id));
110
111 target->spi = spi_new_device(target->host->ctrl, &info);
112 if (!target->spi)
113 return -ENOMEM;
114
115 bitmap_set(target->host->bitmap, target->chip, 1);
116
117 dev = &target->host->ctrl->dev;
118 dev_info(dev, "Instantiated device %s at 0x%02x\n",
119 info.modalias, info.chip_select);
120
121 return 0;
122 }
123
124 static int __target_offline(struct spi_mockup_target *target)
125 {
126 struct device *dev;
127
128 if (!target->spi)
129 return -ENODEV;
130
131 dev = &target->host->ctrl->dev;
132 dev_info(dev, "Deleting device %s at 0x%02hx\n",
133 dev_name(&target->spi->dev), target->chip);
134
135 spi_unregister_device(target->spi);
136 target->spi = NULL;
137
138 bitmap_clear(target->host->bitmap, target->chip, 1);
139
140
141 return 0;
142 }
143
144 static ssize_t
145 spi_mockup_target_live_store(struct config_item *item,
146 const char *buf, size_t len)
147 {
148 struct spi_mockup_target *target = to_spi_mockup_target(item);
149 int ret;
150 bool res;
151
152 ret = kstrtobool(buf, &res);
153 if (ret)
154 return -EINVAL;
155
156 if (!strlen(target->device_id))
157 return -EINVAL;
158
159 mutex_lock(&target->host->lock);
160 if (res)
> 161 ret = __target_online(target);
162 else
163 ret = __target_offline(target);
164 mutex_unlock(&target->host->lock);
165
166 return ret ? ret : len;
167 }
168
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
© 2016 - 2026 Red Hat, Inc.