|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only OR MIT |
| 2 | + |
| 3 | +//! Apple AOP sensors common code |
| 4 | +//! |
| 5 | +//! Copyright (C) The Asahi Linux Contributors |
| 6 | +
|
| 7 | +use core::marker::{PhantomData, PhantomPinned}; |
| 8 | +use core::ptr; |
| 9 | +use core::sync::atomic::{AtomicU32, Ordering}; |
| 10 | + |
| 11 | +use kernel::{ |
| 12 | + bindings, platform, prelude::*, soc::apple::aop::FakehidListener, sync::Arc, |
| 13 | + types::ForeignOwnable, ThisModule, |
| 14 | +}; |
| 15 | + |
| 16 | +pub trait MessageProcessor { |
| 17 | + fn process(&self, message: &[u8]) -> u32; |
| 18 | +} |
| 19 | + |
| 20 | +pub struct AopSensorData<T: MessageProcessor> { |
| 21 | + dev: platform::Device, |
| 22 | + ty: u32, |
| 23 | + value: AtomicU32, |
| 24 | + msg_proc: T, |
| 25 | +} |
| 26 | + |
| 27 | +impl<T: MessageProcessor> AopSensorData<T> { |
| 28 | + pub fn new(dev: platform::Device, ty: u32, msg_proc: T) -> Result<Arc<AopSensorData<T>>> { |
| 29 | + Ok(Arc::new( |
| 30 | + AopSensorData { |
| 31 | + dev, |
| 32 | + ty, |
| 33 | + value: AtomicU32::new(0), |
| 34 | + msg_proc, |
| 35 | + }, |
| 36 | + GFP_KERNEL, |
| 37 | + )?) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl<T: MessageProcessor> FakehidListener for AopSensorData<T> { |
| 42 | + fn process_fakehid_report(&self, data: &[u8]) -> Result<()> { |
| 43 | + self.value |
| 44 | + .store(self.msg_proc.process(data), Ordering::Relaxed); |
| 45 | + Ok(()) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +unsafe extern "C" fn aop_read_raw<T: MessageProcessor + 'static>( |
| 50 | + dev: *mut bindings::iio_dev, |
| 51 | + chan: *const bindings::iio_chan_spec, |
| 52 | + val: *mut i32, |
| 53 | + _: *mut i32, |
| 54 | + mask: isize, |
| 55 | +) -> i32 { |
| 56 | + let data = unsafe { Arc::<AopSensorData<T>>::borrow((*dev).priv_) }; |
| 57 | + let ty = unsafe { (*chan).type_ }; |
| 58 | + if mask != bindings::BINDINGS_IIO_CHAN_INFO_PROCESSED as isize |
| 59 | + && mask != bindings::BINDINGS_IIO_CHAN_INFO_RAW as isize |
| 60 | + { |
| 61 | + return EINVAL.to_errno(); |
| 62 | + } |
| 63 | + if data.ty != ty { |
| 64 | + return EINVAL.to_errno(); |
| 65 | + } |
| 66 | + let value = data.value.load(Ordering::Relaxed); |
| 67 | + unsafe { |
| 68 | + *val = value as i32; |
| 69 | + } |
| 70 | + bindings::IIO_VAL_INT as i32 |
| 71 | +} |
| 72 | + |
| 73 | +struct IIOSpec { |
| 74 | + spec: [bindings::iio_chan_spec; 1], |
| 75 | + vtable: bindings::iio_info, |
| 76 | + _p: PhantomPinned, |
| 77 | +} |
| 78 | + |
| 79 | +pub struct IIORegistration<T: MessageProcessor + 'static> { |
| 80 | + dev: *mut bindings::iio_dev, |
| 81 | + spec: Pin<KBox<IIOSpec>>, |
| 82 | + registered: bool, |
| 83 | + _p: PhantomData<AopSensorData<T>>, |
| 84 | +} |
| 85 | + |
| 86 | +impl<T: MessageProcessor + 'static> IIORegistration<T> { |
| 87 | + pub fn new( |
| 88 | + data: Arc<AopSensorData<T>>, |
| 89 | + name: &'static CStr, |
| 90 | + ty: u32, |
| 91 | + info_mask: isize, |
| 92 | + module: &ThisModule, |
| 93 | + ) -> Result<Self> { |
| 94 | + let spec = KBox::pin( |
| 95 | + IIOSpec { |
| 96 | + spec: [bindings::iio_chan_spec { |
| 97 | + type_: ty, |
| 98 | + __bindgen_anon_1: bindings::iio_chan_spec__bindgen_ty_1 { |
| 99 | + scan_type: bindings::iio_scan_type { |
| 100 | + sign: b'u' as _, |
| 101 | + realbits: 32, |
| 102 | + storagebits: 32, |
| 103 | + ..Default::default() |
| 104 | + }, |
| 105 | + }, |
| 106 | + info_mask_separate: info_mask, |
| 107 | + ..Default::default() |
| 108 | + }], |
| 109 | + vtable: bindings::iio_info { |
| 110 | + read_raw: Some(aop_read_raw::<T>), |
| 111 | + ..Default::default() |
| 112 | + }, |
| 113 | + _p: PhantomPinned, |
| 114 | + }, |
| 115 | + GFP_KERNEL, |
| 116 | + )?; |
| 117 | + let mut this = IIORegistration { |
| 118 | + dev: ptr::null_mut(), |
| 119 | + spec, |
| 120 | + registered: false, |
| 121 | + _p: PhantomData, |
| 122 | + }; |
| 123 | + this.dev = unsafe { bindings::iio_device_alloc(data.dev.as_ref().as_raw(), 0) }; |
| 124 | + unsafe { |
| 125 | + (*this.dev).priv_ = data.clone().into_foreign() as _; |
| 126 | + (*this.dev).name = name.as_ptr() as _; |
| 127 | + // spec is now pinned |
| 128 | + (*this.dev).channels = this.spec.spec.as_ptr(); |
| 129 | + (*this.dev).num_channels = this.spec.spec.len() as i32; |
| 130 | + (*this.dev).info = &this.spec.vtable; |
| 131 | + } |
| 132 | + let ret = unsafe { bindings::__iio_device_register(this.dev, module.as_ptr()) }; |
| 133 | + if ret < 0 { |
| 134 | + dev_err!(data.dev.as_ref(), "Unable to register iio sensor"); |
| 135 | + return Err(Error::from_errno(ret)); |
| 136 | + } |
| 137 | + this.registered = true; |
| 138 | + Ok(this) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl<T: MessageProcessor + 'static> Drop for IIORegistration<T> { |
| 143 | + fn drop(&mut self) { |
| 144 | + if self.dev != ptr::null_mut() { |
| 145 | + unsafe { |
| 146 | + if self.registered { |
| 147 | + bindings::iio_device_unregister(self.dev); |
| 148 | + } |
| 149 | + Arc::<AopSensorData<T>>::from_foreign((*self.dev).priv_); |
| 150 | + bindings::iio_device_free(self.dev); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +unsafe impl<T: MessageProcessor> Send for IIORegistration<T> {} |
| 157 | +unsafe impl<T: MessageProcessor> Sync for IIORegistration<T> {} |
0 commit comments