]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/spi/spi.c
spi: core and gpio expanders use subsys_init
[linux-2.6-omap-h63xx.git] / drivers / spi / spi.c
index 964124b60db2560b12f15d08dbdeeff43a058e16..3734dc9708e12669070ab984cc2fe9f0fbee1b70 100644 (file)
@@ -226,10 +226,11 @@ EXPORT_SYMBOL_GPL(spi_alloc_device);
  * 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; non-zero on failure
+ * Returns 0 on success; negative errno on failure
  */
 int spi_add_device(struct spi_device *spi)
 {
+       static DEFINE_MUTEX(spi_add_lock);
        struct device *dev = spi->master->dev.parent;
        int status;
 
@@ -246,26 +247,43 @@ int spi_add_device(struct spi_device *spi)
                        "%s.%u", spi->master->dev.bus_id,
                        spi->chip_select);
 
-       /* drivers may modify this initial i/o setup */
+
+       /* We need to make sure there's no other device with this
+        * chipselect **BEFORE** we call setup(), else we'll trash
+        * its configuration.  Lock against concurrent add() calls.
+        */
+       mutex_lock(&spi_add_lock);
+
+       if (bus_find_device_by_name(&spi_bus_type, NULL, spi->dev.bus_id)
+                       != NULL) {
+               dev_err(dev, "chipselect %d already in use\n",
+                               spi->chip_select);
+               status = -EBUSY;
+               goto done;
+       }
+
+       /* 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->master->setup(spi);
        if (status < 0) {
                dev_err(dev, "can't %s %s, status %d\n",
                                "setup", spi->dev.bus_id, status);
-               return status;
+               goto done;
        }
 
-       /* driver core catches callers that misbehave by defining
-        * devices that already exist.
-        */
+       /* Device may be bound to an active driver when this returns */
        status = device_add(&spi->dev);
-       if (status < 0) {
+       if (status < 0)
                dev_err(dev, "can't %s %s, status %d\n",
                                "add", spi->dev.bus_id, status);
-               return status;
-       }
+       else
+               dev_dbg(dev, "registered child %s\n", spi->dev.bus_id);
 
-       dev_dbg(dev, "registered child %s\n", spi->dev.bus_id);
-       return 0;
+done:
+       mutex_unlock(&spi_add_lock);
+       return status;
 }
 EXPORT_SYMBOL_GPL(spi_add_device);
 
@@ -642,7 +660,7 @@ int spi_write_then_read(struct spi_device *spi,
 
        int                     status;
        struct spi_message      message;
-       struct spi_transfer     x[2];
+       struct spi_transfer     x;
        u8                      *local_buf;
 
        /* Use preallocated DMA-safe buffer.  We can't avoid copying here,
@@ -653,15 +671,9 @@ int spi_write_then_read(struct spi_device *spi,
                return -EINVAL;
 
        spi_message_init(&message);
-       memset(x, 0, sizeof x);
-       if (n_tx) {
-               x[0].len = n_tx;
-               spi_message_add_tail(&x[0], &message);
-       }
-       if (n_rx) {
-               x[1].len = n_rx;
-               spi_message_add_tail(&x[1], &message);
-       }
+       memset(&x, 0, sizeof x);
+       x.len = n_tx + n_rx;
+       spi_message_add_tail(&x, &message);
 
        /* ... unless someone else is using the pre-allocated buffer */
        if (!mutex_trylock(&lock)) {
@@ -672,15 +684,15 @@ int spi_write_then_read(struct spi_device *spi,
                local_buf = buf;
 
        memcpy(local_buf, txbuf, n_tx);
-       x[0].tx_buf = local_buf;
-       x[1].rx_buf = local_buf + n_tx;
+       x.tx_buf = local_buf;
+       x.rx_buf = local_buf;
 
        /* do the i/o */
        status = spi_sync(spi, &message);
        if (status == 0)
-               memcpy(rxbuf, x[1].rx_buf, n_rx);
+               memcpy(rxbuf, x.rx_buf + n_tx, n_rx);
 
-       if (x[0].tx_buf == buf)
+       if (x.tx_buf == buf)
                mutex_unlock(&lock);
        else
                kfree(local_buf);
@@ -726,5 +738,5 @@ err0:
  * driver registration) _could_ be dynamically linked (modular) ... costs
  * include needing to have boardinfo data structures be much more public.
  */
-subsys_initcall(spi_init);
+postcore_initcall(spi_init);