Skip to contentSkip to conversation

serial: atmel: fix error handling when mctrl_gpio_init fails

PATCH New Uwe Kleine-König 2 participants 7 messages Feb 12, 2015 – Feb 27, 2015

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/5 serial: atmel: fix error handling when mctrl_gpio_init fails

mctrl_gpio_init at present doesn't return NULL. (It might be used in the future when no gpios are to be used indicating success.) Properly pass error returned and also make driver probing fail on error.

---
 drivers/tty/serial/atmel_serial.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/tty/serial/atmel_serial.c +6 -5
index 4d848a29e223..8a30342dfa7c 100644
@@ -2507,8 +2507,8 @@ static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev)
 	struct gpio_desc *gpiod;
 
 	p->gpios = mctrl_gpio_init(dev, 0);
-	if (IS_ERR_OR_NULL(p->gpios))
-		return -1;
+	if (IS_ERR(p->gpios))
+		return PTR_ERR(p->gpios);
 
 	for (i = 0; i < UART_GPIO_MAX; i++) {
 		gpiod = mctrl_gpio_to_gpiod(p->gpios, i);
@@ -2559,9 +2559,10 @@ static int atmel_serial_probe(struct platform_device *pdev)
 	port->uart.line = ret;
 
 	ret = atmel_init_gpios(port, &pdev->dev);
-	if (ret < 0)
-		dev_err(&pdev->dev, "%s",
-			"Failed to initialize GPIOs. The serial port may not work as expected");
+	if (ret < 0) {
+		dev_err(&pdev->dev, "Failed to initialize GPIOs.");
+		goto err;
+	}
 
 	ret = atmel_init_port(port, pdev);
 	if (ret)
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/5 serial: atmel: fix error handling when mctrl_gpio_init fails

Hello Greg,

probably it's just me being impatient, but I wonder about not getting any feedback for the several patches I sent for drivers/tty/serial.

Are you just not yet in patch-collecting mode for 4.1? Although the patches in this series might even be post-merge-window material.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-K?nig            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#2 lore raw

PATCH 1/5 serial: atmel: fix error handling when mctrl_gpio_init fails

Quoted 7 lines · Uwe Kleine-K?nig

Hello Greg,

probably it's just me being impatient, but I wonder about not getting any feedback for the several patches I sent for drivers/tty/serial.

Are you just not yet in patch-collecting mode for 4.1? Although the patches in this series might even be post-merge-window material.

I'm not in a patch-collecting mode for tty/serial drivers for 4.1 just yet, sorry, I hope to get to them next week.

thanks,

greg k-h

#3 lore raw

PATCH 2/5 serial: clps711x: fail if mctrl_gpio_init fails

In reply to Uwe Kleine-König

mctrl_gpio_init is fully aware of being optional. If it returns an error code this indicates a real error that must not be ignored.

---
 drivers/tty/serial/clps711x.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tty/serial/clps711x.c +2 -0
index 6e11c275f2ab..d5d2dd7c7917 100644
@@ -501,6 +501,8 @@ static int uart_clps711x_probe(struct platform_device *pdev)
 	platform_set_drvdata(pdev, s);
 
 	s->gpios = mctrl_gpio_init(&pdev->dev, 0);
+	if (IS_ERR(s->gpios))
+	    return PTR_ERR(s->gpios);
 
 	ret = uart_add_one_port(&clps711x_uart, &s->port);
 	if (ret)
Signature · 1 line
2.1.4
#4 lore raw

PATCH 3/5 serial: mxs-auart: properly handle mctrl_gpio failing

In reply to Uwe Kleine-König

If mctrl_gpio_init returns an error code this value should be forwarded and the driver must not simply ignore this failure.

---
 drivers/tty/serial/mxs-auart.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/tty/serial/mxs-auart.c +9 -7
index ec553f8eb218..14430394985d 100644
@@ -1157,14 +1157,14 @@ static int serial_mxs_probe_dt(struct mxs_auart_port *s,
 	return 0;
 }
 
-static bool mxs_auart_init_gpios(struct mxs_auart_port *s, struct device *dev)
+static int mxs_auart_init_gpios(struct mxs_auart_port *s, struct device *dev)
 {
 	enum mctrl_gpio_idx i;
 	struct gpio_desc *gpiod;
 
 	s->gpios = mctrl_gpio_init(dev, 0);
-	if (IS_ERR_OR_NULL(s->gpios))
-		return false;
+	if (IS_ERR(s->gpios))
+		return PTR_ERR(s->gpios);
 
 	/* Block (enabled before) DMA option if RTS or CTS is GPIO line */
 	if (!RTS_AT_AUART() || !CTS_AT_AUART()) {
@@ -1182,7 +1182,7 @@ static bool mxs_auart_init_gpios(struct mxs_auart_port *s, struct device *dev)
 			s->gpio_irq[i] = -EINVAL;
 	}
 
-	return true;
+	return 0;
 }
 
 static void mxs_auart_free_gpio_irq(struct mxs_auart_port *s)
@@ -1279,9 +1279,11 @@ static int mxs_auart_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, s);
 
