diff mbox

[v2,1/2] input: rotary-encoder: switch to devm_* allocation of GPIOs and IRQs

Message ID 1343202228-1507-1-git-send-email-zonque@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Daniel Mack July 25, 2012, 7:43 a.m. UTC
This simplifies the error unwind pathes.

Also, don't call gpio_to_irq() on GPIOs before dev_gpio_request_one()
succeeded on them. This avoids Ooopses with incorrect DT bindings.

Signed-off-by: Daniel Mack <zonque@gmail.com>
---
 drivers/input/misc/rotary_encoder.c |   67 +++++++++++------------------------
 1 file changed, 21 insertions(+), 46 deletions(-)

Comments

Dmitry Torokhov July 25, 2012, 4:05 p.m. UTC | #1
On Wed, Jul 25, 2012 at 09:43:47AM +0200, Daniel Mack wrote:
> @@ -263,12 +243,7 @@ exit_free_mem:
>  static int __devexit rotary_encoder_remove(struct platform_device *pdev)
>  {
>  	struct rotary_encoder *encoder = platform_get_drvdata(pdev);
> -	struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
>  
> -	free_irq(encoder->irq_a, encoder);
> -	free_irq(encoder->irq_b, encoder);
> -	gpio_free(pdata->gpio_a);
> -	gpio_free(pdata->gpio_b);
>  	input_unregister_device(encoder->input);

Another botched devm_ conversion. *sigh*

Input device gone, IRQ arrives, kernel goes oops, machine hangs hard.

Please, do not use devm_ interfaces unless... Actually, just do not use
nor suggest devm_interfaces until all resources are devm-ized. Mixing 2
styles of releasing resources leads to trouble.

Thanks.
Daniel Mack July 25, 2012, 4:07 p.m. UTC | #2
On 25.07.2012 18:05, Dmitry Torokhov wrote:
> On Wed, Jul 25, 2012 at 09:43:47AM +0200, Daniel Mack wrote:
>> @@ -263,12 +243,7 @@ exit_free_mem:
>>  static int __devexit rotary_encoder_remove(struct platform_device *pdev)
>>  {
>>  	struct rotary_encoder *encoder = platform_get_drvdata(pdev);
>> -	struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
>>  
>> -	free_irq(encoder->irq_a, encoder);
>> -	free_irq(encoder->irq_b, encoder);
>> -	gpio_free(pdata->gpio_a);
>> -	gpio_free(pdata->gpio_b);
>>  	input_unregister_device(encoder->input);
> 
> Another botched devm_ conversion. *sigh*
> 
> Input device gone, IRQ arrives, kernel goes oops, machine hangs hard.
> 
> Please, do not use devm_ interfaces unless... Actually, just do not use
> nor suggest devm_interfaces until all resources are devm-ized. Mixing 2
> styles of releasing resources leads to trouble.

Ok, makes sense. Thanks for noticing. Are you happy with the first
version I submitted then?


Daniel
--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Hartley Sweeten July 25, 2012, 4:07 p.m. UTC | #3
On Wednesday, July 25, 2012 9:05 AM, Dmitry Torokhov wrote:
> On Wed, Jul 25, 2012 at 09:43:47AM +0200, Daniel Mack wrote:
>> @@ -263,12 +243,7 @@ exit_free_mem:
>>  static int __devexit rotary_encoder_remove(struct platform_device *pdev)
>>  {
>>  	struct rotary_encoder *encoder = platform_get_drvdata(pdev);
>> -	struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
>>  
>> -	free_irq(encoder->irq_a, encoder);
>> -	free_irq(encoder->irq_b, encoder);
>> -	gpio_free(pdata->gpio_a);
>> -	gpio_free(pdata->gpio_b);
>>  	input_unregister_device(encoder->input);
>
> Another botched devm_ conversion. *sigh*
>
> Input device gone, IRQ arrives, kernel goes oops, machine hangs hard.
>
> Please, do not use devm_ interfaces unless... Actually, just do not use
> nor suggest devm_interfaces until all resources are devm-ized. Mixing 2
> styles of releasing resources leads to trouble.

Oops.. Sorry about that. I didn't think about the interaction with
input_unregister_device.

Regards,
Hartley

--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Dmitry Torokhov July 25, 2012, 4:29 p.m. UTC | #4
On Wed, Jul 25, 2012 at 06:07:24PM +0200, Daniel Mack wrote:
> On 25.07.2012 18:05, Dmitry Torokhov wrote:
> > On Wed, Jul 25, 2012 at 09:43:47AM +0200, Daniel Mack wrote:
> >> @@ -263,12 +243,7 @@ exit_free_mem:
> >>  static int __devexit rotary_encoder_remove(struct platform_device *pdev)
> >>  {
> >>  	struct rotary_encoder *encoder = platform_get_drvdata(pdev);
> >> -	struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
> >>  
> >> -	free_irq(encoder->irq_a, encoder);
> >> -	free_irq(encoder->irq_b, encoder);
> >> -	gpio_free(pdata->gpio_a);
> >> -	gpio_free(pdata->gpio_b);
> >>  	input_unregister_device(encoder->input);
> > 
> > Another botched devm_ conversion. *sigh*
> > 
> > Input device gone, IRQ arrives, kernel goes oops, machine hangs hard.
> > 
> > Please, do not use devm_ interfaces unless... Actually, just do not use
> > nor suggest devm_interfaces until all resources are devm-ized. Mixing 2
> > styles of releasing resources leads to trouble.
> 
> Ok, makes sense. Thanks for noticing. Are you happy with the first
> version I submitted then?

I have a few issues with that version as well:

1. It writes over platform_data field in the device which does nto
belong to the driver (this field belongs to the platform/board code);

2. I believe we should favor kernel supplied data over firmware
allowing users to override DT bindings, if needed;

3. It still uses devm_* to allocate memory and as I said I do not like
mixing 2 styles of managign resources in one driver.

Thanks.
Daniel Mack July 25, 2012, 6:04 p.m. UTC | #5
On 25.07.2012 18:29, Dmitry Torokhov wrote:
> On Wed, Jul 25, 2012 at 06:07:24PM +0200, Daniel Mack wrote:
>> On 25.07.2012 18:05, Dmitry Torokhov wrote:
>>> On Wed, Jul 25, 2012 at 09:43:47AM +0200, Daniel Mack wrote:
>>>> @@ -263,12 +243,7 @@ exit_free_mem:
>>>>  static int __devexit rotary_encoder_remove(struct platform_device *pdev)
>>>>  {
>>>>  	struct rotary_encoder *encoder = platform_get_drvdata(pdev);
>>>> -	struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
>>>>  
>>>> -	free_irq(encoder->irq_a, encoder);
>>>> -	free_irq(encoder->irq_b, encoder);
>>>> -	gpio_free(pdata->gpio_a);
>>>> -	gpio_free(pdata->gpio_b);
>>>>  	input_unregister_device(encoder->input);
>>>
>>> Another botched devm_ conversion. *sigh*
>>>
>>> Input device gone, IRQ arrives, kernel goes oops, machine hangs hard.
>>>
>>> Please, do not use devm_ interfaces unless... Actually, just do not use
>>> nor suggest devm_interfaces until all resources are devm-ized. Mixing 2
>>> styles of releasing resources leads to trouble.
>>
>> Ok, makes sense. Thanks for noticing. Are you happy with the first
>> version I submitted then?
> 
> I have a few issues with that version as well:
> 
> 1. It writes over platform_data field in the device which does nto
> belong to the driver (this field belongs to the platform/board code);
> 
> 2. I believe we should favor kernel supplied data over firmware
> allowing users to override DT bindings, if needed;

Ok, this is different from what I saw in other drivers, but I'm fine
with that.

> 3. It still uses devm_* to allocate memory and as I said I do not like
> mixing 2 styles of managign resources in one driver.

Ok, sorry again. I didn't closely follow that new development, hence I
wasn't aware of these pitfalls.

New patch series coming up.



--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c
index f07f784..d4e0abb 100644
--- a/drivers/input/misc/rotary_encoder.c
+++ b/drivers/input/misc/rotary_encoder.c
@@ -145,6 +145,7 @@  static int __devinit rotary_encoder_probe(struct platform_device *pdev)
 	struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
 	struct rotary_encoder *encoder;
 	struct input_dev *input;
+	struct device *dev = &pdev->dev;
 	irq_handler_t handler;
 	int err;
 
@@ -163,13 +164,11 @@  static int __devinit rotary_encoder_probe(struct platform_device *pdev)
 
 	encoder->input = input;
 	encoder->pdata = pdata;
-	encoder->irq_a = gpio_to_irq(pdata->gpio_a);
-	encoder->irq_b = gpio_to_irq(pdata->gpio_b);
 
 	/* create and register the input driver */
 	input->name = pdev->name;
 	input->id.bustype = BUS_HOST;
-	input->dev.parent = &pdev->dev;
+	input->dev.parent = dev;
 
 	if (pdata->relative_axis) {
 		input->evbit[0] = BIT_MASK(EV_REL);
@@ -182,38 +181,27 @@  static int __devinit rotary_encoder_probe(struct platform_device *pdev)
 
 	err = input_register_device(input);
 	if (err) {
-		dev_err(&pdev->dev, "failed to register input device\n");
+		dev_err(dev, "failed to register input device\n");
 		goto exit_free_mem;
 	}
 
 	/* request the GPIOs */
-	err = gpio_request(pdata->gpio_a, DRV_NAME);
+	err = devm_gpio_request_one(dev, pdata->gpio_a,
+				    GPIOF_IN, dev_name(dev));
 	if (err) {
-		dev_err(&pdev->dev, "unable to request GPIO %d\n",
-			pdata->gpio_a);
+		dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_a);
 		goto exit_unregister_input;
 	}
 
-	err = gpio_direction_input(pdata->gpio_a);
+	err = devm_gpio_request_one(dev, pdata->gpio_b,
+				    GPIOF_IN, dev_name(dev));
 	if (err) {
-		dev_err(&pdev->dev, "unable to set GPIO %d for input\n",
-			pdata->gpio_a);
+		dev_err(dev, "unable to request GPIO %d\n", pdata->gpio_b);
 		goto exit_unregister_input;
 	}
 
-	err = gpio_request(pdata->gpio_b, DRV_NAME);
-	if (err) {
-		dev_err(&pdev->dev, "unable to request GPIO %d\n",
-			pdata->gpio_b);
-		goto exit_free_gpio_a;
-	}
-
-	err = gpio_direction_input(pdata->gpio_b);
-	if (err) {
-		dev_err(&pdev->dev, "unable to set GPIO %d for input\n",
-			pdata->gpio_b);
-		goto exit_free_gpio_a;
-	}
+	encoder->irq_a = gpio_to_irq(pdata->gpio_a);
+	encoder->irq_b = gpio_to_irq(pdata->gpio_b);
 
 	/* request the IRQs */
 	if (pdata->half_period) {
@@ -223,34 +211,26 @@  static int __devinit rotary_encoder_probe(struct platform_device *pdev)
 		handler = &rotary_encoder_irq;
 	}
 
-	err = request_irq(encoder->irq_a, handler,
-			  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
-			  DRV_NAME, encoder);
+	err = devm_request_irq(dev, encoder->irq_a, handler,
+			       IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
+			       dev_name(dev), encoder);
 	if (err) {
-		dev_err(&pdev->dev, "unable to request IRQ %d\n",
-			encoder->irq_a);
-		goto exit_free_gpio_b;
+		dev_err(dev, "unable to request IRQ %d\n", encoder->irq_a);
+		goto exit_unregister_input;
 	}
 
-	err = request_irq(encoder->irq_b, handler,
-			  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
-			  DRV_NAME, encoder);
+	err = devm_request_irq(dev, encoder->irq_b, handler,
+			       IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
+			       dev_name(dev), encoder);
 	if (err) {
-		dev_err(&pdev->dev, "unable to request IRQ %d\n",
-			encoder->irq_b);
-		goto exit_free_irq_a;
+		dev_err(dev, "unable to request IRQ %d\n", encoder->irq_b);
+		goto exit_unregister_input;
 	}
 
 	platform_set_drvdata(pdev, encoder);
 
 	return 0;
 
-exit_free_irq_a:
-	free_irq(encoder->irq_a, encoder);
-exit_free_gpio_b:
-	gpio_free(pdata->gpio_b);
-exit_free_gpio_a:
-	gpio_free(pdata->gpio_a);
 exit_unregister_input:
 	input_unregister_device(input);
 	input = NULL; /* so we don't try to free it */
@@ -263,12 +243,7 @@  exit_free_mem:
 static int __devexit rotary_encoder_remove(struct platform_device *pdev)
 {
 	struct rotary_encoder *encoder = platform_get_drvdata(pdev);
-	struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
 
-	free_irq(encoder->irq_a, encoder);
-	free_irq(encoder->irq_b, encoder);
-	gpio_free(pdata->gpio_a);
-	gpio_free(pdata->gpio_b);
 	input_unregister_device(encoder->input);
 	platform_set_drvdata(pdev, NULL);
 	kfree(encoder);