#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: openfirm.c,v 1.11 2021/02/07 13:59:36 martin Exp $");
#include <sys/param.h>
#include <dev/ofw/openfirm.h>
int
OF_peer(int phandle)
{
static struct {
const char *name;
int nargs;
int nreturns;
int phandle;
int sibling;
} args = {
"peer",
1,
1,
};
args.phandle = phandle;
if (openfirmware(&args) == -1)
return 0;
return args.sibling;
}
int
OF_child(int phandle)
{
static struct {
const char *name;
int nargs;
int nreturns;
int phandle;
int child;
} args = {
"child",
1,
1,
};
args.phandle = phandle;
if (openfirmware(&args) == -1)
return 0;
return args.child;
}
int
OF_parent(int phandle)
{
static struct {
const char *name;
int nargs;
int nreturns;
int phandle;
int parent;
} args = {
"parent",
1,
1,
};
args.phandle = phandle;
if (openfirmware(&args) == -1)
return 0;
return args.parent;
}
int
OF_instance_to_package(int ihandle)
{
static struct {
const char *name;
int nargs;
int nreturns;
int ihandle;
int phandle;
} args = {
"instance-to-package",
1,
1,
};
args.ihandle = ihandle;
if (openfirmware(&args) == -1)
return -1;
return args.phandle;
}
int
OF_nextprop(int handle, const char *prop, void *nextprop)
{
static struct {
const char *name;
int nargs;
int nreturns;
int phandle;
const char *prop;
void *nextprop;
int flags;
} args = {
"nextprop",
3,
1,
};
args.phandle = handle;
args.prop = prop;
args.nextprop = nextprop;
if (openfirmware(&args) == -1)
return -1;
return args.flags;
}
int
OF_getprop(int handle, const char *prop, void *buf, int buflen)
{
static struct {
const char *name;
int nargs;
int nreturns;
int phandle;
const char *prop;
void *buf;
int buflen;
int size;
} args = {
"getprop",
4,
1,
};
args.phandle = handle;
args.prop = prop;
args.buf = buf;
args.buflen = buflen;
if (openfirmware(&args) == -1)
return -1;
return args.size;
}
int
OF_setprop(int handle, const char *prop, const void *buf, int buflen)
{
static struct {
const char *name;
int nargs;
int nreturns;
int phandle;
const char *prop;
const void *buf;
int buflen;
int size;
} args = {
"setprop",
4,
1,
};
args.phandle = handle;
args.prop = prop;
args.buf = buf;
args.buflen = buflen;
if (openfirmware(&args) == -1)
return -1;
return args.size;
}
int
OF_getproplen(int handle, const char *prop)
{
static struct {
const char *name;
int nargs;
int nreturns;
int phandle;
const char *prop;
int size;
} args = {
"getproplen",
2,
1,
};
args.phandle = handle;
args.prop = prop;
if (openfirmware(&args) == -1)
return -1;
return args.size;
}
int
OF_finddevice(const char *name)
{
static struct {
const char *name;
int nargs;
int nreturns;
const char *device;
int phandle;
} args = {
"finddevice",
1,
1,
};
args.device = name;
if (openfirmware(&args) == -1)
return -1;
return args.phandle;
}
int
OF_instance_to_path(int ihandle, char *buf, int buflen)
{
static struct {
const char *name;
int nargs;
int nreturns;
int ihandle;
char *buf;
int buflen;
int length;
} args = {
"instance-to-path",
3,
1,
};
args.ihandle = ihandle;
args.buf = buf;
args.buflen = buflen;
if (openfirmware(&args) < 0)
return -1;
return args.length;
}
int
OF_package_to_path(int phandle, char *buf, int buflen)
{
static struct {
const char *name;
int nargs;
int nreturns;
int phandle;
char *buf;
int buflen;
int length;
} args = {
"package-to-path",
3,
1,
};
args.phandle = phandle;
args.buf = buf;
args.buflen = buflen;
if (openfirmware(&args) < 0)
return -1;
return args.length;
}
int
#ifdef __STDC__
OF_call_method(const char *method, int ihandle, int nargs, int nreturns, ...)
#else
OF_call_method(method, ihandle, nargs, nreturns, va_alist)
const char *method;
int ihandle;
int nargs;
int nreturns;
va_dcl
#endif
{
va_list ap;
static struct {
const char *name;
int nargs;
int nreturns;
const char *method;
int ihandle;
int args_n_results[12];
} args = {
"call-method",
2,
1,
};
int *ip, n;
if (nargs > 6)
return -1;
args.nargs = nargs + 2;
args.nreturns = nreturns + 1;
args.method = method;
args.ihandle = ihandle;
va_start(ap, nreturns);
for (ip = args.args_n_results + (n = nargs); --n >= 0;)
*--ip = va_arg(ap, int);
if (openfirmware(&args) == -1) {
va_end(ap);
return -1;
}
if (args.args_n_results[nargs]) {
va_end(ap);
return args.args_n_results[nargs];
}
for (ip = args.args_n_results + nargs + (n = args.nreturns); --n > 0;)
*va_arg(ap, int *) = *--ip;
va_end(ap);
return 0;
}
int
#ifdef __STDC__
OF_call_method_1(const char *method, int ihandle, int nargs, ...)
#else
OF_call_method_1(method, ihandle, nargs, va_alist)
const char *method;
int ihandle;
int nargs;
va_dcl
#endif
{
va_list ap;
static struct {
const char *name;
int nargs;
int nreturns;
const char *method;
int ihandle;
int args_n_results[8];
} args = {
"call-method",
2,
2,
};
int *ip, n;
if (nargs > 6)
return -1;
args.nargs = nargs + 2;
args.method = method;
args.ihandle = ihandle;
va_start(ap, nargs);
for (ip = args.args_n_results + (n = nargs); --n >= 0;)
*--ip = va_arg(ap, int);
va_end(ap);
if (openfirmware(&args) == -1)
return -1;
if (args.args_n_results[nargs])
return -1;
return args.args_n_results[nargs + 1];
}
int
OF_open(const char *dname)
{
static struct {
const char *name;
int nargs;
int nreturns;
const char *dname;
int handle;
} args = {
"open",
1,
1,
};
args.dname = dname;
if (openfirmware(&args) == -1)
return -1;
return args.handle;
}
void
OF_close(int handle)
{
static struct {
const char *name;
int nargs;
int nreturns;
int handle;
} args = {
"close",
1,
0,
};
args.handle = handle;
openfirmware(&args);
}
int
OF_read(int handle, void *addr, int len)
{
static struct {
const char *name;
int nargs;
int nreturns;
int ihandle;
void *addr;
int len;
int actual;
} args = {
"read",
3,
1,
};
args.ihandle = handle;
args.addr = addr;
args.len = len;
if (openfirmware(&args) == -1)
return -1;
return args.actual;
}
int
OF_write(int handle, const void *addr, int len)
{
static struct {
const char *name;
int nargs;
int nreturns;
int ihandle;
const void *addr;
int len;
int actual;
} args = {
"write",
3,
1,
};
args.ihandle = handle;
args.addr = addr;
args.len = len;
if (openfirmware(&args) == -1)
return -1;
return args.actual;
}
int
OF_seek(int handle, u_quad_t pos)
{
static struct {
const char *name;
int nargs;
int nreturns;
int handle;
int poshi;
int poslo;
int status;
} args = {
"seek",
3,
1,
};
args.handle = handle;
args.poshi = (int)(pos >> 32);
args.poslo = (int)pos;
if (openfirmware(&args) == -1)
return -1;
return args.status;
}
void *
OF_claim(void *virt, u_int size, u_int align)
{
static struct {
const char *name;
int nargs;
int nreturns;
void *virt;
u_int size;
u_int align;
void *baseaddr;
} args = {
"claim",
3,
1,
};
args.virt = virt;
args.size = size;
args.align = align;
if (openfirmware(&args) == -1)
return (void *)-1;
return args.baseaddr;
}
void
OF_release(void *virt, u_int size)
{
static struct {
const char *name;
int nargs;
int nreturns;
void *virt;
u_int size;
} args = {
"release",
2,
0,
};
args.virt = virt;
args.size = size;
openfirmware(&args);
}
int
OF_milliseconds(void)
{
static struct {
const char *name;
int nargs;
int nreturns;
int ms;
} args = {
"milliseconds",
0,
1,
};
openfirmware(&args);
return args.ms;
}
void
OF_boot(const char *btspec)
{
static struct {
const char *name;
int nargs;
int nreturns;
const char *bootspec;
} args = {
"boot",
1,
0,
};
args.bootspec = btspec;
openfirmware(&args);
while (1);
}
void
OF_enter(void)
{
static struct {
const char *name;
int nargs;
int nreturns;
} args = {
"enter",
0,
0,
};
openfirmware(&args);
}
void
OF_exit(void)
{
static struct {
const char *name;
int nargs;
int nreturns;
} args = {
"exit",
0,
0,
};
openfirmware(&args);
while (1);
}
typedef void (*of_callback_t)(void *);
of_callback_t
OF_set_callback(of_callback_t newfunc)
{
static struct {
const char *name;
int nargs;
int nreturns;
of_callback_t newfunc;
of_callback_t oldfunc;
} args = {
"set-callback",
1,
1,
};
args.newfunc = newfunc;
if (openfirmware(&args) == -1)
return 0;
return args.oldfunc;
}