Skip to content

Commit e2c5979

Browse files
authored
Merge pull request #677 from wedsonaf/miscdev-name
rust: miscdev: support names created at runtime
2 parents 0223500 + df111f3 commit e2c5979

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

drivers/android/rust_binder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ struct BinderModule {
105105
impl KernelModule for BinderModule {
106106
fn init(name: &'static CStr, _module: &'static kernel::ThisModule) -> Result<Self> {
107107
let ctx = Context::new()?;
108-
let reg = Registration::new_pinned(name, ctx)?;
108+
let reg = Registration::new_pinned(fmt!("{name}"), ctx)?;
109109
Ok(Self { _reg: reg })
110110
}
111111
}

drivers/char/hw_random/bcm2835_rng_rust.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Broadcom BCM2835 Random Number Generator support.
44
55
use kernel::{
6-
c_str, device, file::File, file_operations::FileOperations, io_buffer::IoBufferWriter, miscdev,
6+
device, file::File, file_operations::FileOperations, io_buffer::IoBufferWriter, miscdev,
77
module_platform_driver, of, platform, prelude::*, sync::Ref,
88
};
99

@@ -57,7 +57,7 @@ impl platform::Driver for RngDriver {
5757
data.registrations()
5858
.ok_or(Error::ENXIO)?
5959
.as_pinned_mut()
60-
.register(c_str!("rust_hwrng"), ())?;
60+
.register(fmt!("rust_hwrng"), ())?;
6161
Ok(data.into())
6262
}
6363
}

rust/kernel/miscdev.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
use crate::bindings;
1010
use crate::error::{Error, Result};
1111
use crate::file_operations::{FileOpenAdapter, FileOperations, FileOperationsVtable};
12-
use crate::{device, str::CStr, KernelModule, ThisModule};
12+
use crate::{device, str::CStr, str::CString, KernelModule, ThisModule};
1313
use alloc::boxed::Box;
1414
use core::marker::PhantomPinned;
15-
use core::{mem::MaybeUninit, pin::Pin};
15+
use core::{fmt, mem::MaybeUninit, pin::Pin};
1616

1717
/// Options which can be used to configure how a misc device is registered.
1818
///
@@ -28,7 +28,7 @@ use core::{mem::MaybeUninit, pin::Pin};
2828
/// .mode(0o600)
2929
/// .minor(10)
3030
/// .parent(parent)
31-
/// .register(reg, c_str!("sample"), ())
31+
/// .register(reg, fmt!("sample"), ())
3232
/// }
3333
/// ```
3434
#[derive(Default)]
@@ -73,7 +73,7 @@ impl<'a> Options<'a> {
7373
pub fn register<T: FileOperations>(
7474
&self,
7575
reg: Pin<&mut Registration<T>>,
76-
name: &'static CStr,
76+
name: fmt::Arguments<'_>,
7777
open_data: T::OpenData,
7878
) -> Result {
7979
reg.register_with_options(name, open_data, self)
@@ -83,7 +83,7 @@ impl<'a> Options<'a> {
8383
/// configured options.
8484
pub fn register_new<T: FileOperations>(
8585
&self,
86-
name: &'static CStr,
86+
name: fmt::Arguments<'_>,
8787
open_data: T::OpenData,
8888
) -> Result<Pin<Box<Registration<T>>>> {
8989
let mut r = Pin::from(Box::try_new(Registration::new())?);
@@ -100,6 +100,7 @@ impl<'a> Options<'a> {
100100
pub struct Registration<T: FileOperations> {
101101
registered: bool,
102102
mdev: bindings::miscdevice,
103+
name: Option<CString>,
103104
_pin: PhantomPinned,
104105

105106
/// Context initialised on construction and made available to all file instances on
@@ -116,6 +117,7 @@ impl<T: FileOperations> Registration<T> {
116117
Self {
117118
registered: false,
118119
mdev: bindings::miscdevice::default(),
120+
name: None,
119121
_pin: PhantomPinned,
120122
open_data: MaybeUninit::uninit(),
121123
}
@@ -124,15 +126,19 @@ impl<T: FileOperations> Registration<T> {
124126
/// Registers a miscellaneous device.
125127
///
126128
/// Returns a pinned heap-allocated representation of the registration.
127-
pub fn new_pinned(name: &'static CStr, open_data: T::OpenData) -> Result<Pin<Box<Self>>> {
129+
pub fn new_pinned(name: fmt::Arguments<'_>, open_data: T::OpenData) -> Result<Pin<Box<Self>>> {
128130
Options::new().register_new(name, open_data)
129131
}
130132

131133
/// Registers a miscellaneous device with the rest of the kernel.
132134
///
133135
/// It must be pinned because the memory block that represents the registration is
134136
/// self-referential.
135-
pub fn register(self: Pin<&mut Self>, name: &'static CStr, open_data: T::OpenData) -> Result {
137+
pub fn register(
138+
self: Pin<&mut Self>,
139+
name: fmt::Arguments<'_>,
140+
open_data: T::OpenData,
141+
) -> Result {
136142
Options::new().register(self, name, open_data)
137143
}
138144

@@ -143,7 +149,7 @@ impl<T: FileOperations> Registration<T> {
143149
/// self-referential.
144150
pub fn register_with_options(
145151
self: Pin<&mut Self>,
146-
name: &'static CStr,
152+
name: fmt::Arguments<'_>,
147153
open_data: T::OpenData,
148154
opts: &Options<'_>,
149155
) -> Result {
@@ -154,6 +160,8 @@ impl<T: FileOperations> Registration<T> {
154160
return Err(Error::EINVAL);
155161
}
156162

163+
let name = CString::try_from_fmt(name)?;
164+
157165
// SAFETY: The adapter is compatible with `misc_register`.
158166
this.mdev.fops = unsafe { FileOperationsVtable::<Self, T>::build() };
159167
this.mdev.name = name.as_char_ptr();
@@ -179,6 +187,8 @@ impl<T: FileOperations> Registration<T> {
179187
return Err(Error::from_kernel_errno(ret));
180188
}
181189

190+
this.name = Some(name);
191+
182192
Ok(())
183193
}
184194
}
@@ -235,7 +245,7 @@ pub struct Module<T: FileOperations<OpenData = ()>> {
235245
impl<T: FileOperations<OpenData = ()>> KernelModule for Module<T> {
236246
fn init(name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
237247
Ok(Self {
238-
_dev: Registration::new_pinned(name, ())?,
248+
_dev: Registration::new_pinned(crate::fmt!("{name}"), ())?,
239249
})
240250
}
241251
}

samples/rust/rust_miscdev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl KernelModule for RustMiscdev {
132132
let state = SharedState::try_new()?;
133133

134134
Ok(RustMiscdev {
135-
_dev: miscdev::Registration::new_pinned(name, state)?,
135+
_dev: miscdev::Registration::new_pinned(fmt!("{name}"), state)?,
136136
})
137137
}
138138
}

samples/rust/rust_semaphore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl KernelModule for RustSemaphore {
133133
mutex_init!(pinned, "Semaphore::inner");
134134

135135
Ok(Self {
136-
_dev: Registration::new_pinned(name, sema.into())?,
136+
_dev: Registration::new_pinned(fmt!("{name}"), sema.into())?,
137137
})
138138
}
139139
}

0 commit comments

Comments
 (0)