//! User-space device access: enumerate the kernel's device table, claim a //! device, and map its MMIO. A driver uses these to find and take ownership of //! its hardware; the claim is the capability the kernel checks before mapping. const danos = @import("danos"); const sc = @import("syscall.zig"); pub const DeviceDesc = danos.DeviceDesc; pub const ResDesc = danos.ResDesc; pub const DeviceClass = danos.DeviceClass; pub const ResourceKind = danos.ResourceKind; inline fn failed(r: usize) bool { return r > ~@as(usize, 0) - 4095; } /// Copy up to `buf.len` device descriptors into `buf`; returns the total count. pub fn enumerate(buf: []DeviceDesc) usize { return sc.syscall2(.dev_enumerate, @intFromPtr(buf.ptr), buf.len); } /// Take exclusive ownership of device `id`. Returns false if taken or invalid. pub fn claim(id: u64) bool { return !failed(sc.syscall1(.dev_claim, id)); } /// Map resource `res_idx` (which must be an MMIO window) of claimed device /// `dev_id` into this address space; returns the register base virtual address. pub fn mmioMap(dev_id: u64, res_idx: u64) ?usize { const r = sc.syscall2(.mmio_map, dev_id, res_idx); return if (failed(r)) null else r; }