The module is gated behind the new RUST_CRC_CCITT_ABSTRACTIONS Kconfig symbol, which selects CRC_CCITT, so the C library is only built when a Rust user actually needs it.
Signed-off-by: Ayush Singh ayush@beagleboard.org --- MAINTAINERS | 1 + lib/crc/Kconfig | 7 +++++++ rust/bindings/bindings_helper.h | 1 + rust/kernel/crc_ccitt.rs | 26 ++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 ++ 5 files changed, 37 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS index 6008f16ae2ca..1e3b42eff741 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -6921,6 +6921,7 @@ T: git https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git crc-ne F: Documentation/staging/crc* F: include/linux/crc* F: lib/crc/ +F: rust/kernel/crc* F: scripts/gen-crc-consts.py
CREATIVE SB0540 diff --git a/lib/crc/Kconfig b/lib/crc/Kconfig index 927fc6a6b2b9..aceb8fe8f0ed 100644 --- a/lib/crc/Kconfig +++ b/lib/crc/Kconfig @@ -32,6 +32,13 @@ config CRC_CCITT The CRC-CCITT library functions. Select this if your module uses any of the functions from <linux/crc-ccitt.h>.
+config RUST_CRC_CCITT_ABSTRACTIONS + bool "Rust CRC-CCITT abstractions" + depends on RUST + select CRC_CCITT + help + This enables the Rust abstraction for the CRC-CCITT API. + config CRC_ITU_T tristate help diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 24d659a87c1d..6eb3caaee497 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -49,6 +49,7 @@ #include <linux/cpu.h> #include <linux/cpufreq.h> #include <linux/cpumask.h> +#include <linux/crc-ccitt.h> #include <linux/cred.h> #include <linux/debugfs.h> #include <linux/device/faux.h> diff --git a/rust/kernel/crc_ccitt.rs b/rust/kernel/crc_ccitt.rs new file mode 100644 index 000000000000..6042ada16967 --- /dev/null +++ b/rust/kernel/crc_ccitt.rs @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! CRC-CCITT computation. +//! +//! C header: [`include/linux/crc-ccitt.h`](srctree/include/linux/crc-ccitt.h) + +/// Computes the CRC-CCITT of `data`, starting from the seed value `crc`. +/// +/// Pass the result back in as `crc` to compute the checksum of a buffer +/// incrementally. +/// +/// # Examples +/// +/// ``` +/// use kernel::crc_ccitt::crc_ccitt; +/// +/// let one_shot = crc_ccitt(0xffff, b"hello world"); +/// let split = crc_ccitt(crc_ccitt(0xffff, b"hello "), b"world"); +/// assert_eq!(one_shot, split); +/// ``` +#[inline] +pub fn crc_ccitt(crc: u16, data: &[u8]) -> u16 { + // SAFETY: `data.as_ptr()` is valid for reads of `data.len()` bytes for the + // duration of the call, since it is derived from a live shared slice. + unsafe { bindings::crc_ccitt(crc, data.as_ptr(), data.len()) } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 3edc343caba5..45c4f4db51d2 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -61,6 +61,8 @@ #[cfg(CONFIG_CPU_FREQ)] pub mod cpufreq; pub mod cpumask; +#[cfg(CONFIG_RUST_CRC_CCITT_ABSTRACTIONS)] +pub mod crc_ccitt; pub mod cred; pub mod debugfs; pub mod device;