spi: Remove SPI_BUFSIZ restriction on spi_write_then_read()
authorMark Brown <broonie@opensource.wolfsonmicro.com>
Sun, 2 Dec 2012 03:54:25 +0000 (12:54 +0900)
committerGrant Likely <grant.likely@secretlab.ca>
Thu, 6 Dec 2012 14:03:14 +0000 (14:03 +0000)
In order to avoid constantly allocating and deallocating there is a fixed
buffer which spi_write_then_read() uses for transfers, with an early error
check to ensure that the transfer fits within the buffer. This limits the
size of transfers to this size, currently max(32, SMP_CACHE_BYTES).

Since we can dynamically allocate and in fact already have a fallback
to do so when there is contention for the fixed buffer remove this
restriction and instead dynamically allocate a suitably sized buffer if
the transfer won't fit.

Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
drivers/spi/spi.c

index f1217ae59f3d4c90c3abac0186e498320bc22206..22c71e238fdf0444b90955a2af9370de835a311e 100644 (file)
@@ -1545,12 +1545,18 @@ int spi_write_then_read(struct spi_device *spi,
        struct spi_transfer     x[2];
        u8                      *local_buf;
 
-       /* Use preallocated DMA-safe buffer.  We can't avoid copying here,
-        * (as a pure convenience thing), but we can keep heap costs
-        * out of the hot path ...
+       /* Use preallocated DMA-safe buffer if we can.  We can't avoid
+        * copying here, (as a pure convenience thing), but we can
+        * keep heap costs out of the hot path unless someone else is
+        * using the pre-allocated buffer or the transfer is too large.
         */
-       if ((n_tx + n_rx) > SPI_BUFSIZ)
-               return -EINVAL;
+       if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
+               local_buf = kmalloc(max(SPI_BUFSIZ, n_tx + n_rx), GFP_KERNEL);
+               if (!local_buf)
+                       return -ENOMEM;
+       } else {
+               local_buf = buf;
+       }
 
        spi_message_init(&message);
        memset(x, 0, sizeof x);
@@ -1563,14 +1569,6 @@ int spi_write_then_read(struct spi_device *spi,
                spi_message_add_tail(&x[1], &message);
        }
 
-       /* ... unless someone else is using the pre-allocated buffer */
-       if (!mutex_trylock(&lock)) {
-               local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
-               if (!local_buf)
-                       return -ENOMEM;
-       } else
-               local_buf = buf;
-
        memcpy(local_buf, txbuf, n_tx);
        x[0].tx_buf = local_buf;
        x[1].rx_buf = local_buf + n_tx;