Commit Graph

58 Commits

Author SHA1 Message Date
Steve Best 4f261b4a8b gpiolib: cdev: Fix use after free in lineinfo_changed_notify
JIRA: https://issues.redhat.com/browse/RHEL-39849
CVE: CVE-2024-36899

Build Info:  https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=61800387
Tested: Did sanity boot testing Intel (intel-arrowlake-s-02) system.

commit 02f6b0e1ec7e0e7d059dddc893645816552039da
Author: Zhongqiu Han <quic_zhonhan@quicinc.com>
Date:   Sun May 5 22:11:56 2024 +0800

    gpiolib: cdev: Fix use after free in lineinfo_changed_notify

    The use-after-free issue occurs as follows: when the GPIO chip device file
    is being closed by invoking gpio_chrdev_release(), watched_lines is freed
    by bitmap_free(), but the unregistration of lineinfo_changed_nb notifier
    chain failed due to waiting write rwsem. Additionally, one of the GPIO
    chip's lines is also in the release process and holds the notifier chain's
    read rwsem. Consequently, a race condition leads to the use-after-free of
    watched_lines.

    Here is the typical stack when issue happened:

    [free]
    gpio_chrdev_release()
      --> bitmap_free(cdev->watched_lines)                  <-- freed
      --> blocking_notifier_chain_unregister()
        --> down_write(&nh->rwsem)                          <-- waiting rwsem
              --> __down_write_common()
                --> rwsem_down_write_slowpath()
                      --> schedule_preempt_disabled()
                        --> schedule()

    [use]
    st54spi_gpio_dev_release()
      --> gpio_free()
        --> gpiod_free()
          --> gpiod_free_commit()
            --> gpiod_line_state_notify()
              --> blocking_notifier_call_chain()
                --> down_read(&nh->rwsem);                  <-- held rwsem
                --> notifier_call_chain()
                  --> lineinfo_changed_notify()
                    --> test_bit(xxxx, cdev->watched_lines) <-- use after free

    The side effect of the use-after-free issue is that a GPIO line event is
    being generated for userspace where it shouldn't. However, since the chrdev
    is being closed, userspace won't have the chance to read that event anyway.

    To fix the issue, call the bitmap_free() function after the unregistration
    of lineinfo_changed_nb notifier chain.

    Fixes: 51c1064e82 ("gpiolib: add new ioctl() for monitoring changes in line info")
    Signed-off-by: Zhongqiu Han <quic_zhonhan@quicinc.com>
    Link: https://lore.kernel.org/r/20240505141156.2944912-1-quic_zhonhan@quicinc.com
    Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Signed-off-by: Steve Best <sbest@redhat.com>
2024-06-09 08:27:53 -04:00
David Arcari 95e62caba5 treewide: rename pinctrl_gpio_can_use_line_new()
JIRA: https://issues.redhat.com/browse/RHEL-26354

commit 00762e416cd2001270a59fab417fbb1c30e2b7e5
Author: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Date:   Tue Oct 3 11:54:45 2023 +0200

    treewide: rename pinctrl_gpio_can_use_line_new()

    Now that pinctrl_gpio_can_use_line() is no longer used, let's drop the
    '_new' suffix from its improved variant.

    Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
    Acked-by: Linus Walleij <linus.walleij@linaro.org>

Signed-off-by: David Arcari <darcari@redhat.com>
2024-02-29 07:29:02 -05:00
David Arcari 18957641a2 gpio: cdev: use pinctrl_gpio_can_use_line_new()
JIRA: https://issues.redhat.com/browse/RHEL-26354

commit 32fb7d23e76a4a89f9796c81dfde91b3d84257ef
Author: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Date:   Tue Oct 3 09:15:14 2023 +0200

    gpio: cdev: use pinctrl_gpio_can_use_line_new()

    Use the improved variant of pinctrl_gpio_can_use_line() which takes a
    pointer to the gpio_chip and a controller-relative offset.

    Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
    Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
    Acked-by: Linus Walleij <linus.walleij@linaro.org>

Signed-off-by: David Arcari <darcari@redhat.com>
2024-02-29 07:29:02 -05:00
Izabela Bakollari b4226c9abf gpiolib: provide and use gpiod_line_state_notify()
JIRA: https://issues.redhat.com/browse/RHEL-946

commit 9ce4ed5b4db13633f9934a4eac02c4cde04d5c63
Author: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Date:   Mon Aug 21 16:18:27 2023 +0200

    gpiolib: provide and use gpiod_line_state_notify()

    Wrap the calls to blocking_notifier_call_chain() for the line state
    notifier with a helper that allows us to use fewer lines of code and
    simpler syntax.

    Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
    Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Signed-off-by: Izabela Bakollari <ibakolla@redhat.com>
2024-01-09 12:02:24 +01:00
Izabela Bakollari 5fac72d8e7 gpiolib: add a second blocking notifier to struct gpio_device
JIRA: https://issues.redhat.com/browse/RHEL-946

commit a067419ba77da830939852758702388f0fba09a1
Author: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Date:   Thu Aug 17 13:59:05 2023 +0200

    gpiolib: add a second blocking notifier to struct gpio_device

    Add a new blocking notifier to struct gpio_device and use it to notify
    subscribers about the GPIO device being unregistered from the device
    model.

    Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
    Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
    Reviewed-by: Kent Gibson <warthog618@gmail.com>

Signed-off-by: Izabela Bakollari <ibakolla@redhat.com>
2024-01-09 12:02:24 +01:00
Izabela Bakollari 102a61193d gpiolib: rename the gpio_device notifier
JIRA: https://issues.redhat.com/browse/RHEL-946

