Skip to contentSkip to conversation

spi: enable spi_board_info to be registered after spi_master

PATCH Superseded Feng Tang 2 participants 2 messages Jul 28, 2010 – Aug 1, 2010 1 message not in the archive SPI

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Feng Tang

author
#0 lore raw

PATCH v2 1/1 spi: enable spi_board_info to be registered after spi_master

Currently spi_register_board_info() has to be called before its related spi_master be registered, otherwise these board info will be just ignored.

This patch will remove this order limit, it adds a global spi master list like the existing global board info listr. Whenever a board info is registered, the spi master list will be scanned, and a new spi device will be created if master and boardinfo match.

Cc David Brownell <[email protected]>
Cc Grant Likely <grant.likely-s3s/[email protected]>
---
 drivers/spi/spi.c       |   73 ++++++++++++++++++++++++++++++++++-------------
 include/linux/spi/spi.h |    3 ++
 2 files changed, 56 insertions(+), 20 deletions(-)
drivers/spi/spi.c +53 -20
index b3a1f92..1266fb2 100644
@@ -196,13 +196,16 @@ EXPORT_SYMBOL_GPL(spi_register_driver);
 
 struct boardinfo {
 	struct list_head	list;
-	unsigned		n_board_info;
-	struct spi_board_info	board_info[0];
+	struct spi_board_info	board_info;
 };
 
 static LIST_HEAD(board_list);
 static DEFINE_MUTEX(board_lock);
 
+
+static LIST_HEAD(spi_master_list);
+static DEFINE_MUTEX(spi_master_lock);
+
 /**
  * spi_alloc_device - Allocate a new SPI device
  * @master: Controller to which device is connected
@@ -365,6 +368,25 @@ struct spi_device *spi_new_device(struct spi_master *master,
 }
 EXPORT_SYMBOL_GPL(spi_new_device);
 
+static void spi_scan_masterlist(struct spi_board_info *bi)
+{
+	struct spi_master *master;
+	struct spi_device *dev;
+
+	mutex_lock(&spi_master_lock);
+	list_for_each_entry(master, &spi_master_list, list) {
+		if (master->bus_num != bi->bus_num)
+			continue;
+
+		dev = spi_new_device(master, bi);
+		if (!dev)
+			dev_err(master->dev.parent,
+				"can't create new device for %s\n",
+				bi->modalias);
+	}
+	mutex_unlock(&spi_master_lock);
+}
+
 /**
  * spi_register_board_info - register SPI devices for a given board
  * @info: array of chip descriptors
@@ -387,41 +409,45 @@ EXPORT_SYMBOL_GPL(spi_new_device);
 int __init
 spi_register_board_info(struct spi_board_info const *info, unsigned n)
 {
-	struct boardinfo	*bi;
+	struct boardinfo *bi, *tmp_bi;
+	int i;
 
-	bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL);
+	bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
 	if (!bi)
 		return -ENOMEM;
-	bi->n_board_info = n;
-	memcpy(bi->board_info, info, n * sizeof *info);
 
 	mutex_lock(&board_lock);
-	list_add_tail(&bi->list, &board_list);
+	for (i = 0, tmp_bi = bi; i < n; i++, tmp_bi++, info++) {
+		memcpy(&tmp_bi->board_info, info, sizeof(*info));
+		list_add_tail(&tmp_bi->list, &board_list);
+	}
 	mutex_unlock(&board_lock);
+
+	for (i = 0, tmp_bi = bi; i < n; i++, tmp_bi++)
+		spi_scan_masterlist(&tmp_bi->board_info);
+
 	return 0;
 }
 
 /* FIXME someone should add support for a __setup("spi", ...) that
  * creates board info from kernel command lines
  */
-
 static void scan_boardinfo(struct spi_master *master)
 {
 	struct boardinfo	*bi;
+	struct spi_device	*dev;
 
 	mutex_lock(&board_lock);
 	list_for_each_entry(bi, &board_list, list) {
-		struct spi_board_info	*chip = bi->board_info;
-		unsigned		n;
-
-		for (n = bi->n_board_info; n > 0; n--, chip++) {
-			if (chip->bus_num != master->bus_num)
-				continue;
-			/* NOTE: this relies on spi_new_device to
-			 * issue diagnostics when given bogus inputs
-			 */
-			(void) spi_new_device(master, chip);
-		}
+		struct spi_board_info	*chip = &bi->board_info;
+
+		if (chip->bus_num != master->bus_num)
+			continue;
+		dev = spi_new_device(master, chip);
+		if (!dev)
+			dev_err(master->dev.parent,
+				"can't create new device for %s\n",
+				chip->modalias);
 	}
 	mutex_unlock(&board_lock);
 }
