serial.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* modules/console.c
  2. *
  3. * Copyright (C) 2021, 2022 intirain.
  4. *
  5. * This file is part of evx.
  6. *
  7. * evx is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * evx is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with evx. If not, see <https://www.gnu.org/licenses/>
  19. */
  20. #include <sys/types.h>
  21. #include <kernel/init.h>
  22. #include <kernel/module.h>
  23. #include <kernel/tty.h>
  24. #include <kernel/kmlog.h>
  25. #include <modules/serial.h>
  26. #include <arch/io.h>
  27. #include <arch/types.h>
  28. int serial_putc(char);
  29. static int put_kmlog(void);
  30. struct tty serial0_tty = {
  31. .dev_name = "ttyS0",
  32. .priv_data = COM0,
  33. .flags = 0,
  34. .putc = serial_putc,
  35. .getc = NULL
  36. };
  37. static int com_init(__io_t p) {
  38. outb(0x80, p + 3);
  39. outb(0x30, p + 0);
  40. outb(0x00, p + 1);
  41. outb(0x03, p + 3);
  42. outb(0x0b, p + 4);
  43. outb(0x0d, p + 1);
  44. inb(p);
  45. return 0;
  46. }
  47. /* as i know, this only runs on x86 */
  48. int __init init_serial(void) {
  49. com_init(COM0);
  50. put_kmlog();
  51. return tty_register(&serial0_tty);
  52. }
  53. int serial_write(__io_t p, char c) {
  54. while (!(inb(p + 5) & 0x20));
  55. outb(c, p);
  56. return 0;
  57. }
  58. int serial_putc(char c) {
  59. if (c == '\n')
  60. serial_write(COM0, '\r');
  61. return serial_write(COM0, c);
  62. }
  63. static int put_kmlog(void) {
  64. char *s;
  65. for (s = kmlog_buf; *s; s++) {
  66. if (s > kmlog_buf + KMLOG_SIZE)
  67. s = kmlog_buf;
  68. serial_putc(*s);
  69. }
  70. return 0;
  71. }
  72. module_init(init_serial);