Re: [PATCH] spi: Fix warning for Clang build

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Feb 11, 2022 at 04:59:00AM +0000, Lh Kuo 郭力豪 wrote:
> Yes. I think the function can be simplified as follows
> 
> static int sp7021_spi_slave_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
> 				       struct spi_transfer *xfer)
> {
> 	struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
> 	struct device *dev = pspim->dev;
> 	int ret;
> 
> 	mode = SP7021_SPI_IDLE;
> 	if (xfer->tx_buf && xfer->rx_buf) {
> 		dev_dbg(&ctlr->dev, "%s() wrong command\n", __func__);
> 		return -EINVAL;
> 	} else if (xfer->tx_buf) {
> 		xfer->tx_dma = dma_map_single(dev, (void *)xfer->tx_buf,
> 					      xfer->len, DMA_TO_DEVICE);
> 		if (dma_mapping_error(dev, xfer->tx_dma))
> 			return -ENOMEM;
> 		ret = sp7021_spi_slave_tx(spi, xfer);
> 	} else if (xfer->rx_buf) {
> 		xfer->rx_dma = dma_map_single(dev, xfer->rx_buf, xfer->len,
> 					      DMA_FROM_DEVICE);
> 		if (dma_mapping_error(dev, xfer->rx_dma))
> 			return -ENOMEM;
> 		ret = sp7021_spi_slave_rx(spi, xfer);
> 	}
> 
> 	if (xfer->tx_buf)
> 		dma_unmap_single(dev, xfer->tx_dma, xfer->len, DMA_TO_DEVICE);
> 	if (xfer->rx_buf)
> 		dma_unmap_single(dev, xfer->rx_dma, xfer->len, DMA_FROM_DEVICE);
> 
> 	spi_finalize_current_transfer(ctlr);
> 	return ret;
> }

Clang will still warn that ret is uninitialized when the else if
branches are not taken.

How about something like:

static int sp7021_spi_slave_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
				       struct spi_transfer *xfer)
{
	struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
	struct device *dev = pspim->dev;
	int ret;

	if (xfer->tx_buf && !xfer->rx_buf) {
		xfer->tx_dma = dma_map_single(dev, (void *)xfer->tx_buf,
					      xfer->len, DMA_TO_DEVICE);
		if (dma_mapping_error(dev, xfer->tx_dma))
			return -ENOMEM;
		ret = sp7021_spi_slave_tx(spi, xfer);
		dma_unmap_single(dev, xfer->tx_dma, xfer->len, DMA_TO_DEVICE);
	} else if (xfer->rx_buf && !xfer->tx_buf) {
		xfer->rx_dma = dma_map_single(dev, xfer->rx_buf, xfer->len,
					      DMA_FROM_DEVICE);
		if (dma_mapping_error(dev, xfer->rx_dma))
			return -ENOMEM;
		ret = sp7021_spi_slave_rx(spi, xfer);
		dma_unmap_single(dev, xfer->rx_dma, xfer->len, DMA_FROM_DEVICE);
	} else {
		dev_dbg(&ctlr->dev, "%s() wrong command\n", __func__);
		return -EINVAL;
	}

	spi_finalize_current_transfer(ctlr);
	return ret;
}



[Index of Archives]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux