#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <machine/openpromio.h>
#include <machine/conf.h>
#include <dev/ofw/openfirm.h>
static int lastnode;
static int optionsnode;
int openpromcheckid(int, int);
int openpromgetstr(int, char *, char **);
void openpromattach(int);
void
openpromattach(int num)
{
}
int
openpromopen(dev_t dev, int flags, int mode, struct proc *p)
{
return (0);
}
int
openpromclose(dev_t dev, int flags, int mode, struct proc *p)
{
return (0);
}
int
openpromcheckid(int sid, int tid)
{
for (; sid != 0; sid = OF_peer(sid))
if (sid == tid || openpromcheckid(OF_child(sid), tid))
return (1);
return (0);
}
int
openpromgetstr(int len, char *user, char **cpp)
{
int error;
char *cp;
if ((u_int)len > (8 * 1024) - 1)
return (ENAMETOOLONG);
*cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
error = copyin(user, cp, len);
cp[len] = '\0';
return (error);
}
int
openpromioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
{
struct opiocdesc *op;
int node, len, ok, error;
char *name, *value, *nextprop;
if (optionsnode == 0)
optionsnode = OF_getnodebyname(0, "options");
if (cmd == OPIOCGETOPTNODE) {
*(int *)data = optionsnode;
return (0);
}
op = (struct opiocdesc *)data;
node = op->op_nodeid;
if (node != 0 && node != lastnode && node != optionsnode) {
ok = openpromcheckid(OF_peer(0), node);
if (!ok)
return (EINVAL);
lastnode = node;
}
name = value = NULL;
error = 0;
switch (cmd) {
case OPIOCGET:
if ((flags & FREAD) == 0)
return (EBADF);
if (node == 0)
return (EINVAL);
error = openpromgetstr(op->op_namelen, op->op_name, &name);
if (error)
break;
len = OF_getproplen(node, name);
if (len > op->op_buflen) {
error = ENOMEM;
break;
}
op->op_buflen = len;
if (len <= 0)
break;
value = malloc(len, M_TEMP, M_WAITOK);
OF_getprop(node, name, value, len);
error = copyout(value, op->op_buf, len);
break;
case OPIOCNEXTPROP:
if ((flags & FREAD) == 0)
return (EBADF);
if (node == 0)
return (EINVAL);
error = openpromgetstr(op->op_namelen, op->op_name, &name);
if (error)
break;
if (op->op_buflen <= 0) {
error = ENAMETOOLONG;
break;
}
value = nextprop = malloc(OFMAXPARAM, M_TEMP,
M_WAITOK | M_CANFAIL);
if (nextprop == NULL) {
error = ENOMEM;
break;
}
error = OF_nextprop(node, name, nextprop);
if (error == -1) {
error = EINVAL;
break;
}
if (error == 0) {
char nul = '\0';
op->op_buflen = 0;
error = copyout(&nul, op->op_buf, sizeof(char));
break;
}
len = strlen(nextprop);
if (len > op->op_buflen)
len = op->op_buflen;
else
op->op_buflen = len;
error = copyout(nextprop, op->op_buf, len);
break;
case OPIOCGETNEXT:
if ((flags & FREAD) == 0)
return (EBADF);
node = OF_peer(node);
*(int *)data = lastnode = node;
break;
case OPIOCGETCHILD:
if ((flags & FREAD) == 0)
return (EBADF);
if (node == 0)
return (EINVAL);
node = OF_child(node);
*(int *)data = lastnode = node;
break;
default:
return (ENOTTY);
}
free(name, M_TEMP, 0);
free(value, M_TEMP, 0);
return (error);
}