All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michal Suchanek <hramrach@gmail.com>
To: Mark Brown <broonie@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-kernel@vger.kernel.org, Hans de Goede <hdegoede@redhat.com>,
	linux-spi@vger.kernel.org, Martin Sperl <kernel@martin.sperl.org>,
	Michal Suchanek <hramrach@gmail.com>,
	Maxime Ripard <maxime.ripard@free-electrons.com>
Subject: [PATCH] spi: Add option to bind spidev to all chipselects
Date: 13 May 2015 09:34:41 -0000	[thread overview]
Message-ID: <20150513093441.80359.qmail@dec59.ruk.cuni.cz> (raw)
In-Reply-To: <1431462804-30467-1-git-send-email-maxime.ripard@free-electrons.com>

Bypass the check if CS is in use for spidev devices if CONFIG_SPIDEV_SHADOW is
set.  Rename spidev devices to avoid sysfs conflict.

This allows dynamically loading SPI device overlays or communicating
with SPI devices configured by a kernel driver from userspace.

Signed-off-by: Michal Suchanek <hramrach@gmail.com>
---
 drivers/spi/Kconfig     | 13 +++++++++
 drivers/spi/spi.c       | 74 ++++++++++++++++++++++++++++++++++---------------
 include/linux/spi/spi.h |  1 +
 3 files changed, 65 insertions(+), 23 deletions(-)

diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index 198f96b..b477828 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -651,6 +651,19 @@ config SPI_SPIDEV
 	  Note that this application programming interface is EXPERIMENTAL
 	  and hence SUBJECT TO CHANGE WITHOUT NOTICE while it stabilizes.
 
+config SPIDEV_SHADOW
+	depends on SPI_SPIDEV
+	bool "Allow spidev access to configured SPI devices (DANGEROUS)"
+	help
+	  This creates a spidev device node for every chipselect.
+
+	  It is possible to access even SPI devices which are in use by a
+	  kernel driver. This allows invoking features not in use by the kernel
+	  or checking device state from userspace when the kernel driver fails.
+
+	  Sending out-of-order messages to the device or reconfiguring the
+	  device might cause driver failure. DANGEROUS
+
 config SPI_TLE62X0
 	tristate "Infineon TLE62X0 (for power switching)"
 	depends on SYSFS
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index e6ca46e..b48a0dc 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -254,16 +254,23 @@ struct spi_device *spi_alloc_device(struct spi_master *master)
 }
 EXPORT_SYMBOL_GPL(spi_alloc_device);
 
-static void spi_dev_set_name(struct spi_device *spi)
+static void spi_dev_set_name(struct spi_device *spi, const char * alias)
 {
 	struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
 
 	if (adev) {
-		dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
+		if (alias)
+			dev_set_name(&spi->dev, "%s-%s", alias, acpi_dev_name(adev));
+		else
+			dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
 		return;
 	}
 
-	dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
+	if (alias)
+		dev_set_name(&spi->dev, "%s%u.%u", alias, spi->master->bus_num,
+		     spi->chip_select);
+	else
+		dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
 		     spi->chip_select);
 }
 
@@ -272,22 +279,25 @@ static int spi_dev_check(struct device *dev, void *data)
 	struct spi_device *spi = to_spi_device(dev);
 	struct spi_device *new_spi = data;
 
-	if (spi->master == new_spi->master &&
-	    spi->chip_select == new_spi->chip_select)
+	if (spi->master == new_spi->master
+	    && spi->chip_select == new_spi->chip_select
+#ifdef CONFIG_SPIDEV_SHADOW
+	    && !spi->shadow && !new_spi->shadow
+#endif
+	    )
 		return -EBUSY;
 	return 0;
 }
 
 /**
- * spi_add_device - Add spi_device allocated with spi_alloc_device
+ * spi_add_device_alias - Add spi_device allocated with spi_alloc_device
+ * possibly even when device for the CS exists.
  * @spi: spi_device to register
+ * @alias: string used as device name prefix or NULL
  *
- * Companion function to spi_alloc_device.  Devices allocated with
- * spi_alloc_device can be added onto the spi bus with this function.
- *
- * Returns 0 on success; negative errno on failure
+ * See spi_add_device
  */
