99use crate :: bindings;
1010use crate :: error:: { Error , Result } ;
1111use crate :: file_operations:: { FileOpenAdapter , FileOperations , FileOperationsVtable } ;
12- use crate :: { device, str:: CStr , KernelModule , ThisModule } ;
12+ use crate :: { device, str:: CStr , str :: CString , KernelModule , ThisModule } ;
1313use alloc:: boxed:: Box ;
1414use 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> {
100100pub 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 = ()>> {
235245impl < 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}
0 commit comments