#include <kern/spl.h>
#include <mach/std_types.h>
#include <types.h>
#include <kern/thread.h>
#include <console/serial_protos.h>
#include <libkern/section_keywords.h>
extern void cons_cinput(char ch);
SECURITY_READ_ONLY_LATE(unsigned int) serialmode;
void
serial_keyboard_init(void)
{
kern_return_t result;
thread_t thread;
if(!(serialmode & SERIALMODE_INPUT))
return;
kprintf("Serial keyboard started\n");
result = kernel_thread_start_priority((thread_continue_t)serial_keyboard_start, NULL, MAXPRI_KERNEL, &thread);
if (result != KERN_SUCCESS)
panic("serial_keyboard_init");
thread_deallocate(thread);
}
void
serial_keyboard_start(void)
{
serial_keyboard_poll();
panic("serial_keyboard_start: we can't get back here\n");
}
void
serial_keyboard_poll(void)
{
int chr;
uint64_t next;
while(1) {
chr = _serial_getc(0, 1, 0, 1);
if(chr < 0)
break;
cons_cinput((char)chr);
}
clock_interval_to_deadline(16, 1000000, &next);
assert_wait_deadline((event_t)serial_keyboard_poll, THREAD_UNINT, next);
thread_block((thread_continue_t)serial_keyboard_poll);
panic("serial_keyboard_poll: Shouldn't never ever get here...\n");
}
boolean_t
console_is_serial(void)
{
return (cons_ops_index == SERIAL_CONS_OPS);
}
int
switch_to_video_console(void)
{
int old_cons_ops = cons_ops_index;
cons_ops_index = VC_CONS_OPS;
return old_cons_ops;
}
int
switch_to_serial_console(void)
{
int old_cons_ops = cons_ops_index;
cons_ops_index = SERIAL_CONS_OPS;
return old_cons_ops;
}
void
switch_to_old_console(int old_console)
{
static boolean_t squawked;
uint32_t ops = old_console;
if ((ops >= nconsops) && !squawked) {
squawked = TRUE;
printf("switch_to_old_console: unknown ops %d\n", ops);
} else
cons_ops_index = ops;
}
void
console_printbuf_state_init(struct console_printbuf_state * data, int write_on_newline, int can_block)
{
if (data == NULL)
return;
bzero(data, sizeof(struct console_printbuf_state));
if (write_on_newline)
data->flags |= CONS_PB_WRITE_NEWLINE;
if (can_block)
data->flags |= CONS_PB_CANBLOCK;
}
void
console_printbuf_putc(int ch, void * arg)
{
struct console_printbuf_state * info = (struct console_printbuf_state *)arg;
info->total += 1;
if (info->pos < (SERIAL_CONS_BUF_SIZE - 1)) {
info->str[info->pos] = ch;
info->pos += 1;
} else {
if (serialmode & SERIALMODE_SYNCDRAIN) {
info->str[info->pos] = '\0';
console_write(info->str, info->pos);
info->pos = 0;
info->str[info->pos] = ch;
info->pos += 1;
}
}
info->str[info->pos] = '\0';
if (ch == '\n' && info->flags & CONS_PB_WRITE_NEWLINE) {
console_write(info->str, info->pos);
info->pos = 0;
info->str[info->pos] = '\0';
}
}
void
console_printbuf_clear(struct console_printbuf_state * info) {
if (info->pos != 0) {
console_write(info->str, info->pos);
}
info->pos = 0;
info->str[info->pos] = '\0';
info->total = 0;
}