linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mike Frysinger <vapier@gentoo.org>
To: spi-devel-general@lists.sourceforge.net,
	David Brownell <dbrownell@users.sourceforge.net>
Cc: linux-kernel@vger.kernel.org,
	uclinux-dist-devel@blackfin.uclinux.org, Yi Li <yi.li@analog.com>
Subject: [PATCH 5/5] Blackfin SPI: clean up error handling in client setup
Date: Thu, 17 Sep 2009 19:47:53 -0400	[thread overview]
Message-ID: <1253231273-16423-5-git-send-email-vapier@gentoo.org> (raw)
In-Reply-To: <1253231273-16423-1-git-send-email-vapier@gentoo.org>

From: Yi Li <yi.li@analog.com>

Make sure we don't leak peripheral/gpio resources when an error occurs
during setup, and make sure we don't call kfree() twice on the same
object.  Also add/fix some error messages in the process.

Signed-off-by: Yi Li <yi.li@analog.com>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
---
 drivers/spi/spi_bfin5xx.c |   33 ++++++++++++++++++++++++++-------
 1 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/drivers/spi/spi_bfin5xx.c b/drivers/spi/spi_bfin5xx.c
index a8fc922..0dcc2ca 100644
--- a/drivers/spi/spi_bfin5xx.c
+++ b/drivers/spi/spi_bfin5xx.c
@@ -1118,8 +1118,10 @@ static int bfin_spi_setup(struct spi_device *spi)
 	chip = spi_get_ctldata(spi);
 	if (chip == NULL) {
 		chip = kzalloc(sizeof(*chip), GFP_KERNEL);
-		if (!chip)
+		if (!chip) {
+			dev_err(&spi->dev, "cannot allocate chip data\n");
 			return -ENOMEM;
+		}
 
 		chip->enable_dma = 0;
 		chip_info = spi->controller_data;
@@ -1210,7 +1212,7 @@ static int bfin_spi_setup(struct spi_device *spi)
 		/* register dma irq handler */
 		ret = request_dma(drv_data->dma_channel, "BFIN_SPI_DMA");
 		if (ret) {
-			dev_dbg(&spi->dev,
+			dev_err(&spi->dev,
 				"Unable to request BlackFin SPI DMA channel\n");
 			goto error;
 		}
@@ -1219,7 +1221,7 @@ static int bfin_spi_setup(struct spi_device *spi)
 		ret = set_dma_callback(drv_data->dma_channel,
 			bfin_spi_dma_irq_handler, drv_data);
 		if (ret) {
-			dev_dbg(&spi->dev, "Unable to set dma callback\n");
+			dev_err(&spi->dev, "Unable to set dma callback\n");
 			goto error;
 		}
 		dma_disable_irq(drv_data->dma_channel);
@@ -1229,7 +1231,7 @@ static int bfin_spi_setup(struct spi_device *spi)
 		ret = request_irq(drv_data->spi_irq, bfin_spi_pio_irq_handler,
 			IRQF_DISABLED, "BFIN_SPI", drv_data);
 		if (ret) {
-			printk(KERN_NOTICE "Unable to register spi IRQ\n");
+			dev_err(&spi->dev, "Unable to register spi IRQ\n");
 			goto error;
 		}
 		drv_data->irq_requested = 1;
@@ -1239,8 +1241,10 @@ static int bfin_spi_setup(struct spi_device *spi)
 
 	if (chip->chip_select_num == 0) {
 		ret = gpio_request(chip->cs_gpio, spi->modalias);
-		if (ret)
+		if (ret) {
+			dev_err(&spi->dev, "gpio_request() error\n");
 			goto error;
+		}
 		gpio_direction_output(chip->cs_gpio, 1);
 	}
 
@@ -1256,8 +1260,10 @@ static int bfin_spi_setup(struct spi_device *spi)
 	    chip->chip_select_num <= spi->master->num_chipselect) {
 		ret = peripheral_request(ssel[spi->master->bus_num]
 		                         [chip->chip_select_num-1], spi->modalias);
-		if (ret)
+		if (ret) {
+			dev_err(&spi->dev, "peripheral_request() error\n");
 			goto error;
+		}
 	}
 
 	bfin_spi_cs_deactive(drv_data, chip);
@@ -1265,10 +1271,21 @@ static int bfin_spi_setup(struct spi_device *spi)
 
  error:
 	if (ret) {
-		kfree(chip);
 		if (drv_data->dma_requested)
 			free_dma(drv_data->dma_channel);
 		drv_data->dma_requested = 0;
+
+		if ((chip->chip_select_num > 0)
+			&& (chip->chip_select_num <= spi->master->num_chipselect))
+			peripheral_free(ssel[spi->master->bus_num]
+						[chip->chip_select_num-1]);
+
+		if (chip->chip_select_num == 0)
+			gpio_free(chip->cs_gpio);
+
+		kfree(chip);
+		/* prevent free 'chip' twice */
+		spi_set_ctldata(spi, NULL);
 	}
 
 	return ret;
@@ -1294,6 +1311,8 @@ static void bfin_spi_cleanup(struct spi_device *spi)
 		gpio_free(chip->cs_gpio);
 
 	kfree(chip);
+	/* prevent free 'chip' twice */
+	spi_set_ctldata(spi, NULL);
 }
 
 static inline int bfin_spi_init_queue(struct driver_data *drv_data)
-- 
1.6.5.rc1

  parent reply	other threads:[~2009-09-17 23:47 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-17 23:47 [PATCH 1/5] Blackfin SPI: fix resources leakage Mike Frysinger
2009-09-17 23:47 ` [PATCH 2/5] Blackfin SPI: work around anomaly 05000119 Mike Frysinger
2009-09-17 23:47 ` [PATCH 3/5] Blackfin SPI: force sane state at boot Mike Frysinger
     [not found] ` <1253231273-16423-1-git-send-email-vapier-aBrp7R+bbdUdnm+yROfE0A@public.gmane.org>
2009-09-17 23:47   ` [PATCH 4/5] Blackfin SPI: utilize the SPI interrupt in PIO mode Mike Frysinger
2009-09-17 23:47 ` Mike Frysinger [this message]
     [not found]   ` <1253231273-16423-5-git-send-email-vapier-aBrp7R+bbdUdnm+yROfE0A@public.gmane.org>
2009-10-01  4:27     ` [PATCH 5/5] Blackfin SPI: clean up error handling in client setup Mike Frysinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1253231273-16423-5-git-send-email-vapier@gentoo.org \
    --to=vapier@gentoo.org \
    --cc=dbrownell@users.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=spi-devel-general@lists.sourceforge.net \
    --cc=uclinux-dist-devel@blackfin.uclinux.org \
    --cc=yi.li@analog.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).