mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: <oskar.andero@sonyericsson.com>
To: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: "linux-input@vger.kernel.org" <linux-input@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"jic23@cam.ac.uk" <jic23@cam.ac.uk>,
	"aghayal@codeaurora.org" <aghayal@codeaurora.org>,
	"Cavin, Courtney" <Courtney.Cavin@sonyericsson.com>
Subject: Re: [PATCH v2] input: add driver support for Sharp gp2ap002a00f proximity sensor
Date: Tue, 15 Nov 2011 08:56:29 +0100	[thread overview]
Message-ID: <20111115075629.GE9307@caracas.corpusers.net> (raw)
In-Reply-To: <20111114170352.GA12659@core.coreip.homeip.net>

Hi again Dmitry,

Just a quick question before I post v3.

On 18:03 Mon 14 Nov     , Dmitry Torokhov wrote:
> Hi Courtney,
> 
> On Mon, Nov 14, 2011 at 04:39:18PM +0100, oskar.andero@sonyericsson.com wrote:
> > From: Courtney Cavin <courtney.cavin@sonyericsson.com>
> > 
> 
> The driver looks much better, a few more comments still.
> 
> > +
> > +#include <linux/i2c.h>
> > +#include <linux/irq.h>
> > +#include <linux/slab.h>
> > +#include <linux/input.h>
> > +#include <linux/module.h>
> > +#include <linux/workqueue.h>
> 
> As Shubhrajyoti mentioned workqueue.h is probably not needed.
> 
> > +#include <linux/interrupt.h>
> > +#include <linux/gpio.h>
> > +#include <linux/delay.h>
> > +#include <linux/input/gp2ap002a00f.h>
> > +
> > +struct gp2a_data {
> > +	struct input_dev *device;
> > +	const struct gp2a_platform_data *pdata;
> > +	struct i2c_client *i2c_client;
> > +};
> > +
> > +enum gp2a_addr {
> > +	GP2A_ADDR_PROX	= 0x0,
> > +	GP2A_ADDR_GAIN	= 0x1,
> > +	GP2A_ADDR_HYS	= 0x2,
> > +	GP2A_ADDR_CYCLE	= 0x3,
> > +	GP2A_ADDR_OPMOD	= 0x4,
> > +	GP2A_ADDR_CON	= 0x6
> > +};
> > +
> > +enum gp2a_controls {
> > +	GP2A_CTRL_SSD	= 0x01
> > +};
> > +
> > +static int gp2a_enable(struct gp2a_data *dt)
> > +{
> > +	return i2c_smbus_write_byte_data(dt->i2c_client, GP2A_ADDR_OPMOD,
> > +					 GP2A_CTRL_SSD);
> > +}
> > +
> > +static int gp2a_disable(struct gp2a_data *dt)
> > +{
> > +	return i2c_smbus_write_byte_data(dt->i2c_client, GP2A_ADDR_OPMOD,
> > +					 0x00);
> > +}
> > +
> > +static int __devinit gp2a_initialize(struct gp2a_data *dt)
> > +{
> > +	int error;
> > +
> > +	error = i2c_smbus_write_byte_data(dt->i2c_client, GP2A_ADDR_GAIN,
> > +					  0x08);
> > +	if (error < 0)
> > +		return error;
> > +
> > +	error = i2c_smbus_write_byte_data(dt->i2c_client, GP2A_ADDR_HYS,
> > +					  0xc2);
> > +	if (error < 0)
> > +		return error;
> > +
> > +	error = i2c_smbus_write_byte_data(dt->i2c_client, GP2A_ADDR_CYCLE,
> > +					  0x04);
> > +	if (error < 0)
> > +		return error;
> > +
> > +	error = gp2a_disable(dt);
> > +
> > +	return error;
> > +}
> > +
> > +static int gp2a_report(struct gp2a_data *dt)
> > +{
> > +	int vo = gpio_get_value(dt->pdata->vout_gpio);
> > +
> > +	input_report_key(dt->device, SW_FRONT_PROXIMITY, !vo);
> 
> Switch is not a key, so please use input_report_switch().
> 
> > +	input_sync(dt->device);
> > +
> > +	return 0;
> > +}
> > +
> > +static irqreturn_t gp2a_irq(int irq, void *handle)
> > +{
> > +	struct gp2a_data *dt = handle;
> > +
> > +	gp2a_report(dt);
> > +
> > +	return IRQ_HANDLED;
> > +}
> > +
> > +static int gp2a_device_open(struct input_dev *dev)
> > +{
> > +	struct gp2a_data *dt = input_get_drvdata(dev);
> > +	int error;
> > +
> > +	error = gp2a_enable(dt);
> > +	if (error < 0) {
> > +		dev_err(&dev->dev, "unable to activate, err %d\n", error);
> > +		return error;
> > +	}
> > +
> > +	gp2a_report(dt);
> > +
> > +	return 0;
> > +}
> > +
> > +static void gp2a_device_close(struct input_dev *dev)
> > +{
> > +	struct gp2a_data *dt = input_get_drvdata(dev);
> > +	int error;
> > +
> > +	error = gp2a_disable(dt);
> > +	if (error < 0)
> > +		dev_err(&dev->dev, "unable to deactivate, err %d\n", error);
> > +}
> > +
> > +static int __devinit gp2a_probe(struct i2c_client *client,
> > +				const struct i2c_device_id *id)
> > +{
> > +	const struct gp2a_platform_data *pdata;
> > +	struct gp2a_data *dt;
> > +	int error;
> > +
> > +	pdata = client->dev.platform_data;
> > +	if (!pdata)
> > +		return -EINVAL;
> > +
> > +	if (pdata->hw_setup) {
> > +		error = pdata->hw_setup(client);
> > +		if (error < 0)
> > +			return error;
> > +	}
> > +
> > +	error = gpio_direction_input(pdata->vout_gpio);
> > +	if (error < 0)
> > +		goto err_hw_shutdown;
> > +
> > +	dt = kzalloc(sizeof(struct gp2a_data), GFP_KERNEL);
> > +	if (!dt) {
> > +		error = -ENOMEM;
> > +		goto err_hw_shutdown;
> > +	}
> > +
> > +	dt->pdata = pdata;
> > +	dt->i2c_client = client;
> > +	i2c_set_clientdata(client, dt);
> > +
> > +	error = gp2a_initialize(dt);
> > +	if (error < 0)
> > +		goto err_free_dt;
> > +
> > +	dt->device = input_allocate_device();
> > +	if (!dt->device) {
> > +		error = -ENOMEM;
> > +		goto err_free_dt;
> > +	}
> > +	input_set_drvdata(dt->device, dt);
> > +
> > +	error = request_threaded_irq(client->irq, NULL,
> > +			gp2a_irq, IRQF_TRIGGER_RISING |
> > +			IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
> > +			GP2A_I2C_NAME, dt);
> > +	if (error) {
> > +		dev_err(&dt->device->dev, "irq request failed\n");
> 
> You are leaking dt->device here. Please add a separate label for
> input_free_device(dt->device).
> 
> > +		goto err_free_dt;
> > +	}
> > +
> > +	dt->device->open = gp2a_device_open;
> > +	dt->device->close = gp2a_device_close;
> > +	dt->device->name = GP2A_I2C_NAME;
> > +	dt->device->id.bustype = BUS_I2C;
> > +
> > +	input_set_capability(dt->device, EV_KEY, SW_FRONT_PROXIMITY);
> 
> Should be EV_SW instead of EV_KEY.
> 
> > +
> > +	error = input_register_device(dt->device);
> > +	if (error) {
> > +		dev_err(&dt->device->dev, "device registration failed\n");
> > +		input_free_device(dt->device);
> 
> If you swap request_threaded_irq() and input_register_device() so that
> registration is the last action error handling will be much simpler.
>

I am getting a bit confused here, since you asked me to swap the order
of request_threaded_irq() and input_register_device() in my previous
version as well. Swapping again will take us back to square one or maybe
I am misinterpreting your comment?

Anyways, this is what I am planning to do for v3:

error = input_register_device(dt->device);
if (error) {
	...
	goto err_free_dt;
}
...
error = request_threaded_irq(client->irq, NULL, ...
if (error) {
	...
	goto err_free_unregister;
}
...
err_free_unregister:
        input_unregister_device(dt->device);
err_free_dt:
        kfree(dt);
err_hw_shutdown:
        if (pdata->hw_shutdown)
                pdata->hw_shutdown(client);
        return error;

The above takes care of the leaking input_device and keeps the error handling
pretty compact.
Does this sound reasonable?

-Oskar

  parent reply	other threads:[~2011-11-15  7:55 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-14 15:39 oskar.andero
2011-11-14 17:03 ` Dmitry Torokhov
2011-11-15  7:34   ` oskar.andero
2011-11-16 15:39     ` anish kumar
2011-11-16 15:56       ` oskar.andero
2011-11-16 16:20         ` anish kumar
2011-11-16 16:50       ` Dmitry Torokhov
2011-11-15  7:56   ` oskar.andero [this message]
2011-11-15  8:29     ` Dmitry Torokhov
2011-11-15  8:34       ` Felipe Balbi
2011-11-15  8:46         ` Dmitry Torokhov
2011-11-15  8:47           ` Felipe Balbi
     [not found] ` <CAM=Q2cvH4gb5+=+ijE_d_ejoMCPikqd4g4uSw9T-z2yqDZCN+Q@mail.gmail.com>
2011-11-15  6:50   ` oskar.andero

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=20111115075629.GE9307@caracas.corpusers.net \
    --to=oskar.andero@sonyericsson.com \
    --cc=Courtney.Cavin@sonyericsson.com \
    --cc=aghayal@codeaurora.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=jic23@cam.ac.uk \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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

Powered by JetHome