From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 01027FC9EC8 for ; Sun, 8 Mar 2026 14:13:38 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 99B0610E17C; Sun, 8 Mar 2026 14:13:29 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; secure) header.d=posteo.de header.i=@posteo.de header.b="JczRkV5d"; dkim-atps=neutral Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) by gabe.freedesktop.org (Postfix) with ESMTPS id AAFA310E3F6 for ; Fri, 6 Mar 2026 20:36:27 +0000 (UTC) Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id C5AED240101 for ; Fri, 6 Mar 2026 21:36:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=posteo.de; s=2017; t=1772829385; bh=j2O3V7AMyLa1yZB24Ti+OpRWtDSQYTYoVDhod0crmqg=; h=Message-ID:Subject:From:To:Cc:Date:Autocrypt:Content-Type: MIME-Version:OpenPGP:From; b=JczRkV5dnjjb8KPpea3ksVlbDnUaV0aMZqH9slwu1krN1xb/KMCZjgGpAEitJyMRK 4DjC1oA7XwN1ph330iM41P/mY7fCizknxXhnFwaYip5utHygB39eomP9+EeSbTMMDg iw3x6+FUAf8mtQU175ObJPyWt4DpH5BymJAQNfyQ2Zwyk2n4xDXdxdjDJ2T2dm4RqN mIsrSi2LdJyQv65mBC9J+vBx0UZHoUX5rGOg0UF3TG3KpDj0QUMZoD0NQohfiSzClN spoM/svrfjaUaijqncxDydaIDWTetlUuiu/5pd5e85Ji6syO1xgsorqcQMxRSn01fF NNqS9H9J97yTw== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4fSJ8k4b6hz9rxN; Fri, 6 Mar 2026 21:36:22 +0100 (CET) Message-ID: <088970fb441a9ed0ad57b3fa43d30cc0bb5b2db4.camel@posteo.de> Subject: Re: [PATCH v2 3/4] rust: add basic serial device bus abstractions From: Markus Probst To: Rob Herring , Greg Kroah-Hartman , Jiri Slaby , Miguel Ojeda , Gary Guo , =?ISO-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Kari Argillander , "Rafael J. Wysocki" , Viresh Kumar , Boqun Feng , David Airlie , Simona Vetter Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-pm@vger.kernel.org, driver-core@lists.linux.dev, dri-devel@lists.freedesktop.org Date: Fri, 06 Mar 2026 20:36:24 +0000 In-Reply-To: <20260306-rust_serdev-v2-3-e9b23b42b255@posteo.de> References: <20260306-rust_serdev-v2-0-e9b23b42b255@posteo.de> <20260306-rust_serdev-v2-3-e9b23b42b255@posteo.de> Autocrypt: addr=markus.probst@posteo.de; prefer-encrypt=mutual; keydata=mQINBGiDvXgBEADAXUceKafpl46S35UmDh2wRvvx+UfZbcTjeQOlSwKP7YVJ4JOZrVs93 qReNLkOWguIqPBxR9blQ4nyYrqSCV+MMw/3ifyXIm6Pw2YRUDg+WTEOjTixRCoWDgUj1nOsvJ9tVA m76Ww+/pAnepVRafMID0rqEfD9oGv1YrfpeFJhyE2zUw3SyyNLIKWD6QeLRhKQRbSnsXhGLFBXCqt 9k5JARhgQof9zvztcCVlT5KVvuyfC4H+HzeGmu9201BVyihJwKdcKPq+n/aY5FUVxNTgtI9f8wIbm fAjaoT1pjXSp+dszakA98fhONM98pOq723o/1ZGMZukyXFfsDGtA3BB79HoopHKujLGWAG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gIbAwULCQgHAgIiAgYVCgkIC wIEFgIDAQIeBwIXgAAKCRA0dh/4561D0gKJD/9uOQKYlsDoQX65Gd0LiMT0C+5vXgr3VI0PHDOwcv 51fJ3A1vNyPZRFPGrz8+mDEXUQOF/INfnz5Tu1QHwf+iYcWcTGAN/FHgVR6ET6VBNU2hJaKhu+Ggo kjYyJTOvyX+3yNRUfSny0GjTjIPuPTErjqmHF+BtjXslpgwqnNMznf3lRIuUjRORupos6p3k1DndE 5vzUTmXSvMyXyOD2KhBl/kL76k0bHYyAQytZPag12pltrtFbA/r2phDGN2si8PooDT99bSTJjaM45 MTAAHbHKJfvgfK41bNFD5mMtpWpL195XRtS0Nrxdg3PaYBxN5gtTG0RyZfpYRlkdEhm+jj/8RxuSG i/qdhRdbiI7K2IELWeQVHSNDi9JabR/UzlR4NSnhfAjRIVlRM+eFbUl8XwxwVrAkojF5IraH2qRvg VCmuFsHUW07FUlrDrzpjXsD73cKppoFGDCdDR0BHJepXbFLS9+AqkT+guRJlnCTg2p+TQtnbwPgKp Vj98JixovCl99zRYTsL2bRNU5+q8iET65VMJ1ydyNanvLd5vI/NqDkXhlXLsGmdaDTtu4R21PkToX dQNGrZ91M9nlIBKw8Y7c7xZ4098qX2b8JX/CxD+gC1r4C8vuA3GkhFLx+KlkON7LyiJPkrePp6Qky jfGillcaQOqFZ3WwVqyzG1BUfTow== Content-Type: multipart/signed; micalg="pgp-sha256"; protocol="application/pgp-signature"; boundary="=-PF5TUwaOf//AV1lgh2Mq" MIME-Version: 1.0 OpenPGP: url=https://posteo.de/keys/markus.probst@posteo.de.asc; preference=encrypt X-Mailman-Approved-At: Sun, 08 Mar 2026 14:13:21 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" --=-PF5TUwaOf//AV1lgh2Mq Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, 2026-03-06 at 19:35 +0000, Markus Probst wrote: > Implement the basic serial device bus abstractions required to write a > serial device bus device driver with or without the need for initial devi= ce > data. This includes the following data structures: >=20 > The `serdev::Driver` trait represents the interface to the driver. >=20 > The `serdev::Device` abstraction represents a `struct serdev_device`. >=20 > In order to provide the Serdev specific parts to a generic > `driver::Registration` the `driver::RegistrationOps` trait is > implemented by `serdev::Adapter`. >=20 > Signed-off-by: Markus Probst > --- > rust/bindings/bindings_helper.h | 1 + > rust/helpers/helpers.c | 1 + > rust/helpers/serdev.c | 22 ++ > rust/kernel/lib.rs | 2 + > rust/kernel/serdev.rs | 533 ++++++++++++++++++++++++++++++++++= ++++++ > 5 files changed, 559 insertions(+) >=20 > diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_hel= per.h > index 083cc44aa952..ab521ba42673 100644 > --- a/rust/bindings/bindings_helper.h > +++ b/rust/bindings/bindings_helper.h > @@ -80,6 +80,7 @@ > #include > #include > #include > +#include > #include > #include > #include > diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c > index a3c42e51f00a..9b87e9591cfd 100644 > --- a/rust/helpers/helpers.c > +++ b/rust/helpers/helpers.c > @@ -53,6 +53,7 @@ > #include "regulator.c" > #include "scatterlist.c" > #include "security.c" > +#include "serdev.c" > #include "signal.c" > #include "slab.c" > #include "spinlock.c" > diff --git a/rust/helpers/serdev.c b/rust/helpers/serdev.c > new file mode 100644 > index 000000000000..c52b78ca3fc7 > --- /dev/null > +++ b/rust/helpers/serdev.c > @@ -0,0 +1,22 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +#include > + > +__rust_helper > +void rust_helper_serdev_device_driver_unregister(struct serdev_device_dr= iver *sdrv) > +{ > + serdev_device_driver_unregister(sdrv); > +} > + > +__rust_helper > +void rust_helper_serdev_device_put(struct serdev_device *serdev) > +{ > + serdev_device_put(serdev); > +} > + > +__rust_helper > +void rust_helper_serdev_device_set_client_ops(struct serdev_device *serd= ev, > + const struct serdev_device_ops *ops) > +{ > + serdev_device_set_client_ops(serdev, ops); > +} > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 3da92f18f4ee..1d337e112922 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -140,6 +140,8 @@ > pub mod scatterlist; > pub mod security; > pub mod seq_file; > +#[cfg(CONFIG_SERIAL_DEV_BUS)] > +pub mod serdev; > pub mod sizes; > pub mod slice; > #[cfg(CONFIG_SOC_BUS)] > diff --git a/rust/kernel/serdev.rs b/rust/kernel/serdev.rs > new file mode 100644 > index 000000000000..cba5fb6245f7 > --- /dev/null > +++ b/rust/kernel/serdev.rs > @@ -0,0 +1,533 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Abstractions for the serial device bus. > +//! > +//! C header: [`include/linux/serdev.h`](srctree/include/linux/serdev.h) > + > +use crate::{ > + acpi, > + device, > + devres, > + driver, > + error::{from_result, to_result, VTABLE_DEFAULT_ERROR}, > + of, > + prelude::*, > + sync::Completion, > + time::{msecs_to_jiffies, Jiffies, Msecs}, > + types::{AlwaysRefCounted, ForeignOwnable, Opaque}, // > +}; > + > +use core::{ > + cell::UnsafeCell, > + marker::PhantomData, > + mem::offset_of, > + num::NonZero, > + ptr::NonNull, // > +}; > + > +/// Parity bit to use with a serial device. > +#[repr(u32)] > +pub enum Parity { > + /// No parity bit. > + None =3D bindings::serdev_parity_SERDEV_PARITY_NONE, > + /// Even partiy. > + Even =3D bindings::serdev_parity_SERDEV_PARITY_EVEN, > + /// Odd parity. > + Odd =3D bindings::serdev_parity_SERDEV_PARITY_ODD, > +} > + > +/// Timeout in Jiffies. > +pub enum Timeout { > + /// Wait for a specific amount of [`Jiffies`]. > + Jiffies(NonZero), > + /// Wait for a specific amount of [`Msecs`]. > + Milliseconds(NonZero), > + /// Wait as long as possible. > + /// > + /// This is equivalent to [`kernel::task::MAX_SCHEDULE_TIMEOUT`]. > + Max, > +} > + > +impl Timeout { > + fn into_jiffies(self) -> isize { > + match self { > + Self::Jiffies(value) =3D> value.get().try_into().unwrap_or_d= efault(), > + Self::Milliseconds(value) =3D> { > + msecs_to_jiffies(value.get()).try_into().unwrap_or_defau= lt() > + } > + Self::Max =3D> 0, > + } > + } > +} > + > +/// An adapter for the registration of serial device bus device drivers. > +pub struct Adapter(T); > + > +// SAFETY: > +// - `bindings::serdev_device_driver` is a C type declared as `repr(C)`. > +// - `Drvdata` is the type of the driver's device private data. > +// - `struct serdev_device_driver` embeds a `struct device_driver`. > +// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `= struct device_driver`. > +unsafe impl driver::DriverLayout for Adapter { > + type DriverType =3D bindings::serdev_device_driver; > + type DriverData =3D T; > + const DEVICE_DRIVER_OFFSET: usize =3D core::mem::offset_of!(Self::Dr= iverType, driver); > +} > + > +// SAFETY: A call to `unregister` for a given instance of `DriverType` i= s guaranteed to be valid if > +// a preceding call to `register` has been successful. > +unsafe impl driver::RegistrationOps for Adapter = { > + unsafe fn register( > + sdrv: &Opaque, > + name: &'static CStr, > + module: &'static ThisModule, > + ) -> Result { > + let of_table =3D match T::OF_ID_TABLE { > + Some(table) =3D> table.as_ptr(), > + None =3D> core::ptr::null(), > + }; > + > + let acpi_table =3D match T::ACPI_ID_TABLE { > + Some(table) =3D> table.as_ptr(), > + None =3D> core::ptr::null(), > + }; > + > + // SAFETY: It's safe to set the fields of `struct serdev_device_= driver` on initialization. > + unsafe { > + (*sdrv.get()).driver.name =3D name.as_char_ptr(); > + (*sdrv.get()).probe =3D Some(Self::probe_callback); > + (*sdrv.get()).remove =3D Some(Self::remove_callback); > + (*sdrv.get()).driver.of_match_table =3D of_table; > + (*sdrv.get()).driver.acpi_match_table =3D acpi_table; > + } > + > + // SAFETY: `sdrv` is guaranteed to be a valid `DriverType`. > + to_result(unsafe { bindings::__serdev_device_driver_register(sdr= v.get(), module.0) }) > + } > + > + unsafe fn unregister(sdrv: &Opaque) { > + // SAFETY: `sdrv` is guaranteed to be a valid `DriverType`. > + unsafe { bindings::serdev_device_driver_unregister(sdrv.get()) }= ; > + } > +} > + > +#[pin_data] > +struct PrivateData { > + #[pin] > + probe_complete: Completion, > + error: UnsafeCell, > +} > + > +impl Adapter { > + const OPS: &'static bindings::serdev_device_ops =3D &bindings::serde= v_device_ops { > + receive_buf: if T::HAS_RECEIVE { > + Some(Self::receive_buf_callback) > + } else { > + None > + }, > + write_wakeup: Some(bindings::serdev_device_write_wakeup), > + }; > + > + extern "C" fn probe_callback(sdev: *mut bindings::serdev_device) -> = kernel::ffi::c_int { > + // SAFETY: The serial device bus only ever calls the probe callb= ack with a valid pointer to > + // a `struct serdev_device`. > + // > + // INVARIANT: `sdev` is valid for the duration of `probe_callbac= k()`. > + let sdev =3D unsafe { &*sdev.cast::= >() }; > + let id_info =3D ::id_info(sdev.as_ref()= ); > + > + from_result(|| { > + let private_data =3D devres::register( > + sdev.as_ref(), > + try_pin_init!(PrivateData { > + probe_complete <- Completion::new(), > + error: false.into(), > + }), > + GFP_KERNEL, > + )?; > + > + // SAFETY: `sdev.as_raw()` is guaranteed to be a valid point= er to `serdev_device`. > + unsafe { > + (*sdev.as_raw()).private_data =3D > + (&raw const *private_data).cast::().cast_mut= () > + }; > + > + // SAFETY: `sdev.as_raw()` is guaranteed to be a valid point= er to `serdev_device`. > + unsafe { bindings::serdev_device_set_client_ops(sdev.as_raw(= ), Self::OPS) }; > + > + // SAFETY: The serial device bus only ever calls the probe c= allback with a valid pointer > + // to a `serdev_device`. > + to_result(unsafe { > + bindings::devm_serdev_device_open(sdev.as_ref().as_raw()= , sdev.as_raw()) > + })?; > + > + let data =3D T::probe(sdev, id_info); > + let result =3D sdev.as_ref().set_drvdata(data); > + > + // SAFETY: We have exclusive access to `private_data.error`. > + unsafe { *private_data.error.get() =3D result.is_err() }; > + > + private_data.probe_complete.complete_all(); > + > + result.map(|()| 0) > + }) > + } > + > + extern "C" fn remove_callback(sdev: *mut bindings::serdev_device) { > + // SAFETY: The serial device bus only ever calls the remove call= back with a valid pointer > + // to a `struct serdev_device`. > + // > + // INVARIANT: `sdev` is valid for the duration of `remove_callba= ck()`. > + let sdev =3D unsafe { &*sdev.cast::= >() }; > + > + // SAFETY: `remove_callback` is only ever called after a success= ful call to > + // `probe_callback`, hence it's guaranteed that `Device::set_drv= data()` has been called > + // and stored a `Pin>`. > + let data =3D unsafe { sdev.as_ref().drvdata_borrow::() }; > + > + T::unbind(sdev, data); > + > + // SAFETY: > + // - `sdev.as_raw()` is guaranteed to be a pointer to a valid `s= erdev_device`. > + // - `remove_callback` is only ever called after a successful ca= ll to > + // `probe_callback`, hence it's guaranteed that `private_data`= is a valid pointer to > + // `PrivateData` and is stored in a `Pin>`. > + // - `private_data` will not be accessed after this function. > + drop(unsafe { >>::from_foreign((*sdev.as_r= aw()).private_data) }); It seems I forgot to remove this drop statement. Please ignore it. Thanks - Markus Probst > + } > + > + extern "C" fn receive_buf_callback( > + sdev: *mut bindings::serdev_device, > + buf: *const u8, > + length: usize, > + ) -> usize { > + // SAFETY: The serial device bus only ever calls the receive buf= callback with a valid > + // pointer to a `struct serdev_device`. > + // > + // INVARIANT: `sdev` is valid for the duration of `receive_buf_c= allback()`. > + let sdev =3D unsafe { &*sdev.cast::= >() }; > + > + // SAFETY: `receive_buf_callback` is only ever called after a su= ccessful call to > + // `probe_callback`, hence it's guaranteed that `Device::set_drv= data()` has been called > + // and stored a `Pin>`. > + let data =3D unsafe { sdev.as_ref().drvdata_borrow::() }; > + > + // SAFETY: > + // - The serial device bus only ever calls the receive buf callb= ack with a valid pointer to > + // a `struct serdev_device`. > + // - `receive_buf_callback` is only ever called after a successf= ul call to > + // `probe_callback`, hence it's guaranteed that `sdev.private_= data` is a pointer > + // to a valid `PrivateData`. > + let private_data =3D unsafe { &*(*sdev.as_raw()).private_data.ca= st::() }; > + > + private_data.probe_complete.complete_all(); > + > + // SAFETY: No one has exclusive access to `private_data.error`. > + if unsafe { *private_data.error.get() } { > + return length; > + } > + > + // SAFETY: `buf` is guaranteed to be non-null and has the size o= f `length`. > + let buf =3D unsafe { core::slice::from_raw_parts(buf, length) }; > + > + T::receive(sdev, data, buf) > + } > +} > + > +impl driver::Adapter for Adapter { > + type IdInfo =3D T::IdInfo; > + > + fn of_id_table() -> Option> { > + T::OF_ID_TABLE > + } > + > + fn acpi_id_table() -> Option> { > + T::ACPI_ID_TABLE > + } > +} > + > +/// Declares a kernel module that exposes a single serial device bus dev= ice driver. > +/// > +/// # Examples > +/// > +/// ```ignore > +/// kernel::module_serdev_device_driver! { > +/// type: MyDriver, > +/// name: "Module name", > +/// authors: ["Author name"], > +/// description: "Description", > +/// license: "GPL v2", > +/// } > +/// ``` > +#[macro_export] > +macro_rules! module_serdev_device_driver { > + ($($f:tt)*) =3D> { > + $crate::module_driver!(, $crate::serdev::Adapter, { $($f)*= }); > + }; > +} > + > +/// The serial device bus device driver trait. > +/// > +/// Drivers must implement this trait in order to get a serial device bu= s device driver registered. > +/// > +/// # Examples > +/// > +///``` > +/// # use kernel::{ > +/// acpi, > +/// bindings, > +/// device::{ > +/// Bound, > +/// Core, // > +/// }, > +/// of, > +/// serdev, // > +/// }; > +/// > +/// struct MyDriver; > +/// > +/// kernel::of_device_table!( > +/// OF_TABLE, > +/// MODULE_OF_TABLE, > +/// ::IdInfo, > +/// [ > +/// (of::DeviceId::new(c"test,device"), ()) > +/// ] > +/// ); > +/// > +/// kernel::acpi_device_table!( > +/// ACPI_TABLE, > +/// MODULE_ACPI_TABLE, > +/// ::IdInfo, > +/// [ > +/// (acpi::DeviceId::new(c"LNUXBEEF"), ()) > +/// ] > +/// ); > +/// > +/// #[vtable] > +/// impl serdev::Driver for MyDriver { > +/// type IdInfo =3D (); > +/// const OF_ID_TABLE: Option> =3D Some(&O= F_TABLE); > +/// const ACPI_ID_TABLE: Option> =3D Som= e(&ACPI_TABLE); > +/// > +/// fn probe( > +/// sdev: &serdev::Device, > +/// _id_info: Option<&Self::IdInfo>, > +/// ) -> impl PinInit { > +/// sdev.set_baudrate(115200); > +/// sdev.write_all(b"Hello\n", serdev::Timeout::Max)?; > +/// Ok(MyDriver) > +/// } > +/// } > +///``` > +#[vtable] > +pub trait Driver: Send { > + /// The type holding driver private data about each device id suppor= ted by the driver. > + // TODO: Use associated_type_defaults once stabilized: > + // > + // ``` > + // type IdInfo: 'static =3D (); > + // ``` > + type IdInfo: 'static; > + > + /// The table of OF device ids supported by the driver. > + const OF_ID_TABLE: Option> =3D None; > + > + /// The table of ACPI device ids supported by the driver. > + const ACPI_ID_TABLE: Option> =3D None; > + > + /// Serial device bus device driver probe. > + /// > + /// Called when a new serial device bus device is added or discovere= d. > + /// Implementers should attempt to initialize the device here. > + fn probe( > + sdev: &Device, > + id_info: Option<&Self::IdInfo>, > + ) -> impl PinInit; > + > + /// Serial device bus device driver unbind. > + /// > + /// Called when a [`Device`] is unbound from its bound [`Driver`]. I= mplementing this callback > + /// is optional. > + /// > + /// This callback serves as a place for drivers to perform teardown = operations that require a > + /// `&Device` or `&Device` reference. For instance. > + /// > + /// Otherwise, release operations for driver resources should be per= formed in `Self::drop`. > + fn unbind(sdev: &Device, this: Pin<&Self>) { > + let _ =3D (sdev, this); > + } > + > + /// Serial device bus device data receive callback. > + /// > + /// Called when data got received from device. > + /// > + /// Returns the number of bytes accepted. > + fn receive(sdev: &Device, this: Pin<&Self>, data: &[u= 8]) -> usize { > + let _ =3D (sdev, this, data); > + build_error!(VTABLE_DEFAULT_ERROR) > + } > +} > + > +/// The serial device bus device representation. > +/// > +/// This structure represents the Rust abstraction for a C `struct serde= v_device`. The > +/// implementation abstracts the usage of an already existing C `struct = serdev_device` within Rust > +/// code that we get passed from the C side. > +/// > +/// # Invariants > +/// > +/// A [`Device`] instance represents a valid `struct serdev_device` crea= ted by the C portion of > +/// the kernel. > +#[repr(transparent)] > +pub struct Device( > + Opaque, > + PhantomData, > +); > + > +impl Device { > + fn as_raw(&self) -> *mut bindings::serdev_device { > + self.0.get() > + } > +} > + > +impl Device { > + /// Set the baudrate in bits per second. > + /// > + /// Common baudrates are 115200, 9600, 19200, 57600, 4800. > + /// > + /// Use [`Device::write_flush`] before calling this if you have writ= ten data prior to this call. > + pub fn set_baudrate(&self, speed: u32) -> Result<(), u32> { > + // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a va= lid `serdev_device`. > + let ret =3D unsafe { bindings::serdev_device_set_baudrate(self.a= s_raw(), speed) }; > + if ret =3D=3D speed { > + Ok(()) > + } else { > + Err(ret) > + } > + } > + > + /// Set if flow control should be enabled. > + /// > + /// Use [`Device::write_flush`] before calling this if you have writ= ten data prior to this call. > + pub fn set_flow_control(&self, enable: bool) { > + // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a va= lid `serdev_device`. > + unsafe { bindings::serdev_device_set_flow_control(self.as_raw(),= enable) }; > + } > + > + /// Set parity to use. > + /// > + /// Use [`Device::write_flush`] before calling this if you have writ= ten data prior to this call. > + pub fn set_parity(&self, parity: Parity) -> Result { > + // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a va= lid `serdev_device`. > + to_result(unsafe { bindings::serdev_device_set_parity(self.as_ra= w(), parity as u32) }) > + } > + > + /// Write data to the serial device until the controller has accepte= d all the data or has > + /// been interrupted by a timeout or signal. > + /// > + /// Note that any accepted data has only been buffered by the contro= ller. Use > + /// [ Device::wait_until_sent`] to make sure the controller write bu= ffer has actually been > + /// emptied. > + /// > + /// Returns the number of bytes written (less than `data.len()` if i= nterrupted). > + /// [`kernel::error::code::ETIMEDOUT`] or [`kernel::error::code::ERE= STARTSYS`] if interrupted > + /// before any bytes were written. > + pub fn write_all(&self, data: &[u8], timeout: Timeout) -> Result { > + // SAFETY: > + // - `self.as_raw()` is guaranteed to be a pointer to a valid `s= erdev_device`. > + // - `data.as_ptr()` is guaranteed to be a valid array pointer w= ith the size of > + // `data.len()`. > + let ret =3D unsafe { > + bindings::serdev_device_write( > + self.as_raw(), > + data.as_ptr(), > + data.len(), > + timeout.into_jiffies(), > + ) > + }; > + // CAST: negative return values are guaranteed to be between `-M= AX_ERRNO` and `-1`, > + // which always fit into a `i32`. > + to_result(ret as i32).map(|()| ret.unsigned_abs()) > + } > + > + /// Write data to the serial device. > + /// > + /// If you want to write until the controller has accepted all the d= ata, use > + /// [`Device::write_all`]. > + /// > + /// Note that any accepted data has only been buffered by the contro= ller. Use > + /// [ Device::wait_until_sent`] to make sure the controller write bu= ffer has actually been > + /// emptied. > + /// > + /// Returns the number of bytes written (less than `data.len()` if n= ot enough room in the > + /// write buffer). > + pub fn write(&self, data: &[u8]) -> Result { > + // SAFETY: > + // - `self.as_raw()` is guaranteed to be a pointer to a valid `s= erdev_device`. > + // - `data.as_ptr()` is guaranteed to be a valid array pointer w= ith the size of > + // `data.len()`. > + let ret =3D > + unsafe { bindings::serdev_device_write_buf(self.as_raw(), da= ta.as_ptr(), data.len()) }; > + > + to_result(ret as i32).map(|()| ret.unsigned_abs()) > + } > + > + /// Send data to the serial device immediately. > + /// > + /// Note that this doesn't guarantee that the data has been transmit= ted. > + /// Use [`Device::wait_until_sent`] for this purpose. > + pub fn write_flush(&self) { > + // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a va= lid `serdev_device`. > + unsafe { bindings::serdev_device_write_flush(self.as_raw()) }; > + } > + > + /// Wait for the data to be sent. > + /// > + /// After this function, the write buffer of the controller should b= e empty. > + pub fn wait_until_sent(&self, timeout: Timeout) { > + // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a va= lid `serdev_device`. > + unsafe { bindings::serdev_device_wait_until_sent(self.as_raw(), = timeout.into_jiffies()) }; > + } > +} > + > +// SAFETY: `serdev::Device` is a transparent wrapper of `struct serdev_d= evice`. > +// The offset is guaranteed to point to a valid device field inside `ser= dev::Device`. > +unsafe impl device::AsBusDevice for Dev= ice { > + const OFFSET: usize =3D offset_of!(bindings::serdev_device, dev); > +} > + > +// SAFETY: `Device` is a transparent wrapper of a type that doesn't depe= nd on `Device`'s generic > +// argument. > +kernel::impl_device_context_deref!(unsafe { Device }); > +kernel::impl_device_context_into_aref!(Device); > + > +// SAFETY: Instances of `Device` are always reference-counted. > +unsafe impl AlwaysRefCounted for Device { > + fn inc_ref(&self) { > + self.as_ref().inc_ref(); > + } > + > + unsafe fn dec_ref(obj: NonNull) { > + // SAFETY: The safety requirements guarantee that the refcount i= s non-zero. > + unsafe { bindings::serdev_device_put(obj.cast().as_ptr()) } > + } > +} > + > +impl AsRef> for Device { > + fn as_ref(&self) -> &device::Device { > + // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a= pointer to a valid > + // `struct serdev_device`. > + let dev =3D unsafe { &raw mut (*self.as_raw()).dev }; > + > + // SAFETY: `dev` points to a valid `struct device`. > + unsafe { device::Device::from_raw(dev) } > + } > +} > + > +// SAFETY: A `Device` is always reference-counted and can be released fr= om any thread. > +unsafe impl Send for Device {} > + > +// SAFETY: `Device` can be shared among threads because all methods of `= Device` > +// (i.e. `Device) are thread safe. > +unsafe impl Sync for Device {} --=-PF5TUwaOf//AV1lgh2Mq Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- iQJPBAABCAA5FiEEgnQYxPSsWOdyMMRzNHYf+OetQ9IFAmmrOsMbFIAAAAAABAAO bWFudTIsMi41KzEuMTEsMiwyAAoJEDR2H/jnrUPSsiIQALNGzqaENBsRwZBmeBI1 WUFd9PW909Cy0jf6XQkCrdBEephIjoldQtNKI2UjuqkhHg+fO7vrty1gwirgbl2E Au3wcnG4cJOflXMEvuIBAL0j9ZtgOX1qEEdBFzgbcF9C4C3klIFdsLlN4IKf3vxw ajKSFieSHQLRkHXsBs8OCDYblifsUZtgf/60J4LHN6Wqq40Op57Ld6oLDr2hqsLo t+qPvrsWIQleDTW4nua55VlIJF+3gNkZ2VsQcMOtFZ+qZC6G/NtCPCR1W/KUb8Fs 23oHC5P6EVnk7HGAeTji+KMN2FqZBkvsIJNozgqxhxWNy5gsVxhCAMTjHDIU2YUI U1559f8FzCyOmbV0FoCPipG7jVmwlkBVdoluWeMUBLmsJz6VUXgeSLCA4vceak6k LccczD70O0TcO3W/tq0VLeaWk00Sbqbd2U/Uv2sQdAsRazr1H7G7nJ72h/ay7ipq CFEqEfUMxWPkcAGJD99cPtYiRTSub+LkNqR7kiuhZrB4T8QICqfMPfsTVmcYaKAx 8hAN02X6Pta7csUwa8RWJ5u/BVj6EW1ooQfY0rKHxAFvKpYtFCLnt0DVeVM8Z7+P qiQT043EobkaL40/Tz9HnxzOGTiX67FCLLsqDP1k9pT48p7/qBYnjIjWHo1F+wJH ovfLDdFhdjHw3b+3pFzT6Vw8 =wCue -----END PGP SIGNATURE----- --=-PF5TUwaOf//AV1lgh2Mq--