1 // SPDX-License-Identifier: GPL-2.0 2 3 // Copyright (C) 2024 Google LLC. 4 5 //! Miscdevice support. 6 //! 7 //! C headers: [`include/linux/miscdevice.h`](srctree/include/linux/miscdevice.h). 8 //! 9 //! Reference: <https://www.kernel.org/doc/html/latest/driver-api/misc_devices.html> 10 11 use crate::{ 12 bindings, 13 device::Device, 14 error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR}, 15 fs::File, 16 prelude::*, 17 seq_file::SeqFile, 18 str::CStr, 19 types::{ForeignOwnable, Opaque}, 20 }; 21 use core::{ 22 ffi::{c_int, c_long, c_uint, c_ulong}, 23 marker::PhantomData, 24 mem::MaybeUninit, 25 pin::Pin, 26 }; 27 28 /// Options for creating a misc device. 29 #[derive(Copy, Clone)] 30 pub struct MiscDeviceOptions { 31 /// The name of the miscdevice. 32 pub name: &'static CStr, 33 } 34 35 impl MiscDeviceOptions { 36 /// Create a raw `struct miscdev` ready for registration. 37 pub const fn into_raw<T: MiscDevice>(self) -> bindings::miscdevice { 38 // SAFETY: All zeros is valid for this C type. 39 let mut result: bindings::miscdevice = unsafe { MaybeUninit::zeroed().assume_init() }; 40 result.minor = bindings::MISC_DYNAMIC_MINOR as _; 41 result.name = self.name.as_char_ptr(); 42 result.fops = create_vtable::<T>(); 43 result 44 } 45 } 46 47 /// A registration of a miscdevice. 48 /// 49 /// # Invariants 50 /// 51 /// `inner` is a registered misc device. 52 #[repr(transparent)] 53 #[pin_data(PinnedDrop)] 54 pub struct MiscDeviceRegistration<T> { 55 #[pin] 56 inner: Opaque<bindings::miscdevice>, 57 _t: PhantomData<T>, 58 } 59 60 // SAFETY: It is allowed to call `misc_deregister` on a different thread from where you called 61 // `misc_register`. 62 unsafe impl<T> Send for MiscDeviceRegistration<T> {} 63 // SAFETY: All `&self` methods on this type are written to ensure that it is safe to call them in 64 // parallel. 65 unsafe impl<T> Sync for MiscDeviceRegistration<T> {} 66 67 impl<T: MiscDevice> MiscDeviceRegistration<T> { 68 /// Register a misc device. 69 pub fn register(opts: MiscDeviceOptions) -> impl PinInit<Self, Error> { 70 try_pin_init!(Self { 71 inner <- Opaque::try_ffi_init(move |slot: *mut bindings::miscdevice| { 72 // SAFETY: The initializer can write to the provided `slot`. 73 unsafe { slot.write(opts.into_raw::<T>()) }; 74 75 // SAFETY: We just wrote the misc device options to the slot. The miscdevice will 76 // get unregistered before `slot` is deallocated because the memory is pinned and 77 // the destructor of this type deallocates the memory. 78 // INVARIANT: If this returns `Ok(())`, then the `slot` will contain a registered 79 // misc device. 80 to_result(unsafe { bindings::misc_register(slot) }) 81 }), 82 _t: PhantomData, 83 }) 84 } 85 86 /// Returns a raw pointer to the misc device. 87 pub fn as_raw(&self) -> *mut bindings::miscdevice { 88 self.inner.get() 89 } 90 91 /// Access the `this_device` field. 92 pub fn device(&self) -> &Device { 93 // SAFETY: This can only be called after a successful register(), which always 94 // initialises `this_device` with a valid device. Furthermore, the signature of this 95 // function tells the borrow-checker that the `&Device` reference must not outlive the 96 // `&MiscDeviceRegistration<T>` used to obtain it, so the last use of the reference must be 97 // before the underlying `struct miscdevice` is destroyed. 98 unsafe { Device::as_ref((*self.as_raw()).this_device) } 99 } 100 } 101 102 #[pinned_drop] 103 impl<T> PinnedDrop for MiscDeviceRegistration<T> { 104 fn drop(self: Pin<&mut Self>) { 105 // SAFETY: We know that the device is registered by the type invariants. 106 unsafe { bindings::misc_deregister(self.inner.get()) }; 107 } 108 } 109 110 /// Trait implemented by the private data of an open misc device. 111 #[vtable] 112 pub trait MiscDevice: Sized { 113 /// What kind of pointer should `Self` be wrapped in. 114 type Ptr: ForeignOwnable + Send + Sync; 115 116 /// Called when the misc device is opened. 117 /// 118 /// The returned pointer will be stored as the private data for the file. 119 fn open(_file: &File, _misc: &MiscDeviceRegistration<Self>) -> Result<Self::Ptr>; 120 121 /// Called when the misc device is released. 122 fn release(device: Self::Ptr, _file: &File) { 123 drop(device); 124 } 125 126 /// Handler for ioctls. 127 /// 128 /// The `cmd` argument is usually manipulated using the utilties in [`kernel::ioctl`]. 129 /// 130 /// [`kernel::ioctl`]: mod@crate::ioctl 131 fn ioctl( 132 _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>, 133 _file: &File, 134 _cmd: u32, 135 _arg: usize, 136 ) -> Result<isize> { 137 kernel::build_error!(VTABLE_DEFAULT_ERROR) 138 } 139 140 /// Handler for ioctls. 141 /// 142 /// Used for 32-bit userspace on 64-bit platforms. 143 /// 144 /// This method is optional and only needs to be provided if the ioctl relies on structures 145 /// that have different layout on 32-bit and 64-bit userspace. If no implementation is 146 /// provided, then `compat_ptr_ioctl` will be used instead. 147 #[cfg(CONFIG_COMPAT)] 148 fn compat_ioctl( 149 _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>, 150 _file: &File, 151 _cmd: u32, 152 _arg: usize, 153 ) -> Result<isize> { 154 kernel::build_error!(VTABLE_DEFAULT_ERROR) 155 } 156 157 /// Show info for this fd. 158 fn show_fdinfo( 159 _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>, 160 _m: &SeqFile, 161 _file: &File, 162 ) { 163 kernel::build_error!(VTABLE_DEFAULT_ERROR) 164 } 165 } 166 167 const fn create_vtable<T: MiscDevice>() -> &'static bindings::file_operations { 168 const fn maybe_fn<T: Copy>(check: bool, func: T) -> Option<T> { 169 if check { 170 Some(func) 171 } else { 172 None 173 } 174 } 175 176 struct VtableHelper<T: MiscDevice> { 177 _t: PhantomData<T>, 178 } 179 impl<T: MiscDevice> VtableHelper<T> { 180 const VTABLE: bindings::file_operations = bindings::file_operations { 181 open: Some(fops_open::<T>), 182 release: Some(fops_release::<T>), 183 unlocked_ioctl: maybe_fn(T::HAS_IOCTL, fops_ioctl::<T>), 184 #[cfg(CONFIG_COMPAT)] 185 compat_ioctl: if T::HAS_COMPAT_IOCTL { 186 Some(fops_compat_ioctl::<T>) 187 } else if T::HAS_IOCTL { 188 Some(bindings::compat_ptr_ioctl) 189 } else { 190 None 191 }, 192 show_fdinfo: maybe_fn(T::HAS_SHOW_FDINFO, fops_show_fdinfo::<T>), 193 // SAFETY: All zeros is a valid value for `bindings::file_operations`. 194 ..unsafe { MaybeUninit::zeroed().assume_init() } 195 }; 196 } 197 198 &VtableHelper::<T>::VTABLE 199 } 200 201 /// # Safety 202 /// 203 /// `file` and `inode` must be the file and inode for a file that is undergoing initialization. 204 /// The file must be associated with a `MiscDeviceRegistration<T>`. 205 unsafe extern "C" fn fops_open<T: MiscDevice>( 206 inode: *mut bindings::inode, 207 raw_file: *mut bindings::file, 208 ) -> c_int { 209 // SAFETY: The pointers are valid and for a file being opened. 210 let ret = unsafe { bindings::generic_file_open(inode, raw_file) }; 211 if ret != 0 { 212 return ret; 213 } 214 215 // SAFETY: The open call of a file can access the private data. 216 let misc_ptr = unsafe { (*raw_file).private_data }; 217 218 // SAFETY: This is a miscdevice, so `misc_open()` set the private data to a pointer to the 219 // associated `struct miscdevice` before calling into this method. Furthermore, `misc_open()` 220 // ensures that the miscdevice can't be unregistered and freed during this call to `fops_open`. 221 let misc = unsafe { &*misc_ptr.cast::<MiscDeviceRegistration<T>>() }; 222 223 // SAFETY: 224 // * This underlying file is valid for (much longer than) the duration of `T::open`. 225 // * There is no active fdget_pos region on the file on this thread. 226 let file = unsafe { File::from_raw_file(raw_file) }; 227 228 let ptr = match T::open(file, misc) { 229 Ok(ptr) => ptr, 230 Err(err) => return err.to_errno(), 231 }; 232 233 // This overwrites the private data with the value specified by the user, changing the type of 234 // this file's private data. All future accesses to the private data is performed by other 235 // fops_* methods in this file, which all correctly cast the private data to the new type. 236 // 237 // SAFETY: The open call of a file can access the private data. 238 unsafe { (*raw_file).private_data = ptr.into_foreign().cast_mut() }; 239 240 0 241 } 242 243 /// # Safety 244 /// 245 /// `file` and `inode` must be the file and inode for a file that is being released. The file must 246 /// be associated with a `MiscDeviceRegistration<T>`. 247 unsafe extern "C" fn fops_release<T: MiscDevice>( 248 _inode: *mut bindings::inode, 249 file: *mut bindings::file, 250 ) -> c_int { 251 // SAFETY: The release call of a file owns the private data. 252 let private = unsafe { (*file).private_data }; 253 // SAFETY: The release call of a file owns the private data. 254 let ptr = unsafe { <T::Ptr as ForeignOwnable>::from_foreign(private) }; 255 256 // SAFETY: 257 // * The file is valid for the duration of this call. 258 // * There is no active fdget_pos region on the file on this thread. 259 T::release(ptr, unsafe { File::from_raw_file(file) }); 260 261 0 262 } 263 264 /// # Safety 265 /// 266 /// `file` must be a valid file that is associated with a `MiscDeviceRegistration<T>`. 267 unsafe extern "C" fn fops_ioctl<T: MiscDevice>( 268 file: *mut bindings::file, 269 cmd: c_uint, 270 arg: c_ulong, 271 ) -> c_long { 272 // SAFETY: The ioctl call of a file can access the private data. 273 let private = unsafe { (*file).private_data }; 274 // SAFETY: Ioctl calls can borrow the private data of the file. 275 let device = unsafe { <T::Ptr as ForeignOwnable>::borrow(private) }; 276 277 // SAFETY: 278 // * The file is valid for the duration of this call. 279 // * There is no active fdget_pos region on the file on this thread. 280 let file = unsafe { File::from_raw_file(file) }; 281 282 match T::ioctl(device, file, cmd, arg as usize) { 283 Ok(ret) => ret as c_long, 284 Err(err) => err.to_errno() as c_long, 285 } 286 } 287 288 /// # Safety 289 /// 290 /// `file` must be a valid file that is associated with a `MiscDeviceRegistration<T>`. 291 #[cfg(CONFIG_COMPAT)] 292 unsafe extern "C" fn fops_compat_ioctl<T: MiscDevice>( 293 file: *mut bindings::file, 294 cmd: c_uint, 295 arg: c_ulong, 296 ) -> c_long { 297 // SAFETY: The compat ioctl call of a file can access the private data. 298 let private = unsafe { (*file).private_data }; 299 // SAFETY: Ioctl calls can borrow the private data of the file. 300 let device = unsafe { <T::Ptr as ForeignOwnable>::borrow(private) }; 301 302 // SAFETY: 303 // * The file is valid for the duration of this call. 304 // * There is no active fdget_pos region on the file on this thread. 305 let file = unsafe { File::from_raw_file(file) }; 306 307 match T::compat_ioctl(device, file, cmd, arg as usize) { 308 Ok(ret) => ret as c_long, 309 Err(err) => err.to_errno() as c_long, 310 } 311 } 312 313 /// # Safety 314 /// 315 /// - `file` must be a valid file that is associated with a `MiscDeviceRegistration<T>`. 316 /// - `seq_file` must be a valid `struct seq_file` that we can write to. 317 unsafe extern "C" fn fops_show_fdinfo<T: MiscDevice>( 318 seq_file: *mut bindings::seq_file, 319 file: *mut bindings::file, 320 ) { 321 // SAFETY: The release call of a file owns the private data. 322 let private = unsafe { (*file).private_data }; 323 // SAFETY: Ioctl calls can borrow the private data of the file. 324 let device = unsafe { <T::Ptr as ForeignOwnable>::borrow(private) }; 325 // SAFETY: 326 // * The file is valid for the duration of this call. 327 // * There is no active fdget_pos region on the file on this thread. 328 let file = unsafe { File::from_raw_file(file) }; 329 // SAFETY: The caller ensures that the pointer is valid and exclusive for the duration in which 330 // this method is called. 331 let m = unsafe { SeqFile::from_raw(seq_file) }; 332 333 T::show_fdinfo(device, m, file); 334 } 335