linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: Mark Locascio <maldata@gmail.com>
Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski <brgl@bgdev.pl>
Subject: Re: [libgpiod] Either a bug or a misunderstanding regarding bulk operations
Date: Fri, 30 Sep 2022 10:53:19 +0800	[thread overview]
Message-ID: <YzZaHzKyOPc1WFz4@sol> (raw)
In-Reply-To: <CA+8QDpKHEm6UZBRUSsuo+K62MROLcLRqbOZnB9D6qionsD5j5Q@mail.gmail.com>

On Thu, Sep 29, 2022 at 08:13:36AM -0500, Mark Locascio wrote:
>   Hi,
> 
> I was working with libgpiod and I noticed some unintuitive behavior. I
> can't say for sure that it's a bug, but at the very least, it's
> confusing, and I want to get clarification. Sample code is attached.
> 
> Let's say I have four lines that I want to use as outputs. I can
> request them all at once like this:
> 
> struct gpiod_chip* chip = gpiod_chip_open("/dev/gpiochip0");
> struct gpiod_line_bulk lines;
> uint32_t offsets[NUMOUTPUTS] = {6, 13, 19, 26};
> int get_lines_result = gpiod_chip_get_lines(chip, offsets, NUMOUTPUTS, &lines);
> int32_t values[NUMOUTPUTS] = {0, 0, 0, 0};
> int set_dir_result = gpiod_line_request_bulk_output(&lines, "testgpio", values);
> 
> And then when I want to set new values, I can update the "values"
> array and then do:
> 
> int set_result = gpiod_line_set_value_bulk(&lines, values);
> 
> This works exactly as expected. However, I need to maintain an array
> of the current values of all the output lines. If I didn't want to do
> that, perhaps because I was wrapping this in a function, I thought it
> could make sense to get a single line object from the bulk object, and
> then just set that one value.
> 

The line bulk are "Convenience data structures and helper functions for
storing and operating on multiple lines at once."

They are intended to operate on all the requested lines at once, e.g. so
the lines on a bus can be switched at the same time (or as close as
the kernel can manage).
If you would rather treat the lines separately then request them
separately.

AIUI, the purpose of gpiod_line_bulk_get_line() is to allow the user to
access the results of a bulk operation on a per-line basis, not to allow
them to perform per-line operations on the bulk.  The kernel API requires
that the operations for a bulk are on the bulk, and libgpiod v1 doesn't
keep track of the values of the other lines, so when you perform
operations on the single line returned by gpiod_line_bulk_get_line()
libgpiod assumes that it is a single line operation and only sets one
value - which the kernel sees as being for the first line in the bulk.
The remaining lines are zeroed.
Not sure if that is a bug or a feature, but that is my understanding.

libgpiod v2 [1](a WIP) changes this part of the API completely, and IIRC
does allow sets on individual lines in a bulk (now referred to as a line
request), by tracking the requested values for all lines in the bulk.
I'm sure Bart would consider any patches for v1 that you would care to
offer, though at this stage I would lean towards documentation
clarifications over code changes.

Cheers,
Kent.

[1] https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git -
next/libgpiod-2.0 branch

      reply	other threads:[~2022-09-30  2:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-29 13:13 [libgpiod] Either a bug or a misunderstanding regarding bulk operations Mark Locascio
2022-09-30  2:53 ` Kent Gibson [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=YzZaHzKyOPc1WFz4@sol \
    --to=warthog618@gmail.com \
    --cc=brgl@bgdev.pl \
    --cc=linux-gpio@vger.kernel.org \
    --cc=maldata@gmail.com \
    /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).