From 55c8918d28a6d270a42f9d60f3b591a88f09118e Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 7 May 2023 21:44:03 +0100 Subject: [PATCH] x86_64: serial: enable interrupts and announce incoming data --- arch/x86_64/serial.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/arch/x86_64/serial.c b/arch/x86_64/serial.c index 660897d..ac294f6 100644 --- a/arch/x86_64/serial.c +++ b/arch/x86_64/serial.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -33,6 +34,20 @@ void serial_send_byte(int device, char out) } } +char serial_recv_byte(int device) +{ + volatile unsigned int _count = 0; + while (!serial_received(device)) { + _count++; + } + + char c = inportb(device); + + outportb(device + 5, inportb(device + 5) & ~0x1); + + return c; +} + void serial_putchar(int port, char ch) { if (ch == '\n') { @@ -100,12 +115,12 @@ static void init_serial_port(int port, int baud) // Check if serial is faulty (i.e: not same byte as sent) if(inportb(port + 0) != 0xAE) { - panic("serial: port %x init failed", port); return; } // If serial is not faulty set it in normal operation mode // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) + outportb(port + 1, 0x01); outportb(port + 4, 0x0F); printk("serial: port %x initialised", port); } @@ -117,8 +132,28 @@ static struct console serialcon = { .c_lock = SPIN_LOCK_INIT, }; +static int serial_irq1(void) +{ + if (serial_received(COM1)) { + unsigned char c = serial_recv_byte(COM1); + printk("serial: COM1 received %c", c); + } + + if (serial_received(COM3)) { + unsigned char c = serial_recv_byte(COM3); + printk("serial: COM3 received %c", c); + } + + return 0; +} + +static struct irq_hook irq1_hook = { + .irq_callback = serial_irq1, +}; + void serialcon_init(int baud) { + hook_irq(IRQ4, &irq1_hook); init_serial_port(COM1, baud); console_register(&serialcon); }