diff --git a/src/chrdev.rs b/src/chrdev.rs index 969bc0c3..6c4749a4 100644 --- a/src/chrdev.rs +++ b/src/chrdev.rs @@ -1,5 +1,4 @@ use core::convert::TryInto; -use core::ops::Range; use core::{mem, ptr}; use alloc::boxed::Box; @@ -11,29 +10,30 @@ use crate::c_types; use crate::error::{Error, KernelResult}; use crate::user_ptr::{UserSlicePtr, UserSlicePtrWriter}; -pub fn builder(name: &'static str, minors: Range) -> KernelResult { +pub fn builder(name: &'static str) -> KernelResult { if !name.ends_with('\x00') { return Err(Error::EINVAL); } return Ok(Builder { name, - minors, + 0, file_ops: vec![], }); } pub struct Builder { name: &'static str, - minors: Range, + minor_start: u16, file_ops: Vec<&'static FileOperationsVtable>, } impl Builder { + pub fn start_minor_at(&mut self, minor_start: u16) { + self.minor_start = minor_start; + } + pub fn register_device(mut self) -> Builder { - if self.file_ops.len() >= self.minors.len() { - panic!("More devices registered than minor numbers allocated.") - } self.file_ops.push(&T::VTABLE); return self; } @@ -43,8 +43,8 @@ impl Builder { let res = unsafe { bindings::alloc_chrdev_region( &mut dev, - self.minors.start.into(), - self.minors.len().try_into()?, + self.minor_start.into(), + self.file_ops.len().try_into()?, self.name.as_ptr() as *const c_types::c_char, ) }; @@ -65,7 +65,7 @@ impl Builder { for j in 0..=i { bindings::cdev_del(&mut cdevs[j]); } - bindings::unregister_chrdev_region(dev, self.minors.len() as _); + bindings::unregister_chrdev_region(dev, self.file_ops.len() as _); return Err(Error::from_kernel_errno(rc)); } } @@ -73,7 +73,7 @@ impl Builder { return Ok(Registration { dev, - count: self.minors.len(), + count: self.file_ops.len(), cdevs, }); } diff --git a/tests/chrdev/src/lib.rs b/tests/chrdev/src/lib.rs index 79cfca83..d6b678d5 100644 --- a/tests/chrdev/src/lib.rs +++ b/tests/chrdev/src/lib.rs @@ -30,7 +30,7 @@ struct ChrdevTestModule { impl linux_kernel_module::KernelModule for ChrdevTestModule { fn init() -> linux_kernel_module::KernelResult { - let chrdev_registration = linux_kernel_module::chrdev::builder("chrdev-tests\x00", 0..1)? + let chrdev_registration = linux_kernel_module::chrdev::builder("chrdev-tests\x00")? .register_device::() .build()?; Ok(ChrdevTestModule {