Files
linux-stable-mirror/samples/rust/rust_driver_usb.rs
T
Danilo Krummrich 7fdffdda63 rust: driver: decouple driver private data from driver type
Add a type Data<'bound> associated type to all bus driver traits,
decoupling the driver's bus device private data type from the driver
struct itself.

In the context of adding a 'bound lifetime, making this an associated
type has the advantage that it allows us to avoid a driver trait global
lifetime and it avoids the need for ForLt for bus device private data;
both of which make the subsequent implementation by buses much simpler.

All existing drivers and doc examples set type Data = Self to preserve
the current behavior.

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Link: https://patch.msgid.link/20260525202921.124698-5-dakr@kernel.org
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2026-05-27 16:22:41 +02:00

56 lines
1.3 KiB
Rust

// SPDX-License-Identifier: GPL-2.0
// SPDX-FileCopyrightText: Copyright (C) 2025 Collabora Ltd.
//! Rust USB driver sample.
use kernel::{
device::{
self,
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 = ();
type Data = Self;
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",
}