serial.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. serial_info("COM0: %w ", bootarg->com0);
  32. serial_info("COM1: %w\n", bootarg->com1);
  33. serial_info("COM2: %w ", bootarg->com2);
  34. serial_info("COM3: %w\n", bootarg->com3);
  35. if (bootarg->com0) com_init(bootarg->com0);
  36. if (bootarg->com1) com_init(bootarg->com1);
  37. if (bootarg->com2) com_init(bootarg->com2);
  38. if (bootarg->com3) com_init(bootarg->com3);
  39. if (!bootarg->com0) return 0;
  40. tty1.input_fifo = NULL;
  41. tty1.mode = T_COOKED | T_NORD;
  42. tty1.ctl = 0;
  43. tty1.vx = tty1.vy = 0;
  44. tty1.putc = serial_putc;
  45. tty1.next = NULL;
  46. /* register a new tty if com0 exists */
  47. tty0.next = &tty1;
  48. return 0;
  49. }
  50. void com_init(__io_t port) {
  51. outb(0x00, port + 1);
  52. outb(0x80, port + 3);
  53. outb(0x03, port + 0);
  54. outb(0x00, port + 1);
  55. outb(0x03, port + 3);
  56. outb(0xc7, port + 2);
  57. outb(0x0b, port + 4);
  58. outb(0x1e, port + 4);
  59. outb(0xae, port + 0);
  60. if (inb(port + 0) != 0xae) {
  61. serial_info("com_init: warning: port %w not responed. \n",
  62. port);
  63. return;
  64. }
  65. outb(0x0f, port + 4);
  66. return;
  67. }
  68. /* the tab size of serial (on qemu) */
  69. #define SERIAL_TAB_SIZE 8
  70. int serial_putc(struct tty *tty, char c) {
  71. if (c == '\n') {
  72. tty->vx = 0;
  73. tty->vy = 0;
  74. serial_write(bootarg->com0, '\r');
  75. serial_write(bootarg->com0, '\n');
  76. return 0;
  77. }
  78. if (c == '\r') {
  79. tty->vx = 0;
  80. serial_write(bootarg->com0, '\r');
  81. return 0;
  82. }
  83. if (c == '\b') {
  84. tty->vx--;
  85. serial_write(bootarg->com0, '\b');
  86. return 0;
  87. }
  88. if (c == '\t') {
  89. int ax = (tty->vx + SERIAL_TAB_SIZE) & ~SERIAL_TAB_SIZE;
  90. if (ax == tty->vx)
  91. tty->vx += SERIAL_TAB_SIZE;
  92. else
  93. tty->vx = ax;
  94. serial_write(bootarg->com0, '\t');
  95. return 0;
  96. }
  97. serial_write(bootarg->com0, c);
  98. tty->vx++;
  99. return 0;
  100. }
  101. void serial_write(__io_t port, __uint8_t data) {
  102. while (!(inb(port + 5) & 0x20));
  103. outb(data, port + 0);
  104. return;
  105. }