@@ -537,15 +563,18 @@ int spi_register_master(struct spi_master *master)
 	dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
 			dynamic ? " (dynamic)" : "");
 
+	mutex_lock(&spi_master_lock);
+	list_add_tail(&master->list, &spi_master_list);
 	/* populate children from any spi device tables */
 	scan_boardinfo(master);
+	mutex_unlock(&spi_master_lock);
+
 	status = 0;
 done:
 	return status;
 }
 EXPORT_SYMBOL_GPL(spi_register_master);
 
-
 static int __unregister(struct device *dev, void *master_dev)
 {
 	/* note: before about 2.6.14-rc1 this would corrupt memory: */
@@ -568,6 +597,10 @@ void spi_unregister_master(struct spi_master *master)
 {
 	int dummy;
 
+	mutex_lock(&spi_master_lock);
+	list_del(&master->list);
+	mutex_unlock(&spi_master_lock);
+
 	dummy = device_for_each_child(master->dev.parent, &master->dev,
 					__unregister);
 	device_unregister(&master->dev);
include/linux/spi/spi.h +3 -0
index af56071..f4a29b6 100644
@@ -204,6 +204,7 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
 /**
  * struct spi_master - interface to SPI master controller
  * @dev: device interface to this driver
+ * @list: link with the global spi_master list
  * @bus_num: board-specific (and often SOC-specific) identifier for a
  *	given SPI controller.
  * @num_chipselect: chipselects are used to distinguish individual
@@ -235,6 +236,8 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
 struct spi_master {
 	struct device	dev;
 
+	struct list_head list;
+
 	/* other than negative (== assign one dynamically), bus_num is fully
 	 * board-specific.  usually that simplifies to being SOC-specific.
 	 * example:  one SOC has three SPI controllers, numbered 0..2,
Signature · 9 lines
1.7.0.4


------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share 
of $1 Million in cash or HP Products. Visit us here for more details:
http://ad.doubleclick.net/clk;226879339;13503038;l?
http://clk.atdmt.com/CRS/go/247765532/direct/01/
#2 lore raw
In reply to a message not in the archive
Quoted 87 lines · Feng Tang

Currently spi_register_board_info() has to be called before its related spi_master be registered, otherwise these board info will be just ignored.

This patch will remove this order limit, it adds a global spi master list like the existing global board info listr. Whenever a board info is registered, the spi master list will be scanned, and a new spi device will be created if master and boardinfo match.

Cc David Brownell <[email protected]>
Cc Grant Likely <grant.likely-s3s/[email protected]>

---
 drivers/spi/spi.c       |   73 ++++++++++++++++++++++++++++++++++------------- include/linux/spi/spi.h |    3 ++

 2 files changed, 56 insertions(+), 20 deletions(-)
drivers/spi/spi.c +24 -2
index b3a1f92..1266fb2 100644
@@ -196,13 +196,16 @@ EXPORT_SYMBOL_GPL(spi_register_driver);
 
  struct boardinfo {
        struct list_head        list;
-       unsigned                n_board_info;
-       struct spi_board_info   board_info[0];
+       struct spi_board_info   board_info;
  };
 
  static LIST_HEAD(board_list);
  static DEFINE_MUTEX(board_lock);
 
+
+static LIST_HEAD(spi_master_list);
+static DEFINE_MUTEX(spi_master_lock);
+
  /**
  * spi_alloc_device - Allocate a new SPI device
  * @master: Controller to which device is connected
@@ -365,6 +368,25 @@ struct spi_device *spi_new_device(struct spi_master *master,
  }
  EXPORT_SYMBOL_GPL(spi_new_device);
 
+static void spi_scan_masterlist(struct spi_board_info *bi)
+{
+       struct spi_master *master;
+       struct spi_device *dev;
+
+       mutex_lock(&spi_master_lock);
+       list_for_each_entry(master, &spi_master_list, list) {
+               if (master->bus_num != bi->bus_num)
+                       continue;
+
+               dev = spi_new_device(master, bi);
+               if (!dev)
+                       dev_err(master->dev.parent,
+                               "can't create new device for %s\n",
+                               bi->modalias);
+       }
+       mutex_unlock(&spi_master_lock);
+}
+
  /**
  * spi_register_board_info - register SPI devices for a given board
  * @info: array of chip descriptors
@@ -387,41 +409,45 @@ EXPORT_SYMBOL_GPL(spi_new_device);

 int __init
 spi_register_board_info(struct spi_board_info const *info, unsigned n) {
-       struct boardinfo        *bi;
+       struct boardinfo *bi, *tmp_bi;
+       int i;

-       bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL);
+       bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
       if (!bi)
               return -ENOMEM;
-       bi->n_board_info = n;
-       memcpy(bi->board_info, info, n * sizeof *info);

       mutex_lock(&board_lock);
-       list_add_tail(&bi->list, &board_list);
+       for (i = 0, tmp_bi = bi; i < n; i++, tmp_bi++, info++) {
+               memcpy(&tmp_bi->board_info, info, sizeof(*info));
+               list_add_tail(&tmp_bi->list, &board_list);
+       }
       mutex_unlock(&board_lock);

drivers/spi/spi.c EXPORT_SYMBOL_GPL(spi_new_device); · line 428

Quoted 4 lines
drivers/spi/spi.c +4 -0
+
+       for (i = 0, tmp_bi = bi; i < n; i++, tmp_bi++)
+               spi_scan_masterlist(&tmp_bi->board_info);
+

Race condition here also. If a master gets registered between releasing the lock and calling spi_scan_masterlist(), then the devices could again get registered twice. I still think a single mutex should be sufficient to protect both lists.

Quoted 93 lines

       return 0;
 }

 /* FIXME someone should add support for a __setup("spi", ...) that
 * creates board info from kernel command lines
 */
-
 static void scan_boardinfo(struct spi_master *master)
 {
       struct boardinfo        *bi;
+       struct spi_device       *dev;

       mutex_lock(&board_lock); list_for_each_entry(bi, &board_list, list) {
-               struct spi_board_info   *chip = bi->board_info;
-               unsigned                n;

drivers/spi/spi.c +9 -9
-
-               for (n = bi->n_board_info; n > 0; n--, chip++) {
-                       if (chip->bus_num != master->bus_num)
-                               continue;
-                       /* NOTE: this relies on spi_new_device to
-                        * issue diagnostics when given bogus inputs
-                        */
-                       (void) spi_new_device(master, chip);
-               }
+               struct spi_board_info   *chip = &bi->board_info;
+
+               if (chip->bus_num != master->bus_num)
+                       continue;
+               dev = spi_new_device(master, chip);
+               if (!dev)
+                       dev_err(master->dev.parent,
+                               "can't create new device for %s\n",
+                               chip->modalias);

       }
       mutex_unlock(&board_lock);
 }

drivers/spi/spi.c +4 -0
@@ -537,15 +563,18 @@ int spi_register_master(struct spi_master *master)
        dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
                        dynamic ? " (dynamic)" : "");
 
+       mutex_lock(&spi_master_lock);
+       list_add_tail(&master->list, &spi_master_list);
        /* populate children from any spi device tables */
        scan_boardinfo(master);
+       mutex_unlock(&spi_master_lock);
+
        status = 0;

 done:
       return status;
 }
 EXPORT_SYMBOL_GPL(spi_register_master);

