Skip to content
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

Keep ownership of spi::DriverRegistration #46

Merged
merged 2 commits into from
May 4, 2021
Merged
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions rust-module/mfrc522.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,15 @@ impl FileOperations for Mfrc522FileOps {
Err(_) => Err(Error::EINVAL),
// FIXME: Once the Command API is reworked, this will make more sense
Ok(CommandSuccess::BytesWritten(amount)) => Ok(amount),
NoAnswer => Ok(0),
Ok(CommandSuccess::NoAnswer) => Ok(0),
_ => Err(Error::EINVAL),
}
}
}

struct Mfrc522Driver {
misc: Pin<Box<miscdev::Registration>>,
// FIXME: We need to keep ownership of our SPI registration, or else it will get dropped
// when we'll return from the init() function
_spi: Pin<Box<spi::DriverRegistration>>,
_misc: Pin<Box<miscdev::Registration>>,
}

impl KernelModule for Mfrc522Driver {
Expand All @@ -104,10 +103,11 @@ impl KernelModule for Mfrc522Driver {
miscdev::Registration::new_pinned::<Mfrc522FileOps>(cstr!("mfrc522_chrdev"), None, ())?;

let mut spi =
spi::DriverRegistration::new(&THIS_MODULE, cstr!("mfrc522")).with_probe(mfrc522_probe);
spi.register()?;
spi::DriverRegistration::new_pinned(&THIS_MODULE, cstr!("mfrc522"))?
.with_probe(mfrc522_probe);
Copy link
Contributor

Choose a reason for hiding this comment

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

can't we just call .register here ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's why the FIXME is here for now cause I think it's ugly to do spi.as_mut().register().

We have a solution, which helps with cleanliness: remove the builder pattern (which I agree with) and pass all spi methods in the new_pinned function, and register the driver in the new_pinned function (this is what miscdev.rs does). The downside is that the function takes 3 more arguments, which will be None most of the time

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm removing the builder pattern

spi.as_mut().register()?; // FIXME: Not really pretty

Ok(Mfrc522Driver { misc })
Ok(Mfrc522Driver { _spi: spi, _misc: misc })
}
}

Expand Down