Skip to content

Commit 1a62983

Browse files
drm: DRM_IOCTL_MODE_GETRESOURCES fill in other fields
* Fill in the `crtc_id_ptr`, `encoder_id_ptr` and `connector_id_ptr` fields. * Introduce the `ModeObject` trait for more simple API * Create a helper function (`copy_mode_obj_id`) to copy the object IDs into the user buffer. Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent dd1448a commit 1a62983

File tree

1 file changed

+78
-28
lines changed
  • src/aero_kernel/src/drivers/drm

1 file changed

+78
-28
lines changed

src/aero_kernel/src/drivers/drm/mod.rs

Lines changed: 78 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ use crate::utils::sync::Mutex;
3636

3737
use uapi::drm::*;
3838

39+
/// Represents modset objects visible to userspace; this includes connectors,
40+
/// CRTCs, encoders, frambuffers and planes.
41+
trait ModeObject: Send + Sync {
42+
/// Returns the mode object's ID.
43+
fn id(&self) -> u32;
44+
}
45+
46+
trait DrmDevice: Send + Sync {
47+
/// Returns weather the DRM device supports creating dumb buffers.
48+
fn dumb_create(&self) -> bool;
49+
50+
/// Returns tuple containing the minumum dimensions (`xmin`, `ymin`).
51+
fn min_dim(&self) -> (usize, usize);
52+
/// Returns tuple containing the miximum dimensions (`xmax`, `ymax`).
53+
fn max_dim(&self) -> (usize, usize);
54+
55+
/// Returns a tuple containg the driver major, minor and patch level respectively.
56+
fn driver_version(&self) -> (usize, usize, usize);
57+
/// Returns a tuple contaning the driver name, desc and date respectively.
58+
fn driver_info(&self) -> (&'static str, &'static str, &'static str);
59+
}
60+
3961
// ## Notes:
4062
//
4163
// Plane: Image source
@@ -60,32 +82,48 @@ use uapi::drm::*;
6082

6183
#[derive(Default)]
6284
struct Crtc {
63-
id: usize,
85+
id: u32,
86+
}
87+
88+
impl ModeObject for Crtc {
89+
fn id(&self) -> u32 {
90+
self.id
91+
}
6492
}
6593

6694
#[derive(Default)]
6795
struct Encoder {
68-
id: usize,
96+
id: u32,
97+
}
98+
99+
impl ModeObject for Encoder {
100+
fn id(&self) -> u32 {
101+
self.id
102+
}
69103
}
70104

71105
#[derive(Default)]
72106
struct Connector {
73-
id: usize,
107+
id: u32,
74108
}
75109

76-
trait DrmDevice: Send + Sync {
77-
/// Returns weather the DRM device supports creating dumb buffers.
78-
fn dumb_create(&self) -> bool;
110+
impl ModeObject for Connector {
111+
fn id(&self) -> u32 {
112+
self.id
113+
}
114+
}
79115

80-
/// Returns tuple containing the minumum dimensions (`xmin`, `ymin`).
81-
fn min_dim(&self) -> (usize, usize);
82-
/// Returns tuple containing the miximum dimensions (`xmax`, `ymax`).
83-
fn max_dim(&self) -> (usize, usize);
116+
/// Holds information in relation to the framebuffer; this includes the
117+
/// size and pixel format.
118+
#[derive(Default)]
119+
struct Framebuffer {
120+
id: u32,
121+
}
84122

85-
/// Returns a tuple containg the driver major, minor and patch level respectively.
86-
fn driver_version(&self) -> (usize, usize, usize);
87-
/// Returns a tuple contaning the driver name, desc and date respectively.
88-
fn driver_info(&self) -> (&'static str, &'static str, &'static str);
123+
impl ModeObject for Framebuffer {
124+
fn id(&self) -> u32 {
125+
self.id
126+
}
89127
}
90128

91129
fn copy_field<T>(buffer: *mut T, buffer_size: &mut usize, value: &[T]) {
@@ -120,9 +158,11 @@ struct Drm {
120158
card_id: usize,
121159
device: Arc<dyn DrmDevice>,
122160

161+
// All of the mode objects:
123162
crtcs: Mutex<Vec<Crtc>>,
124163
encoders: Mutex<Vec<Encoder>>,
125164
connectors: Mutex<Vec<Connector>>,
165+
framebuffers: Mutex<Vec<Framebuffer>>,
126166
}
127167

128168
impl Drm {
@@ -137,14 +177,15 @@ impl Drm {
137177
crtcs: Mutex::new(alloc::vec![]),
138178
encoders: Mutex::new(alloc::vec![]),
139179
connectors: Mutex::new(alloc::vec![]),
180+
framebuffers: Mutex::new(alloc::vec![]),
140181
})
141182
}
142183

143184
/// Installs and initializes the CRTC identifier.
144185
pub fn install_crtc(&self, mut crtc: Crtc) {
145186
let mut crtcs = self.crtcs.lock();
146187

147-
crtc.id = crtcs.len();
188+
crtc.id = crtcs.len() as u32;
148189
crtcs.push(crtc);
149190
}
150191
}
@@ -196,25 +237,34 @@ impl INodeInterface for Drm {
196237
.read_mut::<DrmModeCardRes>()
197238
.unwrap();
198239

199-
{
200-
let crts = self.crtcs.lock();
201-
let mut count_crtcs = 0;
240+
/// Copies the mode object IDs into the user provided buffer. For saftey, checkout
241+
/// the [`copy_field`] function.
242+
fn copy_mode_obj_id<T: ModeObject>(
243+
obj: &Mutex<Vec<T>>,
244+
buffer: *mut u32,
245+
buffer_size: &mut u32,
246+
) {
247+
let objs = obj.lock();
248+
let mut count_objs = 0;
202249

203250
copy_field::<u32>(
204-
struc.crtc_id_ptr as *mut u32,
205-
&mut count_crtcs,
206-
crts.iter()
207-
.map(|e| e.id as u32)
208-
.collect::<Vec<_>>()
209-
.as_slice(),
251+
buffer,
252+
&mut count_objs,
253+
objs.iter().map(|e| e.id()).collect::<Vec<_>>().as_slice(),
210254
);
211255

212-
struc.count_crtcs = count_crtcs as _;
256+
*buffer_size = count_objs as _;
213257
}
214258

215-
// todo: set DRM encoder ids
216-
// todo: set DRM connector ids
217-
// todo: set DRM framebuffer ids
259+
let crtc_id_ptr = struc.crtc_id_ptr as *mut u32;
260+
let encoder_id_ptr = struc.encoder_id_ptr as *mut u32;
261+
let con_id_ptr = struc.connector_id_ptr as *mut u32;
262+
let fb_id_ptr = struc.fb_id_ptr as *mut u32;
263+
264+
copy_mode_obj_id(&self.crtcs, crtc_id_ptr, &mut struc.count_crtcs);
265+
copy_mode_obj_id(&self.encoders, encoder_id_ptr, &mut struc.count_encoders);
266+
copy_mode_obj_id(&self.connectors, con_id_ptr, &mut struc.count_connectors);
267+
copy_mode_obj_id(&self.framebuffers, fb_id_ptr, &mut struc.count_fbs);
218268

219269
let (xmin, ymin) = self.device.min_dim();
220270

0 commit comments

Comments
 (0)