2021-05-31 04:47:02 +03:00
|
|
|
/**
|
|
|
|
* @file kernel/arch/x86_64/serial.c
|
|
|
|
* @brief PC serial port driver.
|
2018-03-16 15:56:19 +03:00
|
|
|
*
|
2021-05-31 04:47:02 +03:00
|
|
|
* Attaches serial ports to TTY interfaces. Serial input processing
|
|
|
|
* happens in a kernel tasklet so that blocking is handled smoothly.
|
2018-03-16 15:56:19 +03:00
|
|
|
*
|
2021-05-31 04:47:02 +03:00
|
|
|
* @copyright
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2014-2021 K. Lange
|
2018-03-16 15:56:19 +03:00
|
|
|
*/
|
2021-05-31 04:47:02 +03:00
|
|
|
#include <kernel/string.h>
|
|
|
|
#include <kernel/types.h>
|
|
|
|
#include <kernel/vfs.h>
|
2018-03-19 05:38:11 +03:00
|
|
|
#include <kernel/pipe.h>
|
2021-05-31 04:47:02 +03:00
|
|
|
#include <kernel/process.h>
|
|
|
|
#include <kernel/printf.h>
|
2018-03-19 05:38:11 +03:00
|
|
|
#include <kernel/args.h>
|
2018-10-29 13:55:55 +03:00
|
|
|
#include <kernel/pty.h>
|
2021-05-31 04:47:02 +03:00
|
|
|
#include <kernel/arch/x86_64/regs.h>
|
|
|
|
#include <kernel/arch/x86_64/ports.h>
|
|
|
|
#include <kernel/arch/x86_64/irq.h>
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
#define SERIAL_PORT_A 0x3F8
|
|
|
|
#define SERIAL_PORT_B 0x2F8
|
|
|
|
#define SERIAL_PORT_C 0x3E8
|
|
|
|
#define SERIAL_PORT_D 0x2E8
|
|
|
|
|
|
|
|
#define SERIAL_IRQ_AC 4
|
|
|
|
#define SERIAL_IRQ_BD 3
|
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
static pty_t * _serial_port_pty_a = NULL;
|
|
|
|
static pty_t * _serial_port_pty_b = NULL;
|
|
|
|
static pty_t * _serial_port_pty_c = NULL;
|
|
|
|
static pty_t * _serial_port_pty_d = NULL;
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
static pty_t ** pty_for_port(int port) {
|
2018-03-16 15:56:19 +03:00
|
|
|
switch (port) {
|
2018-10-29 13:55:55 +03:00
|
|
|
case SERIAL_PORT_A: return &_serial_port_pty_a;
|
|
|
|
case SERIAL_PORT_B: return &_serial_port_pty_b;
|
|
|
|
case SERIAL_PORT_C: return &_serial_port_pty_c;
|
|
|
|
case SERIAL_PORT_D: return &_serial_port_pty_d;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
static int serial_rcvd(int device) {
|
|
|
|
return inportb(device + 5) & 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char serial_recv(int device) {
|
2021-05-31 04:47:02 +03:00
|
|
|
while (serial_rcvd(device) == 0) switch_task(1);
|
2018-10-29 13:55:55 +03:00
|
|
|
return inportb(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int serial_transmit_empty(int device) {
|
|
|
|
return inportb(device + 5) & 0x20;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_send(int device, char out) {
|
2021-05-31 04:47:02 +03:00
|
|
|
while (serial_transmit_empty(device) == 0) switch_task(1);
|
2018-10-29 13:55:55 +03:00
|
|
|
outportb(device, out);
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static list_t * sem_serial_ac = NULL;
|
|
|
|
static list_t * sem_serial_bd = NULL;
|
|
|
|
static process_t * serial_ac_handler = NULL;
|
|
|
|
static process_t * serial_bd_handler = NULL;
|
|
|
|
|
|
|
|
static void process_serial(void * argp) {
|
|
|
|
int portBase = (argp == sem_serial_ac) ? SERIAL_PORT_A : SERIAL_PORT_B;
|
|
|
|
char ch;
|
|
|
|
pty_t * pty;
|
|
|
|
while (1) {
|
|
|
|
sleep_on((list_t*)argp);
|
|
|
|
int next = 0;
|
|
|
|
int port = 0;
|
|
|
|
if (inportb(portBase+1) & 0x01) {
|
|
|
|
port = portBase;
|
|
|
|
} else {
|
|
|
|
port = portBase - 0x100;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
ch = serial_recv(port);
|
|
|
|
pty = *pty_for_port(port);
|
|
|
|
tty_input_process(pty, ch);
|
|
|
|
next = serial_rcvd(port);
|
|
|
|
/* TODO: Can we handle more than one character here
|
|
|
|
* before yielding? Would that be helpful? */
|
|
|
|
if (next) switch_task(1);
|
|
|
|
} while (next);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int serial_handler_ac(struct regs *r) {
|
2018-03-16 15:56:19 +03:00
|
|
|
irq_ack(SERIAL_IRQ_AC);
|
2021-05-31 04:47:02 +03:00
|
|
|
wakeup_queue(sem_serial_ac);
|
2018-03-16 15:56:19 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
int serial_handler_bd(struct regs *r) {
|
2018-03-16 15:56:19 +03:00
|
|
|
irq_ack(SERIAL_IRQ_BD);
|
2021-05-31 04:47:02 +03:00
|
|
|
wakeup_queue(sem_serial_bd);
|
2018-03-16 15:56:19 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_enable(int port) {
|
|
|
|
outportb(port + 1, 0x00); /* Disable interrupts */
|
|
|
|
outportb(port + 3, 0x80); /* Enable divisor mode */
|
|
|
|
outportb(port + 0, 0x01); /* Div Low: 01 Set the port to 115200 bps */
|
|
|
|
outportb(port + 1, 0x00); /* Div High: 00 */
|
|
|
|
outportb(port + 3, 0x03); /* Disable divisor mode, set parity */
|
|
|
|
outportb(port + 2, 0xC7); /* Enable FIFO and clear */
|
|
|
|
outportb(port + 4, 0x0B); /* Enable interrupts */
|
|
|
|
outportb(port + 1, 0x01); /* Enable interrupts */
|
|
|
|
}
|
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
static int have_installed_ac = 0;
|
|
|
|
static int have_installed_bd = 0;
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
static void serial_write_out(pty_t * pty, uint8_t c) {
|
|
|
|
if (pty == _serial_port_pty_a) serial_send(SERIAL_PORT_A, c);
|
|
|
|
if (pty == _serial_port_pty_b) serial_send(SERIAL_PORT_B, c);
|
|
|
|
if (pty == _serial_port_pty_c) serial_send(SERIAL_PORT_C, c);
|
|
|
|
if (pty == _serial_port_pty_d) serial_send(SERIAL_PORT_D, c);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
#define DEV_PATH "/dev/"
|
|
|
|
#define TTY_A "ttyS0"
|
|
|
|
#define TTY_B "ttyS1"
|
|
|
|
#define TTY_C "ttyS2"
|
|
|
|
#define TTY_D "ttyS3"
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
static void serial_fill_name(pty_t * pty, char * name) {
|
2021-05-31 04:47:02 +03:00
|
|
|
if (pty == _serial_port_pty_a) snprintf(name, 100, DEV_PATH TTY_A);
|
|
|
|
if (pty == _serial_port_pty_b) snprintf(name, 100, DEV_PATH TTY_B);
|
|
|
|
if (pty == _serial_port_pty_c) snprintf(name, 100, DEV_PATH TTY_C);
|
|
|
|
if (pty == _serial_port_pty_d) snprintf(name, 100, DEV_PATH TTY_D);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
static fs_node_t * serial_device_create(int port) {
|
|
|
|
pty_t * pty = pty_new(NULL);
|
|
|
|
*pty_for_port(port) = pty;
|
|
|
|
pty->write_out = serial_write_out;
|
|
|
|
pty->fill_name = serial_fill_name;
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
serial_enable(port);
|
2018-07-21 10:02:39 +03:00
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
if (port == SERIAL_PORT_A || port == SERIAL_PORT_C) {
|
2018-07-21 10:02:39 +03:00
|
|
|
if (!have_installed_ac) {
|
|
|
|
irq_install_handler(SERIAL_IRQ_AC, serial_handler_ac, "serial ac");
|
|
|
|
have_installed_ac = 1;
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
} else {
|
2018-07-21 10:02:39 +03:00
|
|
|
if (!have_installed_bd) {
|
|
|
|
irq_install_handler(SERIAL_IRQ_BD, serial_handler_bd, "serial bd");
|
|
|
|
have_installed_bd = 1;
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-08-31 16:40:49 +03:00
|
|
|
pty->slave->gid = 2; /* dialout group */
|
|
|
|
pty->slave->mask = 0660;
|
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
return pty->slave;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
void serial_initialize(void) {
|
|
|
|
sem_serial_ac = list_create("serial ac semaphore",NULL);
|
|
|
|
sem_serial_bd = list_create("serial bd semaphore",NULL);
|
|
|
|
|
|
|
|
serial_ac_handler = spawn_worker_thread(process_serial, "[serial ac]", sem_serial_ac);
|
|
|
|
serial_bd_handler = spawn_worker_thread(process_serial, "[serial bd]", sem_serial_bd);
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2018-10-29 13:55:55 +03:00
|
|
|
fs_node_t * ttyS0 = serial_device_create(SERIAL_PORT_A); vfs_mount(DEV_PATH TTY_A, ttyS0);
|
|
|
|
fs_node_t * ttyS1 = serial_device_create(SERIAL_PORT_B); vfs_mount(DEV_PATH TTY_B, ttyS1);
|
|
|
|
fs_node_t * ttyS2 = serial_device_create(SERIAL_PORT_C); vfs_mount(DEV_PATH TTY_C, ttyS2);
|
|
|
|
fs_node_t * ttyS3 = serial_device_create(SERIAL_PORT_D); vfs_mount(DEV_PATH TTY_D, ttyS3);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|