Skip to content
This repository has been archived by the owner on Mar 7, 2021. It is now read-only.

chrdev: Instead of passing a Range of minors, default to starting at 0 #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/chrdev.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::convert::TryInto;
use core::ops::Range;
use core::{mem, ptr};

use alloc::boxed::Box;
Expand All @@ -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<u16>) -> KernelResult<Builder> {
pub fn builder(name: &'static str) -> KernelResult<Builder> {
if !name.ends_with('\x00') {
return Err(Error::EINVAL);
}

return Ok(Builder {
name,
minors,
0,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
0,
minor_start: 0,

file_ops: vec![],
});
}

pub struct Builder {
name: &'static str,
minors: Range<u16>,
minor_start: u16,
file_ops: Vec<&'static FileOperationsVtable>,
}

impl Builder {
pub fn start_minor_at(&mut self, minor_start: u16) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this should take self and return Buider, like register_device does.

self.minor_start = minor_start;
}

pub fn register_device<T: FileOperations>(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;
}
Expand All @@ -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,
)
};
Expand All @@ -65,15 +65,15 @@ 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));
}
}
}

return Ok(Registration {
dev,
count: self.minors.len(),
count: self.file_ops.len(),
cdevs,
});
}
Expand Down
2 changes: 1 addition & 1 deletion tests/chrdev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct ChrdevTestModule {

impl linux_kernel_module::KernelModule for ChrdevTestModule {
fn init() -> linux_kernel_module::KernelResult<Self> {
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::<CycleFile>()
.build()?;
Ok(ChrdevTestModule {
Expand Down