9
9
use crate :: bindings;
10
10
use crate :: error:: { Error , Result } ;
11
11
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 } ;
13
13
use alloc:: boxed:: Box ;
14
14
use core:: marker:: PhantomPinned ;
15
- use core:: { mem:: MaybeUninit , pin:: Pin } ;
15
+ use core:: { fmt , mem:: MaybeUninit , pin:: Pin } ;
16
16
17
17
/// Options which can be used to configure how a misc device is registered.
18
18
///
@@ -28,7 +28,7 @@ use core::{mem::MaybeUninit, pin::Pin};
28
28
/// .mode(0o600)
29
29
/// .minor(10)
30
30
/// .parent(parent)
31
- /// .register(reg, c_str !("sample"), ())
31
+ /// .register(reg, fmt !("sample"), ())
32
32
/// }
33
33
/// ```
34
34
#[ derive( Default ) ]
@@ -73,7 +73,7 @@ impl<'a> Options<'a> {
73
73
pub fn register < T : FileOperations > (
74
74
& self ,
75
75
reg : Pin < & mut Registration < T > > ,
76
- name : & ' static CStr ,
76
+ name : fmt :: Arguments < ' _ > ,
77
77
open_data : T :: OpenData ,
78
78
) -> Result {
79
79
reg. register_with_options ( name, open_data, self )
@@ -83,7 +83,7 @@ impl<'a> Options<'a> {
83
83
/// configured options.
84
84
pub fn register_new < T : FileOperations > (
85
85
& self ,
86
- name : & ' static CStr ,
86
+ name : fmt :: Arguments < ' _ > ,
87
87
open_data : T :: OpenData ,
88
88
) -> Result < Pin < Box < Registration < T > > > > {
89
89
let mut r = Pin :: from ( Box :: try_new ( Registration :: new ( ) ) ?) ;
@@ -100,6 +100,7 @@ impl<'a> Options<'a> {
100
100
pub struct Registration < T : FileOperations > {
101
101
registered : bool ,
102
102
mdev : bindings:: miscdevice ,
103
+ name : Option < CString > ,
103
104
_pin : PhantomPinned ,
104
105
105
106
/// Context initialised on construction and made available to all file instances on
@@ -116,6 +117,7 @@ impl<T: FileOperations> Registration<T> {
116
117
Self {
117
118
registered : false ,
118
119
mdev : bindings:: miscdevice:: default ( ) ,
120
+ name : None ,
119
121
_pin : PhantomPinned ,
120
122
open_data : MaybeUninit :: uninit ( ) ,
121
123
}
@@ -124,15 +126,19 @@ impl<T: FileOperations> Registration<T> {
124
126
/// Registers a miscellaneous device.
125
127
///
126
128
/// 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 > > > {
128
130
Options :: new ( ) . register_new ( name, open_data)
129
131
}
130
132
131
133
/// Registers a miscellaneous device with the rest of the kernel.
132
134
///
133
135
/// It must be pinned because the memory block that represents the registration is
134
136
/// 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 {
136
142
Options :: new ( ) . register ( self , name, open_data)
137
143
}
138
144
@@ -143,7 +149,7 @@ impl<T: FileOperations> Registration<T> {
143
149
/// self-referential.
144
150
pub fn register_with_options (
145
151
self : Pin < & mut Self > ,
146
- name : & ' static CStr ,
152
+ name : fmt :: Arguments < ' _ > ,
147
153
open_data : T :: OpenData ,
148
154
opts : & Options < ' _ > ,
149
155
) -> Result {
@@ -154,6 +160,8 @@ impl<T: FileOperations> Registration<T> {
154
160
return Err ( Error :: EINVAL ) ;
155
161
}
156
162
163
+ let name = CString :: try_from_fmt ( name) ?;
164
+
157
165
// SAFETY: The adapter is compatible with `misc_register`.
158
166
this. mdev . fops = unsafe { FileOperationsVtable :: < Self , T > :: build ( ) } ;
159
167
this. mdev . name = name. as_char_ptr ( ) ;
@@ -179,6 +187,8 @@ impl<T: FileOperations> Registration<T> {
179
187
return Err ( Error :: from_kernel_errno ( ret) ) ;
180
188
}
181
189
190
+ this. name = Some ( name) ;
191
+
182
192
Ok ( ( ) )
183
193
}
184
194
}
@@ -235,7 +245,7 @@ pub struct Module<T: FileOperations<OpenData = ()>> {
235
245
impl < T : FileOperations < OpenData = ( ) > > KernelModule for Module < T > {
236
246
fn init ( name : & ' static CStr , _module : & ' static ThisModule ) -> Result < Self > {
237
247
Ok ( Self {
238
- _dev : Registration :: new_pinned ( name, ( ) ) ?,
248
+ _dev : Registration :: new_pinned ( crate :: fmt! ( "{ name}" ) , ( ) ) ?,
239
249
} )
240
250
}
241
251
}
0 commit comments