|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only OR MIT |
| 2 | + |
| 3 | +//! Support for Apple ASC Mailbox. |
| 4 | +//! |
| 5 | +//! C header: [`include/linux/soc/apple/mailbox.h`](../../../../include/linux/gpio/driver.h) |
| 6 | +
|
| 7 | +use crate::{ |
| 8 | + bindings, device, |
| 9 | + error::{from_err_ptr, to_result, Result}, |
| 10 | + str::CStr, |
| 11 | + types::{ForeignOwnable, ScopeGuard}, |
| 12 | +}; |
| 13 | + |
| 14 | +use core::marker::PhantomData; |
| 15 | + |
| 16 | +/// 96-bit message. What it means is up to the upper layer |
| 17 | +pub type Message = bindings::apple_mbox_msg; |
| 18 | + |
| 19 | +/// Mailbox receive callback |
| 20 | +pub trait MailCallback { |
| 21 | + /// Callback context |
| 22 | + type Data: ForeignOwnable + Send + Sync; |
| 23 | + |
| 24 | + /// The actual callback. Called in an interrupt context. |
| 25 | + fn recv_message(data: <Self::Data as ForeignOwnable>::Borrowed<'_>, msg: Message); |
| 26 | +} |
| 27 | + |
| 28 | +/// Wrapper over `struct apple_mbox *` |
| 29 | +#[repr(transparent)] |
| 30 | +pub struct Mailbox<T: MailCallback> { |
| 31 | + mbox: *mut bindings::apple_mbox, |
| 32 | + _p: PhantomData<T>, |
| 33 | +} |
| 34 | + |
| 35 | +extern "C" fn mailbox_rx_callback<T: MailCallback>( |
| 36 | + _mbox: *mut bindings::apple_mbox, |
| 37 | + msg: Message, |
| 38 | + cookie: *mut core::ffi::c_void, |
| 39 | +) { |
| 40 | + // SAFETY: cookie came from a call to `into_foreign` |
| 41 | + T::recv_message(unsafe { T::Data::borrow(cookie) }, msg); |
| 42 | +} |
| 43 | + |
| 44 | +impl<T: MailCallback> Mailbox<T> { |
| 45 | + /// Creates a mailbox for the specified name. |
| 46 | + pub fn new_byname( |
| 47 | + dev: &device::Device, |
| 48 | + mbox_name: &'static CStr, |
| 49 | + data: T::Data, |
| 50 | + ) -> Result<Mailbox<T>> { |
| 51 | + let ptr = data.into_foreign() as *mut _; |
| 52 | + let guard = ScopeGuard::new(|| { |
| 53 | + // SAFETY: `ptr` came from a previous call to `into_foreign`. |
| 54 | + unsafe { T::Data::from_foreign(ptr) }; |
| 55 | + }); |
| 56 | + // SAFETY: Just calling the c function, all values are valid. |
| 57 | + let mbox = unsafe { |
| 58 | + from_err_ptr(bindings::apple_mbox_get_byname( |
| 59 | + dev.as_raw(), |
| 60 | + mbox_name.as_char_ptr(), |
| 61 | + ))? |
| 62 | + }; |
| 63 | + // SAFETY: mbox is a valid pointer |
| 64 | + unsafe { |
| 65 | + (*mbox).cookie = ptr; |
| 66 | + (*mbox).rx = Some(mailbox_rx_callback::<T>); |
| 67 | + to_result(unsafe { bindings::apple_mbox_start(mbox) })?; |
| 68 | + } |
| 69 | + guard.dismiss(); |
| 70 | + Ok(Mailbox { |
| 71 | + mbox, |
| 72 | + _p: PhantomData, |
| 73 | + }) |
| 74 | + } |
| 75 | + /// Sends the specified message |
| 76 | + pub fn send(&self, msg: Message, atomic: bool) -> Result<()> { |
| 77 | + // SAFETY: Calling the c function, `mbox` is a valid pointer |
| 78 | + to_result(unsafe { bindings::apple_mbox_send(self.mbox, msg, atomic) }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl<T: MailCallback> Mailbox<T> { |
| 83 | + fn drop(&mut self) { |
| 84 | + // SAFETY: mbox is a valid pointer |
| 85 | + unsafe { bindings::apple_mbox_stop(self.mbox) }; |
| 86 | + // SAFETY: `cookie` came from `into_foreign` |
| 87 | + unsafe { T::Data::from_foreign((*self.mbox).cookie) }; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +unsafe impl<T> Sync for Mailbox<T> |
| 92 | +where |
| 93 | + T: MailCallback, |
| 94 | + T::Data: Sync, |
| 95 | +{ |
| 96 | +} |
| 97 | + |
| 98 | +unsafe impl<T> Send for Mailbox<T> |
| 99 | +where |
| 100 | + T: MailCallback, |
| 101 | + T::Data: Send, |
| 102 | +{ |
| 103 | +} |
0 commit comments