Dynamic IRQ handling
This commit is contained in:
117
kernel/irq/irqhandler.c
Normal file
117
kernel/irq/irqhandler.c
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
Copyright 2025 Kamil Kowalczyk
|
||||
|
||||
Redistribution and use in source and binary forms, with or
|
||||
without modification, are permitted provided that the following
|
||||
conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||||
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <libk/types.h>
|
||||
#include <libk/list.h>
|
||||
#include <mm/liballoc.h>
|
||||
#include <irq/irqhandler.h>
|
||||
#include <sync/spinlock.h>
|
||||
#include <config.h>
|
||||
|
||||
#if defined(__i386__)
|
||||
#include <sys/pic.h>
|
||||
#endif
|
||||
|
||||
static struct irqhandlers irqhandlers = { .handlers = NULL };
|
||||
|
||||
void irqhandlers_init(void) {
|
||||
sl_init(&irqhandlers.sl, "irqhandlers");
|
||||
}
|
||||
|
||||
void irqhandler_register(int irq, irqhandler_func_t func) {
|
||||
struct irqhandler *irqhandler = malloc(sizeof(*irqhandler));
|
||||
if (irqhandler == NULL)
|
||||
goto done;
|
||||
|
||||
irqhandler->irq = irq;
|
||||
irqhandler->func = func;
|
||||
|
||||
sl_lock(&irqhandlers.sl);
|
||||
|
||||
linklist_append(struct irqhandler *, irqhandlers.handlers, irqhandler);
|
||||
|
||||
sl_unlock(&irqhandlers.sl);
|
||||
|
||||
done:
|
||||
return;
|
||||
}
|
||||
|
||||
void irqhandler_unregister(int irq) {
|
||||
struct irqhandler *irqhandler = NULL;
|
||||
|
||||
sl_lock(&irqhandlers.sl);
|
||||
|
||||
linklist_find(struct irqhandler *, irqhandlers.handlers, irqhandler, irq, irq);
|
||||
|
||||
if (irqhandler == NULL)
|
||||
goto done;
|
||||
|
||||
linklist_remove(struct irqhandler *, irqhandlers.handlers, irqhandler);
|
||||
|
||||
sl_unlock(&irqhandlers.sl);
|
||||
|
||||
free(irqhandler);
|
||||
return;
|
||||
|
||||
done:
|
||||
sl_unlock(&irqhandlers.sl);
|
||||
return;
|
||||
}
|
||||
|
||||
irqhandler_func_t irqhandler_find(int irq) {
|
||||
struct irqhandler *irqhandler = NULL;
|
||||
irqhandler_func_t func = NULL;
|
||||
|
||||
sl_lock(&irqhandlers.sl);
|
||||
|
||||
linklist_find(struct irqhandler *, irqhandlers.handlers, irqhandler, irq, irq);
|
||||
if (irqhandler == NULL) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
func = irqhandler->func;
|
||||
|
||||
done:
|
||||
sl_unlock(&irqhandlers.sl);
|
||||
return func;
|
||||
}
|
||||
|
||||
void irqhandler_enable(int irq) {
|
||||
#if defined(__i386__)
|
||||
pic_unmask_irq(irq);
|
||||
#endif
|
||||
}
|
||||
|
||||
void irqhandler_disable(int irq) {
|
||||
#if defined(__i386__)
|
||||
pic_mask_irq(irq);
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user