linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Coiby Xu <coiby.xu@gmail.com>
To: "Barnabás Pőcze" <pobrn@protonmail.com>
Cc: "linux-input@vger.kernel.org" <linux-input@vger.kernel.org>,
	Helmut Stult <helmut.stult@schinfo.de>,
	"stable@vger.kernel.org" <stable@vger.kernel.org>,
	Jiri Kosina <jikos@kernel.org>,
	Benjamin Tissoires <benjamin.tissoires@redhat.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v3] HID: i2c-hid: add polling mode based on connected GPIO chip's pin status
Date: Wed, 25 Nov 2020 21:18:14 +0800	[thread overview]
Message-ID: <20201125131814.qkaqidk7mdavr2nd@Rk> (raw)
In-Reply-To: <_1j4GSFZpZ-rAOrhM2TQwyID7K4XCCkKwLeIcFMxQ1vlFg6wr544L5Lcrp7BvpsMmkhMYsTUT3yTTM61J7aVTYmGMSddkrz244_uV0gg9mU=@protonmail.com>

On Wed, Nov 25, 2020 at 12:39:02PM +0000, Barnabás Pőcze wrote:
>2020. november 25., szerda 11:57 keltezéssel, Coiby Xu írta:
>
>> On Mon, Nov 23, 2020 at 04:32:40PM +0000, Barnabás Pőcze wrote:
>> >> [...]
>> >> >> >> +static int get_gpio_pin_state(struct irq_desc *irq_desc)
>> >> >> >> +{
>> >> >> >> +	struct gpio_chip *gc = irq_data_get_irq_chip_data(&irq_desc->irq_data);
>> >> >> >> +
>> >> >> >> +	return gc->get(gc, irq_desc->irq_data.hwirq);
>> >> >> >> +}
>> >> >> [...]
>> >> >> >> +	ssize_t	status = get_gpio_pin_state(irq_desc);
>> >> >> >
>> >> >> >`get_gpio_pin_state()` returns an `int`, so I am not sure why `ssize_t` is used here.
>> >> >> >
>> >> >>
>> >> >> I used `ssize_t` because I found gpiolib-sysfs.c uses `ssize_t`
>> >> >>
>> >> >>      // drivers/gpio/gpiolib-sysfs.c
>> >> >>      static ssize_t value_show(struct device *dev,
>> >> >>      		struct device_attribute *attr, char *buf)
>> >> >>      {
>> >> >>      	struct gpiod_data *data = dev_get_drvdata(dev);
>> >> >>      	struct gpio_desc *desc = data->desc;
>> >> >>      	ssize_t			status;
>> >> >>
>> >> >>      	mutex_lock(&data->mutex);
>> >> >>
>> >> >>      	status = gpiod_get_value_cansleep(desc);
>> >> >>          ...
>> >> >>      	return status;
>> >> >>      }
>> >> >>
>> >> >> According to the book Advanced Programming in the UNIX Environment by
>> >> >> W. Richard Stevens,
>> >> >>      With the 1990 POSIX.1 standard, the primitive system data type
>> >> >>      ssize_t was introduced to provide the signed return value...
>> >> >>
>> >> >> So ssize_t is fairly common, for example, the read and write syscall
>> >> >> return a value of type ssize_t. But I haven't found out why ssize_t is
>> >> >> better int.
>> >> >> >
>> >> >
>> >> >Sorry if I wasn't clear, what prompted me to ask that question is the following:
>> >> >`gc->get()` returns `int`, `get_gpio_pin_state()` returns `int`, yet you still
>> >> >save the return value of `get_gpio_pin_state()` into a variable with type
>> >> >`ssize_t` for no apparent reason. In the example you cited, `ssize_t` is used
>> >> >because the show() callback of a sysfs attribute must return `ssize_t`, but here,
>> >> >`interrupt_line_active()` returns `bool`, so I don't see any advantage over a
>> >> >plain `int`. Anyways, I believe either one is fine, I just found it odd.
>> >> >
>> >> I don't understand why "the show() callback of a sysfs attribute
>> >> must return `ssize_t`" instead of int. Do you think the rationale
>> >> behind it is the same for this case? If yes, using "ssize_t" for
>> >> status could be justified.
>> >> [...]
>> >
>> >Because it was decided that way, `ssize_t` is a better choice for that purpose
>> >than plain `int`. You can see it in include/linux/device.h, that both the
>> >show() and store() methods must return `ssize_t`.
>> >
>>
>> Could you explain why `ssize_t` is a better choice? AFAIU, ssize_t
>> is used because we can return negative value to indicate an error.
>
>ssize_t: "Signed integer type used for a count of bytes or an error indication."[1]
>
>And POSIX mandates that the return type of read() and write() be `ssize_t`,
>so it makes sense to keep a similar interface in the kernel since show() and store()
>are called as a direct result of the user using the read() and write() system
>calls, respectively.
>
>
>> If
>> we use ssize_t here, it's a reminder that reading a GPIO pin's status
>> could fail. And ssize_t reminds us it's a operation similar to read
>> or write. So ssize_t is better than int here. And maybe it's the same
>> reason why "it was decided that way".
>> [...]
>
>I believe it's more appropriate to use ssize_t when it's about a "count of elements",
>but the GPIO pin state is a single boolean value (or an error indication), which
>is returned as an `int`. Since it's returned as an `int` - I'm arguing that -
>there is no reason to use `ssize_t` here. Anyways, both `ssize_t` and `int` work fine
>in this case.
>
So value_show in gpiolib-sysfs.c is a kind of being forced to use
ssize_t. I'll use int instead to avoid confusion in v4. Thank you for
the explanation!
>
>[1]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_12
>
>
>Regards,
>Barnabás Pőcze

--
Best regards,
Coiby

      reply	other threads:[~2020-11-25 13:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-21 13:49 [PATCH v3] HID: i2c-hid: add polling mode based on connected GPIO chip's pin status Coiby Xu
2020-10-22 14:22 ` Barnabás Pőcze
2020-11-22 10:15   ` Coiby Xu
2020-11-22 13:33     ` Barnabás Pőcze
2020-11-23 14:36       ` Coiby Xu
2020-11-23 16:32         ` Barnabás Pőcze
2020-11-25 10:57           ` Coiby Xu
2020-11-25 12:39             ` Barnabás Pőcze
2020-11-25 13:18               ` Coiby Xu [this message]

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=20201125131814.qkaqidk7mdavr2nd@Rk \
    --to=coiby.xu@gmail.com \
    --cc=benjamin.tissoires@redhat.com \
    --cc=helmut.stult@schinfo.de \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pobrn@protonmail.com \
    --cc=stable@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;
as well as URLs for NNTP newsgroup(s).