mirror of
https://github.com/torvalds/linux.git
synced 2025-12-07 20:06:24 +00:00
The driver model defines the lifetime of the private data stored in (and owned by) a bus device to be valid from when the driver is bound to a device (i.e. from successful probe()) until the driver is unbound from the device. This is already taken care of by the Rust implementation of the driver model. However, we still ask drivers to return a Result<Pin<KBox<Self>>> from probe(). Unlike in C, where we do not have the concept of initializers, but rather deal with uninitialized memory, drivers can just return an impl PinInit<Self, Error> instead. This contributes to more clarity to the fact that a driver returns it's device private data in probe() and the Rust driver model owns the data, manages the lifetime and - considering the lifetime - provides (safe) accessors for the driver. Hence, let probe() functions return an impl PinInit<Self, Error> instead of Result<Pin<KBox<Self>>>. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Viresh Kumar <viresh.kumar@linaro.org> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
47 lines
1.2 KiB
Rust
47 lines
1.2 KiB
Rust
// SPDX-License-Identifier: GPL-2.0
|
|
// SPDX-FileCopyrightText: Copyright (C) 2025 Collabora Ltd.
|
|
|
|
//! Rust USB driver sample.
|
|
|
|
use kernel::{device, device::Core, prelude::*, sync::aref::ARef, usb};
|
|
|
|
struct SampleDriver {
|
|
_intf: ARef<usb::Interface>,
|
|
}
|
|
|
|
kernel::usb_device_table!(
|
|
USB_TABLE,
|
|
MODULE_USB_TABLE,
|
|
<SampleDriver as usb::Driver>::IdInfo,
|
|
[(usb::DeviceId::from_id(0x1234, 0x5678), ()),]
|
|
);
|
|
|
|
impl usb::Driver for SampleDriver {
|
|
type IdInfo = ();
|
|
const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
|
|
|
|
fn probe(
|
|
intf: &usb::Interface<Core>,
|
|
_id: &usb::DeviceId,
|
|
_info: &Self::IdInfo,
|
|
) -> impl PinInit<Self, Error> {
|
|
let dev: &device::Device<Core> = intf.as_ref();
|
|
dev_info!(dev, "Rust USB driver sample probed\n");
|
|
|
|
Ok(Self { _intf: intf.into() })
|
|
}
|
|
|
|
fn disconnect(intf: &usb::Interface<Core>, _data: Pin<&Self>) {
|
|
let dev: &device::Device<Core> = intf.as_ref();
|
|
dev_info!(dev, "Rust USB driver sample disconnected\n");
|
|
}
|
|
}
|
|
|
|
kernel::module_usb_driver! {
|
|
type: SampleDriver,
|
|
name: "rust_driver_usb",
|
|
authors: ["Daniel Almeida"],
|
|
description: "Rust USB driver sample",
|
|
license: "GPL v2",
|
|
}
|