#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: config_hook.c,v 1.12 2020/11/21 21:08:32 thorpej Exp $");
#include <sys/param.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <machine/config_hook.h>
struct hook_rec {
TAILQ_ENTRY(hook_rec) hr_link;
void *hr_ctx;
int hr_type;
long hr_id;
enum config_hook_mode hr_mode;
int (*hr_func)(void *, int, long, void *);
};
TAILQ_HEAD(hook_list, hook_rec);
struct hook_list hook_lists[CONFIG_HOOK_NTYPES];
struct hook_list call_list;
void
config_hook_init(void)
{
int i;
for (i = 0; i < CONFIG_HOOK_NTYPES; i++) {
TAILQ_INIT(&hook_lists[i]);
}
TAILQ_INIT(&call_list);
}
config_hook_tag
config_hook(int type, long id, enum config_hook_mode mode,
int (*func)(void *, int, long, void *), void *ctx)
{
struct hook_rec *hr, *cr, *prev_hr;
int s;
if (type < 0 || CONFIG_HOOK_NTYPES <= type) {
panic("config_hook: invalid hook type");
}
prev_hr = NULL;
TAILQ_FOREACH(hr, &hook_lists[type], hr_link) {
if (hr->hr_id == id) {
if (hr->hr_mode != mode) {
panic("config_hook: incompatible mode on "
"type=%d/id=%ld != %d",
type, id, hr->hr_mode);
}
prev_hr = hr;
}
}
switch (mode) {
case CONFIG_HOOK_SHARE:
break;
case CONFIG_HOOK_REPLACE:
if (prev_hr != NULL) {
printf("config_hook: type=%d/id=%ld is replaced",
type, id);
s = splhigh();
TAILQ_REMOVE(&hook_lists[type], prev_hr, hr_link);
TAILQ_NEXT(prev_hr, hr_link) = NULL;
splx(s);
}
break;
case CONFIG_HOOK_EXCLUSIVE:
if (prev_hr != NULL) {
panic("config_hook: type=%d/id=%ld is already "
"hooked(%p)", type, id, prev_hr);
}
break;
default:
break;
}
hr = kmem_alloc(sizeof(*hr), KM_SLEEP);
hr->hr_ctx = ctx;
hr->hr_type = type;
hr->hr_id = id;
hr->hr_func = func;
hr->hr_mode = mode;
s = splhigh();
TAILQ_INSERT_HEAD(&hook_lists[type], hr, hr_link);
TAILQ_FOREACH(cr, &call_list, hr_link) {
if (cr->hr_type == type && cr->hr_id == id) {
if (cr->hr_func != NULL &&
cr->hr_mode != mode) {
panic("config_hook: incompatible mode on "
"type=%d/id=%ld != %d",
type, id, cr->hr_mode);
}
cr->hr_ctx = ctx;
cr->hr_func = func;
cr->hr_mode = mode;
}
}
splx(s);
return (hr);
}
void
config_unhook(config_hook_tag hrx)
{
int s;
struct hook_rec *hr = (struct hook_rec*)hrx, *cr;
if (TAILQ_NEXT(hr, hr_link) != NULL) {
s = splhigh();
TAILQ_REMOVE(&hook_lists[hr->hr_type], hr, hr_link);
TAILQ_NEXT(hr, hr_link) = NULL;
TAILQ_FOREACH(cr, &call_list, hr_link) {
if (cr->hr_type == hr->hr_type &&
cr->hr_id == hr->hr_id)
cr->hr_func = NULL;
}
splx(s);
}
kmem_free(hr, sizeof(*hr));
}
int
__config_hook_call(int type, long id, void *msg, int reverse)
{
int res;
struct hook_rec *hr;
if (type < 0 || CONFIG_HOOK_NTYPES <= type) {
panic("config_hook: invalid hook type");
}
res = -1;
if (reverse) {
TAILQ_FOREACH_REVERSE(hr, &hook_lists[type], hook_list,
hr_link) {
if (hr->hr_id == id)
res = (*hr->hr_func)(hr->hr_ctx, type, id,msg);
}
} else {
TAILQ_FOREACH(hr, &hook_lists[type], hr_link) {
if (hr->hr_id == id)
res = (*hr->hr_func)(hr->hr_ctx, type, id,msg);
}
}
return (res);
}
config_hook_tag
config_connect(int type, long id)
{
int s;
struct hook_rec *cr, *hr;
if (type < 0 || CONFIG_HOOK_NTYPES <= type) {
panic("config_hook: invalid hook type");
}
cr = kmem_alloc(sizeof(*hr), KM_SLEEP);
cr->hr_func = NULL;
cr->hr_type = type;
cr->hr_id = id;
s = splhigh();
TAILQ_INSERT_HEAD(&call_list, cr, hr_link);
TAILQ_FOREACH(hr, &hook_lists[type], hr_link) {
if (hr->hr_id == id) {
if (hr->hr_mode == CONFIG_HOOK_SHARE)
panic("config_connect: can't connect with "
"shared hook, type=%d id=%ld", type, id);
cr->hr_ctx = hr->hr_ctx;
cr->hr_func = hr->hr_func;
cr->hr_mode = hr->hr_mode;
}
}
splx(s);
return (cr);
}
void
config_disconnect(config_call_tag crx)
{
int s;
struct hook_rec *cr = (struct hook_rec*)crx;
s = splhigh();
TAILQ_REMOVE(&call_list, cr, hr_link);
splx(s);
kmem_free(cr, sizeof(*cr));
}
int
config_connected_call(config_call_tag crx, void *msg)
{
int res;
struct hook_rec *cr = (struct hook_rec*)crx;
if (cr->hr_func != NULL)
res = (*cr->hr_func)(cr->hr_ctx, cr->hr_type, cr->hr_id, msg);
else
res = -1;
return (res);
}