commit 17a7ca35890b411414a71fbeef13cb65fe9511df
Author: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Date:   Thu Aug 17 10:52:28 2023 +0200

    gpiolib: rename the gpio_device notifier

    Change the generic "notifier" name to "line_state_notifier" in order to
    reflect its purpose in preparation for adding a second notifier which
    will be used to notify wait queues about device unregistering.

    Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
    Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
    Reviewed-by: Kent Gibson <warthog618@gmail.com>

Signed-off-by: Izabela Bakollari <ibakolla@redhat.com>
2024-01-09 12:02:24 +01:00
Shaoqin Huang d538f8c6c1 gpiolib: Introduce gpio_device_get() and gpio_device_put()
Bugzilla: https://bugzilla.redhat.com/2183344

commit dc0989e3aa58dc424a18baf85639248ce9baf818
Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Date:   Wed Dec 28 11:20:43 2022 +0200

    gpiolib: Introduce gpio_device_get() and gpio_device_put()

    Introduce gpio_device_get() and gpio_device_put() helpers
    and convert existing users.

    Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
    Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
    Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:50:48 -04:00
Shaoqin Huang 05a8baf639 gpiolib: protect the GPIO device against being dropped while in use by user-space
Bugzilla: https://bugzilla.redhat.com/2183344

commit bdbbae241a04f387ba910b8609f95fad5f1470c7
Author: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Date:   Mon Dec 5 13:39:03 2022 +0100

    gpiolib: protect the GPIO device against being dropped while in use by user-space

    While any of the GPIO cdev syscalls is in progress, the kernel can call
    gpiochip_remove() (for instance, when a USB GPIO expander is disconnected)
    which will set gdev->chip to NULL after which any subsequent access will
    cause a crash.

    To avoid that: use an RW-semaphore in which the syscalls take it for
    reading (so that we don't needlessly prohibit the user-space from calling
    syscalls simultaneously) while gpiochip_remove() takes it for writing so
    that it can only happen once all syscalls return.

    Fixes: d7c51b47ac ("gpio: userspace ABI for reading/writing GPIO lines")
    Fixes: 3c0d9c635a ("gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL")
    Fixes: aad955842d ("gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL")
    Fixes: a54756cb24 ("gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL")
    Fixes: 7b8e00d981 ("gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL")
    Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
    [Nick: fixed a build failure with CDEV_V1 disabled]
    Co-authored-by: Nick Hainke <vincent@systemli.org>
    Reviewed-by: Kent Gibson <warthog618@gmail.com>
    Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
    Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:50:47 -04:00
Shaoqin Huang 330beaae77 gpiolib: cdev: fix NULL-pointer dereferences
Bugzilla: https://bugzilla.redhat.com/2183344

commit 533aae7c94dbc2b14301cfd68ae7e0e90f0c8438
Author: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Date:   Mon Dec 5 13:39:02 2022 +0100

    gpiolib: cdev: fix NULL-pointer dereferences

    There are several places where we can crash the kernel by requesting
    lines, unbinding the GPIO device, then calling any of the system calls
    relevant to the GPIO character device's annonymous file descriptors:
    ioctl(), read(), poll().

    While I observed it with the GPIO simulator, it will also happen for any
    of the GPIO devices that can be hot-unplugged - for instance any HID GPIO
    expander (e.g. CP2112).

    This affects both v1 and v2 uAPI.

    This fixes it partially by checking if gdev->chip is not NULL but it
    doesn't entirely remedy the situation as we still have a race condition
    in which another thread can remove the device after the check.

    Fixes: d7c51b47ac ("gpio: userspace ABI for reading/writing GPIO lines")
    Fixes: 3c0d9c635a ("gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL")
    Fixes: aad955842d ("gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL")
    Fixes: a54756cb24 ("gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL")
    Fixes: 7b8e00d981 ("gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL")
    Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
    Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
    Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:50:46 -04:00
Shaoqin Huang 35a398df44 gpiolib: cdev: Fix typo in kernel doc for struct line
Bugzilla: https://bugzilla.redhat.com/2183344

commit 8d259847243d1e21a866e828c4ce90d759f3d17b
Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Date:   Thu Oct 20 18:39:14 2022 +0300

    gpiolib: cdev: Fix typo in kernel doc for struct line

    When eflags has been renamed to the edflags, the kernel doc change were
    missed. Update kernel doc accordingly.

    Fixes: b1a92e94560d ("gpiolib: cdev: consolidate edge detector configuration flags")
    Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
    Reviewed-by: Kent Gibson <warthog618@gmail.com>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:50:44 -04:00
Shaoqin Huang 425124253b gpiolib: cdev: Add missing header(s)
Bugzilla: https://bugzilla.redhat.com/2183344

commit 52ee7c02f67808afa533c523fa3e4b66c54ea758
Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Date:   Fri Oct 7 16:44:44 2022 +0300

    gpiolib: cdev: Add missing header(s)

    Do not imply that some of the generic headers may be always included.
    Instead, include explicitly what we are direct user of.

    While at it, sort headers alphabetically.

    Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
    Rewiewed-by: Kent Gibson <warthog618@gmail.com>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:50:43 -04:00
Shaoqin Huang 9995347e2a gpiolib: cdev: add fdinfo output for line request file descriptors
Bugzilla: https://bugzilla.redhat.com/2183344

commit 0ae3109a839192920f09338e9abc4d5321107865
Author: Bartosz Golaszewski <brgl@bgdev.pl>
Date:   Thu Sep 22 11:51:24 2022 +0200

    gpiolib: cdev: add fdinfo output for line request file descriptors

    Add fdinfo output for file descriptors created for user-space line
    requests in GPIO uAPI v2. The fdinfo file now contains the name of the
    GPIO chip that is the "parent" of the request as well as offsets of
    the lines requested. This allows user-space to parse the /proc/$PID/fdinfo
    entries and deduce the PID of the process that requested a specific line.

    Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
    Reviewed-by: Kent Gibson <warthog618@gmail.com>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:50:42 -04:00
