/* Copyright (C) 2021 Rain */ /* This file is part of XNIX. */ /* XNIX is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) and later version. */ /* XNIX is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ /* You should have received a copy of the GNU General Public License along with XNIX. If not, see . */ #include #include #include #include #include #include #include #include #include #include struct tty tty1; errno_t serial_init(void) { serial_info("COM0: %w ", bootarg->com0); serial_info("COM1: %w\n", bootarg->com1); serial_info("COM2: %w ", bootarg->com2); serial_info("COM3: %w\n", bootarg->com3); if (bootarg->com0) com_init(bootarg->com0); if (bootarg->com1) com_init(bootarg->com1); if (bootarg->com2) com_init(bootarg->com2); if (bootarg->com3) com_init(bootarg->com3); if (!bootarg->com0) return 0; tty1.input_fifo = NULL; tty1.mode = T_COOKED | T_NORD; tty1.ctl = 0; tty1.vx = tty1.vy = 0; tty1.putc = serial_putc; tty1.next = NULL; /* register a new tty if com0 exists */ tty0.next = &tty1; return 0; } void com_init(__io_t port) { outb(0x00, port + 1); outb(0x80, port + 3); outb(0x03, port + 0); outb(0x00, port + 1); outb(0x03, port + 3); outb(0xc7, port + 2); outb(0x0b, port + 4); outb(0x1e, port + 4); outb(0xae, port + 0); if (inb(port + 0) != 0xae) { serial_info("com_init: warning: port %w not responed. \n", port); return; } outb(0x0f, port + 4); return; } /* the tab size of serial (on qemu) */ #define SERIAL_TAB_SIZE 8 int serial_putc(struct tty *tty, char c) { if (c == '\n') { tty->vx = 0; tty->vy = 0; serial_write(bootarg->com0, '\r'); serial_write(bootarg->com0, '\n'); return 0; } if (c == '\r') { tty->vx = 0; serial_write(bootarg->com0, '\r'); return 0; } if (c == '\b') { tty->vx--; serial_write(bootarg->com0, '\b'); return 0; } if (c == '\t') { int ax = (tty->vx + SERIAL_TAB_SIZE) & ~SERIAL_TAB_SIZE; if (ax == tty->vx) tty->vx += SERIAL_TAB_SIZE; else tty->vx = ax; serial_write(bootarg->com0, '\t'); return 0; } serial_write(bootarg->com0, c); tty->vx++; return 0; } void serial_write(__io_t port, __uint8_t data) { while (!(inb(port + 5) & 0x20)); outb(data, port + 0); return; }