Skip to content

Commit d2b5105

Browse files
AransentinVexuzigazeljko
authored
Add Linux ioctl creation utilities (#9748)
* Add Linux ioctl creation utilities * Apply suggestions from code review Co-authored-by: Veikka Tuominen <[email protected]> * Update lib/std/os/linux.zig Co-authored-by: zigazeljko <[email protected]> Co-authored-by: Veikka Tuominen <[email protected]> Co-authored-by: zigazeljko <[email protected]>
1 parent f0b1eec commit d2b5105

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

lib/std/os/linux.zig

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub const user_desc = arch_bits.user_desc;
8989
pub const tls = @import("linux/tls.zig");
9090
pub const pie = @import("linux/start_pie.zig");
9191
pub const BPF = @import("linux/bpf.zig");
92+
pub const IOCTL = @import("linux/ioctl.zig");
9293

9394
pub const MAP = struct {
9495
pub usingnamespace arch_bits.MAP;
@@ -2585,18 +2586,18 @@ pub const T = struct {
25852586
pub const IOCGSID = 0x5429;
25862587
pub const IOCGRS485 = 0x542E;
25872588
pub const IOCSRS485 = 0x542F;
2588-
pub const IOCGPTN = 0x80045430;
2589-
pub const IOCSPTLCK = 0x40045431;
2590-
pub const IOCGDEV = 0x80045432;
2589+
pub const IOCGPTN = IOCTL.IOR('T', 0x30, c_uint);
2590+
pub const IOCSPTLCK = IOCTL.IOW('T', 0x31, c_int);
2591+
pub const IOCGDEV = IOCTL.IOR('T', 0x32, c_uint);
25912592
pub const CGETX = 0x5432;
25922593
pub const CSETX = 0x5433;
25932594
pub const CSETXF = 0x5434;
25942595
pub const CSETXW = 0x5435;
2595-
pub const IOCSIG = 0x40045436;
2596+
pub const IOCSIG = IOCTL.IOW('T', 0x36, c_int);
25962597
pub const IOCVHANGUP = 0x5437;
2597-
pub const IOCGPKT = 0x80045438;
2598-
pub const IOCGPTLCK = 0x80045439;
2599-
pub const IOCGEXCL = 0x80045440;
2598+
pub const IOCGPKT = IOCTL.IOR('T', 0x38, c_int);
2599+
pub const IOCGPTLCK = IOCTL.IOR('T', 0x39, c_int);
2600+
pub const IOCGEXCL = IOCTL.IOR('T', 0x40, c_int);
26002601
};
26012602

26022603
pub const EPOLL = struct {

lib/std/os/linux/ioctl.zig

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const std = @import("../../std.zig");
2+
3+
const bits = switch (@import("builtin").cpu.arch) {
4+
.mips,
5+
.mipsel,
6+
.mips64,
7+
.mips64el,
8+
.powerpc,
9+
.powerpcle,
10+
.powerpc64,
11+
.powerpc64le,
12+
.sparc,
13+
.sparcv9,
14+
.sparcel,
15+
=> .{ .size = 13, .dir = 3, .none = 1, .read = 2, .write = 4 },
16+
else => .{ .size = 14, .dir = 2, .none = 0, .read = 2, .write = 1 },
17+
};
18+
19+
const Direction = std.meta.Int(.unsigned, bits.dir);
20+
21+
pub const Request = packed struct {
22+
nr: u8,
23+
io_type: u8,
24+
size: std.meta.Int(.unsigned, bits.size),
25+
dir: Direction,
26+
};
27+
28+
fn io_impl(dir: Direction, io_type: u8, nr: u8, comptime T: type) u32 {
29+
const request = Request{
30+
.dir = dir,
31+
.size = @sizeOf(T),
32+
.io_type = io_type,
33+
.nr = nr,
34+
};
35+
return @bitCast(u32, request);
36+
}
37+
38+
pub fn IO(io_type: u8, nr: u8) u32 {
39+
return io_impl(bits.none, io_type, nr, void);
40+
}
41+
42+
pub fn IOR(io_type: u8, nr: u8, comptime T: type) u32 {
43+
return io_impl(bits.read, io_type, nr, T);
44+
}
45+
46+
pub fn IOW(io_type: u8, nr: u8, comptime T: type) u32 {
47+
return io_impl(bits.write, io_type, nr, T);
48+
}
49+
50+
pub fn IOWR(io_type: u8, nr: u8, comptime T: type) u32 {
51+
return io_impl(bits.read | bits.write, io_type, nr, T);
52+
}
53+
54+
comptime {
55+
std.debug.assert(@bitSizeOf(Request) == 32);
56+
}

0 commit comments

Comments
 (0)