Shaoqin Huang 41e31b1fa4 gpiolib: cdev: Set lineevent_state::irq after IRQ register successfully
Bugzilla: https://bugzilla.redhat.com/2183344

commit 69bef19d6b9700e96285f4b4e28691cda3dcd0d1
Author: Meng Li <Meng.Li@windriver.com>
Date:   Wed Sep 21 11:20:20 2022 +0800

    gpiolib: cdev: Set lineevent_state::irq after IRQ register successfully

    When running gpio test on nxp-ls1028 platform with below command
    gpiomon --num-events=3 --rising-edge gpiochip1 25
    There will be a warning trace as below:
    Call trace:
    free_irq+0x204/0x360
    lineevent_free+0x64/0x70
    gpio_ioctl+0x598/0x6a0
    __arm64_sys_ioctl+0xb4/0x100
    invoke_syscall+0x5c/0x130
    ......
    el0t_64_sync+0x1a0/0x1a4
    The reason of this issue is that calling request_threaded_irq()
    function failed, and then lineevent_free() is invoked to release
    the resource. Since the lineevent_state::irq was already set, so
    the subsequent invocation of free_irq() would trigger the above
    warning call trace. To fix this issue, set the lineevent_state::irq
    after the IRQ register successfully.

    Fixes: 4682427241 ("gpiolib: cdev: refactor lineevent cleanup into lineevent_free")
    Cc: stable@vger.kernel.org
    Signed-off-by: Meng Li <Meng.Li@windriver.com>
    Reviewed-by: Kent Gibson <warthog618@gmail.com>
    Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:50:41 -04:00
Shaoqin Huang d6539af364 gpiolib: cdev: compile out HTE unless CONFIG_HTE selected
Bugzilla: https://bugzilla.redhat.com/2183344

commit 272ddba004731503cb6c30f2336dd501eaf25955
Author: Kent Gibson <warthog618@gmail.com>
Date:   Thu Jul 14 10:03:19 2022 +0800

    gpiolib: cdev: compile out HTE unless CONFIG_HTE selected

    The majority of builds do not include HTE, so compile out hte
    functionality unless CONFIG_HTE is selected.

    Signed-off-by: Kent Gibson <warthog618@gmail.com>
    Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
    Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
    Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:49:07 -04:00
Shaoqin Huang 5aed433ff5 gpiolib: cdev: consolidate edge detector configuration flags
Bugzilla: https://bugzilla.redhat.com/2183344

commit b1a92e94560def63b71057d770e9db2f798ab58c
Author: Kent Gibson <warthog618@gmail.com>
Date:   Thu Jul 14 10:03:18 2022 +0800

    gpiolib: cdev: consolidate edge detector configuration flags

    Combine the polarity_change flag, struct line eflags, and hte enable
    flag into a single flag variable.

    The combination of these flags describes the configuration state
    of the edge detector, so formalize and clarify that by combining
    them into a single variable, edflags, in struct line.

    The edflags is a subset of the GPIO_V2_LINE_FLAGsb relevant to
    the edge detector, and is also a superset of the eflags it replaces.
    The eflags name is still used to describe the subset of edflags
    corresponding to the rising/falling edge flags where edflags is
    masked down to that subset.

    This consolidation reduces the number of variables being passed,
    simplifies state comparisons, and provides a more extensible
    foundation should additional edge sources be integrated in the
    future.

    Signed-off-by: Kent Gibson <warthog618@gmail.com>
    Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
    Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
    Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:49:07 -04:00
Shaoqin Huang 8574769954 gpiolib: cdev: simplify line event identification
Bugzilla: https://bugzilla.redhat.com/2183344

commit 242202329f0d851f64901111217ca4d52795a4b6
Author: Kent Gibson <warthog618@gmail.com>
Date:   Thu Jul 14 10:03:17 2022 +0800

    gpiolib: cdev: simplify line event identification

    Reorganise line event identification code to reduce code duplication,
    and replace if-else initializers with a helper function to improve
    readability.

    Signed-off-by: Kent Gibson <warthog618@gmail.com>
    Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
    Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:49:06 -04:00
Shaoqin Huang b063384e35 gpiolib: cdev: replace if-else chains with switches
Bugzilla: https://bugzilla.redhat.com/2183344

commit cfa53463ac62d30e4f30ec6e171b9228689d5a83
Author: Kent Gibson <warthog618@gmail.com>
Date:   Thu Jul 14 10:03:16 2022 +0800

    gpiolib: cdev: replace if-else chains with switches

    Improve readability by replacing if-else chains with switch
    statements.

    Signed-off-by: Kent Gibson <warthog618@gmail.com>
    Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
    Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
    Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:49:06 -04:00
Shaoqin Huang 2d52ab6327 gpiolib: cdev: simplify parameter in call to hte_edge_setup
Bugzilla: https://bugzilla.redhat.com/2183344

commit 2487a812236cdeecd0729cfc673fa352db44da5d
Author: Kent Gibson <warthog618@gmail.com>
Date:   Thu Jul 14 10:03:15 2022 +0800

    gpiolib: cdev: simplify parameter in call to hte_edge_setup

    Improve readability by using the GPIO_V2_LINE_FLAG_EDGE_BOTH instead
    of combining the rising and falling edge flags.

    Signed-off-by: Kent Gibson <warthog618@gmail.com>
    Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
    Acked-by: Dipen Patel <dipenp@nvidia.com>
    Tested-by: Dipen Patel <dipenp@nvidia.com>
    Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
    Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:49:06 -04:00
Shaoqin Huang ad01100976 gpiolib: cdev: simplify linereq_free
Bugzilla: https://bugzilla.redhat.com/2183344

