serial.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/errno.h>
  21. #include <kernel/types.h>
  22. #include <kernel/console.h>
  23. #include <kernel/mm.h>
  24. #include <kernel/tty.h>
  25. #include <kernel/int.h>
  26. #include <kernel/panic.h>
  27. #include <arch/io.h>
  28. #include <modules/serial.h>
  29. struct tty tty1;
  30. errno_t serial_init(void) {
  31. if (bootarg->com0) com_init(bootarg->com0);
  32. if (bootarg->com1) com_init(bootarg->com1);
  33. if (bootarg->com2) com_init(bootarg->com2);
  34. if (bootarg->com3) com_init(bootarg->com3);
  35. set_irq(3, serial1_interrupt);
  36. set_irq(4, serial0_interrupt);
  37. if (!bootarg->com0) return 0;
  38. tty1.input_fifo = NULL;
  39. tty1.mode = T_COOKED | T_NORD;
  40. tty1.ctl = 0;
  41. tty1.vx = tty1.vy = 0;
  42. tty1.putc = serial_putc;
  43. tty1.next = NULL;
  44. /* register a new tty if com0 exists */
  45. tty0.next = &tty1;
  46. /* com0 exists, so set it to stdout */
  47. stdout = &tty1;
  48. serial_write(bootarg->com0, serial_read(bootarg->com0));
  49. return 0;
  50. }
  51. void com_init(__io_t port) {
  52. outb(0x80, port + 3);
  53. outb(0x30, port + 0);
  54. outb(0x00, port + 1);
  55. outb(0x03, port + 3);
  56. outb(0x0b, port + 4);
  57. outb(0x0d, port + 1);
  58. inb(port + 0);
  59. return;
  60. }
  61. /* the tab size of serial (on qemu) */
  62. #define SERIAL_TAB_SIZE 8
  63. int serial_putc(struct tty *tty, char c) {
  64. if (c == '\n') {
  65. tty->vx = 0;
  66. tty->vy = 0;
  67. serial_write(bootarg->com0, '\r');
  68. serial_write(bootarg->com0, '\n');
  69. return 0;
  70. }
  71. if (c == '\r') {
  72. tty->vx = 0;
  73. serial_write(bootarg->com0, '\r');
  74. return 0;
  75. }
  76. if (c == '\b') {
  77. tty->vx--;
  78. serial_write(bootarg->com0, '\b');
  79. return 0;
  80. }
  81. if (c == '\t') {
  82. int ax = (tty->vx + SERIAL_TAB_SIZE) & ~SERIAL_TAB_SIZE;
  83. if (ax == tty->vx)
  84. tty->vx += SERIAL_TAB_SIZE;
  85. else
  86. tty->vx = ax;
  87. serial_write(bootarg->com0, '\t');
  88. return 0;
  89. }
  90. serial_write(bootarg->com0, c);
  91. tty->vx++;
  92. return 0;
  93. }
  94. void serial_write(__io_t port, __uint8_t data) {
  95. while (!(inb(port + 5) & 0x20));
  96. outb(data, port + 0);
  97. return;
  98. }
  99. char serial_read(__io_t port) {
  100. /* zero if no data */
  101. return inb(port);
  102. }
  103. void serial0_interrupt(void) {
  104. serial_info("interrupt from serial0. \n");
  105. return;
  106. }
  107. void serial1_interrupt(void) {
  108. serial_info("interrupt from serial1. \n");
  109. return;
  110. }