Skip to content

Commit 06126b3

Browse files
committed
rust: use #[vtable] for kernel::gpio::Chip
Signed-off-by: Gary Guo <[email protected]>
1 parent 0cf25ee commit 06126b3

File tree

2 files changed

+9
-64
lines changed

2 files changed

+9
-64
lines changed

drivers/gpio/gpio_pl061_rust.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,10 @@ type DeviceData = device::Data<PL061Registrations, PL061Resources, PL061Data>;
5555

5656
struct PL061Device;
5757

58+
#[vtable]
5859
impl gpio::Chip for PL061Device {
5960
type Data = Ref<DeviceData>;
6061

61-
kernel::declare_gpio_chip_operations!(
62-
get_direction,
63-
direction_input,
64-
direction_output,
65-
get,
66-
set
67-
);
68-
6962
fn get_direction(data: RefBorrow<'_, DeviceData>, offset: u32) -> Result<gpio::LineDirection> {
7063
let pl061 = data.resources().ok_or(ENXIO)?;
7164
Ok(if pl061.base.readb(GPIODIR) & bit(offset) != 0 {

rust/kernel/gpio.rs

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//!
55
//! C header: [`include/linux/gpio/driver.h`](../../../../include/linux/gpio/driver.h)
66
7+
use crate::prelude::*;
78
use crate::{
89
bindings, device, error::code::*, error::from_kernel_result, sync::LockClassKey,
910
types::PointerWrapper, Error, Result,
@@ -27,16 +28,13 @@ pub enum LineDirection {
2728
}
2829

2930
/// A gpio chip.
31+
#[vtable]
3032
pub trait Chip {
3133
/// Context data associated with the gpio chip.
3234
///
3335
/// It determines the type of the context data passed to each of the methods of the trait.
3436
type Data: PointerWrapper + Sync + Send;
3537

36-
/// The methods to use to populate [`struct gpio_chip`]. This is typically populated with
37-
/// [`declare_gpio_chip_operations`].
38-
const TO_USE: ToUse;
39-
4038
/// Returns the direction of the given gpio line.
4139
fn get_direction(
4240
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
@@ -73,52 +71,6 @@ pub trait Chip {
7371
fn set(_data: <Self::Data as PointerWrapper>::Borrowed<'_>, _offset: u32, _value: bool) {}
7472
}
7573

76-
/// Represents which fields of [`struct gpio_chip`] should be populated with pointers.
77-
///
78-
/// This is typically populated with the [`declare_gpio_chip_operations`] macro.
79-
pub struct ToUse {
80-
/// The `get_direction` field of [`struct gpio_chip`].
81-
pub get_direction: bool,
82-
83-
/// The `direction_input` field of [`struct gpio_chip`].
84-
pub direction_input: bool,
85-
86-
/// The `direction_output` field of [`struct gpio_chip`].
87-
pub direction_output: bool,
88-
89-
/// The `get` field of [`struct gpio_chip`].
90-
pub get: bool,
91-
92-
/// The `set` field of [`struct gpio_chip`].
93-
pub set: bool,
94-
}
95-
96-
/// A constant version where all values are set to `false`, that is, all supported fields will be
97-
/// set to null pointers.
98-
pub const USE_NONE: ToUse = ToUse {
99-
get_direction: false,
100-
direction_input: false,
101-
direction_output: false,
102-
get: false,
103-
set: false,
104-
};
105-
106-
/// Defines the [`Chip::TO_USE`] field based on a list of fields to be populated.
107-
#[macro_export]
108-
macro_rules! declare_gpio_chip_operations {
109-
() => {
110-
const TO_USE: $crate::gpio::ToUse = $crate::gpio::USE_NONE;
111-
};
112-
($($i:ident),+) => {
113-
#[allow(clippy::needless_update)]
114-
const TO_USE: $crate::gpio::ToUse =
115-
$crate::gpio::ToUse {
116-
$($i: true),+ ,
117-
..$crate::gpio::USE_NONE
118-
};
119-
};
120-
}
121-
12274
/// A registration of a gpio chip.
12375
///
12476
/// # Examples
@@ -130,9 +82,9 @@ macro_rules! declare_gpio_chip_operations {
13082
/// use kernel::{device::RawDevice, gpio::{self, Registration}};
13183
///
13284
/// struct MyGpioChip;
85+
/// #[vtable]
13386
/// impl gpio::Chip for MyGpioChip {
13487
/// type Data = ();
135-
/// kernel::declare_gpio_chip_operations!();
13688
/// }
13789
///
13890
/// fn example(parent: &dyn RawDevice) -> Result<Pin<Box<Registration<MyGpioChip>>>> {
@@ -186,19 +138,19 @@ impl<T: Chip> Registration<T> {
186138
// Set up the callbacks.
187139
gc.request = Some(bindings::gpiochip_generic_request);
188140
gc.free = Some(bindings::gpiochip_generic_free);
189-
if T::TO_USE.get_direction {
141+
if T::HAS_GET_DIRECTION {
190142
gc.get_direction = Some(get_direction_callback::<T>);
191143
}
192-
if T::TO_USE.direction_input {
144+
if T::HAS_DIRECTION_INPUT {
193145
gc.direction_input = Some(direction_input_callback::<T>);
194146
}
195-
if T::TO_USE.direction_output {
147+
if T::HAS_DIRECTION_OUTPUT {
196148
gc.direction_output = Some(direction_output_callback::<T>);
197149
}
198-
if T::TO_USE.get {
150+
if T::HAS_GET {
199151
gc.get = Some(get_callback::<T>);
200152
}
201-
if T::TO_USE.set {
153+
if T::HAS_SET {
202154
gc.set = Some(set_callback::<T>);
203155
}
204156

0 commit comments

Comments
 (0)