mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Fiona Behrens <me@kloenk.dev>
To: Pavel Machek <pavel@ucw.cz>, Lee Jones <lee@kernel.org>,
	linux-leds@vger.kernel.org
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"FUJITA Tomonori" <fujita.tomonori@gmail.com>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	"Fiona Behrens" <me@kloenk.dev>
Subject: [RFC PATCH 2/2] samples: rust: led sample
Date: Wed,  9 Oct 2024 12:57:59 +0200	[thread overview]
Message-ID: <20241009105759.579579-3-me@kloenk.dev> (raw)
In-Reply-To: <20241009105759.579579-1-me@kloenk.dev>

Provide an initial sample LED driver that just prints the current
requested LED state.

This is not intended to be merged, but as a placeholder until the
abstactions for hardware access are written.

Signed-off-by: Fiona Behrens <me@kloenk.dev>
---
 samples/rust/Kconfig     |  10 ++++
 samples/rust/Makefile    |   1 +
 samples/rust/rust_led.rs | 103 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+)
 create mode 100644 samples/rust/rust_led.rs

diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
index b0f74a81c8f9..910f15ef6951 100644
--- a/samples/rust/Kconfig
+++ b/samples/rust/Kconfig
@@ -30,6 +30,16 @@ config SAMPLE_RUST_PRINT
 
 	  If unsure, say N.
 
+config SAMPLE_RUST_LED
+       tristate "Led subsystem"
+       help
+	This option builds the Rust LED subsystem sample.
+
+	To compile this as a module, choose M here:
+	the module will be called rust_led.
+
+	If unsure, say N.
+
 config SAMPLE_RUST_HOSTPROGS
 	bool "Host programs"
 	help
diff --git a/samples/rust/Makefile b/samples/rust/Makefile
index 03086dabbea4..1299ca1e9ebb 100644
--- a/samples/rust/Makefile
+++ b/samples/rust/Makefile
@@ -2,5 +2,6 @@
 
 obj-$(CONFIG_SAMPLE_RUST_MINIMAL)		+= rust_minimal.o
 obj-$(CONFIG_SAMPLE_RUST_PRINT)			+= rust_print.o
+obj-$(CONFIG_SAMPLE_RUST_LED)			+= rust_led.o
 
 subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS)		+= hostprogs
diff --git a/samples/rust/rust_led.rs b/samples/rust/rust_led.rs
new file mode 100644
index 000000000000..0085f7484ea1
--- /dev/null
+++ b/samples/rust/rust_led.rs
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0
+//! Rust LED sample.
+
+use core::time::Duration;
+
+use kernel::c_str;
+use kernel::leds::{Brightness, Color, Led, LedConfig, Operations};
+use kernel::prelude::*;
+
+module! {
+    type: RustLed,
+    name: "rust_led",
+    author: "Rust for Linux Contributors",
+    description: "Rust led sample",
+    license: "GPL",
+}
+
+/// Rust LED sample driver
+// Hold references in scope as droping would unregister the LED
+#[allow(dead_code)]
+struct RustLed {
+    /// LED which just supports on/off.
+    on_off: Pin<Box<Led<LedOnOff>>>,
+    /// LED which supports brightness levels and blinking.
+    blink: Pin<Box<Led<LedBlinking>>>,
+}
+
+impl kernel::Module for RustLed {
+    fn init(_module: &'static ThisModule) -> Result<Self> {
+        pr_info!("registering on_off led\n");
+        let on_off = Box::pin_init(
+            Led::register_with_name(
+                c_str!("sample:red:on_off"),
+                None,
+                &LedConfig { color: Color::Red },
+                LedOnOff,
+            ),
+            GFP_KERNEL,
+        )?;
+
+        let blink = Box::pin_init(
+            Led::register_with_name(
+                c_str!("sample:green:blink"),
+                None,
+                &LedConfig {
+                    color: Color::Green,
+                },
+                LedBlinking,
+            ),
+            GFP_KERNEL,
+        )?;
+
+        Ok(Self { on_off, blink })
+    }
+}
+
+struct LedOnOff;
+
+#[vtable]
+impl Operations for LedOnOff {
+    const MAX_BRIGHTNESS: u8 = 1;
+
+    fn brightness_set(_this: &mut Led<Self>, brightness: Brightness) {
+        match brightness {
+            Brightness::Off => pr_info!("Switching led off\n"),
+            Brightness::On(v) => pr_info!("Switching led on: {}\n", v.get()),
+        }
+    }
+}
+
+struct LedBlinking;
+
+impl LedBlinking {
+    const HW_DURATION: Duration = Duration::from_millis(1000);
+}
+
+#[vtable]
+impl Operations for LedBlinking {
+    const MAX_BRIGHTNESS: u8 = 255;
+
+    fn brightness_set(_this: &mut Led<Self>, brightness: Brightness) {
+        match brightness {
+            Brightness::Off => pr_info!("blinking: off\n"),
+            Brightness::On(v) => pr_info!("blinking: on at {}\n", v.get()),
+        }
+    }
+
+    fn blink_set(
+        _this: &mut Led<Self>,
+        delay_on: Duration,
+        delay_off: Duration,
+    ) -> Result<(Duration, Duration)> {
+        pr_info!("blinking: try delay {delay_on:?} {delay_off:?}\n");
+        if !(delay_on.is_zero() && delay_off.is_zero())
+            && !(delay_on == Self::HW_DURATION && delay_off == Self::HW_DURATION)
+        {
+            return Err(EINVAL);
+        }
+
+        pr_info!("blinking: setting dealy\n");
+        Ok((Self::HW_DURATION, Self::HW_DURATION))
+    }
+}
-- 
2.46.0


  parent reply	other threads:[~2024-10-09 10:58 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-09 10:57 [RFC PATCH 0/2] rust: LED abstractions Fiona Behrens
2024-10-09 10:57 ` [RFC PATCH 1/2] rust: LED abstraction Fiona Behrens
2024-11-16 15:47   ` Marek Behún
2024-11-18 10:19     ` Alice Ryhl
2024-11-18 16:39       ` Miguel Ojeda
2024-11-18 10:22   ` Alice Ryhl
2024-11-21  9:47     ` Fiona Behrens
2024-11-27 11:39       ` Alice Ryhl
2024-10-09 10:57 ` Fiona Behrens [this message]
2024-11-11  9:41 ` [RFC PATCH 0/2] rust: LED abstractions Lee Jones
2024-11-11 10:21   ` Fiona Behrens

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=20241009105759.579579-3-me@kloenk.dev \
    --to=me@kloenk.dev \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=fujita.tomonori@gmail.com \
    --cc=gary@garyguo.net \
    --cc=lee@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=pavel@ucw.cz \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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

all inboxes | Powered by JetHome®