linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jean Delvare <khali-PUYAD+kWke1g9hUCZPvPmw@public.gmane.org>
To: Benjamin Tissoires
	<benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Cc: Jiri Kosina <jkosina-AlSwsSmVLrQ@public.gmane.org>,
	linux-input-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [PATCH 10/14] HID: i2c-hid: reorder allocation/free of buffers
Date: Wed, 5 Dec 2012 11:10:04 +0100	[thread overview]
Message-ID: <20121205111004.4c18e29e@endymion.delvare> (raw)
In-Reply-To: <1354634875-5182-11-git-send-email-benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>

On Tue,  4 Dec 2012 16:27:51 +0100, Benjamin Tissoires wrote:
> Simplifies i2c_hid_alloc_buffers tests, and makes this function
> responsible of the assignment of ihid->bufsize.
> The condition for the reallocation in i2c_hid_start is then simpler.
> 
> Signed-off-by: Benjamin Tissoires <benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> ---
>  drivers/hid/i2c-hid/i2c-hid.c | 68 ++++++++++++++++++-------------------------
>  1 file changed, 28 insertions(+), 40 deletions(-)
> 
> diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c
> index dcacfc4..4e3f80b 100644
> --- a/drivers/hid/i2c-hid/i2c-hid.c
> +++ b/drivers/hid/i2c-hid/i2c-hid.c
> @@ -465,48 +465,38 @@ static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
>  	}
>  }
>  
> -static int i2c_hid_alloc_buffers(struct i2c_hid *ihid)
> +static void i2c_hid_free_buffers(struct i2c_hid *ihid)
> +{
> +	kfree(ihid->inbuf);
> +	kfree(ihid->argsbuf);
> +	kfree(ihid->cmdbuf);
> +	ihid->inbuf = NULL;
> +	ihid->cmdbuf = NULL;
> +	ihid->argsbuf = NULL;
> +	ihid->bufsize = 0;
> +}
> +
> +static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
>  {
>  	/* the worst case is computed from the set_report command with a
>  	 * reportID > 15 and the maximum report length */
>  	int args_len = sizeof(__u8) + /* optional ReportID byte */
>  		       sizeof(__u16) + /* data register */
>  		       sizeof(__u16) + /* size of the report */
> -		       ihid->bufsize; /* report */
> -
> -	ihid->inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
> -
> -	if (!ihid->inbuf)
> -		return -ENOMEM;
> +		       report_size; /* report */
>  
> +	ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
>  	ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
> -
> -	if (!ihid->argsbuf) {
> -		kfree(ihid->inbuf);
> -		return -ENOMEM;
> -	}
> -
>  	ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
>  
> -	if (!ihid->cmdbuf) {
> -		kfree(ihid->inbuf);
> -		kfree(ihid->argsbuf);
> -		ihid->inbuf = NULL;
> -		ihid->argsbuf = NULL;
> +	if (!ihid->inbuf || !ihid->argsbuf || !ihid->cmdbuf) {
> +		i2c_hid_free_buffers(ihid);
>  		return -ENOMEM;
>  	}
>  
> -	return 0;
> -}
> +	ihid->bufsize = report_size;
>  
> -static void i2c_hid_free_buffers(struct i2c_hid *ihid)
> -{
> -	kfree(ihid->inbuf);
> -	kfree(ihid->argsbuf);
> -	kfree(ihid->cmdbuf);
> -	ihid->inbuf = NULL;
> -	ihid->cmdbuf = NULL;
> -	ihid->argsbuf = NULL;
> +	return 0;
>  }
>  
>  static int i2c_hid_get_raw_report(struct hid_device *hid,
> @@ -611,22 +601,19 @@ static int i2c_hid_start(struct hid_device *hid)
>  	struct i2c_client *client = hid->driver_data;
>  	struct i2c_hid *ihid = i2c_get_clientdata(client);
>  	int ret;
> -	int old_bufsize = ihid->bufsize;
> +	unsigned int bufsize = HID_MIN_BUFFER_SIZE;
>  
> -	ihid->bufsize = HID_MIN_BUFFER_SIZE;
> -	i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &ihid->bufsize);
> -	i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &ihid->bufsize);
> -	i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &ihid->bufsize);
> +	i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
> +	i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
> +	i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
>  
> -	if (ihid->bufsize > old_bufsize || !ihid->inbuf || !ihid->cmdbuf) {
> +	if (bufsize > ihid->bufsize) {
>  		i2c_hid_free_buffers(ihid);
>  
> -		ret = i2c_hid_alloc_buffers(ihid);
> +		ret = i2c_hid_alloc_buffers(ihid, bufsize);
>  
> -		if (ret) {
> -			ihid->bufsize = old_bufsize;
> +		if (ret)
>  			return ret;
> -		}
>  	}
>  
>  	if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
> @@ -844,8 +831,9 @@ static int __devinit i2c_hid_probe(struct i2c_client *client,
>  	/* we need to allocate the command buffer without knowing the maximum
>  	 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
>  	 * real computation later. */
> -	ihid->bufsize = HID_MIN_BUFFER_SIZE;
> -	i2c_hid_alloc_buffers(ihid);
> +	ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
> +	if (ret < 0)
> +		goto err;
>  
>  	ret = i2c_hid_fetch_hid_descriptor(ihid);
>  	if (ret < 0)

Yes, looks much better.

Reviewed-by: Jean Delvare <khali-PUYAD+kWke1g9hUCZPvPmw@public.gmane.org>

-- 
Jean Delvare

  parent reply	other threads:[~2012-12-05 10:10 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-04 15:27 [PATCH 00/14] i2c-hid cleanup and bug fixes Benjamin Tissoires
     [not found] ` <1354634875-5182-1-git-send-email-benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2012-12-04 15:27   ` [PATCH 01/14] HID: i2c-hid: change I2C name Benjamin Tissoires
2012-12-04 16:07     ` Jean Delvare
2012-12-05  9:52       ` Jiri Kosina
2012-12-04 15:27   ` [PATCH 12/14] HID: i2c-hid: remove extra .irq field in struct i2c_hid Benjamin Tissoires
2012-12-05 10:29     ` Jean Delvare
2012-12-04 15:27 ` [PATCH 02/14] HID: i2c-hid: fix memory corruption due to missing hid declaration Benjamin Tissoires
     [not found]   ` <1354634875-5182-3-git-send-email-benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2012-12-04 21:42     ` Jean Delvare
     [not found]       ` <20121204224205.58420e3f-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2012-12-05 10:10         ` Benjamin Tissoires
     [not found]           ` <CAN+gG=Hk+BCexxUif2Eqt9sQ4-_K1dfiUx5xD52-Hj2k5hUPug-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-12-05 10:13             ` Jiri Kosina
2012-12-04 15:27 ` [PATCH 03/14] HID: i2c-hid: enhance Kconfig Benjamin Tissoires
2012-12-04 21:43   ` Jean Delvare
2012-12-05  9:55     ` Jiri Kosina
2012-12-04 15:27 ` [PATCH 04/14] HID: i2c-hid: fix checkpatch.pl warning Benjamin Tissoires
2012-12-04 21:43   ` Jean Delvare
     [not found]     ` <20121204224349.2cefa121-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2012-12-05  9:55       ` Jiri Kosina
2012-12-04 15:27 ` [PATCH 05/14] HID: i2c-hid: fix i2c_hid_dbg macro Benjamin Tissoires
     [not found]   ` <1354634875-5182-6-git-send-email-benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2012-12-04 21:49     ` Jean Delvare
     [not found]       ` <20121204224950.6ebd7346-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2012-12-05  9:56         ` Jiri Kosina
2012-12-04 15:27 ` [PATCH 06/14] HID: i2c-hid: remove unused static declarations Benjamin Tissoires
2012-12-04 21:51   ` Jean Delvare
     [not found]     ` <20121204225132.2b7b6f6f-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2012-12-05 10:03       ` Jiri Kosina
2012-12-04 15:27 ` [PATCH 07/14] HID: i2c-hid: fix return paths Benjamin Tissoires
2012-12-05  9:47   ` Jean Delvare
2012-12-05 10:05   ` Jiri Kosina
2012-12-04 15:27 ` [PATCH 08/14] HID: i2c-hid: fix error messages Benjamin Tissoires
     [not found]   ` <1354634875-5182-9-git-send-email-benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2012-12-05  9:51     ` Jean Delvare
     [not found]       ` <20121205105158.666a19a8-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2012-12-05 10:07         ` Jiri Kosina
2012-12-04 15:27 ` [PATCH 09/14] HID: i2c-hid: i2c_hid_get_report may fail Benjamin Tissoires
2012-12-05  9:59   ` Jean Delvare
2012-12-05 10:07     ` Benjamin Tissoires
     [not found]   ` <1354634875-5182-10-git-send-email-benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2012-12-05 10:28     ` Jiri Kosina
2012-12-04 15:27 ` [PATCH 10/14] HID: i2c-hid: reorder allocation/free of buffers Benjamin Tissoires
     [not found]   ` <1354634875-5182-11-git-send-email-benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2012-12-05 10:10     ` Jean Delvare [this message]
2012-12-05 10:12       ` Benjamin Tissoires
2012-12-04 15:27 ` [PATCH 11/14] HID: i2c-hid: remove unneeded test in i2c_hid_remove Benjamin Tissoires
2012-12-05 10:13   ` Jean Delvare
2012-12-05 10:30     ` Jiri Kosina
2012-12-04 15:27 ` [PATCH 13/14] HID: i2c-hid: also call i2c_hid_free_buffers " Benjamin Tissoires
2012-12-05 10:27   ` Jiri Kosina
2012-12-05 10:32   ` Jean Delvare
2012-12-04 15:27 ` [PATCH 14/14] HID: i2c-hid: fix i2c_hid_get_raw_report count mismatches Benjamin Tissoires
2012-12-05 10:40   ` Benjamin Tissoires

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=20121205111004.4c18e29e@endymion.delvare \
    --to=khali-puyad+kwke1g9huczpvpmw@public.gmane.org \
    --cc=benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=jkosina-AlSwsSmVLrQ@public.gmane.org \
    --cc=linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-input-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.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;
as well as URLs for NNTP newsgroup(s).