|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +use crate::error::{Error, KernelResult}; |
| 4 | +use crate::file_operations::{FileOperations, FileOperationsVtable}; |
| 5 | +use crate::{bindings, c_types, CStr}; |
| 6 | +use alloc::boxed::Box; |
| 7 | +use core::marker::PhantomPinned; |
| 8 | +use core::pin::Pin; |
| 9 | + |
| 10 | +/// A registration of a misc device. |
| 11 | +pub struct Registration { |
| 12 | + mdev: Option<bindings::miscdevice>, |
| 13 | + _pin: PhantomPinned, |
| 14 | +} |
| 15 | + |
| 16 | +impl Registration { |
| 17 | + /// Initialises a new registration but does not register it yet. It is allowed to move. |
| 18 | + pub fn new() -> Self { |
| 19 | + Self { |
| 20 | + mdev: None, |
| 21 | + _pin: PhantomPinned, |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + /// Registers a new misc device. On success, it returns a pinned heap-allocated representation |
| 26 | + /// of the registration. |
| 27 | + pub fn new_pinned<T: FileOperations>( |
| 28 | + name: CStr<'static>, |
| 29 | + minor: Option<i32>, |
| 30 | + ) -> KernelResult<Pin<Box<Self>>> { |
| 31 | + let mut r = crate::try_alloc_pinned(Self::new())?; |
| 32 | + r.as_mut().register::<T>(name, minor)?; |
| 33 | + Ok(r) |
| 34 | + } |
| 35 | + |
| 36 | + /// Attempts to actually register the misc device with the rest of the kernel. It must be |
| 37 | + /// pinned because the memory block that represents the registration is self-referential. If a |
| 38 | + /// minor is not given, the kernel allocates a new one if possible. |
| 39 | + pub fn register<T: FileOperations>( |
| 40 | + self: Pin<&mut Self>, |
| 41 | + name: CStr<'static>, |
| 42 | + minor: Option<i32>, |
| 43 | + ) -> KernelResult<()> { |
| 44 | + // SAFETY: we must ensure that we never move out of `this`. |
| 45 | + let this = unsafe { self.get_unchecked_mut() }; |
| 46 | + if this.mdev.is_some() { |
| 47 | + // Already registered. |
| 48 | + return Err(Error::EINVAL); |
| 49 | + } |
| 50 | + |
| 51 | + this.mdev = Some(bindings::miscdevice::default()); |
| 52 | + let dev = this.mdev.as_mut().unwrap(); |
| 53 | + dev.fops = &FileOperationsVtable::<T>::VTABLE; |
| 54 | + dev.name = name.as_ptr() as *const c_types::c_char; |
| 55 | + dev.minor = minor.unwrap_or(bindings::MISC_DYNAMIC_MINOR as i32); |
| 56 | + let ret = unsafe { bindings::misc_register(dev) }; |
| 57 | + if ret < 0 { |
| 58 | + this.mdev = None; |
| 59 | + return Err(Error::from_kernel_errno(ret)); |
| 60 | + } |
| 61 | + Ok(()) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// SAFETY: The only method is `register`, which requires a (pinned) mutable `Registration`, so it |
| 66 | +// is safe to pass `&Registration` to multiple threads because it offers no interior mutability. |
| 67 | +unsafe impl Sync for Registration {} |
| 68 | + |
| 69 | +impl Drop for Registration { |
| 70 | + /// Removes the registration from the kernel if it has completed successfully before. |
| 71 | + fn drop(&mut self) { |
| 72 | + if let Some(ref mut dev) = self.mdev { |
| 73 | + unsafe { |
| 74 | + bindings::misc_deregister(dev); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments