Skip to content

Add SPI abstraction in Rust #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/kernel/bindings_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <linux/miscdevice.h>
#include <linux/poll.h>
#include <linux/mm.h>
#include <linux/spi/spi.h>
#include <uapi/linux/android/binder.h>

// `bindgen` gets confused at certain things
Expand Down
3 changes: 3 additions & 0 deletions rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pub mod user_ptr;
pub use crate::error::{Error, KernelResult};
pub use crate::types::{CStr, Mode};

#[cfg(CONFIG_SPI)]
pub mod spi;

/// Page size defined in terms of the `PAGE_SHIFT` macro from C.
///
/// [`PAGE_SHIFT`]: ../../../include/asm-generic/page.h
Expand Down
33 changes: 33 additions & 0 deletions rust/kernel/spi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: GPL-2.0

use crate::bindings;
use crate::c_types;
use crate::error::{Error, KernelResult};

pub struct Registration {
this_module: &'static crate::ThisModule,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the module have to be stat of the registration?

spi_driver: bindings::spi_driver,
prout
}

impl Registration {
pub fn new(this_module: &'static crate::ThisModule,
spi_driver: bindings::spi_driver
) -> Self {
Registration {
this_module,
spi_driver
}
}

pub fn register(&mut self) -> KernelResult {
let res = unsafe { bindings::__spi_register_driver(self.this_module.0, &mut self.spi_driver as
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does __* need a lock?

*mut bindings::spi_driver) };

if res != 0 {
return Err(Error::from_kernel_errno(res));
}

Ok(())
}
}