summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorMark Brown <broonie@kernel.org>2026-08-06 19:13:17 +0100
committerMark Brown <broonie@kernel.org>2026-08-06 19:13:17 +0100
commita2d6aa3b84466b2b5bfb74e07eb5b0b308b7022d (patch)
tree4f76ee256ba251888278a523ca578a7ab38910a5 /drivers
parent362a11ef1beadbc301ea949b85748e6bd1e0f384 (diff)
parent036d4b059be01eb67b65b03131855fc42153de6a (diff)
spi: add new_device/delete_device sysfs interface
Vishwaroop A <va@nvidia.com> says: Add I2C-style new_device/delete_device sysfs attributes to SPI host controllers, allowing userspace to instantiate and remove SPI devices at runtime without device-tree changes. Patch 1 adds the new_device/delete_device attributes and the supporting infrastructure (userspace_clients list, manual sysfs group registration). Patch 2 adds the ABI and user-facing documentation. Link: https://lore.kernel.org/linux-spi/20260728191056.2337791-1-va@nvidia.com/ # v8 Link: https://lore.kernel.org/linux-spi/20260728030541.2279518-1-va@nvidia.com/ # v7 Link: https://lore.kernel.org/linux-spi/cover.1784000000.git.va@nvidia.com/ # v6 Link: https://lore.kernel.org/linux-spi/20260517201602.498135-1-va@nvidia.com/ # v5 Link: https://lore.kernel.org/linux-tegra/909f0c92-d110-4253-903e-5c81e21e12c9@nvidia.com/ Link: https://patch.msgid.link/20260803104614.2548375-1-va@nvidia.com
Diffstat (limited to 'drivers')
-rw-r--r--drivers/spi/spi.c246
1 files changed, 246 insertions, 0 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index d9e6b4b87c89..07c572e1df5d 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -43,6 +43,7 @@ EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
#include "internals.h"
static int __spi_setup(struct spi_device *spi, bool initial_setup);
+static int __spi_add_device(struct spi_device *spi, struct spi_device *parent);
static DEFINE_IDR(spi_controller_idr);
@@ -297,6 +298,192 @@ static const struct attribute_group spi_controller_statistics_group = {
.attrs = spi_controller_statistics_attrs,
};
+#if IS_ENABLED(CONFIG_SPI_DYNAMIC)
+
+/*
+ * new_device_store - instantiate a new SPI device from userspace
+ *
+ * Takes parameters: <modalias> <chip_select> [<max_speed_hz> [<mode>]]
+ *
+ * Examples:
+ * echo spidev 0 > new_device
+ * echo spidev 0 10000000 > new_device
+ * echo spidev 0 10000000 3 > new_device
+ */
+static ssize_t
+new_device_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct spi_controller *ctlr = container_of(dev, struct spi_controller,
+ dev);
+ struct spi_device *spi;
+ char modalias[SPI_NAME_SIZE];
+ unsigned int chip_select;
+ u32 max_speed_hz = 0;
+ u32 mode = 0;
+ char *blank;
+ int status;
+
+ blank = strchr(buf, ' ');
+ if (!blank) {
+ dev_err(dev, "new_device: Missing parameters\n");
+ return -EINVAL;
+ }
+
+ if (blank == buf || blank - buf > SPI_NAME_SIZE - 1) {
+ dev_err(dev, "new_device: Invalid device name\n");
+ return -EINVAL;
+ }
+
+ memset(modalias, 0, sizeof(modalias));
+ memcpy(modalias, buf, blank - buf);
+
+ /*
+ * sscanf fills only the fields it matches; unmatched optional
+ * fields (max_speed_hz, mode) stay zero from initialisation above.
+ * max_speed_hz == 0 is clamped to the controller max by spi_setup().
+ * mode == 0 selects SPI mode 0 (CPOL=0, CPHA=0).
+ */
+ if (sscanf(++blank, "%u %u %u", &chip_select, &max_speed_hz, &mode) < 1) {
+ dev_err(dev, "new_device: Can't parse chip select\n");
+ return -EINVAL;
+ }
+
+ /*
+ * spi_device.chip_select[] is u8, so cap at U8_MAX independently of
+ * ctlr->num_chipselect (which is u16 and may exceed 255). Without
+ * this, values in (U8_MAX, num_chipselect) would silently truncate
+ * inside spi_set_chipselect() and select the wrong CS.
+ */
+ if (chip_select > U8_MAX || chip_select >= ctlr->num_chipselect) {
+ dev_err(dev, "new_device: Chip select %u out of range (num_chipselect=%u)\n",
+ chip_select, ctlr->num_chipselect);
+ return -EINVAL;
+ }
+
+ /*
+ * Reject kernel-internal mode bits (SPI_NO_TX, SPI_NO_RX,
+ * SPI_TPM_HW_FLOW, ...). These are set only by in-kernel drivers
+ * that know they are safe on their controller/device pair and must
+ * not be settable through a userspace-writable sysfs. Matches
+ * spidev's SPI_IOC_WR_MODE32 handling (drivers/spi/spidev.c).
+ */
+ if (mode & ~(u32)SPI_MODE_USER_MASK) {
+ dev_err(dev, "new_device: Invalid mode bits 0x%x\n",
+ mode & ~(u32)SPI_MODE_USER_MASK);
+ return -EINVAL;
+ }
+
+ spi = spi_alloc_device(ctlr);
+ if (!spi)
+ return -ENOMEM;
+
+ spi_set_chipselect(spi, 0, chip_select);
+ spi->max_speed_hz = max_speed_hz;
+ spi->mode = mode;
+ spi->cs_index_mask = BIT(0);
+ strscpy(spi->modalias, modalias, sizeof(spi->modalias));
+
+ /*
+ * Set driver_override so that the device binds to the driver
+ * named by modalias regardless of whether that driver's
+ * id_table contains a matching entry. This is needed because
+ * some drivers (e.g. spidev) deliberately omit generic names
+ * from their id_table.
+ */
+ status = device_set_driver_override(&spi->dev, modalias);
+ if (status) {
+ spi_dev_put(spi);
+ return status;
+ }
+
+ /*
+ * spi_unregister_controller() removes the new_device/delete_device
+ * sysfs group before taking add_lock, so kernfs_drain() has already
+ * completed by the time we get here and we cannot be racing with
+ * teardown. Take add_lock to serialise the __spi_add_device() and
+ * list insertion with respect to non-sysfs callers of
+ * __spi_add_device() (DT/ACPI, ancillary), which check
+ * device_is_registered(&ctlr->dev) under the same lock.
+ */
+ mutex_lock(&ctlr->add_lock);
+
+ status = __spi_add_device(spi, NULL);
+ if (status) {
+ mutex_unlock(&ctlr->add_lock);
+ spi_dev_put(spi);
+ return status;
+ }
+
+ list_add_tail(&spi->userspace_node, &ctlr->userspace_clients);
+ mutex_unlock(&ctlr->add_lock);
+
+ dev_info(dev, "new_device: Instantiated device %s at CS%u\n",
+ modalias, chip_select);
+ return count;
+}
+static DEVICE_ATTR_IGNORE_LOCKDEP(new_device, 0200, NULL, new_device_store);
+
+static ssize_t
+delete_device_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct spi_controller *ctlr = container_of(dev, struct spi_controller,
+ dev);
+ struct spi_device *spi, *next;
+ unsigned short cs;
+ char end;
+ int res;
+
+ res = sscanf(buf, "%hu%c", &cs, &end);
+ if (res < 1) {
+ dev_err(dev, "delete_device: Can't parse chip select\n");
+ return -EINVAL;
+ }
+ if (res > 1 && end != '\n') {
+ dev_err(dev, "delete_device: Unexpected parameters\n");
+ return -EINVAL;
+ }
+
+ res = -ENOENT;
+ mutex_lock(&ctlr->add_lock);
+ list_for_each_entry_safe(spi, next, &ctlr->userspace_clients,
+ userspace_node) {
+ if (spi_get_chipselect(spi, 0) == cs) {
+ dev_info(dev, "delete_device: Deleting device %s at CS%u\n",
+ spi->modalias, cs);
+ list_del(&spi->userspace_node);
+ spi_unregister_device(spi);
+ res = count;
+ break;
+ }
+ }
+ mutex_unlock(&ctlr->add_lock);
+
+ if (res < 0)
+ dev_err(dev, "delete_device: Can't find device in list\n");
+ return res;
+}
+static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, 0200, NULL,
+ delete_device_store);
+
+static struct attribute *spi_controller_userspace_attrs[] = {
+ &dev_attr_new_device.attr,
+ &dev_attr_delete_device.attr,
+ NULL,
+};
+
+static const struct attribute_group spi_controller_userspace_group = {
+ .attrs = spi_controller_userspace_attrs,
+};
+
+#endif /* CONFIG_SPI_DYNAMIC */
+
+/*
+ * spi_controller_userspace_group is registered manually for host controllers
+ * at the end of spi_register_controller() so new_device/delete_device only
+ * appear after DT/ACPI children and the queue are set up.
+ */
static const struct attribute_group *spi_controller_groups[] = {
&spi_controller_statistics_group,
NULL,
@@ -3259,6 +3446,9 @@ struct spi_controller *__spi_alloc_controller(struct device *dev,
mutex_init(&ctlr->bus_lock_mutex);
mutex_init(&ctlr->io_mutex);
mutex_init(&ctlr->add_lock);
+#if IS_ENABLED(CONFIG_SPI_DYNAMIC)
+ INIT_LIST_HEAD(&ctlr->userspace_clients);
+#endif
ctlr->bus_num = -1;
ctlr->num_chipselect = 1;
ctlr->num_data_lanes = 1;
@@ -3552,6 +3742,29 @@ int spi_register_controller(struct spi_controller *ctlr)
of_register_spi_devices(ctlr);
acpi_register_spi_devices(ctlr);
+#if IS_ENABLED(CONFIG_SPI_DYNAMIC)
+ /*
+ * Register the new_device/delete_device sysfs interface as the
+ * final step of host controller bringup, only after the queue,
+ * boardinfo matching and DT/ACPI enumeration have all completed.
+ * If this fails, the controller is otherwise usable, so log and
+ * carry on rather than tearing everything down.
+ */
+ if (!spi_controller_is_target(ctlr)) {
+ status = sysfs_create_group(&ctlr->dev.kobj,
+ &spi_controller_userspace_group);
+ if (status) {
+ dev_warn(&ctlr->dev,
+ "Failed to create userspace client interface: %d\n",
+ status);
+ } else {
+ ctlr->userspace_registered = true;
+ /* Notify userspace that the new attributes are available. */
+ kobject_uevent(&ctlr->dev.kobj, KOBJ_CHANGE);
+ }
+ }
+#endif
+
return 0;
del_ctrl:
@@ -3616,10 +3829,43 @@ void spi_unregister_controller(struct spi_controller *ctlr)
struct spi_controller *found;
int id = ctlr->bus_num;
+ /*
+ * Drain in-flight new_device/delete_device sysfs stores and
+ * prevent new ones from starting. Must happen before we take
+ * add_lock so kernfs_drain doesn't wait on a store that is
+ * itself blocked on add_lock.
+ */
+#if IS_ENABLED(CONFIG_SPI_DYNAMIC)
+ if (ctlr->userspace_registered) {
+ sysfs_remove_group(&ctlr->dev.kobj,
+ &spi_controller_userspace_group);
+ ctlr->userspace_registered = false;
+ }
+#endif
+
/* Prevent addition of new devices, unregister existing ones */
if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
mutex_lock(&ctlr->add_lock);
+#if IS_ENABLED(CONFIG_SPI_DYNAMIC)
+ /*
+ * Drain userspace_clients before __unregister since
+ * spi_unregister_device() doesn't do list_del() itself. The
+ * userspace sysfs group has already been removed above and
+ * kernfs_drain() has completed, so no new entries can appear
+ * here.
+ */
+ while (!list_empty(&ctlr->userspace_clients)) {
+ struct spi_device *spi;
+
+ spi = list_first_entry(&ctlr->userspace_clients,
+ struct spi_device,
+ userspace_node);
+ list_del(&spi->userspace_node);
+ spi_unregister_device(spi);
+ }
+#endif
+
device_for_each_child(&ctlr->dev, NULL, __unregister);
/* First make sure that this controller was ever added */