Skip to content

Commit

Permalink
Merge pull request #174 from ids1024/borrowedfd
Browse files Browse the repository at this point in the history
drm-ffi: Accept `BorrowedFd` arguments instead of `RawFd`
  • Loading branch information
Drakulix authored Oct 18, 2023
2 parents 250fa4a + 7c91be7 commit 33a87a0
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 244 deletions.
27 changes: 17 additions & 10 deletions drm-ffi/src/gem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,67 @@ use ioctl;

use result::SystemError as Error;

use std::os::unix::io::RawFd;
use std::os::unix::io::{AsRawFd, BorrowedFd};

/// Open a GEM object given it's 32-bit name, returning the handle.
pub fn open(fd: RawFd, name: u32) -> Result<drm_gem_open, Error> {
pub fn open(fd: BorrowedFd<'_>, name: u32) -> Result<drm_gem_open, Error> {
let mut gem = drm_gem_open {
name,
..Default::default()
};

unsafe {
ioctl::gem::open(fd, &mut gem)?;
ioctl::gem::open(fd.as_raw_fd(), &mut gem)?;
}

Ok(gem)
}

/// Closes a GEM object given it's handle.
pub fn close(fd: RawFd, handle: u32) -> Result<drm_gem_close, Error> {
pub fn close(fd: BorrowedFd<'_>, handle: u32) -> Result<drm_gem_close, Error> {
let gem = drm_gem_close {
handle,
..Default::default()
};

unsafe {
ioctl::gem::close(fd, &gem)?;
ioctl::gem::close(fd.as_raw_fd(), &gem)?;
}

Ok(gem)
}

/// Converts a GEM object's handle to a PRIME file descriptor.
pub fn handle_to_fd(fd: RawFd, handle: u32, flags: u32) -> Result<drm_prime_handle, Error> {
pub fn handle_to_fd(
fd: BorrowedFd<'_>,
handle: u32,
flags: u32,
) -> Result<drm_prime_handle, Error> {
let mut prime = drm_prime_handle {
handle,
flags,
..Default::default()
};

unsafe {
ioctl::gem::prime_handle_to_fd(fd, &mut prime)?;
ioctl::gem::prime_handle_to_fd(fd.as_raw_fd(), &mut prime)?;
}

Ok(prime)
}

/// Converts a PRIME file descriptor to a GEM object's handle.
pub fn fd_to_handle(fd: RawFd, primefd: RawFd) -> Result<drm_prime_handle, Error> {
pub fn fd_to_handle(
fd: BorrowedFd<'_>,
primefd: BorrowedFd<'_>,
) -> Result<drm_prime_handle, Error> {
let mut prime = drm_prime_handle {
fd: primefd,
fd: primefd.as_raw_fd(),
..Default::default()
};

unsafe {
ioctl::gem::prime_fd_to_handle(fd, &mut prime)?;
ioctl::gem::prime_fd_to_handle(fd.as_raw_fd(), &mut prime)?;
}

Ok(prime)
Expand Down
56 changes: 30 additions & 26 deletions drm-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub mod result;
pub mod syncobj;

use nix::libc::{c_int, c_ulong};
use std::os::unix::io::RawFd;
use std::os::unix::io::{AsRawFd, BorrowedFd};

///
/// Bindings to the methods of authentication the DRM provides.
Expand All @@ -33,54 +33,54 @@ pub mod auth {
use ioctl;

use nix::Error;
use std::os::unix::io::RawFd;
use std::os::unix::io::{AsRawFd, BorrowedFd};

/// Get the 'Magic Authentication Token' for this file descriptor.
pub fn get_magic_token(fd: RawFd) -> Result<drm_auth, Error> {
pub fn get_magic_token(fd: BorrowedFd<'_>) -> Result<drm_auth, Error> {
let mut auth = drm_auth::default();

unsafe {
ioctl::get_token(fd, &mut auth)?;
ioctl::get_token(fd.as_raw_fd(), &mut auth)?;
}

Ok(auth)
}

/// Authorize another process' 'Magic Authentication Token'.
pub fn auth_magic_token(fd: RawFd, auth: u32) -> Result<drm_auth, Error> {
pub fn auth_magic_token(fd: BorrowedFd<'_>, auth: u32) -> Result<drm_auth, Error> {
let token = drm_auth { magic: auth };

unsafe {
ioctl::auth_token(fd, &token)?;
ioctl::auth_token(fd.as_raw_fd(), &token)?;
}

Ok(token)
}

/// Acquire the 'Master DRM Lock' for this file descriptor.
pub fn acquire_master(fd: RawFd) -> Result<(), Error> {
pub fn acquire_master(fd: BorrowedFd<'_>) -> Result<(), Error> {
unsafe {
ioctl::acquire_master(fd)?;
ioctl::acquire_master(fd.as_raw_fd())?;
}

Ok(())
}

/// Release the 'Master DRM Lock' for this file descriptor.
pub fn release_master(fd: RawFd) -> Result<(), Error> {
pub fn release_master(fd: BorrowedFd<'_>) -> Result<(), Error> {
unsafe {
ioctl::release_master(fd)?;
ioctl::release_master(fd.as_raw_fd())?;
}

Ok(())
}
}

/// Load this device's Bus ID into a buffer.
pub fn get_bus_id(fd: RawFd, mut buf: Option<&mut Vec<u8>>) -> Result<drm_unique, Error> {
pub fn get_bus_id(fd: BorrowedFd<'_>, mut buf: Option<&mut Vec<u8>>) -> Result<drm_unique, Error> {
let mut sizes = drm_unique::default();
unsafe {
ioctl::get_bus_id(fd, &mut sizes)?;
ioctl::get_bus_id(fd.as_raw_fd(), &mut sizes)?;
}

if buf.is_none() {
Expand All @@ -95,7 +95,7 @@ pub fn get_bus_id(fd: RawFd, mut buf: Option<&mut Vec<u8>>) -> Result<drm_unique
};

unsafe {
ioctl::get_bus_id(fd, &mut busid)?;
ioctl::get_bus_id(fd.as_raw_fd(), &mut busid)?;
}

map_set!(buf, busid.unique_len as usize);
Expand All @@ -105,7 +105,7 @@ pub fn get_bus_id(fd: RawFd, mut buf: Option<&mut Vec<u8>>) -> Result<drm_unique

/// Get a device's IRQ.
pub fn get_interrupt_from_bus_id(
fd: RawFd,
fd: BorrowedFd<'_>,
bus: c_int,
dev: c_int,
func: c_int,
Expand All @@ -118,64 +118,68 @@ pub fn get_interrupt_from_bus_id(
};

unsafe {
ioctl::get_irq_from_bus_id(fd, &mut irq)?;
ioctl::get_irq_from_bus_id(fd.as_raw_fd(), &mut irq)?;
}

Ok(irq)
}

/// Get client information given a client's ID.
pub fn get_client(fd: RawFd, idx: c_int) -> Result<drm_client, Error> {
pub fn get_client(fd: BorrowedFd<'_>, idx: c_int) -> Result<drm_client, Error> {
let mut client = drm_client {
idx,
..Default::default()
};

unsafe {
ioctl::get_client(fd, &mut client)?;
ioctl::get_client(fd.as_raw_fd(), &mut client)?;
}

Ok(client)
}

/// Check if a capability is set.
pub fn get_capability(fd: RawFd, cty: u64) -> Result<drm_get_cap, Error> {
pub fn get_capability(fd: BorrowedFd<'_>, cty: u64) -> Result<drm_get_cap, Error> {
let mut cap = drm_get_cap {
capability: cty,
..Default::default()
};

unsafe {
ioctl::get_cap(fd, &mut cap)?;
ioctl::get_cap(fd.as_raw_fd(), &mut cap)?;
}

Ok(cap)
}

/// Attempt to enable/disable a client's capability.
pub fn set_capability(fd: RawFd, cty: u64, val: bool) -> Result<drm_set_client_cap, Error> {
pub fn set_capability(
fd: BorrowedFd<'_>,
cty: u64,
val: bool,
) -> Result<drm_set_client_cap, Error> {
let cap = drm_set_client_cap {
capability: cty,
value: val as u64,
};

unsafe {
ioctl::set_cap(fd, &cap)?;
ioctl::set_cap(fd.as_raw_fd(), &cap)?;
}

Ok(cap)
}

/// Gets the driver version for this device.
pub fn get_version(
fd: RawFd,
fd: BorrowedFd<'_>,
mut name_buf: Option<&mut Vec<i8>>,
mut date_buf: Option<&mut Vec<i8>>,
mut desc_buf: Option<&mut Vec<i8>>,
) -> Result<drm_version, Error> {
let mut sizes = drm_version::default();
unsafe {
ioctl::get_version(fd, &mut sizes)?;
ioctl::get_version(fd.as_raw_fd(), &mut sizes)?;
}

map_reserve!(name_buf, sizes.name_len as usize);
Expand All @@ -193,7 +197,7 @@ pub fn get_version(
};

unsafe {
ioctl::get_version(fd, &mut version)?;
ioctl::get_version(fd.as_raw_fd(), &mut version)?;
}

map_set!(name_buf, version.name_len as usize);
Expand All @@ -205,7 +209,7 @@ pub fn get_version(

/// Waits for a vblank.
pub fn wait_vblank(
fd: RawFd,
fd: BorrowedFd<'_>,
type_: u32,
sequence: u32,
signal: usize,
Expand All @@ -222,7 +226,7 @@ pub fn wait_vblank(
};

unsafe {
ioctl::wait_vblank(fd, &mut wait_vblank)?;
ioctl::wait_vblank(fd.as_raw_fd(), &mut wait_vblank)?;
};

Ok(unsafe { wait_vblank.reply })
Expand Down
Loading

0 comments on commit 33a87a0

Please sign in to comment.