commit 160d6e402900672d4b0689a7c939be487460a1fe
Author: Kent Gibson <warthog618@gmail.com>
Date:   Thu Jul 14 10:03:14 2022 +0800

    gpiolib: cdev: simplify linereq_free

    The edge detector is only ever started after the line desc has been
    determined, so move edge_detector_stop() inside the line desc check,
    and merge the two checked regions into one.

    Signed-off-by: Kent Gibson <warthog618@gmail.com>
    Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
    Acked-by: Dipen Patel <dipenp@nvidia.com>
    Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
    Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>

Signed-off-by: Shaoqin Huang <shahuang@redhat.com>
2023-06-19 23:49:06 -04:00
Al Stone 767e69b248 gpiolib: cdev: fix null pointer dereference in linereq_free()
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2071835
Tested: This is one of a series of patch sets to enable Arm SystemReady IR
 support in the kernel for NXP i.MX8 platforms.  This set updates GPIO
 support.  It has been tested via simple boot tests and by using the
 kernel GPIO tools to verify pins are being identified and can be used.

commit c8e27a4a5136e7230f9e4ffcf132705bf56864cc
Author: Kent Gibson <warthog618@gmail.com>
Date:   Wed Jul 6 16:45:07 2022 +0800

    gpiolib: cdev: fix null pointer dereference in linereq_free()

    Fix a kernel NULL pointer dereference reported by gpio kselftests.

    linereq_free() can be called as part of the cleanup of a failed request,
    at which time the desc for a line may not have been determined, so it
    is unsafe to dereference without a check.

    Add a check prior to dereferencing the line desc.

    Fixes: 2068339a6c35 ("gpiolib: cdev: Add hardware timestamp clock type")
    Signed-off-by: Kent Gibson <warthog618@gmail.com>
    Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
    (cherry picked from commit c8e27a4a5136e7230f9e4ffcf132705bf56864cc)

Signed-off-by: Al Stone <ahs3@redhat.com>
2022-08-24 13:31:41 -06:00
Al Stone 4ade2236a3 gpiolib: cdev: Fix kernel doc for struct line
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2071835
Tested: This is one of a series of patch sets to enable Arm SystemReady IR
 support in the kernel for NXP i.MX8 platforms.  This set updates GPIO
 support.  It has been tested via simple boot tests and by using the
 kernel GPIO tools to verify pins are being identified and can be used.

commit 85ff37e302efdf173cff6d1a310c2f7f38f1d069
Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Date:   Fri May 20 19:47:26 2022 +0300

    gpiolib: cdev: Fix kernel doc for struct line

    Kernel doc validator is not happy:
      gpiolib-cdev.c:487: warning: Function parameter or member 'hdesc' not described in 'line'
      gpiolib-cdev.c:487: warning: Function parameter or member 'raw_level' not described in 'line'
      gpiolib-cdev.c:487: warning: Function parameter or member 'total_discard_seq' not described in 'line'
      gpiolib-cdev.c:487: warning: Function parameter or member 'last_seqno' not described in 'line'

    Describe above mentioned parameters.

    Fixes: 2068339a6c35 ("gpiolib: cdev: Add hardware timestamp clock type")
    Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
    Acked-by: Dipen Patel <dipenp@nvidia.com>
    Acked-by: Bartosz Golaszewski <brgl@bgdev.pl>
    Signed-off-by: Thierry Reding <treding@nvidia.com>
    (cherry picked from commit 85ff37e302efdf173cff6d1a310c2f7f38f1d069)

Signed-off-by: Al Stone <ahs3@redhat.com>
2022-08-24 13:31:41 -06:00
Al Stone 83260592ee gpiolib: cdev: Add hardware timestamp clock type
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2071835
Tested: This is one of a series of patch sets to enable Arm SystemReady IR
 support in the kernel for NXP i.MX8 platforms.  This set updates GPIO
 support.  It has been tested via simple boot tests and by using the
 kernel GPIO tools to verify pins are being identified and can be used.

commit 2068339a6c35147847ba433fd0da67b313779059
Author: Dipen Patel <dipenp@nvidia.com>
Date:   Fri Apr 22 13:52:18 2022 -0700

    gpiolib: cdev: Add hardware timestamp clock type

    This patch adds new clock type for the GPIO controller which can
    timestamp gpio lines in using hardware means. To expose such
    functionalities to the userspace, code has been added where
    during line create or set config API calls, it checks for new
    clock type and if requested, calls HTE API. During line change
    event, the HTE subsystem pushes timestamp data to userspace
    through gpiolib-cdev.

    Signed-off-by: Dipen Patel <dipenp@nvidia.com>
    Acked-by: Linus Walleij <linus.walleij@linaro.org>
    Reported-by: kernel test robot <lkp@intel.com>
    Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
    Signed-off-by: Thierry Reding <treding@nvidia.com>
    (cherry picked from commit 2068339a6c35147847ba433fd0da67b313779059)

Signed-off-by: Al Stone <ahs3@redhat.com>
2022-08-24 13:31:40 -06:00
Al Stone 0e3e6fc76e gpiolib: Get rid of redundant 'else'
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2071835
Tested: This is one of a series of patch sets to enable Arm SystemReady IR
 support in the kernel for NXP i.MX8 platforms.  This set updates GPIO
 support.  It has been tested via simple boot tests and by using the
 kernel GPIO tools to verify pins are being identified and can be used.

commit 1cef8b5019769d46725932eeace7a383bca97905
Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Date:   Wed Mar 30 18:06:20 2022 +0300

    gpiolib: Get rid of redundant 'else'

    In the snippets like the following

            if (...)
                    return / goto / break / continue ...;
            else
                    ...

    the 'else' is redundant. Get rid of it. In case of IOCTLs use
    switch-case pattern that seems the usual in such cases.

    While at it, clarify necessity of else in gpiod_direction_output()
    by attaching else if to the closing curly brace on a previous line.

    Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
    Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
    (cherry picked from commit 1cef8b5019769d46725932eeace7a383bca97905)

