You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.1 KiB
Lua
48 lines
1.1 KiB
Lua
local bit = require("bit")
|
|
local ffi = require("ffi")
|
|
|
|
local lshift, bor = bit.lshift, bit.bor
|
|
|
|
local module = {}
|
|
|
|
ffi.cdef[[
|
|
int __ioctl_time64(int fd, unsigned long request, ...);
|
|
int ioctl(int fd, unsigned long request, ...);
|
|
]]
|
|
|
|
-- __ioctl_time64 is not always available
|
|
module.ioctl = ffi.C.ioctl
|
|
pcall(function() module.ioctl = ffi.C.__ioctl_time64 end)
|
|
|
|
local typebits, nrbits, sizebits = 8, 8, 14
|
|
local arch = ffi.arch
|
|
if arch == "ppc" or arch:sub(1,4) == "mips" then
|
|
-- TODO: alpha, sparc (when supported by luajit)
|
|
sizebits = 13
|
|
end
|
|
|
|
local nrshift = 0
|
|
local typeshift = nrshift + nrbits
|
|
local sizeshift = typeshift + typebits
|
|
local dirshift = sizeshift + sizebits
|
|
|
|
module.nrshift = nrshift
|
|
module.typeshift = typeshift
|
|
module.sizeshift = sizeshift
|
|
module.dirshift = dirshift
|
|
|
|
local write_bit = 1
|
|
local read_bit = 2
|
|
|
|
function module.ioctl_req(rd, wr, type, nr, size)
|
|
local dir = 0ULL -- do not convert numbers to int32
|
|
if rd then dir = dir + read_bit end
|
|
if wr then dir = dir + write_bit end
|
|
return bor(lshift(dir, dirshift),
|
|
lshift(type, typeshift),
|
|
lshift(nr, nrshift),
|
|
lshift(size, sizeshift))
|
|
end
|
|
|
|
return module
|