| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 |
|
| 9 | use crate::{
|
| 10 | bindings,
|
| 11 | device,
|
| 12 | prelude::*,
|
| 13 | types::Opaque, |
| 14 | };
|
| 15 | use core::{
|
| 16 | marker::PhantomData,
|
| 17 | ptr::{
|
| 18 | null,
|
| 19 | null_mut,
|
| 20 | NonNull, |
| 21 | },
|
| 22 | };
|
| 23 |
|
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | #[repr(transparent)]
|
| 33 | pub struct Device<Ctx: device::DeviceContext = device::Normal>(
|
| 34 | Opaque<bindings::faux_device>,
|
| 35 | PhantomData<Ctx>,
|
| 36 | );
|
| 37 |
|
| 38 | impl<Ctx: device::DeviceContext> Device<Ctx> {
|
| 39 | #[inline]
|
| 40 | fn as_raw(&self) -> *mut bindings::faux_device {
|
| 41 | self.0.get()
|
| 42 | }
|
| 43 |
|
| 44 | |
| 45 | |
| 46 | |
| 47 | #[inline]
|
| 48 | unsafe fn from_raw<'a>(ptr: *mut bindings::faux_device) -> &'a Self {
|
| 49 | |
| 50 | unsafe { &*ptr.cast() }
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
|
| 55 | #[inline]
|
| 56 | fn as_ref(&self) -> &device::Device<Ctx> {
|
| 57 | |
| 58 | |
| 59 | unsafe { device::Device::from_raw(&raw mut (*self.as_raw()).dev) }
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | |
| 64 | |
| 65 | unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx> {
|
| 66 | const OFFSET: usize = core::mem::offset_of!(bindings::faux_device, dev);
|
| 67 | }
|
| 68 |
|
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | pub struct Registration(NonNull<bindings::faux_device>);
|
| 81 |
|
| 82 | impl Registration {
|
| 83 | |
| 84 | #[inline]
|
| 85 | pub fn new(name: &CStr, parent: Option<&device::Device>) -> Result<Self> {
|
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | let dev = unsafe {
|
| 93 | bindings::faux_device_create(
|
| 94 | name.as_char_ptr(),
|
| 95 | parent.map_or(null_mut(), |p| p.as_raw()),
|
| 96 | null(),
|
| 97 | )
|
| 98 | };
|
| 99 |
|
| 100 | |
| 101 | |
| 102 | |
| 103 | Ok(Self(NonNull::new(dev).ok_or(ENODEV)?))
|
| 104 | }
|
| 105 |
|
| 106 | fn as_raw(&self) -> *mut bindings::faux_device {
|
| 107 | self.0.as_ptr()
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | impl AsRef<Device<device::Bound>> for Registration {
|
| 112 | #[inline]
|
| 113 | fn as_ref(&self) -> &Device<device::Bound> {
|
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | unsafe { Device::from_raw(self.as_raw()) }
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | impl Drop for Registration {
|
| 128 | #[inline]
|
| 129 | fn drop(&mut self) {
|
| 130 | |
| 131 | unsafe { bindings::faux_device_destroy(self.as_raw()) }
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | |
| 136 | |
| 137 | |
| 138 | unsafe impl Send for Registration {}
|
| 139 |
|
| 140 | |
| 141 | |
| 142 | |
| 143 | unsafe impl Sync for Registration {}
|