8
8
9
9
use crate :: bindings;
10
10
use crate :: error:: { Error , Result } ;
11
- use crate :: file_operations:: { FileOpenAdapter , FileOpener , FileOperationsVtable } ;
11
+ use crate :: file_operations:: { FileOpenAdapter , FileOperations , FileOperationsVtable } ;
12
12
use crate :: { str:: CStr , KernelModule , ThisModule } ;
13
13
use alloc:: boxed:: Box ;
14
- use core:: marker:: { PhantomData , PhantomPinned } ;
14
+ use core:: marker:: PhantomPinned ;
15
15
use core:: { mem:: MaybeUninit , pin:: Pin } ;
16
16
17
17
/// A registration of a miscellaneous device.
18
18
///
19
19
/// # Invariants
20
20
///
21
21
/// `Context` is always initialised when `registered` is `true`, and not initialised otherwise.
22
- pub struct Registration < T : Sync = ( ) > {
22
+ pub struct Registration < T : FileOperations > {
23
23
registered : bool ,
24
24
mdev : bindings:: miscdevice ,
25
25
_pin : PhantomPinned ,
26
26
27
27
/// Context initialised on construction and made available to all file instances on
28
- /// [`FileOpener ::open`].
29
- open_data : MaybeUninit < T > ,
28
+ /// [`FileOperations ::open`].
29
+ open_data : MaybeUninit < T :: OpenData > ,
30
30
}
31
31
32
- impl < T : Sync > Registration < T > {
32
+ impl < T : FileOperations > Registration < T > {
33
33
/// Creates a new [`Registration`] but does not register it yet.
34
34
///
35
35
/// It is allowed to move.
@@ -46,25 +46,25 @@ impl<T: Sync> Registration<T> {
46
46
/// Registers a miscellaneous device.
47
47
///
48
48
/// Returns a pinned heap-allocated representation of the registration.
49
- pub fn new_pinned < F : FileOpener < T > > (
49
+ pub fn new_pinned (
50
50
name : & ' static CStr ,
51
51
minor : Option < i32 > ,
52
- open_data : T ,
52
+ open_data : T :: OpenData ,
53
53
) -> Result < Pin < Box < Self > > > {
54
54
let mut r = Pin :: from ( Box :: try_new ( Self :: new ( ) ) ?) ;
55
- r. as_mut ( ) . register :: < F > ( name, minor, open_data) ?;
55
+ r. as_mut ( ) . register ( name, minor, open_data) ?;
56
56
Ok ( r)
57
57
}
58
58
59
59
/// Registers a miscellaneous device with the rest of the kernel.
60
60
///
61
61
/// It must be pinned because the memory block that represents the registration is
62
62
/// self-referential. If a minor is not given, the kernel allocates a new one if possible.
63
- pub fn register < F : FileOpener < T > > (
63
+ pub fn register (
64
64
self : Pin < & mut Self > ,
65
65
name : & ' static CStr ,
66
66
minor : Option < i32 > ,
67
- open_data : T ,
67
+ open_data : T :: OpenData ,
68
68
) -> Result {
69
69
// SAFETY: We must ensure that we never move out of `this`.
70
70
let this = unsafe { self . get_unchecked_mut ( ) } ;
@@ -74,7 +74,7 @@ impl<T: Sync> Registration<T> {
74
74
}
75
75
76
76
// SAFETY: The adapter is compatible with `misc_register`.
77
- this. mdev . fops = unsafe { FileOperationsVtable :: < Self , F > :: build ( ) } ;
77
+ this. mdev . fops = unsafe { FileOperationsVtable :: < Self , T > :: build ( ) } ;
78
78
this. mdev . name = name. as_char_ptr ( ) ;
79
79
this. mdev . minor = minor. unwrap_or ( bindings:: MISC_DYNAMIC_MINOR as i32 ) ;
80
80
@@ -98,16 +98,17 @@ impl<T: Sync> Registration<T> {
98
98
}
99
99
}
100
100
101
- impl < T : Sync > Default for Registration < T > {
101
+ impl < T : FileOperations > Default for Registration < T > {
102
102
fn default ( ) -> Self {
103
103
Self :: new ( )
104
104
}
105
105
}
106
106
107
- impl < T : Sync > FileOpenAdapter for Registration < T > {
108
- type Arg = T ;
109
-
110
- unsafe fn convert ( _inode : * mut bindings:: inode , file : * mut bindings:: file ) -> * const Self :: Arg {
107
+ impl < T : FileOperations > FileOpenAdapter < T :: OpenData > for Registration < T > {
108
+ unsafe fn convert (
109
+ _inode : * mut bindings:: inode ,
110
+ file : * mut bindings:: file ,
111
+ ) -> * const T :: OpenData {
111
112
// SAFETY: the caller must guarantee that `file` is valid.
112
113
let reg = crate :: container_of!( unsafe { ( * file) . private_data } , Self , mdev) ;
113
114
@@ -120,14 +121,13 @@ impl<T: Sync> FileOpenAdapter for Registration<T> {
120
121
121
122
// SAFETY: The only method is `register()`, which requires a (pinned) mutable `Registration`, so it
122
123
// is safe to pass `&Registration` to multiple threads because it offers no interior mutability.
123
- unsafe impl < T : Sync > Sync for Registration < T > { }
124
+ unsafe impl < T : FileOperations > Sync for Registration < T > { }
124
125
125
126
// SAFETY: All functions work from any thread. So as long as the `Registration::open_data` is
126
- // `Send`, so is `Registration<T>`. `T` needs to be `Sync` because it's a requirement of
127
- // `Registration<T>`.
128
- unsafe impl < T : Send + Sync > Send for Registration < T > { }
127
+ // `Send`, so is `Registration<T>`.
128
+ unsafe impl < T : FileOperations > Send for Registration < T > where T :: OpenData : Send { }
129
129
130
- impl < T : Sync > Drop for Registration < T > {
130
+ impl < T : FileOperations > Drop for Registration < T > {
131
131
/// Removes the registration from the kernel if it has completed successfully before.
132
132
fn drop ( & mut self ) {
133
133
if self . registered {
@@ -142,17 +142,15 @@ impl<T: Sync> Drop for Registration<T> {
142
142
}
143
143
}
144
144
145
- /// Kernel module that exposes a single miscdev device implemented by `F`.
146
- pub struct Module < F : FileOpener < ( ) > > {
147
- _dev : Pin < Box < Registration > > ,
148
- _p : PhantomData < F > ,
145
+ /// Kernel module that exposes a single miscdev device implemented by `T`.
146
+ pub struct Module < T : FileOperations < OpenData = ( ) > > {
147
+ _dev : Pin < Box < Registration < T > > > ,
149
148
}
150
149
151
- impl < F : FileOpener < ( ) > > KernelModule for Module < F > {
150
+ impl < T : FileOperations < OpenData = ( ) > > KernelModule for Module < T > {
152
151
fn init ( name : & ' static CStr , _module : & ' static ThisModule ) -> Result < Self > {
153
152
Ok ( Self {
154
- _p : PhantomData ,
155
- _dev : Registration :: new_pinned :: < F > ( name, None , ( ) ) ?,
153
+ _dev : Registration :: new_pinned ( name, None , ( ) ) ?,
156
154
} )
157
155
}
158
156
}
0 commit comments