From nobody Wed Jun 17 06:04:03 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C64673ACF04; Mon, 27 Apr 2026 18:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777313151; cv=none; b=uFsYDDLXcQakKwSHb5TL0RB4GEVbWaUIn2CeXiByToVNZzFy+swojhNEfS3VEsAmNu0UchiCZrrO3CyO61UThFOdOpr4T81FyvqiAbGddq2YLUvOuxFaNNEW3Nz0762s6NRjkPUHjm4MxnUTSUXJ2LylVyyFB3tc938v0U49yWI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777313151; c=relaxed/simple; bh=9E94tFildgqUx12/GlGdp2GUC/gHnUuATGZiGSlMpy8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FCD4BkwA7BZlIfpzxuW0mnzhJ37xzr0YF2TctOLTHpDSKkQ+Olk3TCiwh0tq/dGMehVDj9Z04K3v5RCuDsomcEuK+ZnPJe6Wvu3ISseeld41I/xZy0NA2+BQFQZpankfjZSqebFvWM/KKxNhymQ6LxZ0faEBRuWqVfEHKDX9zXM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uKh4k4sN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="uKh4k4sN" Received: by smtp.kernel.org (Postfix) with ESMTPS id 6F795C2BCB6; Mon, 27 Apr 2026 18:05:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777313151; bh=9E94tFildgqUx12/GlGdp2GUC/gHnUuATGZiGSlMpy8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=uKh4k4sNxSD43XHOaRmxVsFTZ+XoC3KwhQNu8uTnYeS+ZNLdnooT36qvnMF4UJa4I k5Ua+EvLr8cVptIW/MFySzKKWXQGQZIhLae3oFFaBbMMRqORg+Qy+Oo6yntHeMbOmb AQncR3NqRBXUxgh0GwdmudXez4AxjWvhrLr0+Rw5r0O5fsR4l7XQ5KBpf2ComCa2C2 UE1pmSNkKaht1F7x6EsYoI795ImEPCymLrjRZ8iVWxYzrkhw+o0p4Zc3AZGgkXF4g/ e1sAgw9RusevxirO0gjJqS7BhIDO4nQAJOCsfrZTb7tcX2skqQ3tAEQcj75vd2YSvS 7XX0D4uBA8eIA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D328FF8868; Mon, 27 Apr 2026 18:05:51 +0000 (UTC) From: Markus Probst via B4 Relay Date: Mon, 27 Apr 2026 20:05:41 +0200 Subject: [PATCH v6 1/4] rust: devres: return reference in `devres::register` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260427-rust_serdev-v6-1-173798d5e1a3@posteo.de> References: <20260427-rust_serdev-v6-0-173798d5e1a3@posteo.de> In-Reply-To: <20260427-rust_serdev-v6-0-173798d5e1a3@posteo.de> To: Rob Herring , Greg Kroah-Hartman , Jiri Slaby , Miguel Ojeda , Gary Guo , =?utf-8?q?Bj=C3=B6rn_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 , Boqun Feng 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, Markus Probst X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2621; i=markus.probst@posteo.de; h=from:subject:message-id; bh=oQKYQhuICX5DBtHaowGZbnJGuwpyDpM+GsZaRCaIX4M=; b=owEBiQJ2/ZANAwAIATR2H/jnrUPSAcsmYgBp76V9jveQ0d8UFFWCXH/ZYKXYbx1j5Z3L5kn+E Syf+XW02bKJAk8EAAEIADkWIQSCdBjE9KxY53IwxHM0dh/4561D0gUCae+lfRsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwyLDIACgkQNHYf+OetQ9KVHhAAjJc7tGq49faeum6CZGHoCRQI0UaRoRu 6SmBsvh1p2l28DUKaEn/dvKXyexu///bZNXPbFw/is3zD6RUn6CHPOmGQ/RHKq7Dhr9/yMCPKvb RwkRt/jgEL8fdG0R6doRrnQWtYVmu4CSv4H/mPq1LkoVbNv/6SSYrbhReWEpQyfzMk5ORou8Xhv 0rEhZmD7zmItkdBdgSh/T08HvqEGCwQavXjQjGOetef8e8lrNYTZY/6GZwQWlsKycoJkmgsIuWV gX89cUjDlTCdkkAAWOolNHgJ7ejQOu2fo/vZ2KcDpyofLKla2fBZOw6i3T66sYb9IfnJ2m5aawQ ZjTq0BAbX1Lgs6BAQh3bq2UQ1aPzAzjm4usmDfXYzcbed0W5NIj9g/55sdRTJpOifmXmpHun++8 kqKuTOjxGWpqtK1Vz+bFbjldF8iBvpo9n+D6C3uT/K1Pek7c4uqbZH2HMogF1WUCKX0RdVBa9j0 VcTAh+543GRxWWoYH3cPkRWH4ya5bKfC4ZtvV4cQLhO8oePehDW6Ttj3P4FUK+fqAq0zjcdmIsa hfFR6O7+3AXDXYWISRlFV1O2z0PpctoL7fOBK/0qC4DO0ZXHUVhXCQWDR5KEXHfYBnMzkD2vnYk GyvJAqtOI42mCna/Hr9ypH/8EsZz9urdeQ7I21Po3E0TZHc0bhKU= X-Developer-Key: i=markus.probst@posteo.de; a=openpgp; fpr=827418C4F4AC58E77230C47334761FF8E7AD43D2 X-Endpoint-Received: by B4 Relay for markus.probst@posteo.de/default with auth_id=680 X-Original-From: Markus Probst Reply-To: markus.probst@posteo.de From: Markus Probst Return the reference to the initialized data in the `devres::register` function. This is needed in a following commit (rust: add basic serial device bus abstractions). Acked-by: Viresh Kumar Signed-off-by: Markus Probst --- rust/kernel/cpufreq.rs | 3 ++- rust/kernel/devres.rs | 15 +++++++++++++-- rust/kernel/drm/driver.rs | 3 ++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs index d8d26870bea2..e1228530598f 100644 --- a/rust/kernel/cpufreq.rs +++ b/rust/kernel/cpufreq.rs @@ -1052,7 +1052,8 @@ pub fn new_foreign_owned(dev: &Device) -> Resu= lt where T: 'static, { - devres::register(dev, Self::new()?, GFP_KERNEL) + devres::register(dev, Self::new()?, GFP_KERNEL)?; + Ok(()) } } =20 diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs index 9e5f93aed20c..d3c9c376693d 100644 --- a/rust/kernel/devres.rs +++ b/rust/kernel/devres.rs @@ -419,15 +419,26 @@ fn register_foreign