-	if (!mxs_auart_init_gpios(s, &pdev->dev))
-		dev_err(&pdev->dev,
-			"Failed to initialize GPIOs. The serial port may not work as expected\n");
+	ret = mxs_auart_init_gpios(s, &pdev->dev);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to initialize GPIOs.\n");
+		got out_free_irq;
+	}
 
 	/*
 	 * Get the GPIO lines IRQ
Signature · 1 line
2.1.4
#5 lore raw

PATCH 4/5 serial: mctrl-gpio: don't check for struct mctrl_gpios * to be invalid

In reply to Uwe Kleine-König

Drivers using mctrl-gpio must not pass invalid values for struct mctrl_gpios *. All drivers were fixed in this regard and so some checks can go away or be simplified.

---
 drivers/tty/serial/serial_mctrl_gpio.c | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)
drivers/tty/serial/serial_mctrl_gpio.c +2 -18
index a38596c5194e..c0381a09f12d 100644
@@ -48,9 +48,6 @@ void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
 	int value_array[UART_GPIO_MAX];
 	unsigned int count = 0;
 
-	if (IS_ERR_OR_NULL(gpios))
-		return;
-
 	for (i = 0; i < UART_GPIO_MAX; i++)
 		if (!IS_ERR_OR_NULL(gpios->gpio[i]) &&
 		    mctrl_gpios_desc[i].dir_out) {
@@ -65,10 +62,7 @@ EXPORT_SYMBOL_GPL(mctrl_gpio_set);
 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
 				      enum mctrl_gpio_idx gidx)
 {
-	if (!IS_ERR_OR_NULL(gpios) && !IS_ERR_OR_NULL(gpios->gpio[gidx]))
-		return gpios->gpio[gidx];
-	else
-		return NULL;
+	return gpios->gpio[gidx];
 }
 EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
 
@@ -76,15 +70,8 @@ unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
 {
 	enum mctrl_gpio_idx i;
 
-	/*
-	 * return it unchanged if the structure is not allocated
-	 */
-	if (IS_ERR_OR_NULL(gpios))
-		return *mctrl;
-
 	for (i = 0; i < UART_GPIO_MAX; i++) {
-		if (!IS_ERR_OR_NULL(gpios->gpio[i]) &&
-		    !mctrl_gpios_desc[i].dir_out) {
+		if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) {
 			if (gpiod_get_value(gpios->gpio[i]))
 				*mctrl |= mctrl_gpios_desc[i].mctrl;
 			else
@@ -138,9 +125,6 @@ void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
 {
 	enum mctrl_gpio_idx i;
 
-	if (IS_ERR_OR_NULL(gpios))
-		return;
-
 	for (i = 0; i < UART_GPIO_MAX; i++)
 		if (!IS_ERR_OR_NULL(gpios->gpio[i]))
 			devm_gpiod_put(dev, gpios->gpio[i]);
Signature · 1 line
2.1.4
#6 lore raw

PATCH 5/5 serial: mctrl-gpio: simplify init routine

In reply to Uwe Kleine-König

Instead of ignoring errors returned by devm_gpiod_get_index use devm_gpiod_get_index_optional which results in slightly more strict error handling which is good.

Also use the fourth parameter to devm_gpiod_get_index_optional to be able to drop the explicit direction setting.

---
 drivers/tty/serial/serial_mctrl_gpio.c | 29 +++++++++++------------------
 1 file changed, 11 insertions(+), 18 deletions(-)
drivers/tty/serial/serial_mctrl_gpio.c +11 -18
index c0381a09f12d..5027db7b5814 100644
@@ -94,27 +94,20 @@ struct mctrl_gpios *mctrl_gpio_init(struct device *dev, unsigned int idx)
 		return ERR_PTR(-ENOMEM);
 
 	for (i = 0; i < UART_GPIO_MAX; i++) {
-		gpios->gpio[i] = devm_gpiod_get_index(dev,
-						      mctrl_gpios_desc[i].name,
-						      idx);
-
-		/*
-		 * The GPIOs are maybe not all filled,
-		 * this is not an error.
-		 */
-		if (IS_ERR_OR_NULL(gpios->gpio[i]))
-			continue;
+		enum gpiod_flags flags;
 
 		if (mctrl_gpios_desc[i].dir_out)
-			err = gpiod_direction_output(gpios->gpio[i], 0);
+			flags = GPIOD_OUT_LOW;
 		else
-			err = gpiod_direction_input(gpios->gpio[i]);
-		if (err) {
-			dev_dbg(dev, "Unable to set direction for %s GPIO",
-				mctrl_gpios_desc[i].name);
-			devm_gpiod_put(dev, gpios->gpio[i]);
-			gpios->gpio[i] = NULL;
-		}
+			flags = GPIOD_IN;
+
+		gpios->gpio[i] =
+			devm_gpiod_get_index_optional(dev,
+						      mctrl_gpios_desc[i].name,
+						      idx, flags);
+
+		if (IS_ERR(gpios->gpio[i]))
+			return PTR_ERR(gpios->gpio[i]);
 	}
 
 	return gpios;
Signature · 1 line
2.1.4