| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use crate::prelude::*;
|
| 8 | use crate::str::{kstrtobool_bytes, BStr};
|
| 9 | use bindings;
|
| 10 | use kernel::sync::SetOnce;
|
| 11 |
|
| 12 | |
| 13 | #[repr(transparent)]
|
| 14 | #[doc(hidden)]
|
| 15 | pub struct KernelParam(bindings::kernel_param);
|
| 16 |
|
| 17 | impl KernelParam {
|
| 18 | #[doc(hidden)]
|
| 19 | pub const fn new(val: bindings::kernel_param) -> Self {
|
| 20 | Self(val)
|
| 21 | }
|
| 22 | }
|
| 23 |
|
| 24 | |
| 25 | |
| 26 | unsafe impl Sync for KernelParam {}
|
| 27 |
|
| 28 | |
| 29 | |
| 30 | pub trait ModuleParam: Sized + Copy {
|
| 31 | |
| 32 | fn try_from_param_arg(arg: &BStr) -> Result<Self>;
|
| 33 | }
|
| 34 |
|
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | unsafe extern "C" fn set_param<T>(val: *const c_char, param: *const bindings::kernel_param) -> c_int
|
| 59 | where
|
| 60 | T: ModuleParam,
|
| 61 | {
|
| 62 | |
| 63 | |
| 64 | if val.is_null() {
|
| 65 | crate::pr_warn_once!("Null pointer passed to `module_param::set_param`\n");
|
| 66 | return EINVAL.to_errno();
|
| 67 | }
|
| 68 |
|
| 69 | |
| 70 | |
| 71 | let arg = unsafe { CStr::from_char_ptr(val) };
|
| 72 | let arg: &BStr = arg.as_ref();
|
| 73 |
|
| 74 | crate::error::from_result(|| {
|
| 75 | let new_value = T::try_from_param_arg(arg)?;
|
| 76 |
|
| 77 | |
| 78 | let container = unsafe { &*((*param).__bindgen_anon_1.arg.cast::<SetOnce<T>>()) };
|
| 79 |
|
| 80 | container
|
| 81 | .populate(new_value)
|
| 82 | .then_some(0)
|
| 83 | .ok_or(kernel::error::code::EEXIST)
|
| 84 | })
|
| 85 | }
|
| 86 |
|
| 87 | macro_rules! impl_int_module_param {
|
| 88 | ($ty:ident) => {
|
| 89 | impl ModuleParam for $ty {
|
| 90 | fn try_from_param_arg(arg: &BStr) -> Result<Self> {
|
| 91 | <$ty as crate::str::parse_int::ParseInt>::from_str(arg)
|
| 92 | }
|
| 93 | }
|
| 94 | };
|
| 95 | }
|
| 96 |
|
| 97 | impl_int_module_param!(i8);
|
| 98 | impl_int_module_param!(u8);
|
| 99 | impl_int_module_param!(i16);
|
| 100 | impl_int_module_param!(u16);
|
| 101 | impl_int_module_param!(i32);
|
| 102 | impl_int_module_param!(u32);
|
| 103 | impl_int_module_param!(i64);
|
| 104 | impl_int_module_param!(u64);
|
| 105 | impl_int_module_param!(isize);
|
| 106 | impl_int_module_param!(usize);
|
| 107 |
|
| 108 | impl ModuleParam for bool {
|
| 109 | fn try_from_param_arg(arg: &BStr) -> Result<Self> {
|
| 110 | kstrtobool_bytes(arg)
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | pub struct ModuleParamAccess<T> {
|
| 122 | value: SetOnce<T>,
|
| 123 | default: T,
|
| 124 | }
|
| 125 |
|
| 126 | |
| 127 | |
| 128 | unsafe impl<T: Sync> Sync for ModuleParamAccess<T> {}
|
| 129 |
|
| 130 | impl<T> ModuleParamAccess<T> {
|
| 131 | #[doc(hidden)]
|
| 132 | pub const fn new(default: T) -> Self {
|
| 133 | Self {
|
| 134 | value: SetOnce::new(),
|
| 135 | default,
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | #[inline]
|
| 144 | pub fn value(&self) -> T
|
| 145 | where
|
| 146 | T: Copy,
|
| 147 | {
|
| 148 | self.value.copy().unwrap_or(self.default)
|
| 149 | }
|
| 150 |
|
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | #[inline]
|
| 158 | pub fn value_ref(&self) -> &T {
|
| 159 | self.value.as_ref().unwrap_or(&self.default)
|
| 160 | }
|
| 161 |
|
| 162 | |
| 163 | |
| 164 | |
| 165 | pub const fn as_void_ptr(&self) -> *mut c_void {
|
| 166 | core::ptr::from_ref(self).cast_mut().cast()
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | #[doc(hidden)]
|
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | macro_rules! make_param_ops {
|
| 183 | ($ops:ident, $ty:ty) => {
|
| 184 | #[doc(hidden)]
|
| 185 | pub static $ops: $crate::bindings::kernel_param_ops = $crate::bindings::kernel_param_ops {
|
| 186 | flags: 0,
|
| 187 | set: Some(set_param::<$ty>),
|
| 188 | get: None,
|
| 189 | free: None,
|
| 190 | };
|
| 191 | };
|
| 192 | }
|
| 193 |
|
| 194 | make_param_ops!(PARAM_OPS_I8, i8);
|
| 195 | make_param_ops!(PARAM_OPS_U8, u8);
|
| 196 | make_param_ops!(PARAM_OPS_I16, i16);
|
| 197 | make_param_ops!(PARAM_OPS_U16, u16);
|
| 198 | make_param_ops!(PARAM_OPS_I32, i32);
|
| 199 | make_param_ops!(PARAM_OPS_U32, u32);
|
| 200 | make_param_ops!(PARAM_OPS_I64, i64);
|
| 201 | make_param_ops!(PARAM_OPS_U64, u64);
|
| 202 | make_param_ops!(PARAM_OPS_ISIZE, isize);
|
| 203 | make_param_ops!(PARAM_OPS_USIZE, usize);
|
| 204 | make_param_ops!(PARAM_OPS_BOOL, bool);
|