(dev: &Device, data: P) = -> Result /// } /// /// fn from_bound_context(dev: &Device) -> Result { -/// devres::register(dev, Registration::new(), GFP_KERNEL) +/// devres::register(dev, Registration::new(), GFP_KERNEL)?; +/// Ok(()) /// } /// ``` -pub fn register(dev: &Device, data: impl PinInit, flags= : Flags) -> Result +pub fn register<'a, T, E>( + dev: &'a Device, + data: impl PinInit, + flags: Flags, +) -> Result<&'a T> where T: Send + 'static, Error: From, { let data =3D KBox::pin_init(data, flags)?; =20 + let data_ptr =3D &raw const *data; + register_foreign(dev, data) + // SAFETY: `dev` is valid for the lifetime of 'a. As long as there= is a reference to + // `Device`, it is guaranteed that the device is not unboun= d and data has not been + // dropped. Thus `data_ptr` is also valid for the lifetime of 'a. + .map(|()| unsafe { &*data_ptr }) } diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs index 5233bdebc9fc..1edfd7bacddb 100644 --- a/rust/kernel/drm/driver.rs +++ b/rust/kernel/drm/driver.rs @@ -147,7 +147,8 @@ pub fn new_foreign_owned( =20 let reg =3D Registration::::new(drm, flags)?; =20 - devres::register(dev, reg, GFP_KERNEL) + devres::register(dev, reg, GFP_KERNEL)?; + Ok(()) } =20 /// Returns a reference to the `Device` instance for this registration. --=20 2.53.0 From nobody Wed Jun 17 06:04:03 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6348359A65; Mon, 27 Apr 2026 18:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777313151; cv=none; b=Wc+2/mljGz8VpewVCR2a4A+MWHwK9G6DU15l467M1pd3IxDYnuDjk868Vmh+iOfZDp+Qc1DFdjTXTuefbvc3tdBUT1hPhlqgp2IpTrtL6fScSUtesWXrMQEnGWpdC3qU7CVhJxNDZ+dSkPYYZDg5GD1lG4gpgbtdQj1pGBprpLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777313151; c=relaxed/simple; bh=0ud1QnU6DSZTrgik1DuQ8GzbwSpCgKyutvxo4R3s3/Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jh9AkAPxbc2fO8wBpyFGoJDVFcnGf5BQovxEQiajNvo/DXMLwBTbQojKefj31AGZPG7xrwKuN9d3dEhvxRSfWO4JWH95nMyMo7PAsd5rJD+3CV9m4v9YabiG1b1y3FoAnSPdhlI21bNKodPnGWmIpjx3G/CBAEpT28u3nN50e74= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Vuo2Oh+V; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Vuo2Oh+V" Received: by smtp.kernel.org (Postfix) with ESMTPS id 86FB9C2BCB7; Mon, 27 Apr 2026 18:05:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777313151; bh=0ud1QnU6DSZTrgik1DuQ8GzbwSpCgKyutvxo4R3s3/Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Vuo2Oh+VsSSIBVNwrxYEiLEskYMdNrx7YnDItq6WOLf4NCHmfXxBX6hyH2t3vTe5p B7Xmk5dqMH5u9n3kVLKVKogVLckHrMLig4LONyDct0mY+3TNk8bqYWwfRPafBh1N+R rxxR0ffEM8J39NFSMDBpKefl1DO04fQ3vTxXa4fU448D/mVeT33AK1zOAocKA6qEE6 3KIQhcDZZNLbjbTCFRjcrynrQqD3n+e6h95XywS0LVUfIO9o/mcIvp245L5wTbbKIe pGa4Slm+2/R1x6ZTC1uN7/5X2La3EQJYcsSOAMWi00yhrNsrt4vXxKwManVsBJI8zG 2LcmLn3Ge66lw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 73892FF8869; Mon, 27 Apr 2026 18:05:51 +0000 (UTC) From: Markus Probst via B4 Relay Date: Mon, 27 Apr 2026 20:05:42 +0200 Subject: [PATCH v6 2/4] serdev: add rust private data to serdev_device Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260427-rust_serdev-v6-2-173798d5e1a3@posteo.de> References: <20260427-rust_serdev-v6-0-173798d5e1a3@posteo.de> In-Reply-To: <20260427-rust_serdev-v6-0-173798d5e1a3@posteo.de> To: Rob Herring , Greg Kroah-Hartman , Jiri Slaby , Miguel Ojeda , Gary Guo , =?utf-8?q?Bj=C3=B6rn_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 , Boqun Feng 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, Markus Probst X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1683; i=markus.probst@posteo.de; h=from:subject:message-id; bh=Lci9sJZ/Hd89WrWg8pDQ/wKU3djKPRT47wh61HUUHxo=; b=owEBiQJ2/ZANAwAIATR2H/jnrUPSAcsmYgBp76V9RbMszIgYWJvSWd0DQQvpRFArS72LDlcV3 7DTy3b7pj+JAk8EAAEIADkWIQSCdBjE9KxY53IwxHM0dh/4561D0gUCae+lfRsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwyLDIACgkQNHYf+OetQ9JuqA/+KWXf7SvsT8sOhGfeNvEPjyBdh3Zb7Su QYx71x8wz5G4X2BfWc1y3I4E6K+R9FeOjMzKGGZxSsgbKk95TumVive0o4+rG4hyNk7WD1RbeAp cQ+uOCUbhWlwSR+Iwnd+5AEADEBWxzOWn+QzvHa6RxMKhhMUwNd5QNumBtMy6JSyjrrpeG4v3v/ 7BWxPOHhooqtdbhhxSgSl+dub5IQCubhPEySPAhwQ3BdeD1iEXW2leQHDErHk/L6GoNn2fyf4aj 4tUqsX0qKBTnj8NbBHJ+txkl0BxRdEAWnoHqc8Bfx2F8JW7tjw/hahr10t+efWWTwg84ePqumlz RiBXatCl8m4Ju2QZGKhKl4VKOoi6KDrGxI6IoadKSDoWyWYBveZmqcN/TCe2MfgX/lx3K1nAv+2 0VbxqgLssAVYp+B1cbXp8XdwMvYyBSrPkdiVHRrqwT1/j5dKjY2RFrpYCGZ6gnpPD04n/OejVTv QRPyF4vx5fznUKHxFofDtbjrB6AevRJ7CwgS1rKq6O+ZaNiTk7l+CGg22EYwaqwXgIs2JJdov11 Jb+t1kQs1cmmcLu6qI8l6WUmcx2ai+z1+6I+UqhNiIespcshLjw1fBW3FzVAESd0zFt7ek5a/+S DKUZ1Qp6Fm5MT1NeJ8dQn7y2P2mHr2eh8IZJl2zZ0KmusC9UngJk= X-Developer-Key: i=markus.probst@posteo.de; a=openpgp; fpr=827418C4F4AC58E77230C47334761FF8E7AD43D2 X-Endpoint-Received: by B4 Relay for markus.probst@posteo.de/default with auth_id=680 X-Original-From: Markus Probst Reply-To: markus.probst@posteo.de From: Markus Probst Add rust private data to `struct serdev_device`, as it is required by the rust abstraction added in the following commit (rust: add basic serial device bus abstractions). Signed-off-by: Markus Probst --- include/linux/serdev.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/include/linux/serdev.h b/include/linux/serdev.h index b6c3d957ec15..048ef5857786 100644 --- a/include/linux/serdev.h +++ b/include/linux/serdev.h @@ -33,12 +33,14 @@ struct serdev_device_ops { =20 /** * struct serdev_device - Basic representation of an serdev device - * @dev: Driver model representation of the device. - * @nr: Device number on serdev bus. - * @ctrl: serdev controller managing this device. - * @ops: Device operations. - * @write_comp: Completion used by serdev_device_write() internally - * @write_lock: Lock to serialize access when writing data + * @dev: Driver model representation of the device. + * @nr: Device number on serdev bus. + * @ctrl: serdev controller managing this device. + * @ops: Device operations. + * @write_comp: Completion used by serdev_device_write() internally + * @write_lock: Lock to serialize access when writing data + * @rust_private_data: Private data for the rust abstraction. This should + * not be used by the C drivers. */ struct serdev_device { struct device dev; @@ -47,6 +49,7 @@ struct serdev_device { const struct serdev_device_ops *ops; struct completion write_comp; struct mutex write_lock; + void *rust_private_data; }; =20 #define to_serdev_device(d) container_of_const(d, struct serdev_device, de= v) --=20 2.53.0 From nobody Wed Jun 17 06:04:03 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC3643B47C5; Mon, 27 Apr 2026 18:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777313151; cv=none; b=EgnYeZEUUaGMaTWcM0/2mAEKFFJl0ufcS+xfYCu1iHlun+T452emN5Gcz4ixwko6kT//dlJ9Od7VPM555p6d5NwqVXZcuxL9doIBm2tJunMtPkF6cnuacoHElgRoYoJtL2y8O7ScsJPKpit+XzAg5ZOC0sBs6LvsTDE1y4UuAhQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777313151; c=relaxed/simple; bh=ZcVROABGepbXjlkrYDIOEwaBGXStQBzHTIJHpsD/sBQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NuLwCf6OmuhTcweuLXjyh1fJgzcDqDPfFZrMFO86UkaP229XWPV0vkiS+ll6DP12YvZKpkTIYxgtYf06DunBR6IbSgRP+rFRJfKRc+zyvdO8RKaFU+xH160ub2m98746V/tQn31YOkf3l+FabSDQSiYrYZVH76grUI34Xxj2Zyw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AEjm5i+8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AEjm5i+8" Received: by smtp.kernel.org (Postfix) with ESMTPS id 975FBC4AF15; Mon, 27 Apr 2026 18:05:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777313151; bh=ZcVROABGepbXjlkrYDIOEwaBGXStQBzHTIJHpsD/sBQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=AEjm5i+8vXcc0mrtjozNH0lg+EU4tOc+mQzDXGKC62/Bw4yXH+54FwcNDqJ1UyFnf 1TaH2CKA3M0AYWEMtdmsHA/QxyuP4kR76h/uE6tF0NdO22r/jHYiEEYMbyLEyBqGcq uPQc3WHBl430ezqShSXpxXcxof+9m1u16WUWD9HGzTRyHgw3XzwZHWtyo+dokVdOX3 fKGCQiPnzH/vYGZOKjHiQsjnhn5ficFefi0sLEPKnMcVL86CLDXUB254LKNvGBwce+ U6bUbgF1AzzevshoK1ShRh62ecYqbvcZjgUd7cuAuJ4k6e2KIuqflKMD1w8YESLGNk 4Pj5NR6PbG6Fw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D030FF8860; Mon, 27 Apr 2026 18:05:51 +0000 (UTC) From: Markus Probst via B4 Relay Date: Mon, 27 Apr 2026 20:05:43 +0200 Subject: [PATCH v6 3/4] rust: add basic serial device bus abstractions Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260427-rust_serdev-v6-3-173798d5e1a3@posteo.de> References: <20260427-rust_serdev-v6-0-173798d5e1a3@posteo.de> In-Reply-To: <20260427-rust_serdev-v6-0-173798d5e1a3@posteo.de> To: Rob Herring , Greg Kroah-Hartman , Jiri Slaby , Miguel Ojeda , Gary Guo , =?utf-8?q?Bj=C3=B6rn_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 , Boqun Feng 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, Markus Probst X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=23565; i=markus.probst@posteo.de; h=from:subject:message-id; bh=CVlMPnubQsHxIcfkX41wgKN3pAt2FdyJeUkOZ32Qakg=; b=owEBiQJ2/ZANAwAIATR2H/jnrUPSAcsmYgBp76V94YK1c6Y/g94dIu3SMFM12vSrUDVmoEMh7 nSyKbvQ6ceJAk8EAAEIADkWIQSCdBjE9KxY53IwxHM0dh/4561D0gUCae+lfRsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwyLDIACgkQNHYf+OetQ9IqBQ/9EMMRvI2g2FWCjBH+33v9pjwFRI1EV5m KfvuqcI11VZZBWtRvo5jee/kOg6OUWp5i8/DE+Q3A9rJPtEuYpoSp5UHt4Pu1DvV8Ndb3p2aR01 XG2TtTbkVTVmRmUFZuTg/TQOoYG7gGJaDPI9t2dMfdn881jRp573eDFXxCSAY9h2iuWSDPKn286 3yA+lRRNiwEPrFu6yaSAhLVGAtFJ7xalj0fVPueNll1HnzXh8VyHhrEi5ekmEO+7CH60I36B4GR 9atK4SThyuDE2i4R5r/SeWejfUk16gRi8iskmiLLSFu8C47tx9nGNaGsK31gSqL0GJin7wasvCW d75JVpeToj/OqcmTp5K5lEzmy6ssmqTc0C9CBvTeb2O9gsvR+SUv9L8eYxNeYXKQtAU7XXaG/bk Wb3k9Zt7ps5fYU3qGEev/0JxTj2OuGJaVN+TUDT+A9nX8YHK4V2eOil+oJnRhK6Daf4QDgV5kP9 cSMmk8HABKMI24C/5e6vDCbpCFQBxP6eycPiACmoh/cZ3WGuh/9kKbVj3TGsQoSLEnbrSAlGcZb uffNvN9Zq2J33WWibViRznvrYHzZJ98bf4ZvoJJStgGAtvGyETaHrfwMV5y5wLHeGTLUZ/45LTP jun5NDFKBRfHW1qSmG00UEV12cXK6kpFFqKxqlbh4xrBCPUpeRm4= X-Developer-Key: i=markus.probst@posteo.de; a=openpgp; fpr=827418C4F4AC58E77230C47334761FF8E7AD43D2 X-Endpoint-Received: by B4 Relay for markus.probst@posteo.de/default with auth_id=680 X-Original-From: Markus Probst Reply-To: markus.probst@posteo.de From: Markus Probst Implement the basic serial device bus abstractions required to write a serial device bus device driver with or without the need for initial device data. This includes the following data structures: The `serdev::Driver` trait represents the interface to the driver. The `serdev::Device` abstraction represents a `struct serdev_device`. In order to provide the Serdev specific parts to a generic `driver::Registration` the `driver::RegistrationOps` trait is implemented by `serdev::Adapter`. Signed-off-by: Markus Probst --- drivers/tty/serdev/Kconfig | 7 + 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 | 536 ++++++++++++++++++++++++++++++++++++= ++++ 6 files changed, 569 insertions(+) diff --git a/drivers/tty/serdev/Kconfig b/drivers/tty/serdev/Kconfig index 46ae732bfc68..e6dfe949ad01 100644 --- a/drivers/tty/serdev/Kconfig +++ b/drivers/tty/serdev/Kconfig @@ -9,6 +9,13 @@ menuconfig SERIAL_DEV_BUS =20 Note that you typically also want to enable TTY port controller support. =20 +config RUST_SERIAL_DEV_BUS_ABSTRACTIONS + bool "Rust Serial device bus abstractions" + depends on RUST + select SERIAL_DEV_BUS + help + This enables the Rust abstraction for the serial device bus API. + if SERIAL_DEV_BUS =20 config SERIAL_DEV_CTRL_TTYPORT diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index 446dbeaf0866..4e42635b8607 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -84,6 +84,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index 625921e27dfb..de14ef55029f 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -85,6 +85,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_driv= er *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 *serdev, + 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 b72b2fbe046d..83bc2c312241 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -118,6 +118,8 @@ pub mod scatterlist; pub mod security; pub mod seq_file; +#[cfg(CONFIG_RUST_SERIAL_DEV_BUS_ABSTRACTIONS)] +pub mod serdev; pub mod sizes; #[cfg(CONFIG_SOC_BUS)] pub mod soc; diff --git a/rust/kernel/serdev.rs b/rust/kernel/serdev.rs new file mode 100644 index 000000000000..d9dc0862bc88 --- /dev/null +++ b/rust/kernel/serdev.rs @@ -0,0 +1,536 @@ +// 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::{ + aref::AlwaysRefCounted, + Completion, // + }, + time::{ + msecs_to_jiffies, + Jiffies, + Msecs, // + }, + types::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_def= ault(), + Self::Milliseconds(value) =3D> { + msecs_to_jiffies(value.get()).try_into().unwrap_or_default= () + } + 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 `st= ruct 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::Driv= erType, driver); +} + +// SAFETY: A call to `unregister` for a given instance of `DriverType` is = 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_dr= iver` 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(sdrv.= 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::serdev_= 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) -> ke= rnel::ffi::c_int { + // SAFETY: The serial device bus only ever calls the probe callbac= k with a valid pointer to + // a `struct serdev_device`. + // + // INVARIANT: `sdev` is valid for the duration of `probe_callback(= )`. + 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 pointer= to `serdev_device`. + unsafe { + (*sdev.as_raw()).rust_private_data =3D + (&raw const *private_data).cast::().cast_mut() + }; + + // SAFETY: `sdev.as_raw()` is guaranteed to be a valid pointer= 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 cal= lback 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 callba= ck with a valid pointer + // to a `struct serdev_device`. + // + // INVARIANT: `sdev` is valid for the duration of `remove_callback= ()`. + let sdev =3D unsafe { &*sdev.cast::>(= ) }; + + // SAFETY: `remove_callback` is only ever called after a successfu= l call to + // `probe_callback`, hence it's guaranteed that `Device::set_drvda= ta()` has been called + // and stored a `Pin>`. + let data =3D unsafe { sdev.as_ref().drvdata_borrow::() }; + + T::unbind(sdev, data); + } + + 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 c= allback with a valid + // pointer to a `struct serdev_device`. + // + // INVARIANT: `sdev` is valid for the duration of `receive_buf_cal= lback()`. + let sdev =3D unsafe { &*sdev.cast::>(= ) }; + + // SAFETY: + // - The serial device bus only ever calls the receive buf callbac= k with a valid pointer to + // a `struct serdev_device`. + // - `receive_buf_callback` is only ever called after a successful= call to + // `probe_callback`, hence it's guaranteed that `sdev.private_da= ta` is a pointer + // to a valid `PrivateData`. + let private_data =3D unsafe { &*(*sdev.as_raw()).rust_private_data= .cast::() }; + + private_data.probe_complete.wait_for_completion(); + + // SAFETY: No one has exclusive access to `private_data.error`. + if unsafe { *private_data.error.get() } { + return length; + } + + // SAFETY: `receive_buf_callback` is only ever called after a succ= essful call to + // `probe_callback`, hence it's guaranteed that `Device::set_drvda= ta()` has been called + // and stored a `Pin>`. + let data =3D unsafe { sdev.as_ref().drvdata_borrow::() }; + + // SAFETY: `buf` is guaranteed to be non-null and has the size of = `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 devic= e 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 bus = 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(&OF_= TABLE); +/// const ACPI_ID_TABLE: Option> =3D Some(= &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 supporte= d 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 discovered. + /// 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`]. Imp= lementing this callback + /// is optional. + /// + /// This callback serves as a place for drivers to perform teardown op= erations that require a + /// `&Device` or `&Device` reference. For instance. + /// + /// Otherwise, release operations for driver resources should be perfo= rmed 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: &[u8]= ) -> 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 serdev_= device`. The +/// implementation abstracts the usage of an already existing C `struct se= rdev_device` within Rust +/// code that we get passed from the C side. +/// +/// # Invariants +/// +/// A [`Device`] instance represents a valid `struct serdev_device` create= d 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 writte= n 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 vali= d `serdev_device`. + let ret =3D unsafe { bindings::serdev_device_set_baudrate(self.as_= 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 writte= n 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 vali= d `serdev_device`. + unsafe { bindings::serdev_device_set_flow_control(self.as_raw(), e= nable) }; + } + + /// Set parity to use. + /// + /// Use [`Device::write_flush`] before calling this if you have writte= n 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 vali= d `serdev_device`. + to_result(unsafe { bindings::serdev_device_set_parity(self.as_raw(= ), parity as u32) }) + } + + /// Write data to the serial device until the controller has accepted = all the data or has + /// been interrupted by a timeout or signal. + /// + /// Note that any accepted data has only been buffered by the controll= er. Use + /// [`Device::wait_until_sent`] to make sure the controller write buff= er has actually been + /// emptied. + /// + /// Returns the number of bytes written (less than `data.len()` if int= errupted). + /// [`kernel::error::code::ETIMEDOUT`] or [`kernel::error::code::EREST= ARTSYS`] 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 `ser= dev_device`. + // - `data.as_ptr()` is guaranteed to be a valid array pointer wit= h 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 `-MAX= _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 dat= a, use + /// [`Device::write_all`]. + /// + /// Note that any accepted data has only been buffered by the controll= er. Use + /// [ Device::wait_until_sent`] to make sure the controller write buff= er has actually been + /// emptied. + /// + /// Returns the number of bytes written (less than `data.len()` if not= 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 `ser= dev_device`. + // - `data.as_ptr()` is guaranteed to be a valid array pointer wit= h the size of + // `data.len()`. + let ret =3D + unsafe { bindings::serdev_device_write_buf(self.as_raw(), data= .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 transmitte= d. + /// 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 vali= d `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 be = empty. + pub fn wait_until_sent(&self, timeout: Timeout) { + // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a vali= d `serdev_device`. + unsafe { bindings::serdev_device_wait_until_sent(self.as_raw(), ti= meout.into_jiffies()) }; + } +} + +// SAFETY: `serdev::Device` is a transparent wrapper of `struct serdev_dev= ice`. +// The offset is guaranteed to point to a valid device field inside `serde= v::Device`. +unsafe impl device::AsBusDevice for Devic= e { + const OFFSET: usize =3D offset_of!(bindings::serdev_device, dev); +} + +// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend= 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 is = 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 p= ointer 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 from= any thread. +unsafe impl Send for Device {} + +// SAFETY: `Device` can be shared among threads because all methods of `De= vice` +// (i.e. `Device) are thread safe. +unsafe impl Sync for Device {} --=20 2.53.0 From nobody Wed Jun 17 06:04:03 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0D0D3B9D97; Mon, 27 Apr 2026 18:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777313151; cv=none; b=s899ETDBRCByNndw8s1DJsJiUf1IPgnoU46dWmuufF2NBJNfIGvoTnCWStoofVlkycQQvjs4IdPw8gHDStTSfYZ5e9fftJ10hVUVlrLXjWUrf95bEipP1x6XVhnBABjf021YauLYQ6i4NEd+u/+I0yUzN7v2IftVDBiGUj87GZg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777313151; c=relaxed/simple; bh=g7dEQSlapKq6RGMjYWiDAE6F4/7WAy4mpD2ViWIE8lc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UekLY74gHGTqIvIz1IlZ6Up5gMBfR2+/arzTjJYnzaf+8r+xKNXqNmwvS1RnbekkWqJ4UxxwBtg41zmyc+uSg9jZzEVCLlGHABFATrIRVn6qLtuW0Ly7PFROIZavoas+balYPCH0SCTIB7lhmXs44HusOtXAkmvU6EEKEP0Om78= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=skW/v4/2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="skW/v4/2" Received: by smtp.kernel.org (Postfix) with ESMTPS id AECF9C2BCF6; Mon, 27 Apr 2026 18:05:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777313151; bh=g7dEQSlapKq6RGMjYWiDAE6F4/7WAy4mpD2ViWIE8lc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=skW/v4/2wrw6S+b+TinHxgWt1LUm0zq9kiVWfOTaOWDTi7/e7dj/kFX7yHVYFPpMZ sC4EdA14zbcBuFcvl4mj2yIq1NwneKSCxTLRadxSx9ST3u9F3lnKmZJkNUZRFS8GHa AJ6gM1ykLHBEPiVCasqAKBoZwjB2aVN5RFQcxDpwa3nccYc5uvJlVGUJy8XBadnrej 0OF/I+5b9e4Nw0FQHCQZ0CzOEPguMMLaJygmwJdqJ3dMtR+912KZxfnJRXrqjGELfe DQovAr2SE6Xyl3zEoT7ye1SvPuVFGGNHzPhwlFRtckFU/oYPYyG03LxJtFVjRLzgNb WugQbMkdQuT3A== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4E4FFF8869; Mon, 27 Apr 2026 18:05:51 +0000 (UTC) From: Markus Probst via B4 Relay Date: Mon, 27 Apr 2026 20:05:44 +0200 Subject: [PATCH v6 4/4] samples: rust: add Rust serial device bus sample device driver Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260427-rust_serdev-v6-4-173798d5e1a3@posteo.de> References: <20260427-rust_serdev-v6-0-173798d5e1a3@posteo.de> In-Reply-To: <20260427-rust_serdev-v6-0-173798d5e1a3@posteo.de> To: Rob Herring , Greg Kroah-Hartman , Jiri Slaby , Miguel Ojeda , Gary Guo , =?utf-8?q?Bj=C3=B6rn_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 , Boqun Feng 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, Markus Probst X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4237; i=markus.probst@posteo.de; h=from:subject:message-id; bh=6TYBQ32OeKPXuo7r972A3rP9t8yQQXh727SZs49HwIY=; b=owEBiQJ2/ZANAwAIATR2H/jnrUPSAcsmYgBp76V9lghfqn38x2LQ2dfJy5nUjDLTxde7mWh7P KSxxSrKcaSJAk8EAAEIADkWIQSCdBjE9KxY53IwxHM0dh/4561D0gUCae+lfRsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwyLDIACgkQNHYf+OetQ9LtNg//YOfRybt6cdFvPowzsGsib73kRTkQLGu /RQlyRQDW3VG9uwtcMo4tA42tZ+vJo5WPtKldfRSQEx3SQCW54ZzL9pjihtej7MAWUIHZMvAwzU 6n+mWojaLeeP5GQodk2bI2VsPipYJW9Qs+fzNDcG55cIPnIwZ5E03hb/FMPgkJEu9HNqcxDx6Xn tnx0anxZ5zMSEgikx2tXhDK/ZBLjXtS0vBUKgpeMFrFGSnmdIX8wMM/WLL5gxN8dzDW5SLEe2BD wOH6lwMWfxyQJSbfCE1VJ6A/jlHh/IIEa0VZhW7BxB7lnlu4zxb9uxsXe4C7ky+0uqH1h7XqUkV Xjlt3Vg5Yl+YEDVV2R+YTiSgP8w1HNebaoc2iraeUGkzJE+9Wb9ucbJbt4Rxb/80eGZZVxPOUJ6 M4Gtpd4YLcPpqbfSdpnPqD1t75kQPmUwnUud0y32Z3zKU26iNXaUuvOEexEZ2QgG+8jH51neE3t COWGiCw2dL/rxHX3OnvQVlbA3cZU+yiCiHHUwgMHeLVdlcffTsYwLmuOvZwPIwVifeVWacP9fVe NucNHTn7BA6Ij+J42d5bGtFVgWRKvpEA5w/TONKadhax3VUoXlz3mXG6U8pThc6fppAm8GrRett JP9Vms+TsnDzucazyNFuKwm9Gv6VYCRIlLl2tFv7F1GVCUH2haz8= X-Developer-Key: i=markus.probst@posteo.de; a=openpgp; fpr=827418C4F4AC58E77230C47334761FF8E7AD43D2 X-Endpoint-Received: by B4 Relay for markus.probst@posteo.de/default with auth_id=680 X-Original-From: Markus Probst Reply-To: markus.probst@posteo.de From: Markus Probst Add a sample Rust serial device bus device driver illustrating the usage of the serial device bus abstractions. This drivers probes through either a match of device / driver name or a match within the OF ID table. Signed-off-by: Markus Probst --- samples/rust/Kconfig | 11 +++++ samples/rust/Makefile | 1 + samples/rust/rust_driver_serdev.rs | 86 ++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 98 insertions(+) diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig index c49ab9106345..31d62533ef25 100644 --- a/samples/rust/Kconfig +++ b/samples/rust/Kconfig @@ -161,6 +161,17 @@ config SAMPLE_RUST_DRIVER_AUXILIARY =20 If unsure, say N. =20 +config SAMPLE_RUST_DRIVER_SERDEV + tristate "Serial Device Bus Device Driver" + select RUST_SERIAL_DEV_BUS_ABSTRACTIONS + help + This option builds the Rust serial device bus driver sample. + + To compile this as a module, choose M here: + the module will be called rust_driver_serdev. + + If unsure, say N. + config SAMPLE_RUST_SOC tristate "SoC Driver" select SOC_BUS diff --git a/samples/rust/Makefile b/samples/rust/Makefile index 6c0aaa58cccc..b986b681cde5 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -14,6 +14,7 @@ obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) +=3D rust_drive= r_platform.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) +=3D rust_driver_usb.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) +=3D rust_driver_faux.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) +=3D rust_driver_auxiliary.o +obj-$(CONFIG_SAMPLE_RUST_DRIVER_SERDEV) +=3D rust_driver_serdev.o obj-$(CONFIG_SAMPLE_RUST_CONFIGFS) +=3D rust_configfs.o obj-$(CONFIG_SAMPLE_RUST_SOC) +=3D rust_soc.o =20 diff --git a/samples/rust/rust_driver_serdev.rs b/samples/rust/rust_driver_= serdev.rs new file mode 100644 index 000000000000..8cf3fb451b22 --- /dev/null +++ b/samples/rust/rust_driver_serdev.rs @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust Serial device bus device driver sample. + +use kernel::{ + acpi, + device::{ + Bound, + Core, // + }, + of, + prelude::*, + serdev, + sync::aref::ARef, // +}; + +struct SampleDriver { + sdev: ARef, +} + +kernel::of_device_table!( + OF_TABLE, + MODULE_OF_TABLE, + ::IdInfo, + [(of::DeviceId::new(c"test,rust_driver_serdev"), ())] +); + +kernel::acpi_device_table!( + ACPI_TABLE, + MODULE_ACPI_TABLE, + ::IdInfo, + [(acpi::DeviceId::new(c"LNUXBEEF"), ())] +); + +#[vtable] +impl serdev::Driver for SampleDriver { + type IdInfo =3D (); + const OF_ID_TABLE: Option> =3D Some(&OF_TABL= E); + const ACPI_ID_TABLE: Option> =3D Some(&ACP= I_TABLE); + + fn probe( + sdev: &serdev::Device, + _info: Option<&Self::IdInfo>, + ) -> impl PinInit { + let dev =3D sdev.as_ref(); + + dev_dbg!(dev, "Probe Rust Serial device bus device driver sample.\= n"); + + if sdev + .set_baudrate( + dev.fwnode() + .and_then(|fwnode| fwnode.property_read(c"baudrate").o= ptional()) + .unwrap_or(115200), + ) + .is_err() + { + return Err(EINVAL); + } + sdev.set_flow_control(false); + sdev.set_parity(serdev::Parity::None)?; + + Ok(Self { sdev: sdev.into() }) + } + + fn receive(sdev: &serdev::Device, _this: Pin<&Self>, data: &[u8= ]) -> usize { + let _ =3D sdev.write_all(data, serdev::Timeout::Max); + data.len() + } +} + +impl Drop for SampleDriver { + fn drop(&mut self) { + dev_dbg!( + self.sdev.as_ref(), + "Remove Rust Serial device bus device driver sample.\n" + ); + } +} + +kernel::module_serdev_device_driver! { + type: SampleDriver, + name: "rust_driver_serdev", + authors: ["Markus Probst"], + description: "Rust Serial device bus device driver", + license: "GPL v2", +} --=20 2.53.0