-
 static int __unregister(struct device *dev, void *master_dev) {
       /* note: before about 2.6.14-rc1 this would corrupt memory: */

drivers/spi/spi.c +4 -0
@@ -568,6 +597,10 @@ void spi_unregister_master(struct spi_master *master)
  {
        int dummy;
 
+       mutex_lock(&spi_master_lock);
+       list_del(&master->list);
+       mutex_unlock(&spi_master_lock);
+
        dummy = device_for_each_child(master->dev.parent, &master->dev,
                                        __unregister);
        device_unregister(&master->dev);
include/linux/spi/spi.h +3 -1
index af56071..f4a29b6 100644
@@ -204,6 +204,7 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
  /**
  * struct spi_master - interface to SPI master controller
  * @dev: device interface to this driver
+ * @list: link with the global spi_master list
  * @bus_num: board-specific (and often SOC-specific) identifier for a
  *     given SPI controller.
  * @num_chipselect: chipselects are used to distinguish individual
@@ -235,6 +236,8 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
  struct spi_master {
        struct device   dev;
 
+       struct list_head list;
+
        /* other than negative (== assign one dynamically), bus_num is fully
         * board-specific.  usually that simplifies to being SOC-specific.
         * example:  one SOC has three SPI controllers, numbered 0..2,
--

1.7.0.4

Signature · 8 lines
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm