@@ -11,7 +11,7 @@ use core::pin::Pin;
11
11
pub struct SpiDevice ( * mut bindings:: spi_device ) ;
12
12
13
13
impl SpiDevice {
14
- pub fn from_ptr ( dev : * mut bindings:: spi_device ) -> Self {
14
+ pub unsafe fn from_ptr ( dev : * mut bindings:: spi_device ) -> Self {
15
15
SpiDevice ( dev)
16
16
}
17
17
@@ -85,7 +85,9 @@ impl DriverRegistration {
85
85
86
86
this. spi_driver = Some ( spi_driver) ;
87
87
88
- let res = unsafe { bindings:: __spi_register_driver ( this. this_module . 0 , & mut spi_driver) } ;
88
+ let res = unsafe {
89
+ bindings:: __spi_register_driver ( this. this_module . 0 , this. spi_driver . as_mut ( ) . unwrap ( ) )
90
+ } ;
89
91
90
92
match res {
91
93
0 => {
@@ -99,7 +101,7 @@ impl DriverRegistration {
99
101
100
102
impl Drop for DriverRegistration {
101
103
fn drop ( & mut self ) {
102
- unsafe { bindings:: driver_unregister ( & mut self . spi_driver . unwrap ( ) . driver ) }
104
+ unsafe { bindings:: driver_unregister ( & mut self . spi_driver . as_mut ( ) . unwrap ( ) . driver ) }
103
105
// FIXME: No unwrap? But it's safe?
104
106
}
105
107
}
@@ -110,8 +112,7 @@ impl Drop for DriverRegistration {
110
112
// is safe to pass `&Registration` to multiple threads because it offers no interior mutability.
111
113
unsafe impl Sync for DriverRegistration { }
112
114
113
- // SAFETY: The only method is `register()`, which requires a (pinned) mutable `Registration`, so it
114
- // is safe to pass `&Registration` to multiple threads because it offers no interior mutability.
115
+ // SAFETY: All functions work from any thread.
115
116
unsafe impl Send for DriverRegistration { }
116
117
117
118
type SpiMethod = unsafe extern "C" fn ( * mut bindings:: spi_device ) -> c_types:: c_int ;
@@ -125,7 +126,8 @@ macro_rules! spi_method {
125
126
126
127
fn inner( mut $device_name: SpiDevice ) -> Result $block
127
128
128
- match inner( SpiDevice :: from_ptr( dev) ) {
129
+ // SAFETY: The dev pointer is provided by the kernel and is sure to be valid
130
+ match inner( unsafe { SpiDevice :: from_ptr( dev) } ) {
129
131
Ok ( _) => 0 ,
130
132
Err ( e) => e. to_kernel_errno( ) ,
131
133
}
@@ -137,28 +139,23 @@ macro_rules! spi_method {
137
139
138
140
fn inner( mut $device_name: SpiDevice ) $block
139
141
140
- inner( SpiDevice :: from_ptr( dev) )
142
+ // SAFETY: The dev pointer is provided by the kernel and is sure to be valid
143
+ inner( unsafe { SpiDevice :: from_ptr( dev) } )
141
144
}
142
145
} ;
143
146
}
144
147
145
148
pub struct Spi ;
146
149
147
150
impl Spi {
148
- pub fn write_then_read (
149
- dev : & mut SpiDevice ,
150
- tx_buf : & [ u8 ] ,
151
- n_tx : usize ,
152
- rx_buf : & mut [ u8 ] ,
153
- n_rx : usize ,
154
- ) -> Result {
151
+ pub fn write_then_read ( dev : & mut SpiDevice , tx_buf : & [ u8 ] , rx_buf : & mut [ u8 ] ) -> Result {
155
152
let res = unsafe {
156
153
bindings:: spi_write_then_read (
157
154
dev. to_ptr ( ) ,
158
155
tx_buf. as_ptr ( ) as * const c_types:: c_void ,
159
- n_tx as c_types:: c_uint ,
160
- rx_buf. as_ptr ( ) as * mut c_types:: c_void ,
161
- n_rx as c_types:: c_uint ,
156
+ tx_buf . len ( ) as c_types:: c_uint ,
157
+ rx_buf. as_mut_ptr ( ) as * mut c_types:: c_void ,
158
+ rx_buf . len ( ) as c_types:: c_uint ,
162
159
)
163
160
} ;
164
161
@@ -169,12 +166,12 @@ impl Spi {
169
166
}
170
167
171
168
#[ inline]
172
- pub fn write ( dev : & mut SpiDevice , tx_buf : & [ u8 ] , n_tx : usize ) -> Result {
173
- Spi :: write_then_read ( dev, tx_buf, n_tx , & mut [ 0u8 ; 0 ] , 0 )
169
+ pub fn write ( dev : & mut SpiDevice , tx_buf : & [ u8 ] ) -> Result {
170
+ Spi :: write_then_read ( dev, tx_buf, & mut [ 0u8 ; 0 ] )
174
171
}
175
172
176
173
#[ inline]
177
- pub fn read ( dev : & mut SpiDevice , rx_buf : & mut [ u8 ] , n_rx : usize ) -> Result {
178
- Spi :: write_then_read ( dev, & [ 0u8 ; 0 ] , 0 , rx_buf, n_rx )
174
+ pub fn read ( dev : & mut SpiDevice , rx_buf : & mut [ u8 ] ) -> Result {
175
+ Spi :: write_then_read ( dev, & [ 0u8 ; 0 ] , rx_buf)
179
176
}
180
177
}
0 commit comments