8
8
//!
9
9
//! Reference: <https://www.kernel.org/doc/html/latest/core-api/kernel-api.html#char-devices>
10
10
11
- use alloc:: boxed:: Box ;
12
11
use core:: convert:: TryInto ;
13
- use core:: marker:: PhantomPinned ;
14
- use core:: pin:: Pin ;
15
12
16
13
use crate :: bindings;
17
14
use crate :: c_types;
@@ -80,12 +77,11 @@ struct RegistrationInner<const N: usize> {
80
77
dev : bindings:: dev_t ,
81
78
used : usize ,
82
79
cdevs : [ Option < Cdev > ; N ] ,
83
- _pin : PhantomPinned ,
84
80
}
85
81
86
82
/// Character device registration.
87
83
///
88
- /// May contain up to a fixed number (`N`) of devices. Must be pinned.
84
+ /// May contain up to a fixed number (`N`) of devices.
89
85
pub struct Registration < const N : usize > {
90
86
name : & ' static CStr ,
91
87
minors_start : u16 ,
@@ -97,12 +93,6 @@ impl<const N: usize> Registration<{ N }> {
97
93
/// Creates a [`Registration`] object for a character device.
98
94
///
99
95
/// This does *not* register the device: see [`Self::register()`].
100
- ///
101
- /// This associated function is intended to be used when you need to avoid
102
- /// a memory allocation, e.g. when the [`Registration`] is a member of
103
- /// a bigger structure inside your [`crate::KernelModule`] instance. If you
104
- /// are going to pin the registration right away, call
105
- /// [`Self::new_pinned()`] instead.
106
96
pub fn new (
107
97
name : & ' static CStr ,
108
98
minors_start : u16 ,
@@ -116,60 +106,42 @@ impl<const N: usize> Registration<{ N }> {
116
106
}
117
107
}
118
108
119
- /// Creates a pinned [`Registration`] object for a character device.
120
- ///
121
- /// This does *not* register the device: see [`Self::register()`].
122
- pub fn new_pinned (
123
- name : & ' static CStr ,
124
- minors_start : u16 ,
125
- this_module : & ' static crate :: ThisModule ,
126
- ) -> Result < Pin < Box < Self > > > {
127
- Ok ( Pin :: from ( Box :: try_new ( Self :: new (
128
- name,
129
- minors_start,
130
- this_module,
131
- ) ) ?) )
132
- }
133
-
134
109
/// Registers a character device.
135
110
///
136
111
/// You may call this once per device type, up to `N` times.
137
- pub fn register < T : file_operations:: FileOpener < ( ) > > ( self : Pin < & mut Self > ) -> Result {
138
- // SAFETY: We must ensure that we never move out of `this`.
139
- let this = unsafe { self . get_unchecked_mut ( ) } ;
140
- if this. inner . is_none ( ) {
112
+ pub fn register < T : file_operations:: FileOpener < ( ) > > ( & mut self ) -> Result {
113
+ if self . inner . is_none ( ) {
141
114
let mut dev: bindings:: dev_t = 0 ;
142
115
// SAFETY: Calling unsafe function. `this.name` has `'static`
143
116
// lifetime.
144
117
let res = unsafe {
145
118
bindings:: alloc_chrdev_region (
146
119
& mut dev,
147
- this . minors_start . into ( ) ,
120
+ self . minors_start . into ( ) ,
148
121
N . try_into ( ) ?,
149
- this . name . as_char_ptr ( ) ,
122
+ self . name . as_char_ptr ( ) ,
150
123
)
151
124
} ;
152
125
if res != 0 {
153
126
return Err ( Error :: from_kernel_errno ( res) ) ;
154
127
}
155
128
const NONE : Option < Cdev > = None ;
156
- this . inner = Some ( RegistrationInner {
129
+ self . inner = Some ( RegistrationInner {
157
130
dev,
158
131
used : 0 ,
159
132
cdevs : [ NONE ; N ] ,
160
- _pin : PhantomPinned ,
161
133
} ) ;
162
134
}
163
135
164
- let mut inner = this . inner . as_mut ( ) . unwrap ( ) ;
136
+ let mut inner = self . inner . as_mut ( ) . unwrap ( ) ;
165
137
if inner. used == N {
166
138
return Err ( Error :: EINVAL ) ;
167
139
}
168
140
169
141
// SAFETY: The adapter doesn't retrieve any state yet, so it's compatible with any
170
142
// registration.
171
143
let fops = unsafe { file_operations:: FileOperationsVtable :: < Self , T > :: build ( ) } ;
172
- let mut cdev = Cdev :: alloc ( fops, & this . this_module ) ?;
144
+ let mut cdev = Cdev :: alloc ( fops, & self . this_module ) ?;
173
145
cdev. add ( inner. dev + inner. used as bindings:: dev_t , 1 ) ?;
174
146
inner. cdevs [ inner. used ] . replace ( cdev) ;
175
147
inner. used += 1 ;
0 commit comments