Skip to content

Commit

Permalink
linux-native: provide better signatures for the ioctl wrappers
Browse files Browse the repository at this point in the history
It looks like I missed the write-only or read-only versions of these macros
which let us specify that a ioctl is only writing or only reading.

Use them so we match the actual functionality. This also allows us to remove a
copy when sending a feature report as we don't have to work around the
misidentified mutability.
  • Loading branch information
carlosmn committed Nov 10, 2024
1 parent 858b66f commit cc1b2b3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
9 changes: 1 addition & 8 deletions src/linux_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,14 +524,7 @@ impl HidDeviceBackendBase for HidDevice {
return Err(HidError::InvalidZeroSizeData);
}

// Have to crate owned buffer, because its not safe to cast shared
// reference to mutable reference, even if the underlying function never
// tries to mutate it.
let mut d = data.to_vec();

// The ioctl is marked as read-write so we need to mess with the
// mutability even though nothing should get written
let res = match unsafe { hidraw_ioc_set_feature(self.fd.as_raw_fd(), &mut d) } {
let res = match unsafe { hidraw_ioc_set_feature(self.fd.as_raw_fd(), data) } {
Ok(n) => n as usize,
Err(e) => {
return Err(HidError::HidApiError {
Expand Down
6 changes: 3 additions & 3 deletions src/linux_native/ioctl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The IOCTL calls we need for the native linux backend
use nix::{ioctl_read, ioctl_readwrite_buf};
use nix::{ioctl_read, ioctl_read_buf, ioctl_write_buf};

// From linux/hidraw.h
const HIDRAW_IOC_MAGIC: u8 = b'H';
Expand All @@ -15,13 +15,13 @@ ioctl_read!(
libc::c_int
);

ioctl_readwrite_buf!(
ioctl_write_buf!(
hidraw_ioc_set_feature,
HIDRAW_IOC_MAGIC,
HIDRAW_SET_FEATURE,
u8
);
ioctl_readwrite_buf!(
ioctl_read_buf!(
hidraw_ioc_get_feature,
HIDRAW_IOC_MAGIC,
HIDRAW_GET_FEATURE,
Expand Down

0 comments on commit cc1b2b3

Please sign in to comment.