85 lines
1.7 KiB
C
85 lines
1.7 KiB
C
#include <mango/console.h>
|
|
#include <mango/queue.h>
|
|
#include <mango/locks.h>
|
|
#include <mango/libc/string.h>
|
|
|
|
static struct queue consoles;
|
|
static spin_lock_t consoles_lock = SPIN_LOCK_INIT;
|
|
|
|
static void unregister_boot_consoles(void)
|
|
{
|
|
struct queue_entry *cur = queue_first(&consoles);
|
|
while (cur) {
|
|
struct queue_entry *next = cur->qe_next;
|
|
struct console *con = QUEUE_CONTAINER(struct console, c_list, cur);
|
|
if (con->c_flags & CON_BOOT) {
|
|
queue_delete(&consoles, cur);
|
|
}
|
|
|
|
cur = next;
|
|
}
|
|
}
|
|
|
|
kern_status_t console_register(struct console *con)
|
|
{
|
|
unsigned long flags;
|
|
spin_lock_irqsave(&consoles_lock, &flags);
|
|
|
|
queue_foreach (struct console, cur, &consoles, c_list) {
|
|
if (!strcmp(cur->c_name, con->c_name)) {
|
|
spin_unlock_irqrestore(&consoles_lock, flags);
|
|
return KERN_NAME_EXISTS;
|
|
}
|
|
}
|
|
|
|
queue_push_back(&consoles, &con->c_list);
|
|
|
|
if (!(con->c_flags & CON_BOOT)) {
|
|
unregister_boot_consoles();
|
|
}
|
|
|
|
spin_unlock_irqrestore(&consoles_lock, flags);
|
|
|
|
return KERN_OK;
|
|
}
|
|
|
|
kern_status_t console_unregister(struct console *con)
|
|
{
|
|
unsigned long flags;
|
|
spin_lock_irqsave(&consoles_lock, &flags);
|
|
|
|
queue_delete(&consoles, &con->c_list);
|
|
|
|
spin_unlock_irqrestore(&consoles_lock, flags);
|
|
return KERN_OK;
|
|
}
|
|
|
|
struct queue *get_consoles(unsigned long *flags)
|
|
{
|
|
spin_lock_irqsave(&consoles_lock, flags);
|
|
return &consoles;
|
|
}
|
|
|
|
void put_consoles(struct queue *consoles, unsigned long flags)
|
|
{
|
|
(void)consoles;
|
|
spin_unlock_irqrestore(&consoles_lock, flags);
|
|
}
|
|
|
|
void console_write(struct console *con, const char *s, unsigned int len)
|
|
{
|
|
if (con->c_write) {
|
|
con->c_write(con, s, len);
|
|
}
|
|
}
|
|
|
|
int console_read(struct console *con, char *s, unsigned int len)
|
|
{
|
|
int ret = -1;
|
|
if (con->c_read) {
|
|
ret = con->c_read(con, s, len);
|
|
}
|
|
|
|
return ret;
|
|
}
|