spi: Fix zero length xfer bug
authorChris Lesiak <chris.lesiak@licor.com>
Thu, 7 Mar 2019 20:39:00 +0000 (20:39 +0000)
committerMark Brown <broonie@kernel.org>
Mon, 11 Mar 2019 16:40:00 +0000 (16:40 +0000)
This fixes a bug for messages containing both zero length and
unidirectional xfers.

The function spi_map_msg will allocate dummy tx and/or rx buffers
for use with unidirectional transfers when the hardware can only do
a bidirectional transfer.  That dummy buffer will be used in place
of a NULL buffer even when the xfer length is 0.

Then in the function __spi_map_msg, if he hardware can dma,
the zero length xfer will have spi_map_buf called on the dummy
buffer.

Eventually, __sg_alloc_table is called and returns -EINVAL
because nents == 0.

This fix prevents the error by not using the dummy buffer when
the xfer length is zero.

Signed-off-by: Chris Lesiak <chris.lesiak@licor.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/spi/spi.c

index 93986f879b09ef2a1e46a8ef4932bd8e0c608070..2be394d3bc592d68cebb16df67c319e9c08cccd8 100644 (file)
@@ -1039,6 +1039,8 @@ static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
                if (max_tx || max_rx) {
                        list_for_each_entry(xfer, &msg->transfers,
                                            transfer_list) {
+                               if (!xfer->len)
+                                       continue;
                                if (!xfer->tx_buf)
                                        xfer->tx_buf = ctlr->dummy_tx;
                                if (!xfer->rx_buf)