-int spi_add_device(struct spi_device *spi)
+static inline int spi_add_device_alias(struct spi_device *spi, const char * alias)
 {
 	static DEFINE_MUTEX(spi_add_lock);
 	struct spi_master *master = spi->master;
@@ -303,7 +313,8 @@ int spi_add_device(struct spi_device *spi)
 	}
 
 	/* Set the bus ID string */
-	spi_dev_set_name(spi);
+	spi_dev_set_name(spi, alias);
+	spi->shadow = !!alias;
 
 	/* We need to make sure there's no other device with this
 	 * chipselect **BEFORE** we call setup(), else we'll trash
@@ -321,15 +332,17 @@ int spi_add_device(struct spi_device *spi)
 	if (master->cs_gpios)
 		spi->cs_gpio = master->cs_gpios[spi->chip_select];
 
-	/* Drivers may modify this initial i/o setup, but will
-	 * normally rely on the device being setup.  Devices
-	 * using SPI_CS_HIGH can't coexist well otherwise...
-	 */
-	status = spi_setup(spi);
-	if (status < 0) {
-		dev_err(dev, "can't setup %s, status %d\n",
-				dev_name(&spi->dev), status);
-		goto done;
+	if (!alias) {
+			/* Drivers may modify this initial i/o setup, but will
+			 * normally rely on the device being setup.  Devices
+			 * using SPI_CS_HIGH can't coexist well otherwise...
+			 */
+			status = spi_setup(spi);
+			if (status < 0) {
+					dev_err(dev, "can't setup %s, status %d\n",
+						dev_name(&spi->dev), status);
+					goto done;
+			}
 	}
 
 	/* Device may be bound to an active driver when this returns */
@@ -344,6 +357,20 @@ done:
 	mutex_unlock(&spi_add_lock);
 	return status;
 }
