@@ -13,6 +13,7 @@ use core::{
1313 marker:: { PhantomData , PhantomPinned } ,
1414 pin:: Pin ,
1515} ;
16+ use macros:: vtable;
1617
1718#[ cfg( CONFIG_GPIOLIB_IRQCHIP ) ]
1819pub use irqchip:: { ChipWithIrqChip , RegistrationWithIrqChip } ;
@@ -27,16 +28,13 @@ pub enum LineDirection {
2728}
2829
2930/// A gpio chip.
31+ #[ vtable]
3032pub 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