Skip to content

Commit

Permalink
Merge pull request #4114 from folkertdev/add-ptp-clock-consts
Browse files Browse the repository at this point in the history
add various `PTP_` consts
  • Loading branch information
tgross35 authored Nov 20, 2024
2 parents b749815 + a2ec642 commit 99248c7
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
16 changes: 16 additions & 0 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3903,6 +3903,22 @@ fn test_linux(target: &str) {
{
return true;
}
// FIXME: Requires >= 4.20 kernel headers
if name == "PTP_SYS_OFFSET_EXTENDED" {
return true;
}
// FIXME: Requires >= 5.4 kernel headers
if name == "PTP_ENABLE_PPS2"
|| name == "PTP_EXTTS_REQUEST2"
|| name == "PTP_PEROUT_REQUEST2"
|| name == "PTP_PIN_GETFUNC2"
|| name == "PTP_PIN_SETFUNC2"
|| name == "PTP_SYS_OFFSET2"
|| name == "PTP_SYS_OFFSET_PRECISE2"
|| name == "PTP_SYS_OFFSET_EXTENDED2"
{
return true;
}
// FIXME: Requires >= 5.4.1 kernel headers
if name.starts_with("J1939")
|| name.starts_with("RTEXT_FILTER_")
Expand Down
20 changes: 20 additions & 0 deletions libc-test/semver/linux.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2252,7 +2252,27 @@ PTHREAD_PRIO_PROTECT
PTHREAD_PROCESS_PRIVATE
PTHREAD_PROCESS_SHARED
PTHREAD_STACK_MIN
PTP_ENABLE_PPS
PTP_ENABLE_PPS2
PTP_EXTTS_REQUEST
PTP_EXTTS_REQUEST2
PTP_MAX_SAMPLES
PTP_PEROUT_REQUEST
PTP_PEROUT_REQUEST2
PTP_PF_EXTTS
PTP_PF_NONE
PTP_PF_PEROUT
PTP_PF_PHYSYNC
PTP_PIN_GETFUNC
PTP_PIN_GETFUNC2
PTP_PIN_SETFUNC
PTP_PIN_SETFUNC2
PTP_SYS_OFFSET
PTP_SYS_OFFSET2
PTP_SYS_OFFSET_EXTENDED
PTP_SYS_OFFSET_EXTENDED2
PTP_SYS_OFFSET_PRECISE
PTP_SYS_OFFSET_PRECISE2
PTRACE_ATTACH
PTRACE_CONT
PTRACE_DETACH
Expand Down
110 changes: 110 additions & 0 deletions src/unix/linux_like/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4541,6 +4541,36 @@ pub const HWTSTAMP_FILTER_NTP_ALL: ::c_uint = 15;
// linux/ptp_clock.h
pub const PTP_MAX_SAMPLES: ::c_uint = 25; // Maximum allowed offset measurement samples.

const PTP_CLK_MAGIC: u32 = b'=' as u32;

// FIXME: needs the ptp_clock_caps struct
// pub const PTP_CLOCK_GETCAPS: ::c_uint = _IOR::<ptp_clock_caps>(PTP_CLK_MAGIC, 1);
pub const PTP_EXTTS_REQUEST: ::c_uint = _IOW::<ptp_extts_request>(PTP_CLK_MAGIC, 2);
pub const PTP_PEROUT_REQUEST: ::c_uint = _IOW::<ptp_perout_request>(PTP_CLK_MAGIC, 3);
pub const PTP_ENABLE_PPS: ::c_uint = _IOW::<::c_int>(PTP_CLK_MAGIC, 4);
pub const PTP_SYS_OFFSET: ::c_uint = _IOW::<ptp_sys_offset>(PTP_CLK_MAGIC, 5);
pub const PTP_PIN_GETFUNC: ::c_uint = _IOWR::<ptp_pin_desc>(PTP_CLK_MAGIC, 6);
pub const PTP_PIN_SETFUNC: ::c_uint = _IOW::<ptp_pin_desc>(PTP_CLK_MAGIC, 7);
pub const PTP_SYS_OFFSET_PRECISE: ::c_uint = _IOWR::<ptp_sys_offset_precise>(PTP_CLK_MAGIC, 8);
pub const PTP_SYS_OFFSET_EXTENDED: ::c_uint = _IOWR::<ptp_sys_offset_extended>(PTP_CLK_MAGIC, 9);

// FIXME: needs the ptp_clock_caps struct
// pub const PTP_CLOCK_GETCAPS2: ::c_uint = _IOR::<ptp_clock_caps>(PTP_CLK_MAGIC, 10);
pub const PTP_EXTTS_REQUEST2: ::c_uint = _IOW::<ptp_extts_request>(PTP_CLK_MAGIC, 11);
pub const PTP_PEROUT_REQUEST2: ::c_uint = _IOW::<ptp_perout_request>(PTP_CLK_MAGIC, 12);
pub const PTP_ENABLE_PPS2: ::c_uint = _IOW::<::c_int>(PTP_CLK_MAGIC, 13);
pub const PTP_SYS_OFFSET2: ::c_uint = _IOW::<ptp_sys_offset>(PTP_CLK_MAGIC, 14);
pub const PTP_PIN_GETFUNC2: ::c_uint = _IOWR::<ptp_pin_desc>(PTP_CLK_MAGIC, 15);
pub const PTP_PIN_SETFUNC2: ::c_uint = _IOW::<ptp_pin_desc>(PTP_CLK_MAGIC, 16);
pub const PTP_SYS_OFFSET_PRECISE2: ::c_uint = _IOWR::<ptp_sys_offset_precise>(PTP_CLK_MAGIC, 17);
pub const PTP_SYS_OFFSET_EXTENDED2: ::c_uint = _IOWR::<ptp_sys_offset_extended>(PTP_CLK_MAGIC, 18);

// enum ptp_pin_function
pub const PTP_PF_NONE: ::c_uint = 0;
pub const PTP_PF_EXTTS: ::c_uint = 1;
pub const PTP_PF_PEROUT: ::c_uint = 2;
pub const PTP_PF_PHYSYNC: ::c_uint = 3;

// linux/tls.h
pub const TLS_TX: ::c_int = 1;
pub const TLS_RX: ::c_int = 2;
Expand Down Expand Up @@ -5595,6 +5625,86 @@ pub const SCHED_FLAG_ALL: ::c_int = SCHED_FLAG_RESET_ON_FORK
pub const EPIOCSPARAMS: ::Ioctl = 0x40088a01;
pub const EPIOCGPARAMS: ::Ioctl = 0x80088a02;

const _IOC_NRBITS: u32 = 8;
const _IOC_TYPEBITS: u32 = 8;

// https://github.com/search?q=repo%3Atorvalds%2Flinux+%22%23define+_IOC_NONE%22&type=code
cfg_if! {
if #[cfg(any(
any(target_arch = "powerpc", target_arch = "powerpc64"),
any(target_arch = "sparc", target_arch = "sparc64"),
any(target_arch = "mips", target_arch = "mips64"),
))] {
// https://github.com/torvalds/linux/blob/b311c1b497e51a628aa89e7cb954481e5f9dced2/arch/powerpc/include/uapi/asm/ioctl.h
// https://github.com/torvalds/linux/blob/b311c1b497e51a628aa89e7cb954481e5f9dced2/arch/sparc/include/uapi/asm/ioctl.h
// https://github.com/torvalds/linux/blob/b311c1b497e51a628aa89e7cb954481e5f9dced2/arch/mips/include/uapi/asm/ioctl.h

const _IOC_SIZEBITS: u32 = 13;
const _IOC_DIRBITS: u32 = 3;

const _IOC_NONE: u32 = 1;
const _IOC_READ: u32 = 2;
const _IOC_WRITE: u32 = 4;
} else {
// https://github.com/torvalds/linux/blob/b311c1b497e51a628aa89e7cb954481e5f9dced2/include/uapi/asm-generic/ioctl.h

const _IOC_SIZEBITS: u32 = 14;
const _IOC_DIRBITS: u32 = 2;

const _IOC_NONE: u32 = 0;
const _IOC_WRITE: u32 = 1;
const _IOC_READ: u32 = 2;
}
}