+
+/**
+ * spi_add_device - Add spi_device allocated with spi_alloc_device
+ * @spi: spi_device to register
+ *
+ * Companion function to spi_alloc_device.  Devices allocated with
+ * spi_alloc_device can be added onto the spi bus with this function.
+ *
+ * Returns 0 on success; negative errno on failure
+ */
+int spi_add_device(struct spi_device *spi)
+{
+	return spi_add_device_alias(spi, NULL);
+}
 EXPORT_SYMBOL_GPL(spi_add_device);
 
 /**
@@ -1400,6 +1427,7 @@ static void spidev_register_devices(struct spi_master *master)
 		spi->chip_select = i;
 		strlcpy(spi->modalias, "spidev", sizeof(spi->modalias));
 
+#ifndef CONFIG_SPIDEV_SHADOW
 		/*
 		 * This is far from perfect since an addition might be
 		 * done between here and the call to spi_add_device,
@@ -1418,8 +1446,8 @@ static void spidev_register_devices(struct spi_master *master)
 			spi_dev_put(spi);
 			continue;
 		}
-
-		if (spi_add_device(spi)) {
+#endif /* CONFIG_SPIDEV_SHADOW */
+		if (spi_add_device_alias(spi, "spidev")) {
 			dev_err(&master->dev, "Couldn't add spidev device\n");
 			spi_dev_put(spi);
 		}
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index d673072..8368714 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -97,6 +97,7 @@ struct spi_device {
 	void			*controller_data;
 	char			modalias[SPI_NAME_SIZE];
 	int			cs_gpio;	/* chip select gpio */
+	int			shadow; /* ignore when determining if CS is in use */
 
 	/*
 	 * likely need more hooks for more protocol options affecting how
-- 
2.1.4


WARNING: multiple messages have this Message-ID (diff)
From: Michal Suchanek <hramrach-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: Mark Brown <broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
Cc: Greg Kroah-Hartman
	<gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r@public.gmane.org>,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	Hans de Goede <hdegoede-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>,
	linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	Martin Sperl <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>,
	Michal Suchanek
	<hramrach-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
	Maxime Ripard
	<maxime.ripard-wi1+55ScJUtKEb57/3fJTNBPR1lH4CV8@public.gmane.org>
Subject: [PATCH] spi: Add option to bind spidev to all chipselects
Date: 13 May 2015 09:34:41 -0000	[thread overview]
Message-ID: <20150513093441.80359.qmail@dec59.ruk.cuni.cz> (raw)
In-Reply-To: <1431462804-30467-1-git-send-email-maxime.ripard-wi1+55ScJUtKEb57/3fJTNBPR1lH4CV8@public.gmane.org>

Bypass the check if CS is in use for spidev devices if CONFIG_SPIDEV_SHADOW is
set.  Rename spidev devices to avoid sysfs conflict.

This allows dynamically loading SPI device overlays or communicating
with SPI devices configured by a kernel driver from userspace.

Signed-off-by: Michal Suchanek <hramrach-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
 drivers/spi/Kconfig     | 13 +++++++++
 drivers/spi/spi.c       | 74 ++++++++++++++++++++++++++++++++++---------------
 include/linux/spi/spi.h |  1 +
 3 files changed, 65 insertions(+), 23 deletions(-)

diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index 198f96b..b477828 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -651,6 +651,19 @@ config SPI_SPIDEV
 	  Note that this application programming interface is EXPERIMENTAL
 	  and hence SUBJECT TO CHANGE WITHOUT NOTICE while it stabilizes.
 
+config SPIDEV_SHADOW
+	depends on SPI_SPIDEV
+	bool "Allow spidev access to configured SPI devices (DANGEROUS)"
+	help
+	  This creates a spidev device node for every chipselect.
+
+	  It is possible to access even SPI devices which are in use by a
+	  kernel driver. This allows invoking features not in use by the kernel
+	  or checking device state from userspace when the kernel driver fails.
+
+	  Sending out-of-order messages to the device or reconfiguring the
+	  device might cause driver failure. DANGEROUS
+
 config SPI_TLE62X0
 	tristate "Infineon TLE62X0 (for power switching)"
 	depends on SYSFS
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index e6ca46e..b48a0dc 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -254,16 +254,23 @@ struct spi_device *spi_alloc_device(struct spi_master *master)
 }
 EXPORT_SYMBOL_GPL(spi_alloc_device);
 
-static void spi_dev_set_name(struct spi_device *spi)
+static void spi_dev_set_name(struct spi_device *spi, const char * alias)
 {
 	struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
 
 	if (adev) {
-		dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
+		if (alias)
+			dev_set_name(&spi->dev, "%s-%s", alias, acpi_dev_name(adev));
+		else
+			dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
 		return;
 	}
 
-	dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
+	if (alias)
+		dev_set_name(&spi->dev, "%s%u.%u", alias, spi->master->bus_num,
+		     spi->chip_select);
+	else
+		dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
 		     spi->chip_select);
 }
 
@@ -272,22 +279,25 @@ static int spi_dev_check(struct device *dev, void *data)
 	struct spi_device *spi = to_spi_device(dev);
 	struct spi_device *new_spi = data;
 
-	if (spi->master == new_spi->master &&
-	    spi->chip_select == new_spi->chip_select)
+	if (spi->master == new_spi->master
+	    && spi->chip_select == new_spi->chip_select
+#ifdef CONFIG_SPIDEV_SHADOW
+	    && !spi->shadow && !new_spi->shadow
+#endif
+	    )
 		return -EBUSY;
 	return 0;
 }
 
 /**
- * spi_add_device - Add spi_device allocated with spi_alloc_device
+ * spi_add_device_alias - Add spi_device allocated with spi_alloc_device
+ * possibly even when device for the CS exists.
  * @spi: spi_device to register
+ * @alias: string used as device name prefix or NULL
  *
- * Companion function to spi_alloc_device.  Devices allocated with
- * spi_alloc_device can be added onto the spi bus with this function.
- *
- * Returns 0 on success; negative errno on failure
+ * See spi_add_device
  */
-int spi_add_device(struct spi_device *spi)
+static inline int spi_add_device_alias(struct spi_device *spi, const char * alias)
 {
 	static DEFINE_MUTEX(spi_add_lock);
 	struct spi_master *master = spi->master;
@@ -303,7 +313,8 @@ int spi_add_device(struct spi_device *spi)
 	}
 
 	/* Set the bus ID string */
-	spi_dev_set_name(spi);
+	spi_dev_set_name(spi, alias);
+	spi->shadow = !!alias;
 
 	/* We need to make sure there's no other device with this
 	 * chipselect **BEFORE** we call setup(), else we'll trash
@@ -321,15 +332,17 @@ int spi_add_device(struct spi_device *spi)
 	if (master->cs_gpios)
 		spi->cs_gpio = master->cs_gpios[spi->chip_select];
 
-	/* Drivers may modify this initial i/o setup, but will
-	 * normally rely on the device being setup.  Devices
-	 * using SPI_CS_HIGH can't coexist well otherwise...
-	 */
-	status = spi_setup(spi);
-	if (status < 0) {
-		dev_err(dev, "can't setup %s, status %d\n",
-				dev_name(&spi->dev), status);
-		goto done;
+	if (!alias) {
+			/* Drivers may modify this initial i/o setup, but will
+			 * normally rely on the device being setup.  Devices
+			 * using SPI_CS_HIGH can't coexist well otherwise...
+			 */
+			status = spi_setup(spi);
+			if (status < 0) {
+					dev_err(dev, "can't setup %s, status %d\n",
+						dev_name(&spi->dev), status);
+					goto done;
+			}
 	}
 
 	/* Device may be bound to an active driver when this returns */
@@ -344,6 +357,20 @@ done:
 	mutex_unlock(&spi_add_lock);
 	return status;
 }
+
+/**
+ * spi_add_device - Add spi_device allocated with spi_alloc_device
+ * @spi: spi_device to register
+ *
+ * Companion function to spi_alloc_device.  Devices allocated with
+ * spi_alloc_device can be added onto the spi bus with this function.
+ *
+ * Returns 0 on success; negative errno on failure
+ */
+int spi_add_device(struct spi_device *spi)
+{
+	return spi_add_device_alias(spi, NULL);
+}
 EXPORT_SYMBOL_GPL(spi_add_device);
 
 /**
@@ -1400,6 +1427,7 @@ static void spidev_register_devices(struct spi_master *master)
 		spi->chip_select = i;
 		strlcpy(spi->modalias, "spidev", sizeof(spi->modalias));
 
+#ifndef CONFIG_SPIDEV_SHADOW
 		/*
 		 * This is far from perfect since an addition might be
 		 * done between here and the call to spi_add_device,
@@ -1418,8 +1446,8 @@ static void spidev_register_devices(struct spi_master *master)
 			spi_dev_put(spi);
 			continue;
 		}
-
-		if (spi_add_device(spi)) {
+#endif /* CONFIG_SPIDEV_SHADOW */
+		if (spi_add_device_alias(spi, "spidev")) {
 			dev_err(&master->dev, "Couldn't add spidev device\n");
 			spi_dev_put(spi);
 		}
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index d673072..8368714 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -97,6 +97,7 @@ struct spi_device {
 	void			*controller_data;
 	char			modalias[SPI_NAME_SIZE];
 	int			cs_gpio;	/* chip select gpio */
+	int			shadow; /* ignore when determining if CS is in use */
 
 	/*
 	 * likely need more hooks for more protocol options affecting how
-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2015-05-13  9:34 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-12 20:33 [PATCH] spi: Force the registration of the spidev devices Maxime Ripard
2015-05-12 20:33 ` Maxime Ripard
2015-05-13  9:34 ` Michal Suchanek [this message]
2015-05-13  9:34   ` [PATCH] spi: Add option to bind spidev to all chipselects Michal Suchanek
2015-05-13 10:16   ` Maxime Ripard
2015-05-13 10:16     ` Maxime Ripard
2015-05-13 10:40     ` Michal Suchanek
2015-05-13 10:40       ` Michal Suchanek
2015-05-13 11:05   ` Mark Brown
2015-05-13 11:05     ` Mark Brown
2015-05-13 11:26 ` [PATCH] spi: Force the registration of the spidev devices Mark Brown
2015-05-13 11:26   ` Mark Brown
2015-05-13 12:35   ` Michal Suchanek
2015-05-13 12:35     ` Michal Suchanek
2015-05-13 12:51   ` Maxime Ripard
2015-05-13 12:51     ` Maxime Ripard
2015-05-13 14:36     ` Mark Brown
2015-05-13 14:36       ` Mark Brown
2015-05-13 15:31       ` Michal Suchanek
2015-05-13 15:31         ` Michal Suchanek
2015-05-13 17:43         ` Mark Brown
2015-05-13 17:43           ` Mark Brown
2015-05-13 19:09       ` Maxime Ripard
2015-05-13 19:10     ` Geert Uytterhoeven
2015-05-13 19:10       ` Geert Uytterhoeven
2015-05-13 19:41       ` Maxime Ripard
2015-05-13 19:41         ` Maxime Ripard
2015-05-13 15:37   ` Greg Kroah-Hartman
2015-05-13 15:37     ` Greg Kroah-Hartman
2015-05-13 15:52     ` Michal Suchanek
2015-05-13 15:52       ` Michal Suchanek
2015-05-13 17:13     ` Mark Brown
2015-05-13 17:13       ` Mark Brown
2015-05-13 17:20       ` Greg Kroah-Hartman
2015-05-13 17:20         ` Greg Kroah-Hartman
2015-05-13 17:39         ` Mark Brown
2015-05-13 17:39           ` Mark Brown
2015-05-13 18:16           ` Greg Kroah-Hartman
2015-05-13 18:16             ` Greg Kroah-Hartman
2015-05-13 18:32             ` Mark Brown
2015-05-13 18:36               ` Greg Kroah-Hartman
2015-05-13 18:36                 ` Greg Kroah-Hartman
2015-05-13 18:51                 ` Mark Brown
2015-05-13 18:51                   ` Mark Brown
2015-05-13 19:17                   ` Maxime Ripard
2015-05-13 19:17                     ` Maxime Ripard
2015-05-13 17:50     ` Maxime Ripard
2015-05-13 17:50       ` Maxime Ripard
2015-05-13 18:12       ` Mark Brown
2015-05-13 18:17       ` Greg Kroah-Hartman
2015-05-13 18:17         ` Greg Kroah-Hartman
2015-05-13 19:23         ` Geert Uytterhoeven
2015-05-13 19:23           ` Geert Uytterhoeven
2015-05-13 19:26         ` Maxime Ripard
2015-05-13 19:26           ` Maxime Ripard
2015-05-13 22:33           ` Greg Kroah-Hartman
2015-05-13 22:33             ` Greg Kroah-Hartman
2015-05-14 14:34             ` Mark Brown
2015-05-14 14:34               ` Mark Brown
2015-05-15  8:09             ` Maxime Ripard
2015-07-15  6:27             ` Lucas De Marchi
2015-07-15  6:27               ` Lucas De Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150513093441.80359.qmail@dec59.ruk.cuni.cz \
    --to=hramrach@gmail.com \
    --cc=broonie@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=hdegoede@redhat.com \
    --cc=kernel@martin.sperl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=maxime.ripard@free-electrons.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.