diff --git a/pkg/ioctl/README.md b/pkg/ioctl/README.md new file mode 100644 index 00000000..41f82dff --- /dev/null +++ b/pkg/ioctl/README.md @@ -0,0 +1,3 @@ +# IOCTL + +This is just an example how Linux IOCTL constants works. diff --git a/pkg/ioctl/ioctl.go b/pkg/ioctl/ioctl.go new file mode 100644 index 00000000..0f21e17f --- /dev/null +++ b/pkg/ioctl/ioctl.go @@ -0,0 +1,28 @@ +package ioctl + +import ( + "bytes" +) + +func Str(b []byte) string { + if i := bytes.IndexByte(b, 0); i >= 0 { + return string(b[:i]) + } + return string(b) +} + +func io(mode byte, type_ byte, number byte, size uint16) uintptr { + return uintptr(mode)<<30 | uintptr(size)<<16 | uintptr(type_)<<8 | uintptr(number) +} + +func IOR(type_ byte, number byte, size uint16) uintptr { + return io(read, type_, number, size) +} + +func IOW(type_ byte, number byte, size uint16) uintptr { + return io(write, type_, number, size) +} + +func IORW(type_ byte, number byte, size uint16) uintptr { + return io(read|write, type_, number, size) +} diff --git a/pkg/ioctl/ioctl_be.go b/pkg/ioctl/ioctl_be.go new file mode 100644 index 00000000..60de9c42 --- /dev/null +++ b/pkg/ioctl/ioctl_be.go @@ -0,0 +1,8 @@ +//go:build arm || arm64 || 386 || amd64 + +package ioctl + +const ( + write = 1 + read = 2 +) diff --git a/pkg/ioctl/ioctl_le.go b/pkg/ioctl/ioctl_le.go new file mode 100644 index 00000000..3bdb1f62 --- /dev/null +++ b/pkg/ioctl/ioctl_le.go @@ -0,0 +1,8 @@ +//go:build mipsle + +package ioctl + +const ( + read = 1 + write = 2 +) diff --git a/pkg/ioctl/ioctl_linux.go b/pkg/ioctl/ioctl_linux.go new file mode 100644 index 00000000..ed38f6ac --- /dev/null +++ b/pkg/ioctl/ioctl_linux.go @@ -0,0 +1,14 @@ +package ioctl + +import ( + "syscall" + "unsafe" +) + +func Ioctl(fd int, req uint, arg unsafe.Pointer) error { + _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(req), uintptr(arg)) + if err != 0 { + return err + } + return nil +} diff --git a/pkg/ioctl/ioctl_test.go b/pkg/ioctl/ioctl_test.go new file mode 100644 index 00000000..52657e64 --- /dev/null +++ b/pkg/ioctl/ioctl_test.go @@ -0,0 +1,16 @@ +package ioctl + +import ( + "runtime" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIOR(t *testing.T) { + // #define SNDRV_PCM_IOCTL_INFO _IOR('A', 0x01, struct snd_pcm_info) + if runtime.GOARCH == "arm64" { + c := IOR('A', 0x01, 288) + require.Equal(t, uintptr(0x81204101), c) + } +}