Signed-off-by: Al Stone <ahs3@redhat.com>
2022-08-24 13:31:04 -06:00
Al Stone 41270a034f gpiolib: Never return internal error codes to user space
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2071835
Tested: This is one of a series of patch sets to enable Arm SystemReady IR
 support in the kernel for NXP i.MX8 platforms.  This set updates GPIO
 support.  It has been tested via simple boot tests and by using the
 kernel GPIO tools to verify pins are being identified and can be used.

commit 95a4eed7dd5b7c1c3664a626174290686ddbee9f
Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Date:   Tue Feb 1 17:27:55 2022 +0200

    gpiolib: Never return internal error codes to user space

    Currently it's possible that character device interface may return
    the error codes which are not supposed to be seen by user space.
    In this case it's EPROBE_DEFER.

    Wrap it to return -ENODEV instead as sysfs does.

    Fixes: d7c51b47ac ("gpio: userspace ABI for reading/writing GPIO lines")
    Fixes: 61f922db72 ("gpio: userspace ABI for reading GPIO line events")
    Fixes: 3c0d9c635a ("gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL")
    Reported-by: Suresh Balakrishnan <suresh.balakrishnan@intel.com>
    Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
    Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
    (cherry picked from commit 95a4eed7dd5b7c1c3664a626174290686ddbee9f)

Signed-off-by: Al Stone <ahs3@redhat.com>
2022-08-24 13:31:02 -06:00
Gabriel Knezek cb8f63b8cb gpiolib: cdev: zero padding during conversion to gpioline_info_changed
When userspace requests a GPIO v1 line info changed event,
lineinfo_watch_read() populates and returns the gpioline_info_changed
structure. It contains 5 words of padding at the end which are not
initialized before being returned to userspace.

Zero the structure in gpio_v2_line_info_change_to_v1() before populating
its contents.

Fixes: aad955842d ("gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL")
Signed-off-by: Gabriel Knezek <gabeknez@linux.microsoft.com>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2021-06-22 09:54:13 +02:00
Kent Gibson 03a58ea590 gpiolib: cdev: clear debounce period if line set to output
When set_config changes a line from input to output debounce is
implicitly disabled, as debounce makes no sense for outputs, but the
debounce period is not being cleared and is still reported in the
line info.

So clear the debounce period when the debouncer is stopped in
edge_detector_stop().

Fixes: 65cff70464 ("gpiolib: cdev: support setting debounce")
Cc: stable@vger.kernel.org
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2021-01-27 15:52:13 +01:00
Kent Gibson 2e202ad873 gpiolib: cdev: fix frame size warning in gpio_ioctl()
The kernel test robot reports the following warning in [1]:

 drivers/gpio/gpiolib-cdev.c: In function 'gpio_ioctl':
 >>drivers/gpio/gpiolib-cdev.c:1437:1: warning: the frame size of 1040 bytes is larger than 1024 bytes [-Wframe-larger-than=]

Refactor gpio_ioctl() to handle each ioctl in its own helper function
and so reduce the variables stored on the stack to those explicitly
required to service the ioctl at hand.

The lineinfo_get_v1() helper handles both the GPIO_GET_LINEINFO_IOCTL
and GPIO_GET_LINEINFO_WATCH_IOCTL, as per the corresponding v2
implementation - lineinfo_get().

[1] https://lore.kernel.org/lkml/202012270910.VW3qc1ER-lkp@intel.com/

Fixes: aad955842d ("gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL")
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2021-01-03 16:32:21 +01:00
Linus Walleij 40b37008eb gpio updates for v5.11-rc1
- several refactoring patches of the core gpiolib code
 - add support for NXP PCAL9554B/C to gpio-pca953x
 - allow probing mockup devices from device tree
 - refactoring and improvements to gpio-rcar
 - improvements to locking in gpio-tegra
 - code shrink in gpiolib devres
 - get the irq offset from device tree in gpio-sifive
 - major refactoring of gpio-exar
 - convert gpio-mvebu pwm access to regmap
 - create a new submenu for virtual GPIO drivers
 - fix clang fall-through warnings treewide
 - minor driver refactoring and tweaks sprinkled all over
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCAAdFiEEFp3rbAvDxGAT0sefEacuoBRx13IFAl/QnM8ACgkQEacuoBRx
 13LkDw/+NKHc32hKgMJmSsLLFHgZ8XB+7Y775G5Ejxj+4Z0hVY9LdqL0zSYwrzCg
 D2dKwrKMtM83T8vf0bVbPdZ/9uuqrljvocGYt2CFmDqQa3sSlJkyDiSak2HAqjK/
 p9cgDQaUIlDrg2xh/1kSqELQoONmp4m82i++bItIwxIqAwPWfjSHVD7EQznDu0QD
 MJ7eYSuVlpeBhMwfyGCYSuUFhd4wN+nTkLTYw3nNlU4PHCKpdgYd7SBCJjSsLAhM
 8FOQ+PBspS6t8lPIEa4Mt+TOWUbbboGUBy40GPNU8QcUjbzj3TwPIBJu/0SIdSOy
 fBrk+GRjWtJ9shSlz0WQ19mG+qMjIiw4xLY1Jh3Tr4b75bQ3a4pk50aPWNE6bbHV
 NP1wZer/s06f1TQFKjfuDVFsOoRLyrTuOzlW0COAHoqFOdgimZi36xtjaS3AL3lE
 POBiBxFo9VaQ0enD99QMBt3940IP3ekRmlqRxeR1t8C0gE5np8IMZ5Dti8Fh4eCN
 hUQTDDuWQadluOdhMdLCBh5cA3VfZY8xksAMBzAYnSUHzM2D8TcKVjfJeegCEuZd
 kapAV3nLUv1/ZU36okFpMaHaKBL+xGDv8gtV2ro7rptkm6khNPQ3hxKvRbCRCG+S
 fO6IKThwozZEL/uVvsZLRKvN47g89oAGYYfasqq49932WChVKf8=
 =RmnM
 -----END PGP SIGNATURE-----

