From f66e60ee28a702b55b5a3d55963fc5b2929c4b3a Mon Sep 17 00:00:00 2001 From: David Venhoek Date: Thu, 14 Sep 2023 16:59:55 +0200 Subject: [PATCH 1/6] Added all definitions from linux/ptp-clock.h --- libc-test/build.rs | 27 ++++- libc-test/semver/linux.txt | 23 ++++ src/unix/linux_like/linux/mod.rs | 202 +++++++++++++++++++++++++++++++ 3 files changed, 250 insertions(+), 2 deletions(-) diff --git a/libc-test/build.rs b/libc-test/build.rs index 30ab41202e945..44f64f1e2fc0a 100644 --- a/libc-test/build.rs +++ b/libc-test/build.rs @@ -3542,6 +3542,8 @@ fn test_linux(target: &str) { "linux/netlink.h", // FIXME: requires Linux >= 5.6: [!musl]: "linux/openat2.h", + "linux/ptp_clock.h", + // FIXME: requires Linux >= 5.6: [!musl]: "linux/ptrace.h", "linux/quota.h", "linux/random.h", @@ -3679,6 +3681,9 @@ fn test_linux(target: &str) { if musl && ty == "seccomp_notif_sizes" { return true; } + if musl && ty == "ptp_sys_offset_extended" { + return true; + } // LFS64 types have been removed in musl 1.2.4+ if musl && (ty.ends_with("64") || ty.ends_with("64_t")) { @@ -3842,6 +3847,7 @@ fn test_linux(target: &str) { if name == "SECCOMP_GET_NOTIF_SIZES" || name == "SECCOMP_FILTER_FLAG_NEW_LISTENER" || name == "SECCOMP_FILTER_FLAG_TSYNC_ESRCH" + || name == "PTP_SYS_OFFSET_EXTENDED" || name == "SECCOMP_USER_NOTIF_FLAG_CONTINUE" // requires >= 5.5 || name == "SECCOMP_ADDFD_FLAG_SETFD" // requires >= 5.9 || name == "SECCOMP_ADDFD_FLAG_SEND" // requires >= 5.9 @@ -3854,6 +3860,15 @@ fn test_linux(target: &str) { || name.starts_with("RTEXT_FILTER_") || name.starts_with("SO_J1939") || name.starts_with("SCM_J1939") + || name == "PTP_CLOCK_GETCAPS2" + || name == "PTP_EXTTS_REQUEST2" + || name == "PTP_PEROUT_REQUEST2" + || name == "PTP_ENABLE_PPS2" + || name == "PTP_SYS_OFFSET2" + || name == "PTP_PIN_GETFUNC2" + || name == "PTP_PIN_SETFUNC2" + || name == "PTP_SYS_OFFSET_PRECISE2" + || name == "PTP_SYS_OFFSET_EXTENDED2" { return true; } @@ -4362,7 +4377,11 @@ fn test_linux(target: &str) { // `__exit_status` type is a patch which is absent in musl (struct_ == "utmpx" && field == "ut_exit" && musl) || // `can_addr` is an anonymous union - (struct_ == "sockaddr_can" && field == "can_addr") + (struct_ == "sockaddr_can" && field == "can_addr") || + // `anonymous_1` is an anonymous union + (struct_ == "ptp_perout_request" && field == "anonymous_1") || + // `anonymous_2` is an anonymous union + (struct_ == "ptp_perout_request" && field == "anonymous_2") }); cfg.volatile_item(|i| { @@ -4420,7 +4439,11 @@ fn test_linux(target: &str) { // the `ifc_ifcu` field is an anonymous union (struct_ == "ifconf" && field == "ifc_ifcu") || // glibc uses a single array `uregs` instead of individual fields. - (struct_ == "user_regs" && arm) + (struct_ == "user_regs" && arm) || + // `anonymous_1` is an anonymous union + (struct_ == "ptp_perout_request" && field == "anonymous_1") || + // `anonymous_2` is an anonymous union + (struct_ == "ptp_perout_request" && field == "anonymous_2") }); cfg.skip_roundtrip(move |s| match s { diff --git a/libc-test/semver/linux.txt b/libc-test/semver/linux.txt index 0bee4e68c16ae..45201fe557ab9 100644 --- a/libc-test/semver/linux.txt +++ b/libc-test/semver/linux.txt @@ -1997,6 +1997,29 @@ PTHREAD_INHERIT_SCHED PTHREAD_EXPLICIT_SCHED PTHREAD_STACK_MIN PTHREAD_ONCE_INIT +PTP_CLOCK_GETCAPS +PTP_CLOCK_GETCAPS2 +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 diff --git a/src/unix/linux_like/linux/mod.rs b/src/unix/linux_like/linux/mod.rs index 779faa81ff16e..3f85643b66c81 100644 --- a/src/unix/linux_like/linux/mod.rs +++ b/src/unix/linux_like/linux/mod.rs @@ -651,6 +651,57 @@ s! { pub val: ::c_int, } + // linux/ptp_clock.h + pub struct ptp_clock_time { + pub sec: ::__s64, + pub nsec: ::__u32, + pub reserved: ::__u32, + } + + pub struct ptp_clock_caps { + pub max_adj: ::c_int, + pub n_alarm: ::c_int, + pub n_ext_ts: ::c_int, + pub n_per_out: ::c_int, + pub pps: ::c_int, + pub n_pins: ::c_int, + pub cross_timestamping: ::c_int, + #[cfg(target_env = "gnu")] + pub adjust_phase: ::c_int, + #[cfg(target_env = "gnu")] + pub max_phase_adj: ::c_int, + #[cfg(target_env = "gnu")] + pub rsv: [::c_int; 11], + #[cfg(any(target_env = "musl", target_env = "ohos"))] + pub rsv: [::c_int; 13], + } + + pub struct ptp_extts_request { + pub index: ::c_uint, + pub flags: ::c_uint, + pub rsv: [::c_uint; 2], + } + + pub struct ptp_sys_offset_extended { + pub n_samples: ::c_uint, + pub rsv: [::c_uint; 3], + pub ts: [[ptp_clock_time; 3]; 25], + } + + pub struct ptp_sys_offset_precise { + pub device: ptp_clock_time, + pub sys_realtime: ptp_clock_time, + pub sys_monoraw: ptp_clock_time, + pub rsv: [::c_uint; 4], + } + + pub struct ptp_extts_event { + pub t: ptp_clock_time, + index: ::c_uint, + flags: ::c_uint, + rsv: [::c_uint; 2], + } + // linux/sctp.h pub struct sctp_initmsg { @@ -890,6 +941,21 @@ s_no_extra_traits! { pub sched_deadline: ::__u64, pub sched_period: ::__u64, } + + // linux/ptp_clock.h + pub struct ptp_sys_offset { + pub n_samples: ::c_uint, + pub rsv: [::c_uint; 3], + pub ts: [ptp_clock_time; 51], + } + + pub struct ptp_pin_desc { + pub name: [::c_char; 64], + pub index: ::c_uint, + pub func: ::c_uint, + pub chan: ::c_uint, + pub rsv: [::c_uint; 5], + } } s_no_extra_traits! { @@ -921,6 +987,34 @@ cfg_if! { } } +cfg_if! { + if #[cfg(libc_union)] { + s_no_extra_traits! { + // linux/ptp_clock.h + #[allow(missing_debug_implementations)] + pub union __c_anonymous_ptp_perout_request_1 { + pub start: ptp_clock_time, + pub phase: ptp_clock_time, + } + + #[allow(missing_debug_implementations)] + pub union __c_anonymous_ptp_perout_request_2 { + pub on: ptp_clock_time, + pub rsv: [::c_uint; 4], + } + + #[allow(missing_debug_implementations)] + pub struct ptp_perout_request { + pub anonymous_1: __c_anonymous_ptp_perout_request_1, + pub period: ptp_clock_time, + pub index: ::c_uint, + pub flags: ::c_uint, + pub anonymous_2: __c_anonymous_ptp_perout_request_2, + } + } + } +} + cfg_if! { if #[cfg(feature = "extra_traits")] { impl PartialEq for sockaddr_nl { @@ -1395,6 +1489,62 @@ cfg_if! { self.sched_period.hash(state); } } + + impl PartialEq for ptp_sys_offset { + fn eq(&self, other: &ptp_sys_offset) -> bool { + self.n_samples == other.n_samples && + self.rsv == other.rsv && + self.ts[..] == other.ts[..] + } + } + impl Eq for ptp_sys_offset {} + impl ::fmt::Debug for ptp_sys_offset { + fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result { + f.debug_struct("ptp_sys_offset") + .field("n_samples", &self.n_samples) + .field("rsv", &self.rsv) + .field("ts", &&self.ts[..]) + .finish() + } + } + impl ::hash::Hash for ptp_sys_offset { + fn hash(&self, state: &mut H) { + self.n_samples.hash(state); + self.rsv.hash(state); + self.ts[..].hash(state); + } + } + + impl PartialEq for ptp_pin_desc { + fn eq(&self, other: &ptp_pin_desc) -> bool { + self.name[..] == other.name[..] && + self.index == other.index && + self.func == other.func && + self.chan == other.chan && + self.rsv == other.rsv + } + } + impl Eq for ptp_pin_desc {} + impl ::fmt::Debug for ptp_pin_desc { + fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result { + f.debug_struct("ptp_pin_desc") + .field("name", &&self.name[..]) + .field("index", &self.index) + .field("func", &self.func) + .field("chan", &self.chan) + .field("rsv", &self.rsv) + .finish() + } + } + impl ::hash::Hash for ptp_pin_desc { + fn hash(&self, state: &mut H) { + self.name[..].hash(state); + self.index.hash(state); + self.func.hash(state); + self.chan.hash(state); + self.rsv.hash(state); + } + } } } @@ -3643,6 +3793,58 @@ pub const HWTSTAMP_FILTER_PTP_V2_SYNC: ::c_uint = 13; pub const HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: ::c_uint = 14; pub const HWTSTAMP_FILTER_NTP_ALL: ::c_uint = 15; +// linux/ptp_clock.h +pub const PTP_MAX_SAMPLES: ::c_uint = 25; + +cfg_if! { + if #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64", target_arch = "sparc64"))] { + pub const PTP_CLOCK_GETCAPS: ::c_uint = 0x40503d01; + pub const PTP_EXTTS_REQUEST: ::c_uint = 0x80103d02; + pub const PTP_PEROUT_REQUEST: ::c_uint = 0x80383d03; + pub const PTP_ENABLE_PPS: ::c_uint = 0x80043d04; + pub const PTP_SYS_OFFSET: ::c_uint = 0x83403d05; + pub const PTP_PIN_GETFUNC: ::c_uint = 0xc0603d06; + pub const PTP_PIN_SETFUNC: ::c_uint = 0x80603d07; + pub const PTP_SYS_OFFSET_PRECISE: ::c_uint = 0xc0403d08; + pub const PTP_SYS_OFFSET_EXTENDED: ::c_uint = 0xc4c03d09; + + pub const PTP_CLOCK_GETCAPS2: ::c_uint = 0x40503d0a; + pub const PTP_EXTTS_REQUEST2: ::c_uint = 0x80103d0b; + pub const PTP_PEROUT_REQUEST2: ::c_uint = 0x80383d0c; + pub const PTP_ENABLE_PPS2: ::c_uint = 0x80043d0d; + pub const PTP_SYS_OFFSET2: ::c_uint = 0x83403d0e; + pub const PTP_PIN_GETFUNC2: ::c_uint = 0xc0603d0f; + pub const PTP_PIN_SETFUNC2: ::c_uint = 0x80603d10; + pub const PTP_SYS_OFFSET_PRECISE2: ::c_uint = 0xc0403d11; + pub const PTP_SYS_OFFSET_EXTENDED2: ::c_uint = 0xc4c03d12; + } else { + pub const PTP_CLOCK_GETCAPS: ::c_uint = 0x80503d01; + pub const PTP_EXTTS_REQUEST: ::c_uint = 0x40103d02; + pub const PTP_PEROUT_REQUEST: ::c_uint = 0x40383d03; + pub const PTP_ENABLE_PPS: ::c_uint = 0x40043d04; + pub const PTP_SYS_OFFSET: ::c_uint = 0x43403d05; + pub const PTP_PIN_GETFUNC: ::c_uint = 0xc0603d06; + pub const PTP_PIN_SETFUNC: ::c_uint = 0x40603d07; + pub const PTP_SYS_OFFSET_PRECISE: ::c_uint = 0xc0403d08; + pub const PTP_SYS_OFFSET_EXTENDED: ::c_uint = 0xc4c03d09; + + pub const PTP_CLOCK_GETCAPS2: ::c_uint = 0x80503d0a; + pub const PTP_EXTTS_REQUEST2: ::c_uint = 0x40103d0b; + pub const PTP_PEROUT_REQUEST2: ::c_uint = 0x40383d0c; + pub const PTP_ENABLE_PPS2: ::c_uint = 0x40043d0d; + pub const PTP_SYS_OFFSET2: ::c_uint = 0x43403d0e; + pub const PTP_PIN_GETFUNC2: ::c_uint = 0xc0603d0f; + pub const PTP_PIN_SETFUNC2: ::c_uint = 0x40603d10; + pub const PTP_SYS_OFFSET_PRECISE2: ::c_uint = 0xc0403d11; + pub const PTP_SYS_OFFSET_EXTENDED2: ::c_uint = 0xc4c03d12; + } +} + +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; From aa5120f3faa41311ca0ac959d0a288ab7edfc972 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Wed, 21 Aug 2024 22:55:23 +0200 Subject: [PATCH 2/6] Apply suggestions from code review Co-authored-by: Trevor Gross --- libc-test/build.rs | 8 ++++---- src/unix/linux_like/linux/mod.rs | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/libc-test/build.rs b/libc-test/build.rs index 44f64f1e2fc0a..a02eb4c11fbaf 100644 --- a/libc-test/build.rs +++ b/libc-test/build.rs @@ -3844,10 +3844,10 @@ fn test_linux(target: &str) { } if musl { // FIXME: Requires >= 5.0 kernel headers - if name == "SECCOMP_GET_NOTIF_SIZES" + if name == "PTP_SYS_OFFSET_EXTENDED" + || name == "SECCOMP_GET_NOTIF_SIZES" || name == "SECCOMP_FILTER_FLAG_NEW_LISTENER" || name == "SECCOMP_FILTER_FLAG_TSYNC_ESRCH" - || name == "PTP_SYS_OFFSET_EXTENDED" || name == "SECCOMP_USER_NOTIF_FLAG_CONTINUE" // requires >= 5.5 || name == "SECCOMP_ADDFD_FLAG_SETFD" // requires >= 5.9 || name == "SECCOMP_ADDFD_FLAG_SEND" // requires >= 5.9 @@ -3861,12 +3861,12 @@ fn test_linux(target: &str) { || name.starts_with("SO_J1939") || name.starts_with("SCM_J1939") || name == "PTP_CLOCK_GETCAPS2" + || name == "PTP_ENABLE_PPS2" || name == "PTP_EXTTS_REQUEST2" || name == "PTP_PEROUT_REQUEST2" - || name == "PTP_ENABLE_PPS2" - || name == "PTP_SYS_OFFSET2" || name == "PTP_PIN_GETFUNC2" || name == "PTP_PIN_SETFUNC2" + || name == "PTP_SYS_OFFSET2" || name == "PTP_SYS_OFFSET_PRECISE2" || name == "PTP_SYS_OFFSET_EXTENDED2" { diff --git a/src/unix/linux_like/linux/mod.rs b/src/unix/linux_like/linux/mod.rs index 3f85643b66c81..ac443d362e563 100644 --- a/src/unix/linux_like/linux/mod.rs +++ b/src/unix/linux_like/linux/mod.rs @@ -3840,6 +3840,7 @@ cfg_if! { } } +// 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; From 30739406f409dc3b820f864f52d8a2859790114f Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Wed, 21 Aug 2024 22:59:55 +0200 Subject: [PATCH 3/6] use `PTP_MAX_SAMPLES` in struct definitions --- src/unix/linux_like/linux/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/unix/linux_like/linux/mod.rs b/src/unix/linux_like/linux/mod.rs index ac443d362e563..7ca80cccd97e4 100644 --- a/src/unix/linux_like/linux/mod.rs +++ b/src/unix/linux_like/linux/mod.rs @@ -685,7 +685,7 @@ s! { pub struct ptp_sys_offset_extended { pub n_samples: ::c_uint, pub rsv: [::c_uint; 3], - pub ts: [[ptp_clock_time; 3]; 25], + pub ts: [[ptp_clock_time; 3]; PTP_MAX_SAMPLES as usize], } pub struct ptp_sys_offset_precise { @@ -946,7 +946,7 @@ s_no_extra_traits! { pub struct ptp_sys_offset { pub n_samples: ::c_uint, pub rsv: [::c_uint; 3], - pub ts: [ptp_clock_time; 51], + pub ts: [ptp_clock_time; 2 * PTP_MAX_SAMPLES as usize + 1], } pub struct ptp_pin_desc { @@ -3794,7 +3794,7 @@ pub const HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: ::c_uint = 14; pub const HWTSTAMP_FILTER_NTP_ALL: ::c_uint = 15; // linux/ptp_clock.h -pub const PTP_MAX_SAMPLES: ::c_uint = 25; +pub const PTP_MAX_SAMPLES: ::c_uint = 25; // Maximum allowed offset measurement samples. cfg_if! { if #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64", target_arch = "sparc64"))] { From d561984d3a9be2cd137f45c07384617029f7aa91 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Wed, 21 Aug 2024 23:45:19 +0200 Subject: [PATCH 4/6] derive traits where possible --- src/unix/linux_like/linux/mod.rs | 58 ++------------------------------ 1 file changed, 2 insertions(+), 56 deletions(-) diff --git a/src/unix/linux_like/linux/mod.rs b/src/unix/linux_like/linux/mod.rs index 7ca80cccd97e4..78289fdd02fe5 100644 --- a/src/unix/linux_like/linux/mod.rs +++ b/src/unix/linux_like/linux/mod.rs @@ -941,7 +941,9 @@ s_no_extra_traits! { pub sched_deadline: ::__u64, pub sched_period: ::__u64, } +} +s! { // linux/ptp_clock.h pub struct ptp_sys_offset { pub n_samples: ::c_uint, @@ -1489,62 +1491,6 @@ cfg_if! { self.sched_period.hash(state); } } - - impl PartialEq for ptp_sys_offset { - fn eq(&self, other: &ptp_sys_offset) -> bool { - self.n_samples == other.n_samples && - self.rsv == other.rsv && - self.ts[..] == other.ts[..] - } - } - impl Eq for ptp_sys_offset {} - impl ::fmt::Debug for ptp_sys_offset { - fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result { - f.debug_struct("ptp_sys_offset") - .field("n_samples", &self.n_samples) - .field("rsv", &self.rsv) - .field("ts", &&self.ts[..]) - .finish() - } - } - impl ::hash::Hash for ptp_sys_offset { - fn hash(&self, state: &mut H) { - self.n_samples.hash(state); - self.rsv.hash(state); - self.ts[..].hash(state); - } - } - - impl PartialEq for ptp_pin_desc { - fn eq(&self, other: &ptp_pin_desc) -> bool { - self.name[..] == other.name[..] && - self.index == other.index && - self.func == other.func && - self.chan == other.chan && - self.rsv == other.rsv - } - } - impl Eq for ptp_pin_desc {} - impl ::fmt::Debug for ptp_pin_desc { - fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result { - f.debug_struct("ptp_pin_desc") - .field("name", &&self.name[..]) - .field("index", &self.index) - .field("func", &self.func) - .field("chan", &self.chan) - .field("rsv", &self.rsv) - .finish() - } - } - impl ::hash::Hash for ptp_pin_desc { - fn hash(&self, state: &mut H) { - self.name[..].hash(state); - self.index.hash(state); - self.func.hash(state); - self.chan.hash(state); - self.rsv.hash(state); - } - } } } From 42d149b0f7e0fe9b8a6da1abfa7708c1004ecb5d Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Wed, 21 Aug 2024 23:57:12 +0200 Subject: [PATCH 5/6] adjust `ptp_clock_caps` to older version --- src/unix/linux_like/linux/mod.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/unix/linux_like/linux/mod.rs b/src/unix/linux_like/linux/mod.rs index 78289fdd02fe5..754fca98cea5d 100644 --- a/src/unix/linux_like/linux/mod.rs +++ b/src/unix/linux_like/linux/mod.rs @@ -666,14 +666,8 @@ s! { pub pps: ::c_int, pub n_pins: ::c_int, pub cross_timestamping: ::c_int, - #[cfg(target_env = "gnu")] pub adjust_phase: ::c_int, - #[cfg(target_env = "gnu")] - pub max_phase_adj: ::c_int, - #[cfg(target_env = "gnu")] - pub rsv: [::c_int; 11], - #[cfg(any(target_env = "musl", target_env = "ohos"))] - pub rsv: [::c_int; 13], + pub rsv: [::c_int; 12], } pub struct ptp_extts_request { From e23acab856bbddfaada3d3b45fec8af142a6aa10 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Wed, 21 Aug 2024 23:40:02 +0200 Subject: [PATCH 6/6] use a `_IOC` const fn to generate constants adopted from RFL --- src/unix/linux_like/linux/mod.rs | 155 ++++++++++++++++++++++--------- 1 file changed, 112 insertions(+), 43 deletions(-) diff --git a/src/unix/linux_like/linux/mod.rs b/src/unix/linux_like/linux/mod.rs index 754fca98cea5d..7e89a5e92f656 100644 --- a/src/unix/linux_like/linux/mod.rs +++ b/src/unix/linux_like/linux/mod.rs @@ -3736,49 +3736,33 @@ 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. -cfg_if! { - if #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64", target_arch = "sparc64"))] { - pub const PTP_CLOCK_GETCAPS: ::c_uint = 0x40503d01; - pub const PTP_EXTTS_REQUEST: ::c_uint = 0x80103d02; - pub const PTP_PEROUT_REQUEST: ::c_uint = 0x80383d03; - pub const PTP_ENABLE_PPS: ::c_uint = 0x80043d04; - pub const PTP_SYS_OFFSET: ::c_uint = 0x83403d05; - pub const PTP_PIN_GETFUNC: ::c_uint = 0xc0603d06; - pub const PTP_PIN_SETFUNC: ::c_uint = 0x80603d07; - pub const PTP_SYS_OFFSET_PRECISE: ::c_uint = 0xc0403d08; - pub const PTP_SYS_OFFSET_EXTENDED: ::c_uint = 0xc4c03d09; - - pub const PTP_CLOCK_GETCAPS2: ::c_uint = 0x40503d0a; - pub const PTP_EXTTS_REQUEST2: ::c_uint = 0x80103d0b; - pub const PTP_PEROUT_REQUEST2: ::c_uint = 0x80383d0c; - pub const PTP_ENABLE_PPS2: ::c_uint = 0x80043d0d; - pub const PTP_SYS_OFFSET2: ::c_uint = 0x83403d0e; - pub const PTP_PIN_GETFUNC2: ::c_uint = 0xc0603d0f; - pub const PTP_PIN_SETFUNC2: ::c_uint = 0x80603d10; - pub const PTP_SYS_OFFSET_PRECISE2: ::c_uint = 0xc0403d11; - pub const PTP_SYS_OFFSET_EXTENDED2: ::c_uint = 0xc4c03d12; - } else { - pub const PTP_CLOCK_GETCAPS: ::c_uint = 0x80503d01; - pub const PTP_EXTTS_REQUEST: ::c_uint = 0x40103d02; - pub const PTP_PEROUT_REQUEST: ::c_uint = 0x40383d03; - pub const PTP_ENABLE_PPS: ::c_uint = 0x40043d04; - pub const PTP_SYS_OFFSET: ::c_uint = 0x43403d05; - pub const PTP_PIN_GETFUNC: ::c_uint = 0xc0603d06; - pub const PTP_PIN_SETFUNC: ::c_uint = 0x40603d07; - pub const PTP_SYS_OFFSET_PRECISE: ::c_uint = 0xc0403d08; - pub const PTP_SYS_OFFSET_EXTENDED: ::c_uint = 0xc4c03d09; - - pub const PTP_CLOCK_GETCAPS2: ::c_uint = 0x80503d0a; - pub const PTP_EXTTS_REQUEST2: ::c_uint = 0x40103d0b; - pub const PTP_PEROUT_REQUEST2: ::c_uint = 0x40383d0c; - pub const PTP_ENABLE_PPS2: ::c_uint = 0x40043d0d; - pub const PTP_SYS_OFFSET2: ::c_uint = 0x43403d0e; - pub const PTP_PIN_GETFUNC2: ::c_uint = 0xc0603d0f; - pub const PTP_PIN_SETFUNC2: ::c_uint = 0x40603d10; - pub const PTP_SYS_OFFSET_PRECISE2: ::c_uint = 0xc0403d11; - pub const PTP_SYS_OFFSET_EXTENDED2: ::c_uint = 0xc4c03d12; - } -} +const PTP_CLK_MAGIC: u32 = b'=' as u32; + +pub const PTP_CLOCK_GETCAPS: ::c_uint = ioctl::_IOR::(PTP_CLK_MAGIC, 1); +pub const PTP_EXTTS_REQUEST: ::c_uint = ioctl::_IOW::(PTP_CLK_MAGIC, 2); +pub const PTP_PEROUT_REQUEST: ::c_uint = ioctl::_IOW::(PTP_CLK_MAGIC, 3); +pub const PTP_ENABLE_PPS: ::c_uint = ioctl::_IOW::<::c_int>(PTP_CLK_MAGIC, 4); +pub const PTP_SYS_OFFSET: ::c_uint = ioctl::_IOW::(PTP_CLK_MAGIC, 5); +pub const PTP_PIN_GETFUNC: ::c_uint = ioctl::_IOWR::(PTP_CLK_MAGIC, 6); +pub const PTP_PIN_SETFUNC: ::c_uint = ioctl::_IOW::(PTP_CLK_MAGIC, 7); +pub const PTP_SYS_OFFSET_PRECISE: ::c_uint = + ioctl::_IOWR::(PTP_CLK_MAGIC, 8); +pub const PTP_SYS_OFFSET_EXTENDED: ::c_uint = + ioctl::_IOWR::(PTP_CLK_MAGIC, 9); + +pub const PTP_CLOCK_GETCAPS2: ::c_uint = ioctl::_IOR::(PTP_CLK_MAGIC, 10); +pub const PTP_EXTTS_REQUEST2: ::c_uint = ioctl::_IOW::(PTP_CLK_MAGIC, 11); +pub const PTP_PEROUT_REQUEST2: ::c_uint = ioctl::_IOW::(PTP_CLK_MAGIC, 12); +pub const PTP_ENABLE_PPS2: ::c_uint = ioctl::_IOW::<::c_int>(PTP_CLK_MAGIC, 13); +pub const PTP_SYS_OFFSET2: ::c_uint = ioctl::_IOW::(PTP_CLK_MAGIC, 14); +pub const PTP_PIN_GETFUNC2: ::c_uint = ioctl::_IOWR::(PTP_CLK_MAGIC, 15); +pub const PTP_PIN_SETFUNC2: ::c_uint = ioctl::_IOW::(PTP_CLK_MAGIC, 16); +pub const PTP_SYS_OFFSET_PRECISE2: ::c_uint = + ioctl::_IOWR::(PTP_CLK_MAGIC, 17); +pub const PTP_SYS_OFFSET_EXTENDED2: ::c_uint = + ioctl::_IOWR::(PTP_CLK_MAGIC, 18); +pub const PTP_MASK_CLEAR_ALL: ::c_uint = ioctl::_IO(PTP_CLK_MAGIC, 19); +pub const PTP_MASK_EN_SINGLE: ::c_uint = ioctl::_IOW::<::c_uint>(PTP_CLK_MAGIC, 20); // enum ptp_pin_function pub const PTP_PF_NONE: ::c_uint = 0; @@ -5840,3 +5824,88 @@ cfg_if! { pub use self::non_exhaustive::*; } } + +mod ioctl { + 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; + + // adopted 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. + #[inline(always)] + const fn _IOC(dir: u32, ty: u32, nr: u32, size: usize) -> u32 { + 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. + #[inline(always)] + pub const fn _IO(ty: u32, nr: u32) -> u32 { + _IOC(_IOC_NONE, ty, nr, 0) + } + + /// Build an ioctl number for an read-only ioctl. + #[inline(always)] + pub const fn _IOR(ty: u32, nr: u32) -> u32 { + _IOC(_IOC_READ, ty, nr, core::mem::size_of::()) + } + + /// Build an ioctl number for an write-only ioctl. + #[inline(always)] + pub const fn _IOW(ty: u32, nr: u32) -> u32 { + _IOC(_IOC_WRITE, ty, nr, core::mem::size_of::()) + } + + /// Build an ioctl number for a read-write ioctl. + #[inline(always)] + pub const fn _IOWR(ty: u32, nr: u32) -> u32 { + _IOC(_IOC_READ | _IOC_WRITE, ty, nr, core::mem::size_of::()) + } +}