LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Ryan Mallon <ryan@bluewatersys.com>
To: Grant Likely <grant.likely@secretlab.ca>
Cc: Thomas Chou <thomas@wytron.com.tw>,
	David Brownell <dbrownell@users.sourceforge.net>,
	linux-kernel@vger.kernel.org, nios2-dev@sopc.et.ntust.edu.tw,
	devicetree-discuss@lists.ozlabs.org,
	spi-devel-general@lists.sourceforge.net
Subject: Re: [PATCH v9] spi: New driver for Altera SPI
Date: Wed, 16 Feb 2011 10:58:41 +1300	[thread overview]
Message-ID: <4D5AF711.3000508@bluewatersys.com> (raw)
In-Reply-To: <20110215194257.GB7649@angua.secretlab.ca>

On 16/02/11 08:42, Grant Likely wrote:
> On Tue, Feb 15, 2011 at 03:04:49PM +0800, Thomas Chou wrote:
>> Dear Ryan,
>>
>> On 02/14/2011 10:20 AM, Ryan Mallon wrote:
>>> On 02/14/2011 03:10 PM, Thomas Chou wrote:
>>>> This patch adds a new SPI driver to support the Altera SOPC Builder
>>>> SPI component. It uses the bitbanging library.
>>>>
>>>> Signed-off-by: Thomas Chou<thomas@wytron.com.tw>
>>>> ---
>>>> +struct altera_spi {
>>>> +	/* bitbang has to be first */
>>>> +	struct spi_bitbang bitbang;
>>> Is this still true? I had a quick look and can't see anything which
>>> relies on spi_bitbang being the first entry. Things like this should be
>>> using container_of so that position in the struct is irrelevant.
>>>
>>> ~Ryan
>>>
>> Yes, sadly true. This is due to the implementation of the bitbanging
>> library, spi_bitbang.c, which assumes the struct spi_bitbang is the
>> first of drvdata. Though it could be changed in the future (beyond
>> this little driver), every bitbanging library user has to follow
>> this for now.
> Should be easy to fix if it is indeed still true (I haven't dug deep
> enough to find the design error yet).  Anybody want to volunteer?
>
> g.
>
The problem is that spi_master_get_devdata is used to get both 
struct spi_bitbang and the controller dependent structure, which means
that struct spi_bitbang must be the first entry in the container
structure.

The following incomplete, untested patch shows a possible way to fix
this by introducing spi_alloc_master_bitbang. spi_master_get_devdata
now returns a pointer to struct spi_bitbang only. Drivers which need
an additional container struct should allocate it themselves and 
pass the bitbang field to spi_alloc_master_bitbang. The driver 
specific container can be fetched with:

static inline struct my_spi *to_my_spi(struct spi_master *master)
{
	struct spi_bitbang *bitbang = spi_master_get_devdata(master);
	return container_of(bitbang, struct my_spi, bitbang);
} 

This patch only adds the alloc function and shows the necessary
changes to the spi_gpio.c driver, which is actually pretty minimal.
If there is any interest I can code up a proper patch.

~Ryan

---
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 34bb17f..52ec691 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -27,6 +27,7 @@
 #include <linux/slab.h>
 #include <linux/mod_devicetable.h>
 #include <linux/spi/spi.h>
+#include <linux/spi/spi_bitbang.h>
 #include <linux/of_spi.h>
 #include <linux/pm_runtime.h>
 
@@ -560,6 +561,24 @@ struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
 }
 EXPORT_SYMBOL_GPL(spi_alloc_master);
 
