Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unix: add PTP IOCTLs on Linux #225

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions unix/ioctl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,61 @@ func IoctlSetHwTstamp(fd int, ifname string, cfg *HwTstampConfig) error {
return ioctlIfreqData(fd, SIOCSHWTSTAMP, &ifrd)
}

// FdToClockID derives the clock ID from the file descriptor number
// - see clock_gettime(3), FD_TO_CLOCKID macros. The resulting ID is
// suitable for system calls like ClockGettime.
func FdToClockID(fd int) int32 { return int32((int(^fd) << 3) | 3) }

// IoctlPtpClockGetcaps returns the description of a given PTP device.
func IoctlPtpClockGetcaps(fd int) (*PtpClockCaps, error) {
var value PtpClockCaps
err := ioctlPtr(fd, PTP_CLOCK_GETCAPS2, unsafe.Pointer(&value))
return &value, err
}

// IoctlPtpSysOffsetPrecise returns a description of the clock
// offset compared to the system clock.
func IoctlPtpSysOffsetPrecise(fd int) (*PtpSysOffsetPrecise, error) {
var value PtpSysOffsetPrecise
err := ioctlPtr(fd, PTP_SYS_OFFSET_PRECISE2, unsafe.Pointer(&value))
return &value, err
}

// IoctlPtpSysOffsetExtended returns an extended description of the
// clock offset compared to the system clock. The samples parameter
// specifies the desired number of measurements.
func IoctlPtpSysOffsetExtended(fd int, samples uint) (*PtpSysOffsetExtended, error) {
value := PtpSysOffsetExtended{Samples: uint32(samples)}
err := ioctlPtr(fd, PTP_SYS_OFFSET_EXTENDED2, unsafe.Pointer(&value))
return &value, err
}

// IoctlPtpPinGetfunc returns the configuration of the specified
// I/O pin on given PTP device.
func IoctlPtpPinGetfunc(fd int, index uint) (*PtpPinDesc, error) {
value := PtpPinDesc{Index: uint32(index)}
err := ioctlPtr(fd, PTP_PIN_GETFUNC2, unsafe.Pointer(&value))
return &value, err
}

// IoctlPtpPinSetfunc updates configuration of the specified PTP
// I/O pin.
func IoctlPtpPinSetfunc(fd int, pd *PtpPinDesc) error {
return ioctlPtr(fd, PTP_PIN_SETFUNC2, unsafe.Pointer(pd))
}

// IoctlPtpPeroutRequest configures the periodic output mode of the
// PTP I/O pins.
func IoctlPtpPeroutRequest(fd int, r *PtpPeroutRequest) error {
return ioctlPtr(fd, PTP_PEROUT_REQUEST2, unsafe.Pointer(r))
}

// IoctlPtpExttsRequest configures the external timestamping mode
// of the PTP I/O pins.
func IoctlPtpExttsRequest(fd int, r *PtpExttsRequest) error {
return ioctlPtr(fd, PTP_EXTTS_REQUEST2, unsafe.Pointer(r))
}

// IoctlGetWatchdogInfo fetches information about a watchdog device from the
// Linux watchdog API. For more information, see:
// https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html.
Expand Down
22 changes: 22 additions & 0 deletions unix/linux/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ struct termios2 {
#include <linux/openat2.h>
#include <linux/perf_event.h>
#include <linux/pps.h>
#include <linux/ptp_clock.h>
#include <linux/random.h>
#include <linux/rtc.h>
#include <linux/rtnetlink.h>
Expand Down Expand Up @@ -525,6 +526,15 @@ struct cachestat {
__u64 nr_evicted;
__u64 nr_recently_evicted;
};

// the one defined in linux/ptp_clock.h has unions
struct my_ptp_perout_request {
struct ptp_clock_time startOrPhase; // start or phase
struct ptp_clock_time period;
unsigned int index;
unsigned int flags;
struct ptp_clock_time on;
};
*/
import "C"

Expand Down Expand Up @@ -4126,6 +4136,18 @@ const (
HWTSTAMP_TX_ONESTEP_SYNC = C.HWTSTAMP_TX_ONESTEP_SYNC
)

type (
PtpClockCaps C.struct_ptp_clock_caps
PtpClockTime C.struct_ptp_clock_time
PtpExttsEvent C.struct_ptp_extts_event
PtpExttsRequest C.struct_ptp_extts_request
PtpPeroutRequest C.struct_my_ptp_perout_request
PtpPinDesc C.struct_ptp_pin_desc
PtpSysOffset C.struct_ptp_sys_offset
PtpSysOffsetExtended C.struct_ptp_sys_offset_extended
PtpSysOffsetPrecise C.struct_ptp_sys_offset_precise
)

type (
HIDRawReportDescriptor C.struct_hidraw_report_descriptor
HIDRawDevInfo C.struct_hidraw_devinfo
Expand Down
2 changes: 2 additions & 0 deletions unix/mkerrors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ struct ltchars {
#include <linux/nsfs.h>
#include <linux/perf_event.h>
#include <linux/pps.h>
#include <linux/ptp_clock.h>
#include <linux/ptrace.h>
#include <linux/random.h>
#include <linux/reboot.h>
Expand Down Expand Up @@ -537,6 +538,7 @@ ccflags="$@"
$2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|TCP|MCAST|EVFILT|NOTE|SHUT|PROT|MAP|MREMAP|MFD|T?PACKET|MSG|SCM|MCL|DT|MADV|PR|LOCAL|TCPOPT|UDP)_/ ||
$2 ~ /^NFC_(GENL|PROTO|COMM|RF|SE|DIRECTION|LLCP|SOCKPROTO)_/ ||
$2 ~ /^NFC_.*_(MAX)?SIZE$/ ||
$2 ~ /^PTP_/ ||
$2 ~ /^RAW_PAYLOAD_/ ||
$2 ~ /^[US]F_/ ||
$2 ~ /^TP_STATUS_/ ||
Expand Down
9 changes: 9 additions & 0 deletions unix/mkpost.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ func main() {
b = bytes.Replace(b, s, newNames, 1)
}

// Convert []int8 to []byte in PtpPinDesc
ptpBytesRegex := regexp.MustCompile(`(Name)(\s+)\[(\d+)\]u?int8`)
ptpIoctlType := regexp.MustCompile(`PtpPinDesc\s+struct {[^}]*}`)
ptpStructs := ptpIoctlType.FindAll(b, -1)
for _, s := range ptpStructs {
newNames := ptpBytesRegex.ReplaceAll(s, []byte("$1$2[$3]byte"))
b = bytes.Replace(b, s, newNames, 1)
}

// Convert []int8 to []byte in ctl_info ioctl interface
convertCtlInfoName := regexp.MustCompile(`(Name)(\s+)\[(\d+)\]int8`)
ctlInfoType := regexp.MustCompile(`type CtlInfo struct {[^}]*}`)
Expand Down
22 changes: 22 additions & 0 deletions unix/zerrors_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions unix/zerrors_linux_386.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions unix/zerrors_linux_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions unix/zerrors_linux_arm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions unix/zerrors_linux_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions unix/zerrors_linux_loong64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions unix/zerrors_linux_mips.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions unix/zerrors_linux_mips64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions unix/zerrors_linux_mips64le.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions unix/zerrors_linux_mipsle.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions unix/zerrors_linux_ppc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions unix/zerrors_linux_ppc64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading