Re: [PATCH 3/3] sample: rust: Add GPIO consumer sample driver

From: Alexandre Courbot

Date: Tue Sep 22 2026 - 10:18:29 EST


On Mon Sep 21, 2026 at 11:38 PM JST, Bartosz Golaszewski wrote:
> On Mon, 21 Sep 2026 15:12:01 +0200, Kohei Ito <koheiito.dev@xxxxxxxxx> said:
>> Hi Bartosz, Alexandre,
>>
>> Thanks both for the feedback.
>>
>> On Mon, Sep 14, 2026 at 04:36:48AM -0400, Bartosz Golaszewski wrote:
>>> On Mon, 14 Sep 2026 03:41:29 +0200, Alexandre Courbot
>>> <acourbot@xxxxxxxxxx> said:
>>> > On Sun Sep 13, 2026 at 5:46 PM JST, Kohei Ito wrote:
>>> >> Hi, Bartosz,
>>> >>
>>> >> On Thu, Sep 10, 2026 at 12:37:17AM -0700, Bartosz Golaszewski wrote:
>>> >>> On Sun, 6 Sep 2026 10:45:51 +0200, Kohei Ito <koheiito.dev@xxxxxxxxx> said:
>>> >>> > Add a sample driver to demonstrate the use of the Rust GPIO APIs.
>>> >>> >
>>> >>> > Signed-off-by: Kohei Ito <koheiito.dev@xxxxxxxxx>
>>> >>> > ---
>>> >>>
>>> >>> I don't like samples as they rarely get built or tested. We seem to already
>>> >>> have kunit support for rust, wouldn't it make more sense to implement a kunit
>>> >>> module for rust GPIO abstractions? If we don't have provider abstractions, you
>>> >>> should be able to reuse gpio-sim as the GPIO controller for testing just by
>>> >>> instantiating simulated GPIO devices.
>>> >>
>>> >> Thank you for your suggestion.
>>> >>
>>> >> I assume the kunit module you have in mind would be implemented like
>>> >> `gpiolib-kunit.c`. Based on your comment, I agree that a kunit-based
>>> >> approach is more appropriate than a sample driver.
>>> >>
>>> >> However, as far as I know, we don't yet have Rust abstractions for
>>> >> platform_device registration and software_node, which are required to
>>> >> implement a kunit-based module for testing GPIO consumer APIs. Given the
>>> >> current state of Rust for Linux, I think creating a sample driver is a
>>> >> more practical approach for now. I would like to consider migrating to a
>>> >> kunit-based module as future work.
>>> >
>>> > The problem is that this sample driver never probes, so in effect it is
>>> > only ever compile-tested. Without support for the provider API, you need
>>> > to include a small C fixture providing a GPIO chip for it to be actually
>>> > runtime-tested.
>>> >
>>> > Doing the same using KUnit would involve building the `gpio_chip` using a
>>> > bunch of unsafe statements working with the C bindings (which would then
>>> > in turn require `gpio/driver.h` to be added), so I guess we'll want to
>>> > wait until we have a proper Rust provider API to go that direction.
>>> >
>>>
>>> No, I was thinking about the gpio-sim module which is implemented as a platform
>>> driver which you can describe with a software node and then register to create
>>> a simulated GPIO provider against which the consumer APIs in rust could be
>>> tested.
>>>
>>> To that end, we'd need to just register a platform device from rust and AFAICT,
>>> there are already APIs for that, except for the software nodes.
>>>
>>> Bart
>>
>> I looked into this further, but couldn't find any Rust abstraction for
>> registering a new `platform_device` (no file contains a platform device
>> registration function such as `platform_device_register()`,
>> `platform_device_register_*()`, or `platform_device_alloc()`/
>> `platform_device_add()`). So, we can't write a fully Rust kunit module
>> for the GPIO consumer API, at least for v7.3-rc3.
>>
>
> Right, I must have misremembered this.
>
>> The best approach I think is a combination of a Rust consumer driver and
>> a C kunit module dedicated to testing the consumer driver. This is
>> almost the same as `drivers/gpio/gpiolib-kunit.c`, except that instead
>> of directly exercising the GPIO consumer APIs, the module I'm suggesting
>> exercises them indirectly, through the Rust consumer driver.
>>
>
> Can't you just call a C function registering the platform device from unsafe
> rust?

You should be able to. It's a but laborious, but better than not having
tests at all, and acceptable as a temporary solution IMHO.

Of course an even better solution would be to provide a safe abstraction
for registering platform devices. :) I don't know exactly how much work
that would be, but there is already a `platform` module, so supposedly
we wouldn't be starting from absolute scratch.