forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 456
Add support for registering misc devices #44
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
use crate::error::{Error, KernelResult}; | ||
use crate::file_operations::{FileOperations, FileOperationsVtable}; | ||
use crate::{bindings, c_types, CStr}; | ||
use alloc::boxed::Box; | ||
use core::marker::PhantomPinned; | ||
use core::pin::Pin; | ||
|
||
/// A registration of a misc device. | ||
pub struct Registration { | ||
mdev: Option<bindings::miscdevice>, | ||
_pin: PhantomPinned, | ||
} | ||
|
||
impl Registration { | ||
/// Initialises a new registration but does not register it yet. It is allowed to move. | ||
pub fn new() -> Self { | ||
Self { | ||
mdev: None, | ||
_pin: PhantomPinned, | ||
} | ||
} | ||
|
||
/// Registers a new misc device. On success, it returns a pinned heap-allocated representation | ||
/// of the registration. | ||
pub fn new_pinned<T: FileOperations>( | ||
name: CStr<'static>, | ||
minor: Option<i32>, | ||
) -> KernelResult<Pin<Box<Self>>> { | ||
let mut r = crate::try_alloc_pinned(Self::new())?; | ||
r.as_mut().register::<T>(name, minor)?; | ||
Ok(r) | ||
} | ||
|
||
/// Attempts to actually register the misc device with the rest of the kernel. It must be | ||
/// pinned because the memory block that represents the registration is self-referential. If a | ||
/// minor is not given, the kernel allocates a new one if possible. | ||
pub fn register<T: FileOperations>( | ||
self: Pin<&mut Self>, | ||
name: CStr<'static>, | ||
minor: Option<i32>, | ||
) -> KernelResult<()> { | ||
// SAFETY: we must ensure that we never move out of `this`. | ||
let this = unsafe { self.get_unchecked_mut() }; | ||
if this.mdev.is_some() { | ||
// Already registered. | ||
return Err(Error::EINVAL); | ||
} | ||
|
||
this.mdev = Some(bindings::miscdevice::default()); | ||
let dev = this.mdev.as_mut().unwrap(); | ||
dev.fops = &FileOperationsVtable::<T>::VTABLE; | ||
dev.name = name.as_ptr() as *const c_types::c_char; | ||
dev.minor = minor.unwrap_or(bindings::MISC_DYNAMIC_MINOR as i32); | ||
let ret = unsafe { bindings::misc_register(dev) }; | ||
if ret < 0 { | ||
this.mdev = None; | ||
return Err(Error::from_kernel_errno(ret)); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
// SAFETY: The only method is `register`, which requires a (pinned) mutable `Registration`, so it | ||
// is safe to pass `&Registration` to multiple threads because it offers no interior mutability. | ||
unsafe impl Sync for Registration {} | ||
wedsonaf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
impl Drop for Registration { | ||
/// Removes the registration from the kernel if it has completed successfully before. | ||
fn drop(&mut self) { | ||
if let Some(ref mut dev) = self.mdev { | ||
unsafe { | ||
bindings::misc_deregister(dev); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.