Merge tag 'gpio-updates-for-v5.11' of git://git.kernel.org/pub/scm/linux/kernel/git/brgl/linux into devel

gpio updates for v5.11-rc1

- several refactoring patches of the core gpiolib code
- add support for NXP PCAL9554B/C to gpio-pca953x
- allow probing mockup devices from device tree
- refactoring and improvements to gpio-rcar
- improvements to locking in gpio-tegra
- code shrink in gpiolib devres
- get the irq offset from device tree in gpio-sifive
- major refactoring of gpio-exar
- convert gpio-mvebu pwm access to regmap
- create a new submenu for virtual GPIO drivers
- fix clang fall-through warnings treewide
- minor driver refactoring and tweaks sprinkled all over
2020-12-09 15:17:24 +01:00
Marc Zyngier a0db197f53 gpiolib: cdev: Flag invalid GPIOs as used
When reporting the state of a GPIO to userspace, we never check
for the actual validity of the line, meaning we report invalid
lines as being usable. A subsequent request will fail though,
which is an inconsistent behaviour from a userspace perspective.

Instead, let's check for the validity of the line and report it
as used if it is invalid. This allows a tool such as gpioinfo
to report something sensible:

gpiochip3 - 4 lines:
	line   0:      unnamed       unused   input  active-high
	line   1:      unnamed       kernel   input  active-high [used]
	line   2:      unnamed       kernel   input  active-high [used]
	line   3:      unnamed       unused   input  active-high

In this example, lines 1 and 2 are invalid, and cannot be used by
userspace.

Signed-off-by: Marc Zyngier <maz@kernel.org>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Link: https://lore.kernel.org/r/20201204164739.781812-2-maz@kernel.org
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
2020-12-09 10:23:04 +01:00
Kent Gibson 26d060e47e gpiolib: cdev: allow edge event timestamps to be configured as REALTIME
Using CLOCK_REALTIME as the source for event timestamps is crucial for
some specific applications, particularly those requiring timetamps
relative to a PTP clock, so provide an option to switch the event
timestamp source from the default CLOCK_MONOTONIC to CLOCK_REALTIME.

Note that CLOCK_REALTIME was the default source clock for GPIO until
Linux 5.7 when it was changed to CLOCK_MONOTONIC due to issues with the
shifting of the realtime clock.
Providing this option maintains the CLOCK_MONOTONIC as the default,
while also providing a path forward for those dependent on the pre-5.7
behaviour.

Suggested-by: Jack Winch <sunt.un.morcov@gmail.com>
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20201014231158.34117-2-warthog618@gmail.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
2020-12-05 23:22:47 +01:00
Kent Gibson 5e2ca893d7 gpiolib: cdev: add GPIO_V2_LINE_FLAG_EDGE_BOTH and use it in edge_irq_thread()
Add GPIO_V2_LINE_FLAG_EDGE_BOTH macro and use it in edge_irq_thread() to
improve readability of edge handling cases.

Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-10-30 15:53:19 +01:00
Andy Shevchenko 163d1719d3 gpiolib: Switch to use compat_need_64bit_alignment_fixup() helper
Use the new compat_need_64bit_alignment_fixup() helper to avoid
ugly ifdeffery in IOCTL compatibility code.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Tested-by: Kent Gibson <warthog618@gmail.com>
Depends-on: 527c412519 ("compat: add a compat_need_64bit_alignment_fixup() helper")
Link: https://lore.kernel.org/r/20201014103315.82662-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
2020-10-28 15:47:00 +01:00
Kent Gibson 3ffb7c45d1 gpiolib: cdev: document that line eflags are shared
The line.eflags field is shared so document this fact and highlight it
throughout using READ_ONCE() and WRITE_ONCE() accessors.