const _IOC_NRMASK: u32 = (1 << _IOC_NRBITS) - 1;
const _IOC_TYPEMASK: u32 = (1 << _IOC_TYPEBITS) - 1;
const _IOC_SIZEMASK: u32 = (1 << _IOC_SIZEBITS) - 1;
const _IOC_DIRMASK: u32 = (1 << _IOC_DIRBITS) - 1;

const _IOC_NRSHIFT: u32 = 0;
const _IOC_TYPESHIFT: u32 = _IOC_NRSHIFT + _IOC_NRBITS;
const _IOC_SIZESHIFT: u32 = _IOC_TYPESHIFT + _IOC_TYPEBITS;
const _IOC_DIRSHIFT: u32 = _IOC_SIZESHIFT + _IOC_SIZEBITS;

// adapted from https://github.com/torvalds/linux/blob/8a696a29c6905594e4abf78eaafcb62165ac61f1/rust/kernel/ioctl.rs

/// Build an ioctl number, analogous to the C macro of the same name.
const fn _IOC(dir: u32, ty: u32, nr: u32, size: usize) -> u32 {
// FIXME(ctest) the `garando_syntax` crate (used by ctest2 in the CI test suite)
// cannot currently parse these `debug_assert!`s
//
// debug_assert!(dir <= _IOC_DIRMASK);
// debug_assert!(ty <= _IOC_TYPEMASK);
// debug_assert!(nr <= _IOC_NRMASK);
// debug_assert!(size <= (_IOC_SIZEMASK as usize));

(dir << _IOC_DIRSHIFT)
| (ty << _IOC_TYPESHIFT)
| (nr << _IOC_NRSHIFT)
| ((size as u32) << _IOC_SIZESHIFT)
}

/// Build an ioctl number for an argumentless ioctl.
pub(crate) const fn _IO(ty: u32, nr: u32) -> u32 {
_IOC(_IOC_NONE, ty, nr, 0)
}

/// Build an ioctl number for an read-only ioctl.
pub(crate) const fn _IOR<T>(ty: u32, nr: u32) -> u32 {
_IOC(_IOC_READ, ty, nr, core::mem::size_of::<T>())
}

/// Build an ioctl number for an write-only ioctl.
pub(crate) const fn _IOW<T>(ty: u32, nr: u32) -> u32 {
_IOC(_IOC_WRITE, ty, nr, core::mem::size_of::<T>())
}

/// Build an ioctl number for a read-write ioctl.
pub(crate) const fn _IOWR<T>(ty: u32, nr: u32) -> u32 {
_IOC(_IOC_READ | _IOC_WRITE, ty, nr, core::mem::size_of::<T>())
}

f! {
pub fn NLA_ALIGN(len: ::c_int) -> ::c_int {
return ((len) + NLA_ALIGNTO - 1) & !(NLA_ALIGNTO - 1);
Expand Down

0 comments on commit 99248c7

Please sign in to comment.