+struct spi_master *spi_alloc_master_bitbang(struct device *dev,
+					    struct spi_bitbang *bitbang)
+{
+	struct spi_master *master;
+
+	master = kzalloc(sizeof(struct spi_master), GFP_KERNEL);
+	if (!master)
+		return NULL;
+
+	device_initialize(&master->dev);
+	master->dev.class = &spi_master_class;
+	master->dev.parent = get_device(dev);
+	spi_master_set_devdata(master, bitbang);
+
+	return master;
+}
+EXPORT_SYMBOL_GPL(spi_alloc_master_bitbang);
+
 /**
  * spi_register_master - register SPI master controller
  * @master: initialized master, originally from spi_alloc_master()
diff --git a/drivers/spi/spi_gpio.c b/drivers/spi/spi_gpio.c
index 63e51b0..169dbf0 100644
--- a/drivers/spi/spi_gpio.c
+++ b/drivers/spi/spi_gpio.c
@@ -329,12 +329,17 @@ static int __init spi_gpio_probe(struct platform_device *pdev)
 	if (status < 0)
 		return status;
 
-	master = spi_alloc_master(&pdev->dev, sizeof *spi_gpio);
-	if (!master) {
+	spi_gpio = kzalloc(sizeof(struct spi_gpio), GFP_KERNEL);
+	if (!spi_gpio) {
 		status = -ENOMEM;
 		goto gpio_free;
 	}
-	spi_gpio = spi_master_get_devdata(master);
+
+	master = spi_alloc_master_bitbang(&pdev->dev, &spi_gpio->bitbang);
+	if (!master) {
+		status = -ENOMEM;
+		goto spi_gpio_free;
+	}
 	platform_set_drvdata(pdev, spi_gpio);
 
 	spi_gpio->pdev = pdev;
@@ -367,6 +372,8 @@ static int __init spi_gpio_probe(struct platform_device *pdev)
 	status = spi_bitbang_start(&spi_gpio->bitbang);
 	if (status < 0) {
 		spi_master_put(spi_gpio->bitbang.master);
+spi_gpio_free:
+		kfree(spi_gpio);
 gpio_free:
 		if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
 			gpio_free(SPI_MISO_GPIO);
@@ -391,6 +398,7 @@ static int __exit spi_gpio_remove(struct platform_device *pdev)
 	/* stop() unregisters child devices too */
 	status = spi_bitbang_stop(&spi_gpio->bitbang);
 	spi_master_put(spi_gpio->bitbang.master);
+	kfree(spi_gpio);
 
 	platform_set_drvdata(pdev, NULL);
 
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index b4d7710..3f72a34 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -336,7 +336,8 @@ static inline void spi_master_put(struct spi_master *master)
 /* the spi driver core manages memory for the spi_master classdev */
 extern struct spi_master *
 spi_alloc_master(struct device *host, unsigned size);
-
+extern struct spi_master *
+spi_alloc_master_bitbang(struct device *host, struct spi_bitbang *bitbang);
 extern int spi_register_master(struct spi_master *master);
 extern void spi_unregister_master(struct spi_master *master);
 



  reply	other threads:[~2011-02-15 21:59 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-08  6:03 [PATCH] " Thomas Chou
2011-01-17  5:46 ` [PATCH v2] " Thomas Chou
2011-01-17  5:59   ` Stephen Rothwell
2011-01-17  6:22     ` Thomas Chou
2011-01-17  6:45       ` Grant Likely
2011-01-17  7:04         ` [PATCH v3] " Thomas Chou
2011-01-17  7:27           ` Sinan Akman
2011-01-18  1:33             ` Thomas Chou
2011-01-19  1:36             ` Thomas Chou
2011-01-17  6:56   ` [PATCH v2] " Grant Likely
2011-01-17  7:13     ` [PATCH v4] " Thomas Chou
2011-01-17  7:42       ` Grant Likely
2011-01-24  6:38         ` [PATCH v5] " Thomas Chou
2011-02-03 10:02           ` [PATCH v6] " Thomas Chou
2011-02-03 17:34             ` Grant Likely
2011-02-05 13:08               ` Thomas Chou
2011-02-05 14:02               ` [PATCH v7] " Thomas Chou
2011-02-06 20:23                 ` Peter Korsgaard
2011-02-08  2:43                   ` Thomas Chou
2011-02-08  5:21                   ` [PATCH v8] " Thomas Chou
2011-02-14  2:10                     ` [PATCH v9] " Thomas Chou
2011-02-14  2:20                       ` Ryan Mallon
2011-02-15  7:04                         ` Thomas Chou
2011-02-15  9:12                           ` Ryan Mallon
2011-02-15 19:42                           ` Grant Likely
2011-02-15 21:58                             ` Ryan Mallon [this message]
2011-02-16  2:47                       ` Grant Likely

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=4D5AF711.3000508@bluewatersys.com \
    --to=ryan@bluewatersys.com \
    --cc=dbrownell@users.sourceforge.net \
    --cc=devicetree-discuss@lists.ozlabs.org \
    --cc=grant.likely@secretlab.ca \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nios2-dev@sopc.et.ntust.edu.tw \
    --cc=spi-devel-general@lists.sourceforge.net \
    --cc=thomas@wytron.com.tw \
    --subject='Re: [PATCH v9] spi: New driver for Altera SPI' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).