| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use crate::{
|
| 8 | bindings,
|
| 9 | device,
|
| 10 | drm,
|
| 11 | error::to_result,
|
| 12 | prelude::*,
|
| 13 | sync::aref::ARef, |
| 14 | };
|
| 15 | use core::ptr::NonNull;
|
| 16 |
|
| 17 | |
| 18 | pub(crate) const FEAT_GEM: u32 = bindings::drm_driver_feature_DRIVER_GEM;
|
| 19 | |
| 20 | pub(crate) const FEAT_RENDER: u32 = bindings::drm_driver_feature_DRIVER_RENDER;
|
| 21 |
|
| 22 | |
| 23 | pub struct DriverInfo {
|
| 24 | |
| 25 | pub major: i32,
|
| 26 | |
| 27 | pub minor: i32,
|
| 28 | |
| 29 | pub patchlevel: i32,
|
| 30 | |
| 31 | pub name: &'static CStr,
|
| 32 | |
| 33 | pub desc: &'static CStr,
|
| 34 | }
|
| 35 |
|
| 36 | |
| 37 | pub struct AllocOps {
|
| 38 | pub(crate) gem_create_object: Option<
|
| 39 | unsafe extern "C" fn(
|
| 40 | dev: *mut bindings::drm_device,
|
| 41 | size: usize,
|
| 42 | ) -> *mut bindings::drm_gem_object,
|
| 43 | >,
|
| 44 | pub(crate) prime_handle_to_fd: Option<
|
| 45 | unsafe extern "C" fn(
|
| 46 | dev: *mut bindings::drm_device,
|
| 47 | file_priv: *mut bindings::drm_file,
|
| 48 | handle: u32,
|
| 49 | flags: u32,
|
| 50 | prime_fd: *mut core::ffi::c_int,
|
| 51 | ) -> core::ffi::c_int,
|
| 52 | >,
|
| 53 | pub(crate) prime_fd_to_handle: Option<
|
| 54 | unsafe extern "C" fn(
|
| 55 | dev: *mut bindings::drm_device,
|
| 56 | file_priv: *mut bindings::drm_file,
|
| 57 | prime_fd: core::ffi::c_int,
|
| 58 | handle: *mut u32,
|
| 59 | ) -> core::ffi::c_int,
|
| 60 | >,
|
| 61 | pub(crate) gem_prime_import: Option<
|
| 62 | unsafe extern "C" fn(
|
| 63 | dev: *mut bindings::drm_device,
|
| 64 | dma_buf: *mut bindings::dma_buf,
|
| 65 | ) -> *mut bindings::drm_gem_object,
|
| 66 | >,
|
| 67 | pub(crate) gem_prime_import_sg_table: Option<
|
| 68 | unsafe extern "C" fn(
|
| 69 | dev: *mut bindings::drm_device,
|
| 70 | attach: *mut bindings::dma_buf_attachment,
|
| 71 | sgt: *mut bindings::sg_table,
|
| 72 | ) -> *mut bindings::drm_gem_object,
|
| 73 | >,
|
| 74 | pub(crate) dumb_create: Option<
|
| 75 | unsafe extern "C" fn(
|
| 76 | file_priv: *mut bindings::drm_file,
|
| 77 | dev: *mut bindings::drm_device,
|
| 78 | args: *mut bindings::drm_mode_create_dumb,
|
| 79 | ) -> core::ffi::c_int,
|
| 80 | >,
|
| 81 | pub(crate) dumb_map_offset: Option<
|
| 82 | unsafe extern "C" fn(
|
| 83 | file_priv: *mut bindings::drm_file,
|
| 84 | dev: *mut bindings::drm_device,
|
| 85 | handle: u32,
|
| 86 | offset: *mut u64,
|
| 87 | ) -> core::ffi::c_int,
|
| 88 | >,
|
| 89 | }
|
| 90 |
|
| 91 | |
| 92 | pub trait AllocImpl: super::private::Sealed + drm::gem::IntoGEMObject {
|
| 93 | |
| 94 | type Driver: drm::Driver;
|
| 95 |
|
| 96 | |
| 97 | const ALLOC_OPS: AllocOps;
|
| 98 | }
|
| 99 |
|
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | #[vtable]
|
| 105 | pub trait Driver {
|
| 106 | |
| 107 | type Data: Sync + Send;
|
| 108 |
|
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | type RegistrationData<'a>: Send + Sync + 'a;
|
| 116 |
|
| 117 | |
| 118 | type Object: AllocImpl;
|
| 119 |
|
| 120 | |
| 121 | type File: drm::file::DriverFile;
|
| 122 |
|
| 123 | |
| 124 | type ParentDevice<Ctx: device::DeviceContext>: device::AsBusDevice<Ctx>;
|
| 125 |
|
| 126 | |
| 127 | const INFO: DriverInfo;
|
| 128 |
|
| 129 | |
| 130 | const IOCTLS: &'static [drm::ioctl::DrmIoctlDescriptor];
|
| 131 |
|
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | const FEAT_RENDER: bool = false;
|
| 141 | }
|
| 142 |
|
| 143 | |
| 144 | |
| 145 | |
| 146 | pub struct Registration<'a, T: Driver> {
|
| 147 | drm: ARef<drm::Device<T>>,
|
| 148 | _reg_data: Pin<KBox<T::RegistrationData<'a>>>,
|
| 149 | }
|
| 150 |
|
| 151 | impl<'a, T: Driver> Registration<'a, T> {
|
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | pub unsafe fn new<E>(
|
| 160 | dev: &'a device::Device<device::Bound>,
|
| 161 | drm: drm::UnregisteredDevice<T>,
|
| 162 | reg_data: impl PinInit<T::RegistrationData<'a>, E>,
|
| 163 | flags: usize,
|
| 164 | ) -> Result<Self>
|
| 165 | where
|
| 166 | Error: From<E>,
|
| 167 | {
|
| 168 | let parent = drm.as_ref();
|
| 169 | if parent.as_ref().as_raw() != dev.as_raw() {
|
| 170 | return Err(EINVAL);
|
| 171 | }
|
| 172 |
|
| 173 | let reg_data: Pin<KBox<T::RegistrationData<'a>>> = KBox::pin_init(reg_data, GFP_KERNEL)?;
|
| 174 |
|
| 175 | |
| 176 | |
| 177 | let ptr: NonNull<T::RegistrationData<'static>> =
|
| 178 | NonNull::from(Pin::get_ref(reg_data.as_ref())).cast();
|
| 179 |
|
| 180 | |
| 181 | unsafe { *drm.registration_data.get() = ptr };
|
| 182 |
|
| 183 | |
| 184 | let ret = unsafe { bindings::drm_dev_register(drm.as_raw(), flags) };
|
| 185 | if let Err(e) = to_result(ret) {
|
| 186 | |
| 187 | |
| 188 | unsafe { *drm.registration_data.get() = NonNull::dangling() };
|
| 189 | return Err(e);
|
| 190 | }
|
| 191 |
|
| 192 | Ok(Self {
|
| 193 | drm: (&*drm).into(),
|
| 194 | _reg_data: reg_data,
|
| 195 | })
|
| 196 | }
|
| 197 |
|
| 198 | |
| 199 | pub fn device(&self) -> &drm::Device<T> {
|
| 200 | &self.drm
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | |
| 205 | |
| 206 | unsafe impl<T: Driver> Sync for Registration<'_, T> {}
|
| 207 |
|
| 208 | |
| 209 | unsafe impl<T: Driver> Send for Registration<'_, T> {}
|
| 210 |
|
| 211 | impl<T: Driver> Drop for Registration<'_, T> {
|
| 212 | fn drop(&mut self) {
|
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | unsafe { bindings::drm_dev_unplug(self.drm.as_raw()) };
|
| 222 | |
| 223 | |
| 224 | |
| 225 | }
|
| 226 | }
|