Also use a local copy of the eflags in edge_irq_thread() to ensure
consistent control flow even if eflags changes.  This is only a defensive
measure as edge_irq_thread() is currently disabled when the eflags are
changed.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-10-26 15:26:31 +01:00
Linus Torvalds 0486beaf88 GPIO bulk changes for the v5.10 kernel cycle:
Core changes:
 
 - The big core change is the updated (v2) userspace character
   device API. This corrects badly designed 64-bit alignment around
   the line events. We also add the debounce request feature.
   This echoes the often quotes passage from Frederick Brooks
   "The mythical man-month" to always throw one away, which we
   have seen before in things such as V4L2. So we put in a new
   one and deprecate and obsolete the old one.
 
 - All example tools in tools/gpio/* are migrated to the new API
   to set a good example. The libgpiod userspace library has been
   augmented to use this new API pretty much from day 1.
 
 - Some misc API hardening by using strn* function calls has been
   added as well.
 
 - Use the simpler IDA interface for GPIO chip instance enumeration.
 
 - Add device core function for counting string arrays in
   device properties.
 
 - Provide a generic library function kfree_strarray() that can
   be used throughout the kernel.
 
 Driver enhancements:
 
 - The DesignWare dwapb-gpio driver has been enhanced and now
   uses the IRQ handling in the gpiolib core.
 
 - The mockup and aggregator drivers have seen some substantial
   code clean-up and now use more of the core kernel
   inftrastructure.
 
 - Misc cleanups using dev_err_probe().
 
 - The MXC drivers (Freescale/NXP) can now be built modularized,
   which makes modularized GKI Android kernels happy.
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCAAdFiEElDRnuGcz/wPCXQWMQRCzN7AZXXMFAl+FdjkACgkQQRCzN7AZ
 XXMYgQ/+JgpHrp7yS1IkS1KiAxHdeIGnKzloTCQQo1JxYEymAnIeMwo/iWAk5wHu
 NeJIEVxD0YzZwoI3BXbnO5Qy/62g1z7Ik8ToIa0TiFMwYxz5a7lqsiHwpBgHa50h
 T2N8FRFdslVrhpUYBH4Q9wlfYxTki4FwdTD6aaoFFGcMwIVJXWyaYzE+o+qEUEne
 VaPsGoNhRKTdKASP3c6+zbbPonzpZW7s/wvIBQAyBgPxEizlL97RzzX3bSSraoCX
 i0NsDLHMe+9twqE064KN+CYu0Cy80etQSQsYcfnstVshMuY9+WC1YdyJqzYMciuQ
 CYUIQBeskft86IBlsEU/fNCbV+FeAgrxRW6TJK7Hn+sUWZ5+UGdpJ03UE1hA3jjO
 SniwG0vpqvZIkio49B6h51VdjNqVJn+AE8tN3hCzqpFknblXgJOVysD7RS7rNM6D
 flV1bCsUYtC6jN43qsGFiRYLE9ml2iUxFFoBQUaAEh+pXgUzPTQqD7aSjyzmE3x2
 uapKXgxN0dCNH+tFXij73Ro4bYf4ZTZhx3Z3XoEUNEyJpl8fE1bv1SZ2EykOmK8g
 c78fAmT0vG3xYZvK10WZj4zuHV6GlPAYVm/MlhB7QHsrF3wa9vervOuqhEPmp2th
 hTsVj/Zlz0SSDLncMQL64B7gbxOmzOYlVRxIkSrDEXUOFU7kiWE=
 =8CE2
 -----END PGP SIGNATURE-----

Merge tag 'gpio-v5.10-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio

Pull GPIO updates from Linus Walleij:
 "This time very little driver changes but lots of core changes.

  We have some interesting cooperative work for ARM and Intel alike,
  making the GPIO subsystem more and more suitable for industrial
  systems and the like, in addition to the in-kernel users.

  We touch driver core (device properties) and lib/* by adding one
  simple string array free function, these are authored by Andy
  Shevchenko who is a well known and recognized core helpers maintainers
  so this should be fine.

  We also see some Android GKI-related modularization in the MXC
  drivers.

  Core changes:

   - The big core change is the updated (v2) userspace character device
     API.

     This corrects badly designed 64-bit alignment around the line
     events. We also add the debounce request feature. This echoes the
     often quotes passage from Frederick Brooks "The mythical man-month"
     to always throw one away, which we have seen before in things such
     as V4L2. So we put in a new one and deprecate and obsolete the old
     one.

   - All example tools in tools/gpio/* are migrated to the new API to
     set a good example. The libgpiod userspace library has been
     augmented to use this new API pretty much from day 1.

   - Some misc API hardening by using strn* function calls has been
     added as well.

   - Use the simpler IDA interface for GPIO chip instance enumeration.

   - Add device core function for counting string arrays in device
     properties.

   - Provide a generic library function kfree_strarray() that can be
     used throughout the kernel.

  Driver enhancements:

   - The DesignWare dwapb-gpio driver has been enhanced and now uses the
     IRQ handling in the gpiolib core.

   - The mockup and aggregator drivers have seen some substantial code
     clean-up and now use more of the core kernel inftrastructure.

   - Misc cleanups using dev_err_probe().

   - The MXC drivers (Freescale/NXP) can now be built modularized, which
     makes modularized GKI Android kernels happy"

* tag 'gpio-v5.10-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio: (73 commits)
  gpiolib: Update header block in gpiolib-cdev.h
  gpiolib: cdev: switch from kstrdup() to kstrndup()
  docs: gpio: add a new document to its index.rst
  gpio: pca953x: Add support for the NXP PCAL9554B/C
  tools: gpio: add debounce support to gpio-event-mon
  tools: gpio: add multi-line monitoring to gpio-event-mon
  tools: gpio: port gpio-event-mon to v2 uAPI
  tools: gpio: port gpio-hammer to v2 uAPI
  tools: gpio: rename nlines to num_lines
  tools: gpio: port gpio-watch to v2 uAPI
  tools: gpio: port lsgpio to v2 uAPI
  gpio: uapi: document uAPI v1 as deprecated
  gpiolib: cdev: support setting debounce
  gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL
  gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL
  gpiolib: cdev: support edge detection for uAPI v2
  gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL
  gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL
  gpiolib: add build option for CDEV v1 ABI
  gpiolib: make cdev a build option
  ...
2020-10-13 10:09:33 -07:00
Kent Gibson f188ac1251 gpiolib: cdev: switch from kstrdup() to kstrndup()
Use kstrndup() to copy line labels from the userspace provided char
array, rather than ensuring the char array contains a null terminator
and using kstrdup().

Note that the length provided to kstrndup() still assumes that the char
array does contain a null terminator, so the maximum string length is one
less than the array.  This is consistent with the previous behaviour.

Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20201005070246.20927-1-warthog618@gmail.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
2020-10-08 22:57:16 +02:00
Andy Shevchenko 47e538d86d gpiolib: Disable compat ->read() code in UML case
It appears that UML (arch/um) has no compat.h header defined and hence
can't compile a recently provided piece of code in GPIO library.

Disable compat ->read() code in UML case to avoid compilation errors.

While at it, use pattern which is already being used in the kernel elsewhere.

Fixes: 5ad284ab3a ("gpiolib: Fix line event handling in syscall compatible mode")
Reported-by: Geert Uytterhoeven <geert@linux-m68k.org>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20201005131044.87276-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
2020-10-07 11:42:03 +02:00
Kent Gibson 65cff70464 gpiolib: cdev: support setting debounce
Add support for setting debounce on a line via the GPIO uAPI.
Where debounce is not supported by hardware, a software debounce is
provided.

The implementation of the software debouncer waits for the line to be
stable for the debounce period before determining if a level change,
and a corresponding edge event, has occurred.  This provides maximum
protection against glitches, but also introduces a debounce_period
latency to edge events.

The software debouncer is integrated with the edge detection as it
utilises the line interrupt, and integration is simpler than getting
the two to interwork.  Where software debounce AND edge detection is
required, the debouncer provides both.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-09-30 10:57:04 +02:00
Kent Gibson 7b8e00d981 gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL
Add support for the GPIO_V2_LINE_SET_VALUES_IOCTL.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-09-30 10:57:02 +02:00
Kent Gibson a54756cb24 gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL
Add support for GPIO_V2_LINE_SET_CONFIG_IOCTL, the uAPI v2
line set config ioctl.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-09-30 10:56:59 +02:00
Kent Gibson 73e0341992 gpiolib: cdev: support edge detection for uAPI v2
Add support for edge detection to lines requested using
GPIO_V2_GET_LINE_IOCTL.

The edge_detector implementation is based on the v1 lineevent
implementation.  Unlike the v1 implementation, an overflow of the event
buffer results in discarding older events, rather than the most
recent, so the final event in a burst will correspond to the current
state of the line.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-09-30 10:56:56 +02:00
Kent Gibson aad955842d gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL
Add support for GPIO_V2_GET_LINEINFO_IOCTL and
GPIO_V2_GET_LINEINFO_WATCH_IOCTL.

The core of this change is the event kfifo switching to contain
struct gpioline_info_changed_v2, instead of v1 as v2 is richer.

The two uAPI versions are mostly independent - other than where they both
provide line info changes via reads on the chip fd.  As the info change
structs differ between v1 and v2, the infowatch implementation tracks which
version of the infowatch ioctl, either GPIO_GET_LINEINFO_WATCH_IOCTL or
GPIO_V2_GET_LINEINFO_WATCH_IOCTL, initiates the initial watch and returns
the corresponding info change struct to the read.  The version supported
on that fd locks to that version on the first watch request, so subsequent
watches from that process must use the same uAPI version.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-09-30 10:56:52 +02:00
Kent Gibson 3c0d9c635a gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL
Add support for requesting lines using the GPIO_V2_GET_LINE_IOCTL, and
returning their current values using GPIO_V2_LINE_GET_VALUES_IOCTL.

The struct linereq implementation is based on the v1 struct linehandle
implementation.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-09-30 10:56:49 +02:00
Kent Gibson 69e4e13688 gpiolib: cdev: replace strncpy() with strscpy()
Replace usage of strncpy() with strscpy() to remove -Wstringop-truncation
warnings.

The structures being populated are zeroed, to prevent stack leakage as
they are returned to userspace, so strscpy() performs the equivalent
function without the warnings.

Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-09-30 10:56:27 +02:00
Kent Gibson 0dc11e3ad3 gpiolib: cdev: gpio_desc_to_lineinfo() should set info offset
Set the value of the line info offset in gpio_desc_to_lineinfo(), rather
than relying on it being passed in the info.  This makes the function
behave as you would expect from the name - it generates the line info
corresponding to a given GPIO desc.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-09-30 10:56:23 +02:00
Andy Shevchenko 5ad284ab3a gpiolib: Fix line event handling in syscall compatible mode
The introduced line event handling ABI in the commit

  61f922db72 ("gpio: userspace ABI for reading GPIO line events")

missed the fact that 64-bit kernel may serve for 32-bit applications.
In such case the very first check in the lineevent_read() will fail
due to alignment differences.

To workaround this introduce lineevent_get_size() helper which returns actual
size of the structure in user space.

Fixes: 61f922db72 ("gpio: userspace ABI for reading GPIO line events")
Suggested-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Tested-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-09-17 12:03:37 +02:00
Kent Gibson 4682427241 gpiolib: cdev: refactor lineevent cleanup into lineevent_free
Consolidate the cleanup of lineevents, currently duplicated in
lineevent_create and lineevent_release, into a helper function
lineevent_free.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-07-12 10:22:01 +02:00
Kent Gibson 883f919818 gpiolib: cdev: refactor linehandle cleanup into linehandle_free
Consolidate the cleanup of linehandles, currently duplicated in
linehandle_create and linehandle_release, into a helper function
linehandle_free.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-07-12 10:22:01 +02:00
Kent Gibson 1bf7ba4001 gpiolib: cdev: remove recalculation of offset
Remove recalculation of offset from desc, where desc itself was calculated
from offset.

There is no benefit from the desc -> hwgpio conversion in this context.
The only implicit benefit of the offset -> desc -> hwgpio is
the range check in the offset -> desc, but where desc is required you
still get that, and where desc isn't required it is simpler to perform
the range check directly.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-07-12 10:22:01 +02:00
Kent Gibson f30ef3e837 gpiolib: cdev: fix minor race in GET_LINEINFO_WATCH
Merge separate usage of test_bit/set_bit into test_and_set_bit to remove
the possibility of a race between the test and set.

Similarly test_bit and clear_bit.

In the existing code it is possible for two threads to race past the
test_bit and then set or clear the watch bit, and neither return EBUSY.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-07-12 10:22:01 +02:00
Kent Gibson e2b781c5f0 gpiolib: cdev: rename priv to cdev
Rename priv to cdev to improve readability.

The name "priv" indicates that the object is pointed to by
file->private_data, not what the object is actually is.
As it is always used to point to a struct gpio_chardev_data, renaming
it to cdev is more appropriate.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-07-12 10:22:00 +02:00