| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 |
|
| 8 | use core::{
|
| 9 | ops::Deref,
|
| 10 | ptr::NonNull, |
| 11 | };
|
| 12 |
|
| 13 | use crate::{
|
| 14 | prelude::*,
|
| 15 | str::CString,
|
| 16 | types::Opaque, |
| 17 | };
|
| 18 |
|
| 19 | pub use super::{
|
| 20 | PhysAddr,
|
| 21 | ResourceSize, |
| 22 | };
|
| 23 |
|
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | pub struct Region {
|
| 31 | |
| 32 | resource: NonNull<bindings::resource>,
|
| 33 | |
| 34 | |
| 35 | _name: CString,
|
| 36 | }
|
| 37 |
|
| 38 | impl Deref for Region {
|
| 39 | type Target = Resource;
|
| 40 |
|
| 41 | fn deref(&self) -> &Self::Target {
|
| 42 | |
| 43 | unsafe { Resource::from_raw(self.resource.as_ptr()) }
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | impl Drop for Region {
|
| 48 | fn drop(&mut self) {
|
| 49 | let (flags, start, size) = {
|
| 50 | let res = &**self;
|
| 51 | (res.flags(), res.start(), res.size())
|
| 52 | };
|
| 53 |
|
| 54 | let release_fn = if flags.contains(Flags::IORESOURCE_MEM) {
|
| 55 | bindings::release_mem_region
|
| 56 | } else {
|
| 57 | bindings::release_region
|
| 58 | };
|
| 59 |
|
| 60 | |
| 61 | unsafe { release_fn(start, size) };
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | |
| 66 | |
| 67 | unsafe impl Send for Region {}
|
| 68 |
|
| 69 | |
| 70 | |
| 71 | unsafe impl Sync for Region {}
|
| 72 |
|
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | #[repr(transparent)]
|
| 79 | pub struct Resource(Opaque<bindings::resource>);
|
| 80 |
|
| 81 | impl Resource {
|
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | pub(crate) const unsafe fn from_raw<'a>(ptr: *mut bindings::resource) -> &'a Self {
|
| 92 | |
| 93 | unsafe { &*ptr.cast() }
|
| 94 | }
|
| 95 |
|
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | pub fn request_region(
|
| 102 | &self,
|
| 103 | start: PhysAddr,
|
| 104 | size: ResourceSize,
|
| 105 | name: CString,
|
| 106 | flags: Flags,
|
| 107 | ) -> Option<Region> {
|
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | let region = unsafe {
|
| 114 | bindings::__request_region(
|
| 115 | self.0.get(),
|
| 116 | start,
|
| 117 | size,
|
| 118 | name.as_char_ptr(),
|
| 119 | flags.0 as c_int,
|
| 120 | )
|
| 121 | };
|
| 122 |
|
| 123 | Some(Region {
|
| 124 | resource: NonNull::new(region)?,
|
| 125 | _name: name,
|
| 126 | })
|
| 127 | }
|
| 128 |
|
| 129 | |
| 130 | pub fn size(&self) -> ResourceSize {
|
| 131 | let inner = self.0.get();
|
| 132 | |
| 133 | unsafe { bindings::resource_size(inner) }
|
| 134 | }
|
| 135 |
|
| 136 | |
| 137 | pub fn start(&self) -> PhysAddr {
|
| 138 | let inner = self.0.get();
|
| 139 | |
| 140 | unsafe { (*inner).start }
|
| 141 | }
|
| 142 |
|
| 143 | |
| 144 | pub fn name(&self) -> Option<&CStr> {
|
| 145 | let inner = self.0.get();
|
| 146 |
|
| 147 | |
| 148 | let name = unsafe { (*inner).name };
|
| 149 |
|
| 150 | if name.is_null() {
|
| 151 | return None;
|
| 152 | }
|
| 153 |
|
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | Some(unsafe { CStr::from_char_ptr(name) })
|
| 159 | }
|
| 160 |
|
| 161 | |
| 162 | pub fn flags(&self) -> Flags {
|
| 163 | let inner = self.0.get();
|
| 164 | |
| 165 | let flags = unsafe { (*inner).flags };
|
| 166 |
|
| 167 | Flags(flags)
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | |
| 172 | |
| 173 | unsafe impl Send for Resource {}
|
| 174 |
|
| 175 | |
| 176 | |
| 177 | unsafe impl Sync for Resource {}
|
| 178 |
|
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | #[derive(Clone, Copy, PartialEq)]
|
| 186 | pub struct Flags(c_ulong);
|
| 187 |
|
| 188 | impl Flags {
|
| 189 | |
| 190 | pub fn contains(self, flags: Flags) -> bool {
|
| 191 | (self & flags) == flags
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | impl core::ops::BitOr for Flags {
|
| 196 | type Output = Self;
|
| 197 | fn bitor(self, rhs: Self) -> Self::Output {
|
| 198 | Self(self.0 | rhs.0)
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | impl core::ops::BitAnd for Flags {
|
| 203 | type Output = Self;
|
| 204 | fn bitand(self, rhs: Self) -> Self::Output {
|
| 205 | Self(self.0 & rhs.0)
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | impl core::ops::Not for Flags {
|
| 210 | type Output = Self;
|
| 211 | fn not(self) -> Self::Output {
|
| 212 | Self(!self.0)
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | impl Flags {
|
| 217 | |
| 218 | pub const IORESOURCE_IO: Flags = Flags::new(bindings::IORESOURCE_IO);
|
| 219 |
|
| 220 | |
| 221 | pub const IORESOURCE_MUXED: Flags = Flags::new(bindings::IORESOURCE_MUXED);
|
| 222 |
|
| 223 | |
| 224 | pub const IORESOURCE_MEM: Flags = Flags::new(bindings::IORESOURCE_MEM);
|
| 225 |
|
| 226 | |
| 227 | pub const IORESOURCE_MEM_NONPOSTED: Flags = Flags::new(bindings::IORESOURCE_MEM_NONPOSTED);
|
| 228 |
|
| 229 | |
| 230 | #[inline(always)]
|
| 231 | const fn new(value: u32) -> Self {
|
| 232 | build_assert!(value as u64 <= c_ulong::MAX as u64);
|
| 233 | Flags(value as c_ulong)
|
| 234 | }
|
| 235 | }
|